-
Notifications
You must be signed in to change notification settings - Fork 24
/
docker_tasks.go
1003 lines (810 loc) · 28 KB
/
docker_tasks.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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package clients
import (
"archive/tar"
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"os"
gosignal "os/signal"
"path"
"strconv"
"strings"
"sync"
"time"
"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"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/signal"
"github.com/docker/docker/pkg/term"
"github.com/docker/go-connections/nat"
"github.com/hashicorp/go-hclog"
"github.com/shipyard-run/shipyard/pkg/clients/streams"
"github.com/shipyard-run/shipyard/pkg/config"
"github.com/shipyard-run/shipyard/pkg/utils"
"golang.org/x/xerrors"
)
// DockerTasks is a concrete implementation of ContainerTasks which uses the Docker SDK
type DockerTasks struct {
c Docker
il ImageLog
force bool
l hclog.Logger
}
// NewDockerTasks creates a DockerTasks with the given Docker client
func NewDockerTasks(c Docker, il ImageLog, l hclog.Logger) *DockerTasks {
return &DockerTasks{c: c, il: il, l: l}
}
// SetForcePull sets a global override for the DockerTasks, when set to true
// Images will always be pulled from remote registries
func (d *DockerTasks) SetForcePull(force bool) {
d.force = force
}
// CreateContainer creates a new Docker container for the given configuation
func (d *DockerTasks) CreateContainer(c *config.Container) (string, error) {
d.l.Debug("Creating Docker Container", "ref", c.Name)
// create a unique name based on service network [container].[network].shipyard
// attach to networks
// - networkRef
// - wanRef
// convert the environment vars to a list of [key]=[value]
env := make([]string, 0)
for _, kv := range c.Environment {
env = append(env, fmt.Sprintf("%s=%s", kv.Key, kv.Value))
}
// convert the new environment map to a list of [key]=[value]
for k, v := range c.EnvVar {
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
// create the container config
dc := &container.Config{
Hostname: c.Name,
Image: c.Image.Name,
Env: env,
Cmd: c.Command,
Entrypoint: c.Entrypoint,
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
}
// create the host and network configs
hc := &container.HostConfig{}
nc := &network.NetworkingConfig{}
// https: //docs.docker.com/config/containers/resource_constraints/#cpu
rc := container.Resources{}
if c.Resources != nil {
// set memory if set
if c.Resources.Memory > 0 {
rc.Memory = int64(c.Resources.Memory) * 1000000 // docker specifies memory in bytes, shipyard megabytes
}
if c.Resources.CPU > 0 {
rc.CPUQuota = int64(c.Resources.CPU) * 100
}
// cupsets are not supported on windows
if len(c.Resources.CPUPin) > 0 {
cpuPin := make([]string, len(c.Resources.CPUPin))
for i, v := range c.Resources.CPUPin {
cpuPin[i] = fmt.Sprintf("%d", v)
}
rc.CpusetCpus = strings.Join(cpuPin, ",")
}
hc.Resources = rc
}
// by default the container should NOT be attached to a network
nc.EndpointsConfig = make(map[string]*network.EndpointSettings)
// Create volume mounts
mounts := make([]mount.Mount, 0)
for _, vc := range c.Volumes {
// default mount type to bind
t := mount.TypeBind
// select mount type if set
switch vc.Type {
case "bind":
t = mount.TypeBind
case "volume":
t = mount.TypeVolume
case "tmpfs":
t = mount.TypeTmpfs
}
// if we have a bind type mount then ensure that the local folder exists or
// an error will be raised when creating
if t == mount.TypeBind {
// check to see id the source exists
_, err := os.Stat(vc.Source)
if err != nil {
d.l.Debug("Creating directory for container volume", "ref", c.Name, "directory", vc.Source, "volume", vc.Destination)
// source does not exist, create the source as a directory
err := os.MkdirAll(vc.Source, os.ModePerm)
if err != nil {
return "", xerrors.Errorf("Source for Volume %s does not exist, error creating directory: %w", err)
}
}
}
// create the mount
mounts = append(mounts, mount.Mount{
Type: t,
Source: vc.Source,
Target: vc.Destination,
ReadOnly: vc.ReadOnly,
})
}
hc.Mounts = mounts
// create the ports config
ports := createPublishedPorts(c.Ports)
dc.ExposedPorts = ports.ExposedPorts
hc.PortBindings = ports.PortBindings
// create the port ranges
portRanges, err := createPublishedPortRanges(c.PortRanges)
if err != nil {
return "", xerrors.Errorf("Unable to attach to container network, invalid port range: %w", err)
}
for k, p := range portRanges.ExposedPorts {
// check that the port has not already been defined
if _, ok := dc.ExposedPorts[k]; !ok {
dc.ExposedPorts[k] = p
hc.PortBindings[k] = portRanges.PortBindings[k]
}
}
//dc.ExposedPorts = ports.ExposedPorts
//hc.PortBindings = ports.PortBindings
// is this a privlidged container
hc.Privileged = c.Privileged
// are we attaching the container to a sidecar network?
for _, n := range c.Networks {
net, err := c.FindDependentResource(n.Name)
if err != nil {
return "", xerrors.Errorf("Network not found: %w", err)
}
if net.Info().Type == config.TypeContainer {
// find the id of the container
ids, err := d.FindContainerIDs(net.Info().Name, net.Info().Type)
if err != nil {
return "", xerrors.Errorf("Unable to attach to container network, ID for container not found: %w", err)
}
if len(ids) != 1 {
return "", xerrors.Errorf("Unable to attach to container network, ID for container not found")
}
d.l.Debug("Attaching container as sidecar", "ref", c.Name, "container", n.Name)
// set the container network
hc.NetworkMode = container.NetworkMode(fmt.Sprintf("container:%s", ids[0]))
// when using container networking can not use a hostname
dc.Hostname = ""
}
}
cont, err := d.c.ContainerCreate(
context.Background(),
dc,
hc,
nc,
utils.FQDN(c.Name, string(c.Type)),
)
if err != nil {
return "", err
}
// first remove the container from the bridge network if we are adding custom networks
// all containers should have custom networks
// only add networks if we are not adding the container network
if len(c.Networks) > 0 && !hc.NetworkMode.IsContainer() {
err := d.c.NetworkDisconnect(context.Background(), "bridge", cont.ID, true)
if err != nil {
return "", xerrors.Errorf("Unable to remove container from the default bridge network: %w", err)
}
for _, n := range c.Networks {
net, err := c.FindDependentResource(n.Name)
if err != nil {
errRemove := d.RemoveContainer(cont.ID)
if errRemove != nil {
return "", xerrors.Errorf("Unable to connect container to network %s, unable to roll back container: %w", n.Name, err)
}
return "", xerrors.Errorf("Network not found: %w", err)
}
d.l.Debug("Attaching container to network", "ref", c.Name, "network", n.Name)
es := &network.EndpointSettings{NetworkID: net.Info().Name}
// if we have network aliases defined, add them to the network connection
if n.Aliases != nil && len(n.Aliases) > 0 {
es.Aliases = n.Aliases
}
// are we binding to a specific ip
if n.IPAddress != "" {
d.l.Debug("Assigning static ip address", "ref", c.Name, "network", n.Name, "ip_address", n.IPAddress)
es.IPAMConfig = &network.EndpointIPAMConfig{IPv4Address: n.IPAddress}
}
err = d.c.NetworkConnect(context.Background(), net.Info().Name, cont.ID, es)
if err != nil {
// if we fail to connect to the network roll back the container
errRemove := d.RemoveContainer(cont.ID)
if errRemove != nil {
return "", xerrors.Errorf("Unable to connect container to network %s, unable to roll back container: %w", n.Name, err)
}
return "", xerrors.Errorf("Unable to connect container to network %s: %w", n.Name, err)
}
}
}
err = d.c.ContainerStart(context.Background(), cont.ID, types.ContainerStartOptions{})
if err != nil {
return "", err
}
return cont.ID, nil
}
// ContainerInfo returns the Docker container info
func (d *DockerTasks) ContainerInfo(id string) (interface{}, error) {
cj, err := d.c.ContainerInspect(context.Background(), id)
if err != nil {
return nil, xerrors.Errorf("Unable to read information about Docker container %s: %w", id, err)
}
return cj, nil
}
// PullImage pulls a Docker image from a remote repo
func (d *DockerTasks) PullImage(image config.Image, force bool) error {
in := makeImageCanonical(image.Name)
args := filters.NewArgs()
args.Add("reference", image.Name)
// only pull if image is not in current registry so check to see if the image is present
// if force then skil this check
if !force && !d.force {
sum, err := d.c.ImageList(context.Background(), types.ImageListOptions{Filters: args})
if err != nil {
return xerrors.Errorf("unable to list images in local Docker cache: %w", err)
}
// if we have images do not pull
if len(sum) > 0 {
d.l.Debug("Image exists in local cache", "image", image.Name)
return nil
}
}
ipo := types.ImagePullOptions{}
// if the username and password is not null make an authenticated
// image pull
if image.Username != "" && image.Password != "" {
ipo.RegistryAuth = createRegistryAuth(image.Username, image.Password)
}
d.l.Debug("Pulling image", "image", image.Name)
out, err := d.c.ImagePull(context.Background(), in, ipo)
if err != nil {
return xerrors.Errorf("Error pulling image: %w", err)
}
// update the image log
err = d.il.Log(in, ImageTypeDocker)
if err != nil {
d.l.Error("Unable to add image name to cache", "error", err)
}
// write the output to /dev/null
// TODO this stuff needs to be logged correctly
io.Copy(ioutil.Discard, out)
return nil
}
// FindContainerIDs returns the Container IDs for the given identifier
func (d *DockerTasks) FindContainerIDs(containerName string, typeName config.ResourceType) ([]string, error) {
fullName := utils.FQDN(containerName, string(typeName))
args := filters.NewArgs()
// By default Docker will wildcard searches, use regex to return the absolute
args.Add("name", fmt.Sprintf("^/%s$", fullName))
opts := types.ContainerListOptions{Filters: args, All: true}
cl, err := d.c.ContainerList(context.Background(), opts)
if err != nil || cl == nil {
return nil, err
}
if len(cl) > 0 {
ids := []string{}
for _, c := range cl {
ids = append(ids, c.ID)
}
return ids, nil
}
return nil, nil
}
// RemoveContainer with the given id
func (d *DockerTasks) RemoveContainer(id string) error {
// try and shutdown graceful
err := d.c.ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{Force: false, RemoveVolumes: true})
// unable to shutdown graceful try force
if err != nil {
return d.c.ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{Force: true, RemoveVolumes: true})
}
return nil
}
func (d *DockerTasks) BuildContainer(config *config.Container, force bool) (string, error) {
imageName := fmt.Sprintf("shipyard.run/localcache/%s:latest", config.Name)
imageName = makeImageCanonical(imageName)
args := filters.NewArgs()
args.Add("reference", imageName)
// check if the image already exists, if so do not rebuild unless force
if !force && !d.force {
sum, err := d.c.ImageList(context.Background(), types.ImageListOptions{Filters: args})
if err != nil {
return "", xerrors.Errorf("unable to list images in local Docker cache: %w", err)
}
// if we have images do not pull
if len(sum) > 0 {
d.l.Debug("Image exists in local cache, skip build", "image", imageName)
return imageName, nil
}
}
// if the Dockerfile is not set, set to default
if config.Build.File == "" {
config.Build.File = "./Dockerfile"
}
// tar the build context folder and send to the server
buildOpts := types.ImageBuildOptions{
Dockerfile: config.Build.File,
Tags: []string{imageName},
}
buildCtx, _ := archive.TarWithOptions(config.Build.Context, &archive.TarOptions{})
resp, err := d.c.ImageBuild(context.Background(), buildCtx, buildOpts)
if err != nil {
return "", err
}
defer resp.Body.Close()
out := d.l.StandardWriter(&hclog.StandardLoggerOptions{ForceLevel: hclog.Debug})
termFd, _ := term.GetFdInfo(out)
err = jsonmessage.DisplayJSONMessagesStream(resp.Body, out, termFd, false, nil)
if err != nil {
return "", err
}
return imageName, nil
}
// CreateVolume creates a Docker volume for a cluster
// if the volume exists performs no action
// returns the volume name and an error if unsuccessful
func (d *DockerTasks) CreateVolume(name string) (string, error) {
vn := utils.FQDNVolumeName(name)
args := filters.NewArgs()
// By default Docker will wildcard searches, use regex to return the absolute
args.Add("name", vn)
ops, err := d.c.VolumeList(context.Background(), args)
if err != nil {
return "", fmt.Errorf("unable to lookup volume [%s] for cluster [%s]\n%+v", vn, name, err)
}
if len(ops.Volumes) > 0 {
d.l.Debug("Volume exists", "ref", name, "name", vn)
return vn, nil
}
d.l.Debug("Create Volume", "ref", name, "name", vn)
volumeCreateOptions := volume.VolumeCreateBody{
Name: vn,
Driver: "local", //TODO: allow setting driver + opts
DriverOpts: map[string]string{},
}
vol, err := d.c.VolumeCreate(context.Background(), volumeCreateOptions)
if err != nil {
return "", fmt.Errorf("failed to create image volume [%s] for cluster [%s]\n%+v", vn, name, err)
}
return vol.Name, nil
}
// RemoveVolume deletes the Docker volume associated with a cluster
func (d *DockerTasks) RemoveVolume(name string) error {
vn := utils.FQDNVolumeName(name)
d.l.Debug("Deleting Volume", "ref", name, "name", vn)
return d.c.VolumeRemove(context.Background(), vn, true)
}
// ContainerLogs streams the logs for the container to the returned io.ReadCloser
func (d *DockerTasks) ContainerLogs(id string, stdOut, stdErr bool) (io.ReadCloser, error) {
return d.c.ContainerLogs(context.Background(), id, types.ContainerLogsOptions{ShowStderr: stdErr, ShowStdout: stdOut})
}
// CopyFromContainer copies a file from a container
func (d *DockerTasks) CopyFromContainer(id, src, dst string) error {
d.l.Debug("Copying file from", "id", id, "src", src, "dst", dst)
reader, _, err := d.c.CopyFromContainer(context.Background(), id, src)
if err != nil {
return fmt.Errorf("Couldn't copy kubeconfig.yaml from server container %s\n%+v", id, err)
}
defer reader.Close()
readBytes, err := ioutil.ReadAll(reader)
if err != nil {
return fmt.Errorf("Couldn't read kubeconfig from container\n%+v", err)
}
// write to file, skipping the first 512 bytes which contain file metadata
// and trimming any NULL characters
trimBytes := bytes.Trim(readBytes[512:], "\x00")
file, err := os.Create(dst)
if err != nil {
return fmt.Errorf("Couldn't create file %s\n%+v", dst, err)
}
defer file.Close()
file.Write(trimBytes)
return nil
}
var importMutex = sync.Mutex{}
// CopyLocalDockerImageToVolume writes multiple Docker images to a Docker container as a compressed archive
// returns the filename of the archive and an error if one occured
func (d *DockerTasks) CopyLocalDockerImageToVolume(images []string, volume string, force bool) ([]string, error) {
d.l.Debug("Writing docker images to volume", "images", images, "volume", volume)
// make sure this operation runs sequentially as we do not want to update the same volume at the same time
// for now it should be ok to block globally
importMutex.Lock()
defer importMutex.Unlock()
savedImages := []string{}
// make sure we have the alpine image needed to copy
err := d.PullImage(config.Image{Name: "alpine:latest"}, false)
if err != nil {
return nil, xerrors.Errorf("Unable pull alpine:latest for importing images: %w", err)
}
// create a dummy container to import to volume
cc := config.NewContainer("temp-import")
cc.Image = &config.Image{Name: "alpine:latest"}
cc.Volumes = []config.Volume{
config.Volume{
Source: volume,
Destination: "/images",
Type: "volume",
},
}
cc.Command = []string{"tail", "-f", "/dev/null"}
tmpID, err := d.CreateContainer(cc)
if err != nil {
return nil, xerrors.Errorf("unable to create dummy container for importing images: %w", err)
}
defer d.RemoveContainer(tmpID)
// add each image individually
for _, i := range images {
compressedImageName := fmt.Sprintf("%s.tar", base64.StdEncoding.EncodeToString([]byte(i)))
// check if the image exists if we are not doing a forced update
if !d.force && !force {
err := d.ExecuteCommand(tmpID, []string{"find", "/images/" + compressedImageName}, nil, "/", nil)
if err == nil {
// we have the image already
d.l.Debug("Image already cached", "image", i)
savedImages = append(savedImages, compressedImageName)
continue
}
}
d.l.Debug("Copying image to container", "image", i)
imageFile, err := d.saveImageToTempFile(i, compressedImageName)
if err != nil {
return nil, err
}
// clean up after ourselfs
defer os.Remove(imageFile)
err = d.CopyFileToContainer(utils.FQDN(cc.Name, string(cc.Type)), imageFile, "/images")
if err != nil {
return nil, err
}
savedImages = append(savedImages, compressedImageName)
}
// return the name of the archive
return savedImages, nil
}
// CopyFileToContainer copies the file at path filename to the container containerID and
// stores it in the container at the path path.
func (d *DockerTasks) CopyFileToContainer(containerID, filename, path string) error {
f, err := os.Open(filename)
if err != nil {
return xerrors.Errorf("unable to open file: %w", err)
}
defer f.Close()
// create temp file for a tar which will be used to package the file
// CopyToContainer expects a tar which has individual file entries
// if we write the original file the output will not be a single file
// but the contents of the tar. To bypass this we need to add the output from
// save image to a tar
tmpTarFile, err := ioutil.TempFile("", "")
if err != nil {
return xerrors.Errorf("unable to create temporary file: %w for tar achive", err)
}
defer func() {
tmpTarFile.Close()
os.Remove(tmpTarFile.Name())
}()
ta := tar.NewWriter(tmpTarFile)
fi, _ := f.Stat()
hdr, err := tar.FileInfoHeader(fi, fi.Name())
if err != nil {
return xerrors.Errorf("unable to create header for tar: %w", err)
}
// write the header to the tar file, this has to happen before the file
err = ta.WriteHeader(hdr)
if err != nil {
return xerrors.Errorf("unable to write tar header: %w", err)
}
io.Copy(ta, f)
if err != nil {
return xerrors.Errorf("unable to copy image to tar file: %w", err)
}
ta.Close()
// reset the file seek so we can copy to the container
tmpTarFile.Seek(0, 0)
err = d.c.CopyToContainer(context.Background(), containerID, path, tmpTarFile, types.CopyToContainerOptions{})
if err != nil {
return xerrors.Errorf("unable to copy file to container: %w", err)
}
return nil
}
// ExecuteCommand allows the execution of commands in a running docker container
// id is the id of the container to execute the command in
// command is a slice of strings to execute
// writer [optional] will be used to write any output from the command execution.
func (d *DockerTasks) ExecuteCommand(id string, command []string, env []string, workingDir string, writer io.Writer) error {
execid, err := d.c.ContainerExecCreate(context.Background(), id, types.ExecConfig{
Cmd: command,
AttachStdout: true,
AttachStderr: true,
Env: env,
WorkingDir: workingDir,
})
if err != nil {
return xerrors.Errorf("unable to create container exec: %w", err)
}
// get logs from an attach
stream, err := d.c.ContainerExecAttach(context.Background(), execid.ID, types.ExecStartCheck{})
if err != nil {
return xerrors.Errorf("unable to attach logging to exec process: %w", err)
}
defer stream.Close()
streamContext, cancelStream := context.WithCancel(context.Background())
// if we have a writer stream the logs from the container to the writer
if writer != nil {
ttyOut := streams.NewOut(writer)
ttyErr := streams.NewOut(writer)
errCh := make(chan error, 1)
go func() {
defer close(errCh)
errCh <- func() error {
streamer := streams.NewHijackedStreamer(nil, ttyOut, nil, ttyOut, ttyErr, stream, false, "", d.l)
return streamer.Stream(streamContext)
}()
}()
if err := <-errCh; err != nil {
d.l.Error("unable to hijack exec stream: %s", err)
cancelStream()
return err
}
}
err = d.c.ContainerExecStart(context.Background(), execid.ID, types.ExecStartCheck{})
if err != nil {
cancelStream()
return xerrors.Errorf("unable to start exec process: %w", err)
}
// loop until the container finishes execution
for {
i, err := d.c.ContainerExecInspect(context.Background(), execid.ID)
if err != nil {
cancelStream()
return xerrors.Errorf("unable to determine status of exec process: %w", err)
}
if !i.Running {
if i.ExitCode == 0 {
cancelStream()
return nil
}
cancelStream()
return xerrors.Errorf("container exec failed with exit code %d", i.ExitCode)
}
time.Sleep(1 * time.Second)
}
}
// TODO: this is all exploritory, works but needs a major tidy
// CreateShell creates an interactive shell inside a container
// https://github.com/docker/cli/blob/ae1618713f83e7da07317d579d0675f578de22fa/cli/command/container/exec.go
func (d *DockerTasks) CreateShell(id string, command []string, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) error {
execid, err := d.c.ContainerExecCreate(context.Background(), id, types.ExecConfig{
Cmd: command,
WorkingDir: "/",
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Tty: true,
})
if err != nil {
return xerrors.Errorf("unable to create container exec: %w", err)
}
// err = d.c.ContainerExecStart(context.Background(), execid.ID, types.ExecStartCheck{})
// if err != nil {
// return xerrors.Errorf("unable to start exec process: %w", err)
// }
resp, err := d.c.ContainerExecAttach(context.Background(), execid.ID, types.ExecStartCheck{Tty: true})
if err != nil {
return err
}
// wrap the standard streams
ttyIn := streams.NewIn(stdin)
ttyOut := streams.NewOut(stdout)
ttyErr := streams.NewOut(stderr)
defer resp.Close()
errCh := make(chan error, 1)
streamContext, streamCancel := context.WithCancel(context.Background())
go func() {
defer close(errCh)
errCh <- func() error {
streamer := streams.NewHijackedStreamer(ttyIn, ttyOut, ttyIn, ttyOut, ttyErr, resp, true, "", d.l)
return streamer.Stream(streamContext)
}()
}()
// init the TTY
d.initTTY(execid.ID, ttyOut)
// monitor for TTY changes
sigchan := make(chan os.Signal, 1)
gosignal.Notify(sigchan, signal.SIGWINCH)
go func() {
for range sigchan {
d.resizeTTY(execid.ID, ttyOut)
}
}()
// loop until the container finishes execution
for {
i, err := d.c.ContainerExecInspect(context.Background(), execid.ID)
if err != nil {
streamCancel()
return xerrors.Errorf("unable to determine status of exec process: %w", err)
}
if !i.Running {
if i.ExitCode == 0 {
streamCancel()
return nil
}
streamCancel()
return xerrors.Errorf("container exec failed with exit code %d", i.ExitCode)
}
time.Sleep(1 * time.Second)
}
}
func (d *DockerTasks) initTTY(id string, out *streams.Out) error {
if err := d.resizeTTY(id, out); err != nil {
go func() {
var err error
for retry := 0; retry < 5; retry++ {
time.Sleep(10 * time.Millisecond)
if err = d.resizeTTY(id, out); err == nil {
break
}
}
if err != nil {
//something
d.l.Error("Unable to resize TTY use default", "error", err)
}
}()
}
return nil
}
func (d *DockerTasks) resizeTTY(id string, out *streams.Out) error {
h, w := out.GetTtySize()
if h == 0 && w == 0 {
return nil
}
options := types.ResizeOptions{
Height: uint(h),
Width: uint(w),
}
// resize the contiainer
err := d.c.ContainerExecResize(context.Background(), id, options)
if err != nil {
return err
}
return nil
}
func (d *DockerTasks) AttachNetwork(network, containerid string) error {
return fmt.Errorf("Not implemented")
}
// ListNetworks lists the networks a container is attached to
func (d *DockerTasks) ListNetworks(id string) []config.NetworkAttachment {
return nil
}
// DetachNetwork detaches a container from a network
// TODO: Docker returns success before removing a container
// tasks which depend on the network being removed may fail in the future
// we need to check it has been removed before returning
func (d *DockerTasks) DetachNetwork(network, containerid string) error {
network = strings.Replace(network, "network.", "", -1)
err := d.c.NetworkDisconnect(context.Background(), network, containerid, true)
// Hacky hack for now
//time.Sleep(1000 * time.Millisecond)
return err
}
// publishedPorts defines a Docker published port
type publishedPorts struct {
ExposedPorts map[nat.Port]struct{}
PortBindings map[nat.Port][]nat.PortBinding
}
// createPublishedPorts converts a list of config.Port to Docker publishedPorts type
func createPublishedPorts(ps []config.Port) publishedPorts {
pp := publishedPorts{
ExposedPorts: make(map[nat.Port]struct{}, 0),
PortBindings: make(map[nat.Port][]nat.PortBinding, 0),
}
for _, p := range ps {
dp, _ := nat.NewPort(p.Protocol, p.Local)
pp.ExposedPorts[dp] = struct{}{}
pb := []nat.PortBinding{
nat.PortBinding{
HostIP: "0.0.0.0",
HostPort: p.Host,
},
}
pp.PortBindings[dp] = pb
}
return pp
}
func createPublishedPortRanges(ps []config.PortRange) (publishedPorts, error) {
pp := publishedPorts{
ExposedPorts: make(map[nat.Port]struct{}, 0),
PortBindings: make(map[nat.Port][]nat.PortBinding, 0),
}
for _, p := range ps {
// split the range
parts := strings.Split(p.Range, "-")
if len(parts) != 2 {
return pp, fmt.Errorf("Invalid port range, range should be written start-end, e.g 80-82")
}
// ensure the start is less than the end
start, serr := strconv.Atoi(parts[0])
end, eerr := strconv.Atoi(parts[1])
if serr != nil || eerr != nil {
return pp, fmt.Errorf(
"Invalid port range, range should be numbers and written start-end, e.g 80-82",
)
}
if start > end {
return pp, fmt.Errorf(
"Invalid port range, start and end ports should be numeric and written start-end, e.g 80-82",
)
}
// range is ok, generate ports
for i := start; i < end+1; i++ {
port := strconv.Itoa(i)
dp, _ := nat.NewPort(p.Protocol, port)
pp.ExposedPorts[dp] = struct{}{}
if p.EnableHost {
pb := []nat.PortBinding{
nat.PortBinding{
HostIP: "0.0.0.0",
HostPort: port,
},
}
pp.PortBindings[dp] = pb
}
}
}
return pp, nil
}
// credentials are a json string and need to be base64 encoded
func createRegistryAuth(username, password string) string {
return base64.StdEncoding.EncodeToString(
[]byte(
fmt.Sprintf(`{"Username": "%s", "Password": "%s"}`, username, password),
),
)
}
// makeImageCanonical makes sure the image reference uses full canonical name i.e.
// consul:1.6.1 -> docker.io/library/consul:1.6.1
func makeImageCanonical(image string) string {
imageParts := strings.Split(image, "/")
switch len(imageParts) {
case 1:
return fmt.Sprintf("docker.io/library/%s", imageParts[0])
case 2:
return fmt.Sprintf("docker.io/%s/%s", imageParts[0], imageParts[1])
}
return image
}
// saveImageToTempFile saves a Docker image to a temporary tar file
// it is the responsibility of the caller to remove the temporary file
func (d *DockerTasks) saveImageToTempFile(image, filename string) (string, error) {
// save the image to a local temp file
ir, err := d.c.ImageSave(context.Background(), []string{image})
if err != nil {
return "", xerrors.Errorf("unable to save images: %w", err)
}
defer ir.Close()
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
return "", xerrors.Errorf("unable to create temporary file: %w", err)
}
// create a temp file to hold the tar
tmpFileName := path.Join(tmpDir, filename)
tmpFile, err := os.Create(tmpFileName)
if err != nil {
return "", xerrors.Errorf("unable to create temporary file: %w", err)
}
defer tmpFile.Close()
_, err = io.Copy(tmpFile, ir)
if err != nil {
return "", xerrors.Errorf("unable to copy image to temp file: %w", err)
}