This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
job.go
446 lines (382 loc) · 11.2 KB
/
job.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package main
import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strings"
"time"
dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/mount"
docker "github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/stdcopy"
"github.com/skroutz/mistry/pkg/filesystem"
"github.com/skroutz/mistry/pkg/types"
"github.com/skroutz/mistry/pkg/utils"
)
// Job is the core unit of work. It is essentially something that needs to
// be executed in order to produce the desired artifacts.
type Job struct {
ID string
// user-provided
Project string
Params types.Params
Group string
Rebuild bool
RootBuildPath string
PendingBuildPath string
ReadyBuildPath string
LatestBuildPath string
ReadyDataPath string
ProjectPath string
// NOTE: after a job is complete, this points to an invalid (pending)
// path
BuildLogPath string
BuildInfoFilePath string
// docker-related
Image string
ImageTar []byte
Container string
StartedAt time.Time
BuildInfo *types.BuildInfo
State string
Log *log.Logger
}
// NewJobFromRequest returns a new Job from the JobRequest
func NewJobFromRequest(jr types.JobRequest, cfg *Config) (*Job, error) {
j, err := NewJob(jr.Project, jr.Params, jr.Group, cfg)
if err != nil {
return nil, err
}
j.Rebuild = jr.Rebuild
return j, nil
}
// NewJob returns a new Job for the given project. project and cfg cannot be
// empty.
func NewJob(project string, params types.Params, group string, cfg *Config) (*Job, error) {
var err error
if project == "" {
return nil, errors.New("no project given")
}
if cfg == nil {
return nil, errors.New("invalid configuration")
}
j := new(Job)
j.Project = project
j.Group = group
j.Params = params
j.ProjectPath = filepath.Join(cfg.ProjectsPath, j.Project)
j.RootBuildPath = filepath.Join(cfg.BuildPath, j.Project)
if j.Group == "" {
j.LatestBuildPath = filepath.Join(j.RootBuildPath, "latest")
} else {
j.LatestBuildPath = filepath.Join(j.RootBuildPath, "groups", j.Group)
}
j.ImageTar, err = utils.Tar(j.ProjectPath)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("Unknown project '%s'", j.Project)
}
return nil, err
}
// compute ID
keys := []string{}
for k := range params {
// params opaque to the build are not taken into account
// when calculating a job's ID
if strings.HasPrefix(k, "_") {
continue
}
keys = append(keys, k)
}
sort.Strings(keys)
seed := project + group
for _, v := range keys {
seed += v + params[v]
}
seed += string(j.ImageTar)
j.ID = fmt.Sprintf("%x", sha256.Sum256([]byte(seed)))
j.PendingBuildPath = filepath.Join(j.RootBuildPath, "pending", j.ID)
j.ReadyBuildPath = filepath.Join(j.RootBuildPath, "ready", j.ID)
j.ReadyDataPath = filepath.Join(j.ReadyBuildPath, DataDir)
j.BuildLogPath = BuildLogPath(j.PendingBuildPath)
j.BuildInfoFilePath = filepath.Join(j.PendingBuildPath, BuildInfoFname)
j.Image = ImgCntPrefix + j.Project
j.Container = ImgCntPrefix + j.ID
j.StartedAt = time.Now()
j.BuildInfo = new(types.BuildInfo)
j.State = "pending"
j.Log = log.New(os.Stderr, fmt.Sprintf("[%s] ", j), log.Ldate|log.Ltime)
return j, nil
}
// BuildImage builds the Docker image denoted by j.Image. If there is an
// error, it will be of type types.ErrImageBuild.
func (j *Job) BuildImage(ctx context.Context, uid string, c *docker.Client, out io.Writer, pullParent, noCache bool) error {
buildArgs := make(map[string]*string)
buildArgs["uid"] = &uid
buildOpts := dockertypes.ImageBuildOptions{
Tags: []string{j.Image},
BuildArgs: buildArgs,
NetworkMode: "host",
PullParent: pullParent,
NoCache: noCache,
ForceRemove: true,
}
resp, err := c.ImageBuild(context.Background(), bytes.NewBuffer(j.ImageTar), buildOpts)
if err != nil {
return types.ErrImageBuild{Image: j.Image, Err: err}
}
defer resp.Body.Close()
err = jsonmessage.DisplayJSONMessagesStream(resp.Body, out, 0, false, nil)
if err != nil {
return types.ErrImageBuild{Image: j.Image, Err: err}
}
_, _, err = c.ImageInspectWithRaw(context.Background(), j.Image)
if err != nil {
return types.ErrImageBuild{Image: j.Image, Err: err}
}
return nil
}
// StartContainer creates and runs the container. It blocks until the container
// exits and returns the exit code of the container command. If there was an error
// starting the container, the exit code is irrelevant.
//
// NOTE: If there was an error with the user's dockerfile, the returned exit
// code will be 1 and the error nil.
func (j *Job) StartContainer(ctx context.Context, cfg *Config, c *docker.Client, out, outErr io.Writer) (int, error) {
config := container.Config{User: cfg.UID, Image: j.Image}
mnts := []mount.Mount{{Type: mount.TypeBind, Source: filepath.Join(j.PendingBuildPath, DataDir), Target: DataDir}}
for src, target := range cfg.Mounts {
mnts = append(mnts, mount.Mount{Type: mount.TypeBind, Source: src, Target: target})
}
hostConfig := container.HostConfig{Mounts: mnts, AutoRemove: false, NetworkMode: "host"}
err := renameIfExists(ctx, c, j.Container)
res, err := c.ContainerCreate(ctx, &config, &hostConfig, nil, j.Container)
if err != nil {
return 0, err
}
err = c.ContainerStart(ctx, res.ID, dockertypes.ContainerStartOptions{})
if err != nil {
return 0, err
}
defer func(id string) {
err = c.ContainerRemove(ctx, id, dockertypes.ContainerRemoveOptions{})
if err != nil {
log.Printf("[%s] cannot remove container: %s", j, err)
}
}(res.ID)
logs, err := c.ContainerLogs(ctx, res.ID,
dockertypes.ContainerLogsOptions{Follow: true, ShowStdout: true, ShowStderr: true,
Details: true})
if err != nil {
return 0, err
}
defer logs.Close()
_, err = stdcopy.StdCopy(out, io.MultiWriter(out, outErr), logs)
if err != nil {
return 0, err
}
var result struct {
State struct {
ExitCode int
}
}
_, inspect, err := c.ContainerInspectWithRaw(ctx, res.ID, false)
if err != nil {
return 0, err
}
err = json.Unmarshal(inspect, &result)
if err != nil {
return 0, err
}
return result.State.ExitCode, nil
}
// renameIfExists searches for containers with the passed name and renames them
// by appending a random suffix to their name
func renameIfExists(ctx context.Context, c *docker.Client, name string) error {
filter := filters.NewArgs()
filter.Add("name", name)
containers, err := c.ContainerList(ctx, dockertypes.ContainerListOptions{
Quiet: true,
All: true,
Limit: -1,
Filters: filter,
})
if err != nil {
return err
}
for _, container := range containers {
err := c.ContainerRename(ctx, container.ID, name+"-renamed-"+randomHexString())
if err != nil {
return err
}
}
return nil
}
func randomHexString() string {
buf := make([]byte, 16)
rand.Read(buf)
return hex.EncodeToString(buf)
}
func (j *Job) String() string {
return fmt.Sprintf(
"{project=%s group=%s id=%s}",
j.Project, j.Group, j.ID[:7])
}
// MarshalJSON serializes the Job to JSON
func (j *Job) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
ID string `json:"id"`
Project string `json:"project"`
StartedAt string `json:"startedAt"`
BuildInfo types.BuildInfo `json:"buildInfo"`
State string `json:"state"`
}{
ID: j.ID,
Project: j.Project,
StartedAt: j.StartedAt.Format(DateFmt),
BuildInfo: *j.BuildInfo,
State: j.State,
})
}
// UnmarshalJSON deserializes JSON data and updates the Job
// with them
func (j *Job) UnmarshalJSON(data []byte) error {
jData := &struct {
ID string `json:"id"`
Project string `json:"project"`
StartedAt string `json:"startedAt"`
BuildInfo types.BuildInfo `json:"buildInfo"`
State string `json:"state"`
}{}
err := json.Unmarshal(data, &jData)
if err != nil {
return err
}
j.ID = jData.ID
j.Project = jData.Project
j.StartedAt, err = time.Parse(DateFmt, jData.StartedAt)
if err != nil {
return err
}
j.BuildInfo = &jData.BuildInfo
j.State = jData.State
return nil
}
// GetState determines the job's current state by using it's path in the filesystem.
func GetState(path, project, id string) (string, error) {
pPath := filepath.Join(path, project, "pending", id)
rPath := filepath.Join(path, project, "ready", id)
_, err := os.Stat(pPath)
if err == nil {
return "pending", nil
}
_, err = os.Stat(rPath)
if err == nil {
return "ready", nil
}
return "", fmt.Errorf("job with id=%s not found error", id)
}
// CloneSrcPath returns the build path that should be used as the base
// point for j (ie. incremental building) or an empty string if none should
// be used.
func (j *Job) CloneSrcPath() string {
cloneSrc := ""
if j.Group != "" {
var symlinkErr error
cloneSrc, symlinkErr = filepath.EvalSymlinks(j.LatestBuildPath)
if symlinkErr != nil {
cloneSrc = ""
s := "skipping build cache"
if os.IsNotExist(symlinkErr) {
j.Log.Printf("latest link doesn't exist, %s", s)
} else {
j.Log.Printf("error reading latest link: %s, %s", symlinkErr, s)
}
}
}
return cloneSrc
}
// BootstrapBuildDir creates all required build directories. Cleans the
// pending directory if there were any errors.
func (j *Job) BootstrapBuildDir(fs filesystem.FileSystem) error {
var err error
cloneSrc := j.CloneSrcPath()
if cloneSrc == "" {
err = fs.Create(j.PendingBuildPath)
} else {
err = fs.Clone(cloneSrc, j.PendingBuildPath)
j.BuildInfo.Incremental = true
}
if err != nil {
return workErr("could not create pending build path", err)
}
// if we cloned, empty the params dir
if cloneSrc != "" {
err = os.RemoveAll(filepath.Join(j.PendingBuildPath, DataDir, ParamsDir))
if err != nil {
return workErr("could not remove params dir", err)
}
}
dirs := [4]string{
filepath.Join(j.PendingBuildPath, DataDir),
filepath.Join(j.PendingBuildPath, DataDir, CacheDir),
filepath.Join(j.PendingBuildPath, DataDir, ArtifactsDir),
filepath.Join(j.PendingBuildPath, DataDir, ParamsDir),
}
for _, dir := range dirs {
err = utils.EnsureDirExists(dir)
if err != nil {
return workErr("could not ensure directory exists", err)
}
}
return err
}
// BuildLogPath returns the path of the job logs found at jobPath
func BuildLogPath(jobPath string) string {
return filepath.Join(jobPath, BuildLogFname)
}
// ReadJobLogs returns the job logs found at jobPath
func ReadJobLogs(jobPath string) ([]byte, error) {
buildLogPath := BuildLogPath(jobPath)
log, err := ioutil.ReadFile(buildLogPath)
if err != nil {
return nil, err
}
return log, nil
}
// ReadJobBuildInfo returns a BuildInfo from the given path. If logs is true,
// BuildInfo.Log will contain the build logs.
func ReadJobBuildInfo(path string, logs bool) (*types.BuildInfo, error) {
buildInfoPath := filepath.Join(path, BuildInfoFname)
buildInfo := types.NewBuildInfo()
buildInfoBytes, err := ioutil.ReadFile(buildInfoPath)
if err != nil {
return nil, err
}
err = json.Unmarshal(buildInfoBytes, &buildInfo)
if err != nil {
return nil, err
}
if logs {
log, err := ReadJobLogs(path)
if err != nil {
return nil, err
}
buildInfo.ContainerStdouterr = string(log)
}
return buildInfo, nil
}