/
container.go
143 lines (115 loc) 路 3.5 KB
/
container.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
package docker
import (
"context"
"errors"
"fmt"
"os"
"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/network"
"github.com/docker/docker/client"
"go.uber.org/zap"
)
type EnsureContainerOpts struct {
ContainerName string
ContainerConfig *container.Config
HostConfig *container.HostConfig
NetworkingConfig *network.NetworkingConfig
PullImageIfMissing bool
}
func CheckContainer(ctx context.Context, log *zap.SugaredLogger, cli *client.Client, name string) (container *types.ContainerJSON, err error) {
log = log.With("container_name", name)
log.Debug("checking state of container")
// Check if a ${name} container exists.
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{
All: true,
Filters: filters.NewArgs(filters.Arg("name", name)),
})
if err != nil || len(containers) == 0 {
return nil, err
}
c := containers[0]
log.Debugw("container found", "container_id", c.ID, "state", c.State)
ci, err := cli.ContainerInspect(ctx, c.ID)
if err != nil {
log.Errorw("inspecting container failed", "container_id", container.ID)
return nil, err
}
return &ci, nil
}
// EnsureContainer ensures there's a container started of the specified kind.
func EnsureContainer(ctx context.Context, log *zap.SugaredLogger, cli *client.Client,
opts *EnsureContainerOpts) (container *types.ContainerJSON, created bool, err error) {
log = log.With("container_name", opts.ContainerName)
log.Debug("checking state of container")
ci, err := CheckContainer(ctx, log, cli, opts.ContainerName)
if err != nil {
return nil, false, err
}
if ci != nil {
if ci.State.Status == "running" {
log.Info("container is already running")
return ci, false, err
}
log.Infof("container isn't running; starting")
err := cli.ContainerStart(ctx, ci.ID, types.ContainerStartOptions{})
if err != nil {
log.Errorw("starting container failed", "container_id", container.ID)
return nil, false, err
}
ci, err = CheckContainer(ctx, log, cli, opts.ContainerName)
return ci, false, err
}
log.Infow("container not found; creating")
if opts.PullImageIfMissing {
out, err := cli.ImagePull(ctx, opts.ContainerConfig.Image, types.ImagePullOptions{})
if err != nil {
return nil, false, err
}
if err := PipeOutput(out, os.Stdout); err != nil {
return nil, false, err
}
} else {
imageListOpts := types.ImageListOptions{
All: true,
}
images, err := cli.ImageList(ctx, imageListOpts)
if err != nil {
log.Errorw("retrieving list of images failed")
return nil, false, err
}
found := false
for _, summary := range images {
if len(summary.RepoTags) > 0 && summary.RepoTags[0] == opts.ContainerConfig.Image {
found = true
break
}
}
if !found {
log.Errorw("image not found", "image", opts.ContainerConfig.Image)
err := errors.New("image not found")
return nil, false, err
}
}
res, err := cli.ContainerCreate(ctx,
opts.ContainerConfig,
opts.HostConfig,
opts.NetworkingConfig,
opts.ContainerName,
)
if err != nil {
return nil, false, err
}
log.Infow("starting new container", "id", res.ID)
err = cli.ContainerStart(ctx, res.ID, types.ContainerStartOptions{})
if err != nil {
return nil, false, err
}
log.Infow("started container", "id", res.ID)
c, err := cli.ContainerInspect(ctx, res.ID)
if err != nil {
return nil, false, fmt.Errorf("failed to inspect container: %w", err)
}
return &c, true, err
}