|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "time" |
| 10 | + |
| 11 | + dockertypes "github.com/docker/docker/api/types" |
| 12 | + "github.com/docker/docker/api/types/container" |
| 13 | + "github.com/docker/docker/client" |
| 14 | + "github.com/docker/docker/pkg/stdcopy" |
| 15 | + "github.com/markhuang1212/code-grader/types" |
| 16 | + "github.com/pkg/errors" |
| 17 | +) |
| 18 | + |
| 19 | +const imageExec = "markhuang1212/code-grader/runtime-exec:latest" |
| 20 | + |
| 21 | +func exec_user_code(ctx context.Context, gr types.GradeRequest) ([]byte, error) { |
| 22 | + |
| 23 | + var testCase types.TestCaseOptions |
| 24 | + |
| 25 | + testCaseJson, err := os.ReadFile(filepath.Join(AppRoot, "testcases", gr.TestCaseName, "testcase.json")) |
| 26 | + if err != nil { |
| 27 | + return nil, errors.Wrap(InternalError, "cannot open testcase.json") |
| 28 | + } |
| 29 | + |
| 30 | + err = json.Unmarshal(testCaseJson, &testCase) |
| 31 | + if err != nil { |
| 32 | + return nil, errors.Wrap(InternalError, "cannot parse testcase.json") |
| 33 | + } |
| 34 | + |
| 35 | + cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) |
| 36 | + |
| 37 | + resp, err := cli.ContainerCreate(ctx, &container.Config{ |
| 38 | + Image: imageExec, |
| 39 | + Env: []string{ |
| 40 | + "TEST_CASE_DIR=" + filepath.Join("/code-grader/testcases", gr.TestCaseName), |
| 41 | + }, |
| 42 | + OpenStdin: true, |
| 43 | + StdinOnce: true, |
| 44 | + }, &container.HostConfig{ |
| 45 | + NetworkMode: "none", |
| 46 | + Resources: container.Resources{ |
| 47 | + Memory: CompilationMemoryLimit, |
| 48 | + }, |
| 49 | + }, nil, nil, "") |
| 50 | + |
| 51 | + if err != nil { |
| 52 | + return nil, errors.Wrap(InternalError, "cannot create container") |
| 53 | + } |
| 54 | + |
| 55 | + hjresp, err := cli.ContainerAttach(ctx, resp.ID, dockertypes.ContainerAttachOptions{ |
| 56 | + Stream: true, |
| 57 | + Stdin: true, |
| 58 | + Stdout: true, |
| 59 | + Stderr: true, |
| 60 | + }) |
| 61 | + |
| 62 | + if err != nil { |
| 63 | + return nil, errors.Wrap(InternalError, "cannot attach container") |
| 64 | + } |
| 65 | + |
| 66 | + ctxdl, cancel := context.WithTimeout(ctx, time.Second*time.Duration(testCase.RuntimeOptions.RuntimeLimit)) |
| 67 | + defer cancel() |
| 68 | + |
| 69 | + statusCh, errCh := cli.ContainerWait(ctxdl, resp.ID, container.WaitConditionNextExit) |
| 70 | + |
| 71 | + err = cli.ContainerStart(ctxdl, resp.ID, dockertypes.ContainerStartOptions{}) |
| 72 | + if err != nil { |
| 73 | + return nil, errors.Wrap(InternalError, "cannot start container") |
| 74 | + } |
| 75 | + |
| 76 | + outR, outW := io.Pipe() |
| 77 | + errR, errW := io.Pipe() |
| 78 | + hjresp.Conn.Write([]byte(gr.UserCode)) |
| 79 | + hjresp.Conn.Close() |
| 80 | + stdcopy.StdCopy(outW, errW, hjresp.Conn) |
| 81 | + outW.Close() |
| 82 | + errW.Close() |
| 83 | + |
| 84 | + select { |
| 85 | + case status := <-statusCh: |
| 86 | + switch status.StatusCode { |
| 87 | + case 0: |
| 88 | + result, err := io.ReadAll(outR) |
| 89 | + if err != nil { |
| 90 | + return nil, errors.Wrap(InternalError, "error reading outR") |
| 91 | + } |
| 92 | + return result, nil |
| 93 | + case 1: |
| 94 | + result, err := io.ReadAll(errR) |
| 95 | + if err != nil { |
| 96 | + return nil, errors.Wrap(InternalError, "error reading errR") |
| 97 | + } |
| 98 | + return result, CompilationError |
| 99 | + case 2: |
| 100 | + return nil, InternalError |
| 101 | + default: |
| 102 | + return nil, InternalError |
| 103 | + } |
| 104 | + case err := <-errCh: |
| 105 | + if errors.Is(err, context.DeadlineExceeded) { |
| 106 | + return nil, TimeLimitExceed |
| 107 | + } |
| 108 | + return nil, errors.Wrap(InternalError, "error waiting container") |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments