-
Notifications
You must be signed in to change notification settings - Fork 1
/
env.container.go
648 lines (572 loc) · 17.2 KB
/
env.container.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
package action
import (
"context"
"errors"
"fmt"
"io"
osuser "os/user"
"path/filepath"
"runtime"
"strings"
"github.com/moby/moby/pkg/archive"
"github.com/moby/moby/pkg/jsonmessage"
"github.com/moby/moby/pkg/namesgenerator"
"github.com/moby/sys/signal"
"github.com/moby/term"
"github.com/launchrctl/launchr/pkg/cli"
"github.com/launchrctl/launchr/pkg/driver"
"github.com/launchrctl/launchr/pkg/jsonschema"
"github.com/launchrctl/launchr/pkg/log"
"github.com/launchrctl/launchr/pkg/types"
)
const (
// Container mount paths.
containerHostMount = "/host"
containerActionMount = "/action"
// Environment specific flags.
containerFlagUseVolumeWD = "use-volume-wd"
containerFlagRemoveImage = "remove-image"
containerFlagNoCache = "no-cache"
containerFlagEntrypoint = "entrypoint"
containerFlagExec = "exec"
)
type containerEnv struct {
driver driver.ContainerRunner
dtype driver.Type
// Container related functionality extenders
imgres ChainImageBuildResolver
imgccres *ImageBuildCacheResolver
nameprv ContainerNameProvider
// Runtime flags
useVolWD bool
removeImg bool
noCache bool
entrypoint string
entrypointSet bool
exec bool
}
// ContainerNameProvider provides an ability to generate a random container name
type ContainerNameProvider struct {
Prefix string
RandomSuffix bool
}
// Get generates a new container name
func (p ContainerNameProvider) Get(name string) string {
var rpl = strings.NewReplacer("-", "_", ":", "_", ".", "_")
suffix := ""
if p.RandomSuffix {
suffix = "_" + namesgenerator.GetRandomName(0)
}
return p.Prefix + rpl.Replace(name) + suffix
}
// NewDockerEnvironment creates a new action Docker environment.
func NewDockerEnvironment() RunEnvironment {
return NewContainerEnvironment(driver.Docker)
}
// NewContainerEnvironment creates a new action container run environment.
func NewContainerEnvironment(t driver.Type) RunEnvironment {
return &containerEnv{dtype: t, nameprv: ContainerNameProvider{Prefix: "launchr_", RandomSuffix: true}}
}
func (c *containerEnv) FlagsDefinition() OptionsList {
return OptionsList{
&Option{
Name: containerFlagUseVolumeWD,
Title: "Use volume as a WD",
Description: "Copy the working directory to a container volume and not bind local paths. Usually used with remote environments.",
Type: jsonschema.Boolean,
Default: false,
},
&Option{
Name: containerFlagRemoveImage,
Title: "Remove Image",
Description: "Remove an image after execution of action",
Type: jsonschema.Boolean,
Default: false,
},
&Option{
Name: containerFlagNoCache,
Title: "No cache",
Description: "Send command to build container without cache",
Type: jsonschema.Boolean,
Default: false,
},
&Option{
Name: containerFlagEntrypoint,
Title: "Image Entrypoint",
Description: "Overwrite the default ENTRYPOINT of the image",
Type: jsonschema.String,
Default: "",
},
&Option{
Name: containerFlagExec,
Title: "Exec command",
Description: "Overwrite CMD definition of the container",
Type: jsonschema.Boolean,
Default: false,
},
}
}
func (c *containerEnv) UseFlags(flags TypeOpts) error {
if v, ok := flags[containerFlagUseVolumeWD]; ok {
c.useVolWD = v.(bool)
}
if r, ok := flags[containerFlagRemoveImage]; ok {
c.removeImg = r.(bool)
}
if nc, ok := flags[containerFlagNoCache]; ok {
c.noCache = nc.(bool)
}
if e, ok := flags[containerFlagEntrypoint]; ok {
c.entrypointSet = true
c.entrypoint = e.(string)
}
if ex, ok := flags[containerFlagExec]; ok {
c.exec = ex.(bool)
}
return nil
}
func (c *containerEnv) ValidateInput(a *Action, args TypeArgs) error {
if c.exec {
return nil
}
// Check arguments if no exec flag present.
return a.ValidateInput(args)
}
func (c *containerEnv) AddImageBuildResolver(r ImageBuildResolver) { c.imgres = append(c.imgres, r) }
func (c *containerEnv) SetImageBuildCacheResolver(s *ImageBuildCacheResolver) { c.imgccres = s }
func (c *containerEnv) SetContainerNameProvider(p ContainerNameProvider) { c.nameprv = p }
func (c *containerEnv) Init() (err error) {
if c.driver == nil {
c.driver, err = driver.New(c.dtype)
}
return err
}
func (c *containerEnv) Execute(ctx context.Context, a *Action) (err error) {
ctx, cancelFn := context.WithCancel(ctx)
defer cancelFn()
if err = c.Init(); err != nil {
return err
}
streams := a.GetInput().IO
actConf := a.ActionDef()
log.Debug("Starting execution of the action %q in %q environment, command %v", a.ID, c.dtype, actConf.Command)
// @todo consider reusing the same container and run exec
name := c.nameprv.Get(a.ID)
existing := c.driver.ContainerList(ctx, types.ContainerListOptions{SearchName: name})
if len(existing) > 0 {
return fmt.Errorf("the action %q can't start, the container name is in use, please, try again", a.ID)
}
var autoRemove = true
if c.useVolWD {
// Do not remove the volume until we copy the data back.
autoRemove = false
}
// Add entrypoint command option.
var entrypoint []string
if c.entrypointSet {
entrypoint = []string{c.entrypoint}
}
// Create container.
runConfig := &types.ContainerCreateOptions{
ContainerName: name,
ExtraHosts: actConf.ExtraHosts,
AutoRemove: autoRemove,
OpenStdin: true,
StdinOnce: true,
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Tty: streams.In().IsTerminal(),
Env: actConf.Env,
User: getCurrentUser(),
Entrypoint: entrypoint,
}
log.Debug("Creating a container for action %q", a.ID)
cid, err := c.containerCreate(ctx, a, runConfig)
if err != nil {
return err
}
if cid == "" {
return errors.New("error on creating a container")
}
log.Debug("Successfully created container %q for action %q", cid, a.ID)
// Copy working dirs to the container.
if c.useVolWD {
// @todo test somehow.
cli.Println(`Flag "--%s" is set. Copying the working directory inside the container.`, containerFlagUseVolumeWD)
err = c.copyDirToContainer(ctx, cid, a.WorkDir(), containerHostMount)
if err != nil {
return err
}
err = c.copyDirToContainer(ctx, cid, a.Dir(), containerActionMount)
if err != nil {
return err
}
}
// Check if TTY was requested, but not supported.
if ttyErr := streams.In().CheckTty(runConfig.AttachStdin, runConfig.Tty); ttyErr != nil {
return ttyErr
}
if !runConfig.Tty {
log.Debug("Start watching signals %q, action %q", cid, a.ID)
sigc := notifyAllSignals()
go ForwardAllSignals(ctx, c.driver, cid, sigc)
defer signal.StopCatch(sigc)
}
// Attach streams to the terminal.
log.Debug("Attaching streams of %q, action %q", cid, a.ID)
cio, errCh, err := c.attachContainer(ctx, streams, cid, runConfig)
if err != nil {
return err
}
defer func() {
_ = cio.Close()
}()
log.Debug("Watching status of %q, action %q", cid, a.ID)
statusCh := c.containerWait(ctx, cid, runConfig)
// Start the container
log.Debug("Starting container %q, action %q", cid, a.ID)
if err = c.driver.ContainerStart(ctx, cid, types.ContainerStartOptions{}); err != nil {
log.Debug("Failed starting the container %q, action %q", cid, a.ID)
cancelFn()
<-errCh
if runConfig.AutoRemove {
<-statusCh
}
return err
}
// Resize TTY on window resize.
if runConfig.Tty {
log.Debug("Watching TTY resize %q, action %q", cid, a.ID)
if err = driver.MonitorTtySize(ctx, c.driver, streams, cid, false); err != nil {
log.Err("Error monitoring TTY size:", err)
}
}
log.Debug("Waiting execution of %q, action %q", cid, a.ID)
if errCh != nil {
if err = <-errCh; err != nil {
if _, ok := err.(term.EscapeError); ok {
// The user entered the detach escape sequence.
return nil
}
log.Debug("Error hijack: %s", err)
return err
}
}
status := <-statusCh
// @todo maybe we should note that SIG was sent to the container. Code 130 is sent on Ctlr+C.
msg := fmt.Sprintf("action %q finished with the exit code %d", a.ID, status)
log.Info(msg)
if status != 0 {
err = RunStatusError{code: status, msg: msg}
}
// Copy back the result from the volume.
// @todo it's a bad implementation considering consequential runs, need to find a better way to sync with remote.
if c.useVolWD {
path := a.WorkDir()
cli.Println(`Flag "--%s" is set. Copying back the result of the action run.`, containerFlagUseVolumeWD)
err = c.copyFromContainer(ctx, cid, containerHostMount, filepath.Dir(path), filepath.Base(path))
defer func() {
err = c.driver.ContainerRemove(ctx, cid, types.ContainerRemoveOptions{})
if err != nil {
log.Err("Error on cleaning the running environment: %v", err)
}
}()
if err != nil {
return err
}
}
defer func() {
if !c.removeImg {
return
}
log.Debug("Removing container %q, action %q", cid, a.ID)
errImg := c.imageRemove(ctx, a)
if errImg != nil {
log.Err("Image remove returned an error: %v", errImg)
} else {
cli.Println("Image %q was successfully removed", a.ActionDef().Image)
}
}()
return err
}
func getCurrentUser() string {
curuser := ""
// If running in a container native environment, run container as a current user.
// @todo review, it won't work with a remote context.
switch runtime.GOOS {
case "linux", "darwin":
u, err := osuser.Current()
if err == nil {
curuser = u.Uid + ":" + u.Gid
}
}
return curuser
}
func (c *containerEnv) Close() error {
return c.driver.Close()
}
func (c *containerEnv) imageRemove(ctx context.Context, a *Action) error {
_, err := c.driver.ImageRemove(ctx, a.ActionDef().Image, types.ImageRemoveOptions{
Force: true,
PruneChildren: false,
})
return err
}
func (c *containerEnv) isRebuildRequired(bi *types.BuildDefinition) (bool, error) {
// @todo test image cache resolution somehow.
if c.imgccres == nil || bi == nil {
return false, nil
}
err := c.imgccres.EnsureLoaded()
if err != nil {
return false, err
}
dirSum, err := c.imgccres.DirHash(bi.Context)
if err != nil {
return false, err
}
doRebuild := false
for _, tag := range bi.Tags {
sum := c.imgccres.GetSum(tag)
if sum != dirSum {
c.imgccres.SetSum(tag, dirSum)
doRebuild = true
}
}
if errCache := c.imgccres.Save(); errCache != nil {
log.Warn("Failed to update actions.sum file: %v", errCache)
}
return doRebuild, nil
}
func (c *containerEnv) imageEnsure(ctx context.Context, a *Action) error {
streams := a.GetInput().IO
image := a.ActionDef().Image
// Prepend action to have the top priority in image build resolution.
r := ChainImageBuildResolver{append(ChainImageBuildResolver{a}, c.imgres...)}
buildInfo := r.ImageBuildInfo(image)
forceRebuild, err := c.isRebuildRequired(buildInfo)
if err != nil {
return err
}
status, err := c.driver.ImageEnsure(ctx, types.ImageOptions{
Name: image,
Build: buildInfo,
NoCache: c.noCache,
ForceRebuild: forceRebuild,
})
if err != nil {
return err
}
switch status.Status {
case types.ImageExists:
msg := fmt.Sprintf("Image %q exists locally", image)
cli.Println(msg)
log.Info(msg)
case types.ImagePull:
if status.Progress == nil {
break
}
defer func() {
_ = status.Progress.Close()
}()
msg := fmt.Sprintf("Image %q doesn't exist locally, pulling from the registry", image)
cli.Println(msg)
log.Info(msg)
// Output docker status only in Debug.
err = displayJSONMessages(status.Progress, streams)
if err != nil {
cli.Println("There was an error while pulling the image")
}
case types.ImageBuild:
if status.Progress == nil {
break
}
defer func() {
_ = status.Progress.Close()
}()
msg := fmt.Sprintf("Image %q doesn't exist locally, building...", image)
cli.Println(msg)
log.Info(msg)
// Output docker status only in Debug.
err = displayJSONMessages(status.Progress, streams)
if err != nil {
cli.Println("There was an error while building the image")
}
}
return err
}
func displayJSONMessages(in io.Reader, streams cli.Streams) error {
err := jsonmessage.DisplayJSONMessagesToStream(in, streams.Out(), nil)
if err != nil {
if jerr, ok := err.(*jsonmessage.JSONError); ok {
// If no error code is set, default to 1
if jerr.Code == 0 {
jerr.Code = 1
}
return jerr
}
}
return err
}
func (c *containerEnv) containerCreate(ctx context.Context, a *Action, opts *types.ContainerCreateOptions) (string, error) {
if err := c.imageEnsure(ctx, a); err != nil {
return "", err
}
// Create a container
actConf := a.ActionDef()
// Override Cmd with exec command.
if c.exec {
actConf.Command = a.GetInput().ArgsRaw
}
createOpts := types.ContainerCreateOptions{
ContainerName: opts.ContainerName,
Image: actConf.Image,
Cmd: actConf.Command,
WorkingDir: containerHostMount,
NetworkMode: types.NetworkModeHost,
ExtraHosts: opts.ExtraHosts,
AutoRemove: opts.AutoRemove,
OpenStdin: opts.OpenStdin,
StdinOnce: opts.StdinOnce,
AttachStdin: opts.AttachStdin,
AttachStdout: opts.AttachStdout,
AttachStderr: opts.AttachStderr,
Tty: opts.Tty,
Env: opts.Env,
User: opts.User,
Entrypoint: opts.Entrypoint,
}
if c.useVolWD {
// Use anonymous volumes to be removed after finish.
createOpts.Volumes = map[string]struct{}{
containerHostMount: {},
containerActionMount: {},
}
} else {
createOpts.Binds = []string{
absPath(a.WorkDir()) + ":" + containerHostMount,
absPath(a.Dir()) + ":" + containerActionMount,
}
}
cid, err := c.driver.ContainerCreate(ctx, createOpts)
if err != nil {
return "", err
}
return cid, nil
}
func absPath(src string) string {
abs, err := filepath.Abs(filepath.Clean(src))
if err != nil {
panic(err)
}
return abs
}
// copyDirToContainer copies dir content to a container.
func (c *containerEnv) copyDirToContainer(ctx context.Context, cid, srcPath, dstPath string) error {
return c.copyToContainer(ctx, cid, srcPath, filepath.Dir(dstPath), filepath.Base(dstPath))
}
// copyToContainer copies dir/file to a container. Directory will be copied as a subdirectory.
func (c *containerEnv) copyToContainer(ctx context.Context, cid, srcPath, dstPath, rebaseName string) error {
// Prepare destination copy info by stat-ing the container path.
dstInfo := archive.CopyInfo{Path: dstPath}
dstStat, err := c.driver.ContainerStatPath(ctx, cid, dstPath)
if err != nil {
return err
}
dstInfo.Exists, dstInfo.IsDir = true, dstStat.Mode.IsDir()
// Prepare source copy info.
srcInfo, err := archive.CopyInfoSourcePath(absPath(srcPath), false)
if err != nil {
return err
}
srcInfo.RebaseName = rebaseName
srcArchive, err := archive.TarResource(srcInfo)
if err != nil {
return err
}
defer srcArchive.Close()
dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, dstInfo)
if err != nil {
return err
}
defer preparedArchive.Close()
options := types.CopyToContainerOptions{
AllowOverwriteDirWithFile: false,
CopyUIDGID: false,
}
return c.driver.CopyToContainer(ctx, cid, dstDir, preparedArchive, options)
}
func resolveLocalPath(localPath string) (absPath string, err error) {
if absPath, err = filepath.Abs(localPath); err != nil {
return
}
return archive.PreserveTrailingDotOrSeparator(absPath, localPath), nil
}
func (c *containerEnv) copyFromContainer(ctx context.Context, cid, srcPath, dstPath, rebaseName string) (err error) {
// Get an absolute destination path.
dstPath, err = resolveLocalPath(dstPath)
if err != nil {
return err
}
content, stat, err := c.driver.CopyFromContainer(ctx, cid, srcPath)
if err != nil {
return err
}
defer content.Close()
srcInfo := archive.CopyInfo{
Path: srcPath,
Exists: true,
IsDir: stat.Mode.IsDir(),
RebaseName: rebaseName,
}
preArchive := content
if len(srcInfo.RebaseName) != 0 {
_, srcBase := archive.SplitPathDirEntry(srcInfo.Path)
preArchive = archive.RebaseArchiveEntries(content, srcBase, srcInfo.RebaseName)
}
return archive.CopyTo(preArchive, srcInfo, dstPath)
}
func (c *containerEnv) containerWait(ctx context.Context, cid string, opts *types.ContainerCreateOptions) <-chan int {
// Wait for the container to stop or catch error.
waitCond := types.WaitConditionNextExit
if opts.AutoRemove {
waitCond = types.WaitConditionRemoved
}
resCh, errCh := c.driver.ContainerWait(ctx, cid, types.ContainerWaitOptions{Condition: waitCond})
statusC := make(chan int)
go func() {
select {
case err := <-errCh:
log.Err("error waiting for container: %v", err)
statusC <- 125
case res := <-resCh:
if res.Error != nil {
log.Err("error waiting for container: %v", res.Error)
statusC <- 125
} else {
statusC <- res.StatusCode
}
case <-ctx.Done():
log.Info("stopping waiting for container on context finish")
statusC <- 125
}
}()
return statusC
}
func (c *containerEnv) attachContainer(ctx context.Context, streams cli.Streams, cid string, opts *types.ContainerCreateOptions) (io.Closer, <-chan error, error) {
cio, errAttach := c.driver.ContainerAttach(ctx, cid, types.ContainerAttachOptions{
Stream: true,
Stdin: opts.AttachStdin,
Stdout: opts.AttachStdout,
Stderr: opts.AttachStderr,
})
if errAttach != nil {
return nil, nil, errAttach
}
errCh := make(chan error, 1)
go func() {
errCh <- driver.ContainerIOStream(ctx, streams, cio, opts)
}()
return cio, errCh, nil
}