/
docker.go
339 lines (290 loc) · 9.24 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
package helpers
import (
"bytes"
"context"
"fmt"
"math/rand"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/apecloud/dataprotection-wal-g/tests_func/utils"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/wal-g/tracelog"
)
var Docker *client.Client
const envDockerMachineName = "DOCKER_MACHINE_NAME"
type ExecResult struct {
ExitCode int
stdoutBuffer *bytes.Buffer
stderrBuffer *bytes.Buffer
}
func (res *ExecResult) Stdout() string {
return res.stdoutBuffer.String()
}
func (res *ExecResult) Stderr() string {
return res.stderrBuffer.String()
}
func (res *ExecResult) Combined() string {
return res.stdoutBuffer.String() + res.stderrBuffer.String()
}
func (res *ExecResult) String() string {
return fmt.Sprintf("code: %d\nstdout:\n%s\nstderr:\n%s\n", res.ExitCode, res.Stdout(), res.Stderr())
}
type RunOptions struct {
user string
}
type RunOption func(*RunOptions)
func TimeInContainer(ctx context.Context, container string) (time.Time, error) {
exc, err := RunCommandStrict(ctx, container, []string{"date", "+%s"})
if err != nil {
return time.Time{}, err
}
ts, err := strconv.ParseInt(strings.Trim(exc.Stdout(), "\n"), 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(ts, 0), nil
}
func RunCommandStrict(ctx context.Context, container string, command []string) (ExecResult, error) {
exc, err := RunCommand(ctx, container, command)
if err != nil {
return exc, err
}
if exc.ExitCode != 0 {
cmdLine := strings.Join(command, " ")
tracelog.ErrorLogger.Printf("'%s' failed with %d\nstdout:\n%s\nstderr:\n%s\n",
cmdLine, exc.ExitCode, exc.Stdout(), exc.Stderr())
return exc, fmt.Errorf("%s exit code: %d", cmdLine, exc.ExitCode)
}
return exc, nil
}
func RunCommand(ctx context.Context, container string, cmd []string) (ExecResult, error) {
args := &RunOptions{}
execConfig := types.ExecConfig{
AttachStdout: true,
AttachStderr: true,
User: args.user,
Cmd: cmd,
}
containerExec, err := Docker.ContainerExecCreate(ctx, container, execConfig)
if err != nil {
return ExecResult{}, err
}
attach, err := Docker.ContainerExecAttach(ctx, containerExec.ID, types.ExecConfig{})
if err != nil {
return ExecResult{}, err
}
defer attach.Close()
cmdLine := strings.Join(cmd, " ")
tracelog.DebugLogger.Printf("Running command on %s: %v", container, cmdLine)
var outBuf, errBuf bytes.Buffer
outputDone := make(chan error)
go func() {
_, err = stdcopy.StdCopy(&outBuf, &errBuf, attach.Reader)
outputDone <- err
}()
select {
case err := <-outputDone:
if err != nil {
return ExecResult{}, err
}
case <-ctx.Done():
return ExecResult{}, ctx.Err()
}
execInspect, err := Docker.ContainerExecInspect(ctx, containerExec.ID)
if err != nil {
return ExecResult{}, err
}
exc := ExecResult{ExitCode: execInspect.ExitCode, stdoutBuffer: &outBuf, stderrBuffer: &errBuf}
tracelog.DebugLogger.Printf("'%s' exit code %d\nstdout:\n%s\nstderr:\n%s\n",
cmdLine, exc.ExitCode, exc.Stdout(), exc.Stderr())
return exc, nil
}
func ContainerWithPrefix(containers []types.Container, name string) (*types.Container, error) {
for _, container := range containers {
if utils.StringInSlice(name, container.Names) {
return &container, nil
}
}
return nil, fmt.Errorf("cannot find container with name %s", name)
}
func DockerContainer(ctx context.Context, prefix string) (*types.Container, error) {
containers, err := Docker.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
return nil, fmt.Errorf("error in getting docker container: %v", err)
}
containerWithPrefixPointer, err := ContainerWithPrefix(containers, fmt.Sprintf("/%s", prefix))
if err != nil {
return nil, fmt.Errorf("error in getting docker container: %v", err)
}
return containerWithPrefixPointer, nil
}
func ExposedPort(container types.Container, port int) (string, int, error) {
machineName, hasMachineName := os.LookupEnv(envDockerMachineName)
host := "localhost"
if hasMachineName {
hostBytes, err := exec.Command("docker-machine", "ip", machineName).Output()
if err != nil {
return "", 0, fmt.Errorf("error in getting exposed port: %v", err)
}
host = string(hostBytes)
}
bindings := container.Ports
for _, value := range bindings {
if value.Type != "tcp" {
continue
}
if int(value.PrivatePort) == port {
return host, int(value.PublicPort), nil
}
}
return "", 0, fmt.Errorf("error in getting exposed port")
}
func ListNets(ctx context.Context, name string) ([]types.NetworkResource, error) {
networkFilters := filters.NewArgs()
networkResources, err := Docker.NetworkList(ctx, types.NetworkListOptions{
Filters: networkFilters,
})
var result []types.NetworkResource
for _, value := range networkResources {
if value.Name == name {
result = append(result, value)
}
}
if err != nil {
return []types.NetworkResource{}, fmt.Errorf("error in getting network list with name: %v", err)
}
return result, nil
}
func CreateNet(ctx context.Context, netName string) error {
networkList, err := ListNets(ctx, netName)
if err != nil {
return fmt.Errorf("error in creating network: %v", err)
}
if len(networkList) != 0 {
tracelog.DebugLogger.Printf("Found networks: %+v", networkList)
return nil
}
ipam := &network.IPAM{
Config: []network.IPAMConfig{{Subnet: fmt.Sprintf("10.0.%d.0/24", rand.Intn(255))}},
}
netOpts := map[string]string{
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.netName": netName,
}
config := types.NetworkCreate{
IPAM: ipam,
Options: netOpts,
}
_, err = Docker.NetworkCreate(ctx, netName, config)
if err != nil {
return fmt.Errorf("error in creating network: %v", err)
}
tracelog.DebugLogger.Printf("Network %v has been created", netName)
return nil
}
func ExposedHostPort(ctx context.Context, fqdn string, port int) (string, int, error) {
dockerContainer, err := DockerContainer(ctx, fqdn)
if err != nil {
return "", 0, fmt.Errorf("can not get docker container: %v", err)
}
return ExposedPort(*dockerContainer, port)
}
func BuildImage(ctx context.Context, tag string, path string) error {
cmd := exec.CommandContext(ctx, "docker", "build", "-t", tag, path)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
tracelog.DebugLogger.Printf("Building image %v with tag %v by command %v", path, tag, cmd)
if err := cmd.Run(); err != nil {
return fmt.Errorf("error in building base: %v", err)
}
tracelog.DebugLogger.Printf("Image %v with tag %v was build successfully", path, tag)
return nil
}
type BaseImage struct {
Path string
Tag string
}
type Infra struct {
ctx context.Context
config string
env map[string]string
net string
baseImage BaseImage
}
func NewInfra(ctx context.Context,
config string, env map[string]string, net string, base BaseImage) *Infra {
return &Infra{
ctx: ctx,
config: config,
env: env,
net: net,
baseImage: base,
}
}
func (inf *Infra) Setup() error {
if err := CreateNet(inf.ctx, inf.net); err != nil {
return fmt.Errorf("can not create network: %v", err)
}
if err := BuildImage(inf.ctx, inf.baseImage.Tag, inf.baseImage.Path); err != nil {
return fmt.Errorf("can not build base image: %v", err)
}
actions := []string{"--verbose", "--log-level", "WARNING", "build"}
if err := inf.callCompose(actions); err != nil {
return fmt.Errorf("can not build images: %v", err)
}
return nil
}
func (inf *Infra) RecreateContainers() error {
actions := []string{"--verbose", "--log-level", "WARNING", "down", "--volumes", "--timeout", "0"}
if err := inf.callCompose(actions); err != nil {
return err
}
return inf.callCompose([]string{"--verbose", "--log-level", "WARNING", "up", "--detach"})
}
func (inf *Infra) Shutdown() error {
actions := []string{"down", "--rmi", "local", "--remove-orphans", "--timeout", "0"}
if err := inf.callCompose(actions); err != nil {
return fmt.Errorf("can not shutdown containers: %v", err)
}
if err := Docker.NetworkRemove(inf.ctx, inf.net); err != nil {
return fmt.Errorf("error in shutting down network: %v", err)
}
return nil
}
func (inf *Infra) callCompose(actions []string) error {
baseArgs := []string{"--file", inf.config, "-p", "test"}
baseArgs = append(baseArgs, actions...)
// lookup the full path before exec.CommandContext call
// (fixes https://github.com/docker/compose/issues/1135)
fullPath, err := exec.LookPath("docker-compose")
if err != nil {
return err
}
tracelog.DebugLogger.Printf("Running command %s: with args %v", fullPath, baseArgs)
cmd := exec.CommandContext(inf.ctx, fullPath, baseArgs...)
cmd.Env = append(cmd.Env, utils.EnvToList(inf.env)...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
tracelog.DebugLogger.Printf("Running command: %+v", cmd)
if err := cmd.Start(); err != nil {
return fmt.Errorf("can not start command: %v", err)
}
if err := cmd.Wait(); err != nil {
return fmt.Errorf("error when calling compose: %v", err)
}
tracelog.DebugLogger.Printf("Command '%v' has been completed successfully!", cmd)
return nil
}
func init() {
var err error
Docker, err = client.NewEnvClient()
tracelog.ErrorLogger.FatalOnError(err)
}