diff options
| -rw-r--r-- | worker/Dockerfile | 16 | ||||
| -rw-r--r-- | worker/Makefile | 4 | ||||
| -rw-r--r-- | worker/exec.go | 186 | ||||
| -rw-r--r-- | worker/go.mod | 20 | ||||
| -rw-r--r-- | worker/go.sum | 39 | ||||
| -rw-r--r-- | worker/handlers.go | 67 | ||||
| -rw-r--r-- | worker/main.go | 64 | ||||
| -rw-r--r-- | worker/models.go | 84 |
8 files changed, 432 insertions, 48 deletions
diff --git a/worker/Dockerfile b/worker/Dockerfile index 1d1523d..2373f57 100644 --- a/worker/Dockerfile +++ b/worker/Dockerfile @@ -1,13 +1,21 @@ FROM golang:1.22.3 AS builder WORKDIR /build -COPY . /build -RUN go build -o /build/server . -################################################################################ -FROM golang:1.22.3 +RUN apt-get update && apt-get install -y curl xz-utils +RUN curl https://wasmtime.dev/install.sh -sSf | bash -s -- --version v23.0.1 + +COPY go.mod go.sum ./ +RUN go mod download +COPY *.go /build +RUN CGO_ENABLED=0 go build -o /build/server . + +# ################################################################################ +FROM ghcr.io/swiftwasm/swift:5.10-focal WORKDIR /app + +COPY --from=builder /root/.wasmtime/bin/wasmtime /usr/bin/wasmtime COPY --from=builder /build/server /app/server CMD ["/app/server"] diff --git a/worker/Makefile b/worker/Makefile index 4d6cf39..3c69e4b 100644 --- a/worker/Makefile +++ b/worker/Makefile @@ -5,3 +5,7 @@ fmt: .PHONY: check check: go build -o /dev/null ./... + +.PHONY: lint +lint: + go vet ./... diff --git a/worker/exec.go b/worker/exec.go new file mode 100644 index 0000000..2ef16fa --- /dev/null +++ b/worker/exec.go @@ -0,0 +1,186 @@ +package main + +import ( + "bytes" + "context" + "crypto/md5" + "fmt" + "os" + "os/exec" + "strings" + "time" +) + +const ( + dataRootDir = "/app/data" + // Stores *.swift files. + dataSwiftRootDir = dataRootDir + "/swift" + // Stores *.wasm files. + dataWasmRootDir = dataRootDir + "/wasm" + // Stores *.cwasm files (compiled wasm generated by "wasmtime compile"). + dataCwasmRootDir = dataRootDir + "/cwasm" + + wasmMaxMemorySize = 10 * 1024 * 1024 // 10 MiB +) + +func prepareDirectories() error { + if err := os.MkdirAll(dataSwiftRootDir, 0755); err != nil { + return err + } + if err := os.MkdirAll(dataWasmRootDir, 0755); err != nil { + return err + } + if err := os.MkdirAll(dataCwasmRootDir, 0755); err != nil { + return err + } + return nil +} + +func calcHash(code string) string { + return fmt.Sprintf("%x", md5.Sum([]byte(code))) +} + +func calcFilePath(hash, ext string) string { + return fmt.Sprintf("%s/%s/%s.%s", dataRootDir, ext, hash, ext) +} + +func execCommandWithTimeout( + ctx context.Context, + maxDuration time.Duration, + makeCmd func(context.Context) *exec.Cmd, +) (string, string, error) { + ctx, cancel := context.WithTimeout(ctx, maxDuration) + defer cancel() + + cmd := makeCmd(ctx) + + var stdout bytes.Buffer + var stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + + exitCh := make(chan error) + go func() { + exitCh <- cmd.Run() + }() + + select { + case <-ctx.Done(): + return stdout.String(), stderr.String(), ctx.Err() + case err := <-exitCh: + return stdout.String(), stderr.String(), err + } +} + +func convertCommandErrorToResultType(err error) string { + if err != nil { + if err == context.DeadlineExceeded { + return resultTimeout + } else { + return resultFailure + } + } else { + return resultSuccess + } +} + +func execSwiftCompile( + ctx context.Context, + code string, + maxDuration time.Duration, +) swiftCompileResponseData { + hash := calcHash(code) + inPath := calcFilePath(hash, "swift") + outPath := calcFilePath(hash, "wasm") + + if err := os.WriteFile(inPath, []byte(code), 0644); err != nil { + return swiftCompileResponseData{ + Result: resultInternalError, + Stdout: "", + Stderr: err.Error(), + } + } + + stdout, stderr, err := execCommandWithTimeout( + ctx, + maxDuration, + func(ctx context.Context) *exec.Cmd { + return exec.CommandContext( + ctx, + "swiftc", + "-target", "wasm32-unknown-wasi", + "-o", outPath, + inPath, + ) + }, + ) + + return swiftCompileResponseData{ + Result: convertCommandErrorToResultType(err), + Stdout: stdout, + Stderr: stderr, + } +} + +func execWasmCompile( + ctx context.Context, + code string, + maxDuration time.Duration, +) wasmCompileResponseData { + hash := calcHash(code) + inPath := calcFilePath(hash, "wasm") + outPath := calcFilePath(hash, "cwasm") + + stdout, stderr, err := execCommandWithTimeout( + ctx, + maxDuration, + func(ctx context.Context) *exec.Cmd { + return exec.CommandContext( + ctx, + "wasmtime", "compile", + "-O", "opt-level=0", + "-C", "cache=n", + "-W", fmt.Sprintf("max-memory-size=%d", wasmMaxMemorySize), + "-o", outPath, + inPath, + ) + }, + ) + + return wasmCompileResponseData{ + Result: convertCommandErrorToResultType(err), + Stdout: stdout, + Stderr: stderr, + } +} + +func execTestRun( + ctx context.Context, + code string, + stdin string, + maxDuration time.Duration, +) testRunResponseData { + hash := calcHash(code) + inPath := calcFilePath(hash, "cwasm") + + stdout, stderr, err := execCommandWithTimeout( + ctx, + maxDuration, + func(ctx context.Context) *exec.Cmd { + cmd := exec.CommandContext( + ctx, + "wasmtime", "run", + "--allow-precompiled", + inPath, + ) + cmd.Stdin = strings.NewReader(stdin) + return cmd + }, + ) + + return testRunResponseData{ + Result: convertCommandErrorToResultType(err), + Stdout: stdout, + Stderr: stderr, + } +} diff --git a/worker/go.mod b/worker/go.mod index 007435d..1d01907 100644 --- a/worker/go.mod +++ b/worker/go.mod @@ -1,3 +1,23 @@ module github.com/nsfisis/iosdc-japan-2024-albatross/worker go 1.22.3 + +require ( + github.com/labstack/echo-jwt/v4 v4.2.0 + github.com/labstack/echo/v4 v4.12.0 +) + +require ( + github.com/golang-jwt/jwt v3.2.2+incompatible // indirect + github.com/golang-jwt/jwt/v5 v5.2.1 // indirect + github.com/labstack/gommon v0.4.2 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasttemplate v1.2.2 // indirect + golang.org/x/crypto v0.22.0 // indirect + golang.org/x/net v0.24.0 // indirect + golang.org/x/sys v0.19.0 // indirect + golang.org/x/text v0.14.0 // indirect + golang.org/x/time v0.5.0 // indirect +) diff --git a/worker/go.sum b/worker/go.sum new file mode 100644 index 0000000..26616a1 --- /dev/null +++ b/worker/go.sum @@ -0,0 +1,39 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= +github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= +github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= +github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/labstack/echo-jwt/v4 v4.2.0 h1:odSISV9JgcSCuhgQSV/6Io3i7nUmfM/QkBeR5GVJj5c= +github.com/labstack/echo-jwt/v4 v4.2.0/go.mod h1:MA2RqdXdEn4/uEglx0HcUOgQSyBaTh5JcaHIan3biwU= +github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0= +github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM= +github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= +github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= +github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= +golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= +golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= +golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= +golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/worker/handlers.go b/worker/handlers.go new file mode 100644 index 0000000..e7ceef6 --- /dev/null +++ b/worker/handlers.go @@ -0,0 +1,67 @@ +package main + +import ( + "fmt" + "net/http" + + "github.com/labstack/echo/v4" +) + +func newBadRequestError(err error) *echo.HTTPError { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid request: %s", err.Error())) +} + +func handleSwiftCompile(c echo.Context) error { + var req swiftCompileRequestData + if err := c.Bind(&req); err != nil { + return newBadRequestError(err) + } + if err := req.validate(); err != nil { + return newBadRequestError(err) + } + + res := execSwiftCompile( + c.Request().Context(), + req.Code, + req.maxDuration(), + ) + + return c.JSON(http.StatusOK, res) +} + +func handleWasmCompile(c echo.Context) error { + var req wasmCompileRequestData + if err := c.Bind(&req); err != nil { + return newBadRequestError(err) + } + if err := req.validate(); err != nil { + return newBadRequestError(err) + } + + res := execWasmCompile( + c.Request().Context(), + req.Code, + req.maxDuration(), + ) + + return c.JSON(http.StatusOK, res) +} + +func handleTestRun(c echo.Context) error { + var req testRunRequestData + if err := c.Bind(&req); err != nil { + return newBadRequestError(err) + } + if err := req.validate(); err != nil { + return newBadRequestError(err) + } + + res := execTestRun( + c.Request().Context(), + req.Code, + req.Stdin, + req.maxDuration(), + ) + + return c.JSON(http.StatusOK, res) +} diff --git a/worker/main.go b/worker/main.go index 0a52b0e..8134a56 100644 --- a/worker/main.go +++ b/worker/main.go @@ -1,57 +1,33 @@ package main import ( - "encoding/json" + "log" "net/http" - "strconv" - "time" -) - -type RequestBody struct { - Code string `json:"code"` - Stdin string `json:"stdin"` -} -type ResponseBody struct { - Result string `json:"result"` - Stdout string `json:"stdout"` - Stderr string `json:"stderr"` -} - -func doExec(code string, stdin string, maxDuration time.Duration) ResponseBody { - _ = code - _ = stdin - _ = maxDuration + echojwt "github.com/labstack/echo-jwt/v4" + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" +) - return ResponseBody{ - Result: "success", - Stdout: "42", - Stderr: "", +func main() { + if err := prepareDirectories(); err != nil { + log.Fatal(err) } -} -func execHandler(w http.ResponseWriter, r *http.Request) { - maxDurationStr := r.URL.Query().Get("max_duration") - maxDuration, err := strconv.Atoi(maxDurationStr) - if err != nil || maxDuration <= 0 { - http.Error(w, "Invalid max_duration parameter", http.StatusBadRequest) - return - } + e := echo.New() - var reqBody RequestBody - err = json.NewDecoder(r.Body).Decode(&reqBody) - if err != nil { - http.Error(w, "Invalid request body", http.StatusBadRequest) - return - } + e.Use(middleware.Logger()) + e.Use(middleware.Recover()) - resBody := doExec(reqBody.Code, reqBody.Stdin, time.Duration(maxDuration)*time.Second) + e.Use(echojwt.WithConfig(echojwt.Config{ + SigningKey: []byte("TODO"), + })) - w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(resBody) -} + e.POST("/api/swiftc", handleSwiftCompile) + e.POST("/api/wasmc", handleWasmCompile) + e.POST("/api/testrun", handleTestRun) -func main() { - http.HandleFunc("/api/exec", execHandler) - http.ListenAndServe(":80", nil) + if err := e.Start(":80"); err != http.ErrServerClosed { + log.Fatal(err) + } } diff --git a/worker/models.go b/worker/models.go new file mode 100644 index 0000000..b838fe0 --- /dev/null +++ b/worker/models.go @@ -0,0 +1,84 @@ +package main + +import ( + "errors" + "time" +) + +const ( + resultSuccess = "success" + resultFailure = "failure" + resultTimeout = "timeout" + resultInternalError = "internal_error" +) + +var ( + errInvalidMaxDuration = errors.New("'max_duration_ms' must be positive") +) + +type swiftCompileRequestData struct { + MaxDurationMilliseconds int `json:"max_duration_ms"` + Code string `json:"code"` +} + +func (req *swiftCompileRequestData) maxDuration() time.Duration { + return time.Duration(req.MaxDurationMilliseconds) * time.Millisecond +} + +func (req *swiftCompileRequestData) validate() error { + if req.MaxDurationMilliseconds <= 0 { + return errInvalidMaxDuration + } + return nil +} + +type swiftCompileResponseData struct { + Result string `json:"result"` + Stdout string `json:"stdout"` + Stderr string `json:"stderr"` +} + +type wasmCompileRequestData struct { + MaxDurationMilliseconds int `json:"max_duration_ms"` + Code string `json:"code"` +} + +type wasmCompileResponseData struct { + Result string `json:"result"` + Stdout string `json:"stdout"` + Stderr string `json:"stderr"` +} + +func (req *wasmCompileRequestData) maxDuration() time.Duration { + return time.Duration(req.MaxDurationMilliseconds) * time.Millisecond +} + +func (req *wasmCompileRequestData) validate() error { + if req.MaxDurationMilliseconds <= 0 { + return errInvalidMaxDuration + } + return nil +} + +type testRunRequestData struct { + MaxDurationMilliseconds int `json:"max_duration_ms"` + Code string `json:"code"` + Stdin string `json:"stdin"` +} + +func (req *testRunRequestData) maxDuration() time.Duration { + return time.Duration(req.MaxDurationMilliseconds) * time.Millisecond +} + +func (req *testRunRequestData) validate() error { + if req.MaxDurationMilliseconds <= 0 { + return errInvalidMaxDuration + } + return nil +} + +type testRunResponseData struct { + Result string `json:"result"` + Stdout string `json:"stdout"` + Stderr string `json:"stderr"` +} |
