This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
docker.go
402 lines (318 loc) · 10.4 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
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
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tests
import (
"bytes"
"fmt"
"strconv"
"strings"
"time"
)
const (
// Docker command
Docker = "docker"
// Image used to run containers
Image = "busybox"
// AlpineImage is the alpine image
AlpineImage = "alpine"
// PostgresImage is the postgres image
PostgresImage = "postgres"
// DebianImage is the debian image
DebianImage = "debian"
// FedoraImage is the fedora image
FedoraImage = "fedora"
)
func runDockerCommandWithTimeout(timeout time.Duration, command string, args ...string) (string, string, int) {
return runDockerCommandWithTimeoutAndPipe(nil, timeout, command, args...)
}
func runDockerCommandWithTimeoutAndPipe(stdin *bytes.Buffer, timeout time.Duration, command string, args ...string) (string, string, int) {
a := []string{command}
a = append(a, args...)
cmd := NewCommand(Docker, a...)
cmd.Timeout = timeout
return cmd.RunWithPipe(stdin)
}
func runDockerCommand(command string, args ...string) (string, string, int) {
return runDockerCommandWithTimeout(time.Duration(Timeout), command, args...)
}
func runDockerCommandWithPipe(stdin *bytes.Buffer, command string, args ...string) (string, string, int) {
return runDockerCommandWithTimeoutAndPipe(stdin, time.Duration(Timeout), command, args...)
}
// LogsDockerContainer returns the container logs
func LogsDockerContainer(name string) (string, error) {
args := []string{name}
stdout, _, exitCode := runDockerCommand("logs", args...)
if exitCode != 0 {
return "", fmt.Errorf("failed to run docker logs command")
}
return strings.TrimSpace(stdout), nil
}
// StatusDockerContainer returns the container status
func StatusDockerContainer(name string) string {
args := []string{"-a", "-f", "name=" + name, "--format", "{{.Status}}"}
stdout, _, exitCode := runDockerCommand("ps", args...)
if exitCode != 0 || stdout == "" {
return ""
}
state := strings.Split(stdout, " ")
return state[0]
}
// hasExitedDockerContainer checks if the container has exited.
func hasExitedDockerContainer(name string) (bool, error) {
args := []string{"--format={{.State.Status}}", name}
stdout, _, exitCode := runDockerCommand("inspect", args...)
if exitCode != 0 || stdout == "" {
return false, fmt.Errorf("failed to run docker inspect command")
}
status := strings.TrimSpace(stdout)
if status == "exited" {
return true, nil
}
return false, nil
}
// ExitCodeDockerContainer returns the container exit code
func ExitCodeDockerContainer(name string, waitForExit bool) (int, error) {
// It makes no sense to try to retrieve the exit code of the container
// if it is still running. That's why this infinite loop takes care of
// waiting for the status to become "exited" before to ask for the exit
// code.
// However, we might want to bypass this check on purpose, that's why
// we check waitForExit boolean.
if waitForExit {
errCh := make(chan error)
exitCh := make(chan bool)
go func() {
for {
exited, err := hasExitedDockerContainer(name)
if err != nil {
errCh <- err
}
if exited {
break
}
time.Sleep(time.Second)
}
close(exitCh)
}()
select {
case <-exitCh:
break
case err := <-errCh:
return -1, err
case <-time.After(time.Duration(Timeout) * time.Second):
return -1, fmt.Errorf("Timeout reached after %ds", Timeout)
}
}
args := []string{"--format={{.State.ExitCode}}", name}
stdout, _, exitCode := runDockerCommand("inspect", args...)
if exitCode != 0 || stdout == "" {
return -1, fmt.Errorf("failed to run docker inspect command")
}
return strconv.Atoi(strings.TrimSpace(stdout))
}
func WaitForRunningDockerContainer(name string, running bool) error {
ch := make(chan bool)
go func() {
if IsRunningDockerContainer(name) == running {
close(ch)
return
}
time.Sleep(time.Second)
}()
select {
case <-ch:
case <-time.After(time.Duration(Timeout) * time.Second):
return fmt.Errorf("Timeout reached after %ds", Timeout)
}
return nil
}
// IsRunningDockerContainer inspects a container
// returns true if is running
func IsRunningDockerContainer(name string) bool {
stdout, _, exitCode := runDockerCommand("inspect", "--format={{.State.Running}}", name)
if exitCode != 0 {
return false
}
output := strings.TrimSpace(stdout)
LogIfFail("container running: " + output)
if output == "false" {
return false
}
return true
}
// ExistDockerContainer returns true if any of next cases is true:
// - 'docker ps -a' command shows the container
// - the VM is running (qemu)
// else false is returned
func ExistDockerContainer(name string) bool {
state := StatusDockerContainer(name)
if state != "" {
return true
}
return IsVMRunning(name)
}
// RemoveDockerContainer removes a container using docker rm -f
func RemoveDockerContainer(name string) bool {
_, _, exitCode := DockerRm("-f", name)
if exitCode != 0 {
return false
}
return true
}
// StopDockerContainer stops a container
func StopDockerContainer(name string) bool {
_, _, exitCode := DockerStop(name)
if exitCode != 0 {
return false
}
return true
}
// KillDockerContainer kills a container
func KillDockerContainer(name string) bool {
_, _, exitCode := DockerKill(name)
if exitCode != 0 {
return false
}
return true
}
// DockerRm removes a container
func DockerRm(args ...string) (string, string, int) {
return runDockerCommand("rm", args...)
}
// DockerStop stops a container
// returns true on success else false
func DockerStop(args ...string) (string, string, int) {
// docker stop takes ~15 seconds
return runDockerCommand("stop", args...)
}
// DockerPull downloads the specific image
func DockerPull(args ...string) (string, string, int) {
// 10 minutes should be enough to download a image
return runDockerCommandWithTimeout(600, "pull", args...)
}
// DockerRun runs a container
func DockerRun(args ...string) (string, string, int) {
if Runtime != "" {
args = append(args, []string{"", ""}...)
copy(args[2:], args[:])
args[0] = "--runtime"
args[1] = Runtime
}
return runDockerCommand("run", args...)
}
// DockerRunWithPipe runs a container with stdin
func DockerRunWithPipe(stdin *bytes.Buffer, args ...string) (string, string, int) {
if Runtime != "" {
args = append(args, []string{"", ""}...)
copy(args[2:], args[:])
args[0] = "--runtime"
args[1] = Runtime
}
return runDockerCommandWithPipe(stdin, "run", args...)
}
// DockerKill kills a container
func DockerKill(args ...string) (string, string, int) {
return runDockerCommand("kill", args...)
}
// DockerVolume manages volumes
func DockerVolume(args ...string) (string, string, int) {
return runDockerCommand("volume", args...)
}
// DockerAttach attach to a running container
func DockerAttach(args ...string) (string, string, int) {
return runDockerCommand("attach", args...)
}
// DockerCommit creates a new image from a container's changes
func DockerCommit(args ...string) (string, string, int) {
return runDockerCommand("commit", args...)
}
// DockerImages list images
func DockerImages(args ...string) (string, string, int) {
return runDockerCommand("images", args...)
}
// DockerRmi removes one or more images
func DockerRmi(args ...string) (string, string, int) {
// docker takes more than 5 seconds to remove an image, it depends
// of the image size and this operation does not involve to the
// runtime
return runDockerCommand("rmi", args...)
}
// DockerCp copies files/folders between a container and the local filesystem
func DockerCp(args ...string) (string, string, int) {
return runDockerCommand("cp", args...)
}
// DockerExec runs a command in a running container
func DockerExec(args ...string) (string, string, int) {
return runDockerCommand("exec", args...)
}
// DockerPs list containers
func DockerPs(args ...string) (string, string, int) {
return runDockerCommand("ps", args...)
}
// DockerSearch searchs docker hub images
func DockerSearch(args ...string) (string, string, int) {
return runDockerCommand("search", args...)
}
// DockerCreate creates a new container
func DockerCreate(args ...string) (string, string, int) {
return runDockerCommand("create", args...)
}
// DockerDiff inspect changes to files or directories on a container’s filesystem
func DockerDiff(args ...string) (string, string, int) {
return runDockerCommand("diff", args...)
}
// DockerBuild builds an image from a Dockerfile
func DockerBuild(args ...string) (string, string, int) {
return runDockerCommand("build", args...)
}
// DockerNetwork manages networks
func DockerNetwork(args ...string) (string, string, int) {
return runDockerCommand("network", args...)
}
// DockerExport will export a container’s filesystem as a tar archive
func DockerExport(args ...string) (string, string, int) {
return runDockerCommand("export", args...)
}
// DockerImport imports the contents from a tarball to create a filesystem image
func DockerImport(args ...string) (string, string, int) {
return runDockerCommand("import", args...)
}
// DockerInfo displays system-wide information
func DockerInfo() (string, string, int) {
return runDockerCommand("info")
}
// DockerSwarm manages swarm
func DockerSwarm(args ...string) (string, string, int) {
return runDockerCommand("swarm", args...)
}
// DockerService manages services
func DockerService(args ...string) (string, string, int) {
return runDockerCommand("service", args...)
}
// DockerStart starts one or more stopped containers
func DockerStart(args ...string) (string, string, int) {
return runDockerCommand("start", args...)
}
// DockerPause pauses all processes within one or more containers
func DockerPause(args ...string) (string, string, int) {
return runDockerCommand("pause", args...)
}
// DockerUnpause unpauses all processes within one or more containers
func DockerUnpause(args ...string) (string, string, int) {
return runDockerCommand("unpause", args...)
}
// DockerTop displays the running processes of a container
func DockerTop(args ...string) (string, string, int) {
return runDockerCommand("top", args...)
}