-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocker.go
More file actions
86 lines (68 loc) · 2.04 KB
/
docker.go
File metadata and controls
86 lines (68 loc) · 2.04 KB
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
package main
import "fmt"
var (
errNoSuchContainer = fmt.Errorf("No such container")
)
// DockerClient interface for interacting with docker
type DockerClient interface {
Pull(ctx *Context, image string) error
GetImageID(ctx *Context, name string) (string, error)
RemoveContainer(ctx *Context, name string) error
RemoveImage(ctx *Context, image string) error
}
type cliDockerClient struct{}
func (c *cliDockerClient) Pull(ctx *Context, image string) error {
log.Debugw("Pulling image", "image", image, "requestId", ctx.id)
if true {
return nil
}
target := Target{
Binary: "docker",
Script: "pull",
}
output, err := target.execute(ctx, image)
if err != nil {
log.Errorw("Failed to pull image", "output", output, "error", err, "requestId", ctx.id)
}
return err
}
func (c *cliDockerClient) GetImageID(ctx *Context, name string) (string, error) {
log.Debugw("Retrieving image id", "name", name, "requestId", ctx.id)
target := Target{
Binary: "docker",
Script: "ps",
}
filter := fmt.Sprintf("name=^%s$", name)
output, err := target.execute(ctx, "-a", "--filter", filter, "--format", "{{.Image}}")
if output == "" {
return "", errNoSuchContainer
}
if err != nil {
log.Errorw("Failed to get image id", "output", output, "error", err, "requestId", ctx.id)
}
return output, err
}
func (c *cliDockerClient) RemoveContainer(ctx *Context, name string) error {
log.Debugw("Stopping and removing container", "name", name, "requestId", ctx.id)
target := Target{
Binary: "docker",
Script: "rm",
}
output, err := target.execute(ctx, "-f", name)
if err != nil {
log.Errorw("Failed to remove container", "output", output, "error", err, "requestId", ctx.id)
}
return err
}
func (c *cliDockerClient) RemoveImage(ctx *Context, image string) error {
log.Debugw("Removing image", "name", image, "requestId", ctx.id)
target := Target{
Binary: "docker",
Script: "rmi",
}
output, err := target.execute(ctx, image)
if err != nil {
log.Errorw("Failed to remove image", "output", output, "error", err, "requestId", ctx.id)
}
return err
}