-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdockerHelpers.go
173 lines (147 loc) · 4.63 KB
/
dockerHelpers.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
package main
import (
"archive/tar"
"bytes"
"context"
"io"
"io/ioutil"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
libDatabox "github.com/me-box/lib-go-databox"
)
//pullImageIfRequired will try and pull the image form DefaultRegistry if it dose not exist locally.
// If the image is taged latest then it will allways attempt to pull the image.
func pullImageIfRequired(image string, DefaultRegistry string, DefaultRegistryHost string) {
needToPull := true
ctx := context.Background()
cli, _ := client.NewEnvClient()
//do we have the image on disk?
images, _ := cli.ImageList(ctx, types.ImageListOptions{})
for _, i := range images {
for _, tag := range i.RepoTags {
if image == tag {
//we have the image no need to pull it !!
needToPull = false
break
}
}
}
//is it from the default registry (databoxsystems or whatever we overroad with) and tagged with latest?
if strings.Contains(image, DefaultRegistry) == true && strings.Contains(image, ":latest") == true {
//its in the default registry and has the :latest tag lets pull it to make sure we are up-to-date
needToPull = true
}
if needToPull == true {
libDatabox.Info("Pulling Image " + image)
reader, err := cli.ImagePull(ctx, DefaultRegistryHost+"/"+image, types.ImagePullOptions{})
if err != nil {
libDatabox.Warn(err.Error())
return
}
io.Copy(ioutil.Discard, reader)
libDatabox.Info("Done pulling Image " + image)
reader.Close()
}
}
// copyFileToContainer copies a single file of any format to the target container
// dockers CopyToContainer only works with tar archives.
func copyFileToContainer(targetFullPath string, fileReader io.Reader, containerID string) error {
cli, _ := client.NewEnvClient()
ctx := context.Background()
fileBody, _ := ioutil.ReadAll(fileReader)
var tarBuf bytes.Buffer
tw := tar.NewWriter(&tarBuf)
hdr := &tar.Header{
Name: targetFullPath,
Mode: 0660,
Size: int64(len(fileBody)),
}
if err := tw.WriteHeader(hdr); err != nil {
return err
}
if _, err := tw.Write(fileBody); err != err {
return err
}
var r io.Reader
r = &tarBuf
err := cli.CopyToContainer(ctx, containerID, "/", r, types.CopyToContainerOptions{})
if err != nil {
return err
}
return nil
}
func createSecretIfNotExists(name, data string) string {
cli, _ := client.NewEnvClient()
ctx := context.Background()
filters := filters.NewArgs()
filters.Add("name", name)
secrestsList, _ := cli.SecretList(ctx, types.SecretListOptions{Filters: filters})
if len(secrestsList) > 0 {
//we have made this before just return the ID
return secrestsList[0].ID
}
secret := swarm.SecretSpec{
Annotations: swarm.Annotations{
Name: name,
},
Data: []byte(data),
}
libDatabox.Debug("createSecret for " + name)
secretCreateResponse, err := cli.SecretCreate(ctx, secret)
libDatabox.ChkErr(err)
return secretCreateResponse.ID
}
func createSecretFromFileIfNotExists(name, dataPath string) string {
data, _ := ioutil.ReadFile(dataPath)
return createSecretIfNotExists(name, string(data))
}
func removeContainer(name string) {
cli, _ := client.NewEnvClient()
ctx := context.Background()
filters := filters.NewArgs()
filters.Add("name", name)
containers, cerr := cli.ContainerList(ctx, types.ContainerListOptions{
Filters: filters,
All: true,
})
libDatabox.ChkErrFatal(cerr)
if len(containers) > 0 {
rerr := cli.ContainerRemove(ctx, containers[0].ID, types.ContainerRemoveOptions{Force: true})
libDatabox.ChkErr(rerr)
}
}
func constructDefaultServiceSpec(localContainerName string, imageName string, databoxType libDatabox.DataboxType, databoxVersion string, netConf NetworkConfig) swarm.ServiceSpec {
return swarm.ServiceSpec{
Annotations: swarm.Annotations{
Labels: map[string]string{"databox.type": string(databoxType)},
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
Hostname: localContainerName,
Image: imageName,
Labels: map[string]string{"databox.type": string(databoxType)},
Env: []string{
"DATABOX_ARBITER_ENDPOINT=tcp://arbiter:4444",
"DATABOX_LOCAL_NAME=" + localContainerName,
"DATABOX_VERSION=" + databoxVersion,
},
DNSConfig: &swarm.DNSConfig{
Nameservers: []string{netConf.DNS},
},
},
Networks: []swarm.NetworkAttachmentConfig{swarm.NetworkAttachmentConfig{
Target: netConf.NetworkName,
Aliases: []string{localContainerName},
}},
Placement: &swarm.Placement{
Constraints: []string{"node.Role == manager"},
},
},
EndpointSpec: &swarm.EndpointSpec{
Mode: swarm.ResolutionModeDNSRR,
},
}
}