-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker.go
327 lines (275 loc) · 8.12 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
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
package main
import (
"archive/tar"
"bytes"
"errors"
"fmt"
"io"
"os"
"path"
"runtime"
"time"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/fsouza/go-dockerclient"
)
type ConnectOpts struct {
TLSCaCert string
TLSCert string
TLSKey string
TLSVerify bool
Host string
}
type Port struct {
HostIp string `json:"hostIp"`
HostPort int64 `json:"hostPort"`
ContainerPort int64 `json:"containerPort"`
Type string `json:"type"`
}
type Container struct {
Name string `json:"name"`
Image string `json:"image"`
Running bool `json:"running"`
Ports []Port `json:"ports"`
Labels map[string]string `json:"labels"`
Mounts []map[string]string `json:"mounts"`
}
type CreateContainerOpts struct {
Name string
Hostname string
Ports []Port
Volumes []string
Image string
Labels map[string]string
}
type CreateVolumeOpts struct {
Name string
Labels map[string]string
}
type DockerClient interface {
ListContainers() ([]docker.APIContainers, error)
ListContainersWithLabels(labels []string) ([]docker.APIContainers, error)
InspectContainer(cont string) (*docker.Container, error)
ListImages() ([]docker.APIImages, error)
ListImagesWithLabels(labels []string) ([]docker.APIImages, error)
PullImage(image string, output *os.File) error
BuildImage(name string, dockerfile string, sshkey string, output io.Writer) error
CreateContainer(cco CreateContainerOpts) error
StartContainer(name string) error
StopContainer(name string) error
RemoveContainer(name string) error
ParseRepositoryTag(repoTag string) (string, string)
ListVolumes() ([]docker.Volume, error)
CreateVolume(CreateVolumeOpts) error
RemoveVolume(string) error
RemoveImage(string) error
}
type RealDockerClient struct {
dcl *docker.Client
}
func (rdc *RealDockerClient) InspectContainer(cont string) (*docker.Container, error) {
return rdc.dcl.InspectContainer(cont)
}
func (rdc *RealDockerClient) StartContainer(name string) error {
err := rdc.dcl.StartContainer(name, nil)
if err != nil {
return err
}
return nil
}
func (rdc *RealDockerClient) StopContainer(name string) error {
err := rdc.dcl.StopContainer(name, 10)
if err != nil {
return err
}
return nil
}
func (rdc *RealDockerClient) RemoveContainer(name string) error {
removeOpts := docker.RemoveContainerOptions{
ID: name,
}
err := rdc.dcl.RemoveContainer(removeOpts)
if err != nil {
return err
}
return nil
}
func (rdc *RealDockerClient) CreateContainer(cco CreateContainerOpts) error {
exposedPorts := make(map[docker.Port]struct{})
portBindings := make(map[docker.Port][]docker.PortBinding)
for _, port := range cco.Ports {
dport := docker.Port(fmt.Sprintf("%d/%s", port.ContainerPort, port.Type))
exposedPorts[dport] = struct{}{}
portBindings[dport] = []docker.PortBinding{{port.HostIp, fmt.Sprintf("%d", port.HostPort)}}
}
config := docker.Config{
ExposedPorts: exposedPorts,
Image: cco.Image,
Hostname: cco.Hostname,
Labels: cco.Labels,
}
hostConfig := docker.HostConfig{
Binds: cco.Volumes,
PortBindings: portBindings,
}
_, err := rdc.dcl.CreateContainer(docker.CreateContainerOptions{Name: cco.Name, Config: &config, HostConfig: &hostConfig})
if err != nil {
return err
}
return nil
}
func (rdc *RealDockerClient) CreateVolume(cvo CreateVolumeOpts) error {
_, err := rdc.dcl.CreateVolume(docker.CreateVolumeOptions{Name: cvo.Name, Labels: cvo.Labels})
if err != nil {
return err
}
return nil
}
func (rdc *RealDockerClient) ListVolumes() ([]docker.Volume, error) {
var volumes []docker.Volume
volumes, err := rdc.dcl.ListVolumes(docker.ListVolumesOptions{})
if err != nil {
return volumes, err
}
return volumes, nil
}
func (rdc *RealDockerClient) RemoveVolume(name string) error {
return rdc.dcl.RemoveVolume(name)
}
func (rdc *RealDockerClient) RemoveImage(name string) error {
return rdc.dcl.RemoveImage(name)
}
func (rdc *RealDockerClient) BuildImage(name string, dockerfile, sshkey string, output io.Writer) error {
t := time.Now()
inputbuf := bytes.NewBuffer(nil)
tr := tar.NewWriter(inputbuf)
tr.WriteHeader(&tar.Header{Name: "Dockerfile", Size: int64(len(dockerfile)), ModTime: t, AccessTime: t, ChangeTime: t})
tr.Write([]byte(dockerfile))
tr.WriteHeader(&tar.Header{Name: "ssh_pub", Size: int64(len(sshkey)), ModTime: t, AccessTime: t, ChangeTime: t})
tr.Write([]byte(sshkey))
tr.Close()
opts := docker.BuildImageOptions{
Name: name,
InputStream: inputbuf,
OutputStream: output,
}
if err := rdc.dcl.BuildImage(opts); err != nil {
return err
}
return nil
}
func (rdc *RealDockerClient) ListContainers() ([]docker.APIContainers, error) {
var containers []docker.APIContainers
containers, err := rdc.dcl.ListContainers(docker.ListContainersOptions{All: true})
if err != nil {
return containers, err
}
return containers, nil
}
func (rdc *RealDockerClient) ListContainersWithLabels(labels []string) ([]docker.APIContainers, error) {
var containers []docker.APIContainers
containers, err := rdc.dcl.ListContainers(docker.ListContainersOptions{
All: true,
Filters: map[string][]string{
"label": labels,
},
})
if err != nil {
return containers, err
}
return containers, nil
}
func (rdc *RealDockerClient) ListImages() ([]docker.APIImages, error) {
var images []docker.APIImages
images, err := rdc.dcl.ListImages(docker.ListImagesOptions{})
if err != nil {
return images, err
}
return images, nil
}
func (rdc *RealDockerClient) ListImagesWithLabels(labels []string) ([]docker.APIImages, error) {
var images []docker.APIImages
images, err := rdc.dcl.ListImages(docker.ListImagesOptions{
Filters: map[string][]string{
"label": labels,
},
})
if err != nil {
return images, err
}
return images, nil
}
func (rdc *RealDockerClient) ParseRepositoryTag(repoTag string) (string, string) {
return docker.ParseRepositoryTag(repoTag)
}
func (rdc *RealDockerClient) PullImage(fullImage string, output *os.File) error {
image, tag := docker.ParseRepositoryTag(fullImage)
pipeRead, pipeWrite := io.Pipe()
opts := docker.PullImageOptions{
Repository: image,
Tag: tag,
OutputStream: pipeWrite,
RawJSONStream: true,
}
// TODO: pull auth config from dockercfg
go func() {
rdc.dcl.PullImage(opts, docker.AuthConfiguration{})
err := pipeWrite.Close()
if err != nil {
logrus.Warnf("Error closing pipe: %s", err)
}
}()
return jsonmessage.DisplayJSONMessagesStream(pipeRead, output, output.Fd(), true, nil)
}
func NewDockerClient(opts ConnectOpts) (*RealDockerClient, error) {
dcl, err := connectDocker()
if err != nil {
return nil, err
}
dockerClient := RealDockerClient{dcl: dcl}
return &dockerClient, nil
}
func connectDocker() (*docker.Client, error) {
// grab directly from docker daemon
var endpoint string
if env_endpoint := os.Getenv("DOCKER_HOST"); len(env_endpoint) > 0 {
endpoint = env_endpoint
} else if len(globalOptions.Host) > 0 {
endpoint = globalOptions.Host
} else {
if runtime.GOOS == "windows" {
// use Docker for Windows endpoint
endpoint = "http://localhost:2375"
} else {
// assume local socket
endpoint = "unix:///var/run/docker.sock"
}
}
var client *docker.Client
var err error
dockerTlsVerifyEnv := os.Getenv("DOCKER_TLS_VERIFY")
if dockerTlsVerifyEnv == "1" || globalOptions.TLSVerify {
if dockerCertPath := os.Getenv("DOCKER_CERT_PATH"); len(dockerCertPath) > 0 {
cert := path.Join(dockerCertPath, "cert.pem")
key := path.Join(dockerCertPath, "key.pem")
ca := path.Join(dockerCertPath, "ca.pem")
client, err = docker.NewTLSClient(endpoint, cert, key, ca)
if err != nil {
return nil, err
}
} else if len(globalOptions.TLSCert) > 0 && len(globalOptions.TLSKey) > 0 && len(globalOptions.TLSCaCert) > 0 {
client, err = docker.NewTLSClient(endpoint, globalOptions.TLSCert, globalOptions.TLSKey, globalOptions.TLSCaCert)
if err != nil {
return nil, err
}
} else {
return nil, errors.New("TLS Verification requested but certs not specified")
}
} else {
client, err = docker.NewClient(endpoint)
if err != nil {
return nil, err
}
}
return client, nil
}