-
Notifications
You must be signed in to change notification settings - Fork 2
/
sandbox.go
79 lines (68 loc) · 1.94 KB
/
sandbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
"golang.org/x/net/context"
)
const defaultDockerAPIVersion = "v1.38"
/*
RunSandbox : Spawns a sandbox container for the language, mounts testcasesPath
and submissionPath.
*/
func RunSandbox(testcasesPath string, submissionPath string, language string, containerName string) int64 {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.WithVersion(defaultDockerAPIVersion))
if err != nil {
panic(err)
}
resp, err := cli.ContainerCreate(ctx,
&container.Config{
Image: "cpjudge/" + language,
Tty: true,
NetworkDisabled: true,
},
&container.HostConfig{
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: testcasesPath,
Target: "/sandbox/testcases",
},
{
Type: mount.TypeBind,
Source: submissionPath,
Target: "/sandbox/submission",
},
},
}, nil, "cpjudge_"+containerName)
if err != nil {
panic(err)
}
// TODO: Check return status of compilation, timelimit
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
panic(err)
}
case status := <-statusCh:
cli.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{})
fmt.Printf("Status Code %d", status.StatusCode)
return status.StatusCode
}
cli.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{})
// out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
// out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
// if err != nil {
// panic(err)
// }
// io.Copy(os.Stdout, out)
//TODO: return status code
return 0
}