-
Notifications
You must be signed in to change notification settings - Fork 16
/
docker.go
278 lines (252 loc) · 8.04 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package ci
import (
"archive/tar"
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"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"
"github.com/docker/docker/pkg/stdcopy"
"github.com/quickfeed/quickfeed/internal/multierr"
"go.uber.org/zap"
)
var (
DefaultContainerTimeout = time.Duration(10 * time.Minute)
QuickFeedPath = "/quickfeed"
maxToScan = 1_000_000 // bytes
maxLogSize = 30_000 // bytes
lastSegmentSize = 1_000 // bytes
)
// Docker is an implementation of the CI interface using Docker.
type Docker struct {
client *client.Client
logger *zap.SugaredLogger
}
// NewDockerCI returns a runner to run CI tests.
func NewDockerCI(logger *zap.SugaredLogger) (*Docker, error) {
cli, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
)
if err != nil {
return nil, err
}
return &Docker{
client: cli,
logger: logger,
}, nil
}
// Close ensures that the docker client is closed.
func (d *Docker) Close() error {
var syncErr error
if d.logger != nil {
syncErr = d.logger.Sync()
}
closeErr := d.client.Close()
return multierr.Join(syncErr, closeErr)
}
// Run implements the CI interface. This method blocks until the job has been
// completed or an error occurs, e.g., the context times out.
func (d *Docker) Run(ctx context.Context, job *Job) (string, error) {
if d.client == nil {
return "", fmt.Errorf("cannot run job: %s; docker client not initialized", job.Name)
}
resp, err := d.createImage(ctx, job)
if err != nil {
return "", err
}
d.logger.Infof("Created container image '%s' for %s", job.Image, job.Name)
if err = d.client.ContainerStart(ctx, resp.ID, container.StartOptions{}); err != nil {
return "", err
}
d.logger.Infof("Waiting for container image '%s' for %s", job.Image, job.Name)
msg, err := d.waitForContainer(ctx, job, resp.ID)
if err != nil {
return msg, err
}
d.logger.Infof("Done waiting for container image '%s' for %s", job.Image, job.Name)
// extract the logs before removing the container below
logReader, err := d.client.ContainerLogs(ctx, resp.ID, container.LogsOptions{
ShowStdout: true,
})
if err != nil {
return "", err
}
d.logger.Infof("Removing container image '%s' for %s", job.Image, job.Name)
// remove the container when finished to prevent too many open files
err = d.client.ContainerRemove(ctx, resp.ID, container.RemoveOptions{})
if err != nil {
return "", err
}
var stdout bytes.Buffer
if _, err := stdcopy.StdCopy(&stdout, io.Discard, logReader); err != nil {
return "", err
}
if stdout.Len() > maxLogSize+lastSegmentSize {
return truncateLog(&stdout, maxLogSize, lastSegmentSize, maxToScan), nil
}
return stdout.String(), nil
}
// createImage creates an image for the given job.
func (d *Docker) createImage(ctx context.Context, job *Job) (*container.CreateResponse, error) {
if job.Image == "" {
// image name should be specified in a run.sh file in the tests repository
return nil, fmt.Errorf("no image name specified for '%s'", job.Name)
}
if job.Dockerfile != "" {
d.logger.Infof("Removing image '%s' for '%s' prior to rebuild", job.Image, job.Name)
resp, err := d.client.ImageRemove(ctx, job.Image, types.ImageRemoveOptions{Force: true})
if err != nil {
d.logger.Debugf("Expected error (continuing): %v", err)
// continue because we may not have an image to remove
}
for _, r := range resp {
d.logger.Infof("Removed image '%s' for '%s'", r.Deleted, job.Name)
}
d.logger.Infof("Trying to build image: '%s' from Dockerfile", job.Image)
// Log first line of Dockerfile
d.logger.Infof("[%s] Dockerfile: %s ...", job.Image, job.Dockerfile[:strings.Index(job.Dockerfile, "\n")+1])
if err := d.buildImage(ctx, job); err != nil {
return nil, err
}
}
var hostConfig *container.HostConfig
if job.BindDir != "" {
hostConfig = &container.HostConfig{
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: job.BindDir,
Target: QuickFeedPath,
},
},
}
}
create := func() (container.CreateResponse, error) {
return d.client.ContainerCreate(ctx, &container.Config{
Image: job.Image,
User: fmt.Sprintf("%d:%d", os.Getuid(), os.Getgid()), // Run the image as the current user, e.g., quickfeed
Env: job.Env, // Set default environment variables
Cmd: []string{"/bin/bash", "-c", strings.Join(job.Commands, "\n")},
}, hostConfig, nil, nil, job.Name)
}
resp, err := create()
if err != nil {
d.logger.Infof("Image '%s' not yet available for '%s': %v", job.Image, job.Name, err)
d.logger.Infof("Trying to pull image: '%s' from remote repository", job.Image)
if err := d.pullImage(ctx, job.Image); err != nil {
return nil, err
}
// try to create the container again
resp, err = create()
}
return &resp, err
}
// waitForContainer waits until the container stops or context times out.
func (d *Docker) waitForContainer(ctx context.Context, job *Job, respID string) (string, error) {
statusCh, errCh := d.client.ContainerWait(ctx, respID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
d.logger.Errorf("Failed to stop container image '%s' for %s: %v", job.Image, job.Name, err)
if !errors.Is(err, context.DeadlineExceeded) {
return "", err
}
// stop runaway container whose deadline was exceeded
timeout := 1 // seconds to wait before forcefully killing the container
stopErr := d.client.ContainerStop(context.Background(), respID, container.StopOptions{Timeout: &timeout})
if stopErr != nil {
return "", stopErr
}
// remove the docker container (when stopped due to timeout) to prevent too many open files
rmErr := d.client.ContainerRemove(context.Background(), respID, container.RemoveOptions{})
if rmErr != nil {
return "", rmErr
}
// return message to user to be shown in the results log
return "Container timeout. Please check for infinite loops or other slowness.", err
}
case status := <-statusCh:
d.logger.Infof("Container: '%s' for %s: exited with status: %v", job.Image, job.Name, status.StatusCode)
}
return "", nil
}
// pullImage pulls an image from docker hub.
// This can be slow and should be avoided if possible.
func (d *Docker) pullImage(ctx context.Context, image string) error {
progress, err := d.client.ImagePull(ctx, image, types.ImagePullOptions{})
if err != nil {
return err
}
defer progress.Close()
_, err = io.Copy(io.Discard, progress)
return err
}
// buildImage builds and installs an image locally to be reused in a future run.
func (d *Docker) buildImage(ctx context.Context, job *Job) error {
dockerFileContents := []byte(job.Dockerfile)
header := &tar.Header{
Name: "Dockerfile",
Mode: 0o777,
Size: int64(len(dockerFileContents)),
Typeflag: tar.TypeReg,
}
var buf bytes.Buffer
tarWriter := tar.NewWriter(&buf)
if err := tarWriter.WriteHeader(header); err != nil {
return err
}
if _, err := tarWriter.Write(dockerFileContents); err != nil {
return err
}
if err := tarWriter.Close(); err != nil {
return err
}
reader := bytes.NewReader(buf.Bytes())
opts := types.ImageBuildOptions{
Context: reader,
Dockerfile: "Dockerfile",
Tags: []string{job.Image},
}
res, err := d.client.ImageBuild(ctx, reader, opts)
if err != nil {
return err
}
defer res.Body.Close()
return printInfo(d.logger, res.Body)
}
func printInfo(logger *zap.SugaredLogger, rd io.Reader) error {
scanner := bufio.NewScanner(rd)
for scanner.Scan() {
out := &dockerJSON{}
if err := json.Unmarshal([]byte(scanner.Text()), out); err != nil {
return err
}
if out.Error != "" {
return errors.New(out.Error)
}
logger.Info(out)
}
return scanner.Err()
}
type dockerJSON struct {
Status string `json:"status"`
ID string `json:"id"`
Stream string `json:"stream"`
Error string `json:"error"`
}
func (s dockerJSON) String() string {
if len(s.Status) > 0 {
return s.Status + s.ID
}
return strings.TrimSpace(s.Stream)
}