-
Notifications
You must be signed in to change notification settings - Fork 7
/
docker.go
188 lines (159 loc) · 4.96 KB
/
docker.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// InfoMark - a platform for managing courses with
// distributing exercise sheets and testing exercise submissions
// Copyright (C) 2019 ComputerGraphics Tuebingen
// 2020-present InfoMark.org
// Authors: Patrick Wieschollek
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package service
import (
"bytes"
"context"
"fmt"
"time"
"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"
)
// DockerService contains all settings to talk to the docker api
type DockerService struct {
Client *client.Client
Timeout time.Duration
}
func NewDockerServiceWithTimeout(timeout time.Duration) (*DockerService, error) {
cli, err := client.NewClientWithOpts(client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
return &DockerService{
Timeout: timeout,
Client: cli,
}, nil
}
// ListContainers lists all docker containers
func (ds *DockerService) ListContainers() {
ctx := context.Background()
containers, err := ds.Client.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
panic(err)
}
for _, container := range containers {
fmt.Println(container.ID)
fmt.Println(container.Names)
}
}
// ListImages lists all docker images
func (ds *DockerService) ListImages() {
ctx := context.Background()
images, err := ds.Client.ImageList(ctx, types.ImageListOptions{})
if err != nil {
panic(err)
}
for _, image := range images {
fmt.Println(image.ID)
fmt.Println(image.RepoTags)
fmt.Println(image.Size)
fmt.Println(image.VirtualSize)
if len(image.RepoTags) > 0 {
fmt.Println(image.RepoTags[0])
}
fmt.Println("")
}
}
// Pull pulls a docker image
func (ds *DockerService) Pull(image string) (string, error) {
ctx := context.Background()
// image example: "docker.io/library/alpine"
outputReader, err := ds.Client.ImagePull(ctx, image, types.ImagePullOptions{})
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
buf.ReadFrom(outputReader)
return buf.String(), nil
}
// Run executes a docker container and waits for the output
func (ds *DockerService) Run(
imageName string,
submissionZipFile string,
frameworkZipFile string,
DockerMemoryBytes int64,
) (string, int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), ds.Timeout)
defer cancel()
cmds := []string{}
cfg := &container.Config{
Image: imageName,
Cmd: cmds,
Tty: true,
AttachStdin: false,
AttachStdout: true,
AttachStderr: true,
NetworkDisabled: true, // no network activity required
}
// See https://docs.docker.com/config/containers/resource_constraints/#cpu
// Each Worker gets something equivalent to 1 core. If you have 4 cores, this
// will allow each worker to get 100% (eg. 25% per core).
cpu_maximum := int64(100000)
hostCfg := &container.HostConfig{
Resources: container.Resources{
CPUPeriod: cpu_maximum,
CPUQuota: cpu_maximum,
Memory: DockerMemoryBytes,
MemorySwap: 0,
},
Mounts: []mount.Mount{
{
ReadOnly: true,
Type: mount.TypeBind,
Source: submissionZipFile,
Target: "/data/submission.zip",
},
{
ReadOnly: true,
Type: mount.TypeBind,
Source: frameworkZipFile,
Target: "/data/unittest.zip",
},
},
}
resp, err := ds.Client.ContainerCreate(ctx, cfg, hostCfg, nil, nil, "")
if err != nil {
return "", 0, err
}
defer ds.Client.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{Force:true})
if err := ds.Client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
return "", 0, err
}
defer ds.Client.ContainerKill(context.Background(), resp.ID, "9")
statusCh, errCh := ds.Client.ContainerWait(ctx, resp.ID, "")
select {
case <-ctx.Done():
return "Execution took too long (Timeout: "+ds.Timeout.String()+")", 0, nil
case err := <-errCh:
return err.Error(), 0, err
case <-statusCh:
}
outputReader, err := ds.Client.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
if err != nil {
return "", 0, err
}
buf := new(bytes.Buffer)
len, err := buf.ReadFrom(outputReader)
// avoid submitting large outputs to the database
// postgres will not accept more than 64kB and we don't want that much either
if (len > 32*1024) {
return "Output too large (you're printing too much stuff)", 0, nil
}
return buf.String(), 0, nil
}