diff --git a/environments/binary/Dockerfile b/environments/binary/Dockerfile index cd7ac19a..5c0ac2f4 100644 --- a/environments/binary/Dockerfile +++ b/environments/binary/Dockerfile @@ -1,10 +1,18 @@ +FROM golang:onbuild + +WORKDIR /go +COPY *.go /go/ + +RUN GOOS=linux GOARCH=386 go build -o server . + FROM alpine:3.5 +WORKDIR /app + RUN apk update RUN apk add coreutils binutils findutils grep -COPY . /app -WORKDIR /app +COPY --from=0 /go/server /app/server EXPOSE 8888 -ENTRYPOINT ["./server"] \ No newline at end of file +ENTRYPOINT ["./server"] diff --git a/environments/binary/README.md b/environments/binary/README.md index a0b842da..a49ddfd4 100644 --- a/environments/binary/README.md +++ b/environments/binary/README.md @@ -36,15 +36,10 @@ All output that is provided to the server over the STDOUT will be transformed in ## Compiling -In order to build the Dockerfile, the server needs to be compiled to the right architecture. - -```bash -sh ./build.sh -``` Build the Dockerfile: ```bash docker build --tag=${USER}/binary-env . ``` -See the [README](../../examples/binary/README.md) in the binary examples directory for usage instructions. \ No newline at end of file +See the [README](../../examples/binary/README.md) in the binary examples directory for usage instructions. diff --git a/environments/binary/build.sh b/environments/binary/build.sh deleted file mode 100755 index 069d2d96..00000000 --- a/environments/binary/build.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -GOOS=linux GOARCH=386 go build -o server . - diff --git a/environments/binary/builder/Dockerfile b/environments/binary/builder/Dockerfile new file mode 100644 index 00000000..99cefe26 --- /dev/null +++ b/environments/binary/builder/Dockerfile @@ -0,0 +1,4 @@ +ARG BUILDER_IMAGE=fission/builder:latest +FROM ${BUILDER_IMAGE} + +ADD build.sh /usr/local/bin/build diff --git a/environments/binary/builder/build.sh b/environments/binary/builder/build.sh new file mode 100755 index 00000000..c70eb663 --- /dev/null +++ b/environments/binary/builder/build.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +apk update + +CWD=$(pwd) + +if [ -f ${SRC_PKG}/build.sh ]; then + cd ${SRC_PKG} + ./build.sh + cd ${CWD} +fi + +cp -rf ${SRC_PKG} ${DEPLOY_PKG} diff --git a/environments/binary/server.go b/environments/binary/server.go index c8edf43a..2d22c2d2 100644 --- a/environments/binary/server.go +++ b/environments/binary/server.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "flag" "fmt" "io" @@ -10,6 +11,8 @@ import ( "os/exec" "path/filepath" "strings" + + "github.com/fission/fission" ) const ( @@ -31,11 +34,34 @@ func (bs *BinaryServer) SpecializeHandler(w http.ResponseWriter, r *http.Request return } - _, err := os.Stat(bs.fetchedCodePath) + request := fission.FunctionLoadRequest{} + + codePath := bs.fetchedCodePath + err := json.NewDecoder(r.Body).Decode(&request) + switch { + case err == io.EOF: + case err != nil: + panic(err) + } + + if request.FilePath != "" { + fileStat, err := os.Stat(request.FilePath) + if err != nil { + panic(err) + } + + codePath = request.FilePath + switch mode := fileStat.Mode(); { + case mode.IsDir(): + codePath = filepath.Join(request.FilePath, request.FunctionName) + } + } + + _, err = os.Stat(codePath) if err != nil { if os.IsNotExist(err) { w.WriteHeader(http.StatusNotFound) - w.Write([]byte(bs.fetchedCodePath + ": not found")) + w.Write([]byte(codePath + ": not found")) return } else { panic(err) @@ -45,7 +71,7 @@ func (bs *BinaryServer) SpecializeHandler(w http.ResponseWriter, r *http.Request // Future: Check if executable is correct architecture/executable. // Copy the executable to ensure that file is executable and immutable. - userFunc, err := ioutil.ReadFile(bs.fetchedCodePath) + userFunc, err := ioutil.ReadFile(codePath) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("Failed to read executable.")) @@ -125,6 +151,7 @@ func main() { server := &BinaryServer{*codePath, absInternalCodePath} http.HandleFunc("/", server.InvocationHandler) http.HandleFunc("/specialize", server.SpecializeHandler) + http.HandleFunc("/v2/specialize", server.SpecializeHandler) fmt.Println("Listening on 8888 ...") err = http.ListenAndServe(":8888", nil)