-
Notifications
You must be signed in to change notification settings - Fork 0
/
idock.go
413 lines (344 loc) · 10 KB
/
idock.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
403
404
405
406
407
408
409
410
411
412
413
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package idock
import (
"context"
"errors"
"fmt"
"net"
"os"
"os/exec"
"sort"
"sync"
"time"
)
const (
// IDOCK_VERBOSITY_FLAG is the default environment variable that controls
// the verbosity of the output.
VERBOSITY_FLAG = "IDOCK_VERBOSITY"
// DOCKER_MAX_PULL_WAIT_FLAG is the default environment variable that
// controls how long to wait for the docker-compose program to pull images.
DOCKER_MAX_PULL_WAIT_FLAG = "IDOCK_DOCKER_MAX_PULL_WAIT"
// DOCKER_MAX_WAIT_FLAG is the default environment variable that controls
// how long to wait for the docker-compose program to start.
DOCKER_MAX_WAIT_FLAG = "IDOCK_DOCKER_MAX_WAIT"
// PROGRAM_MAX_WAIT_FLAG is the default environment variable that controls
// how long to wait for the program to start.
PROGRAM_MAX_WAIT_FLAG = "IDOCK_PROGRAM_MAX_WAIT"
// CLEANUP_ATTEMPTS_FLAG is the default environment variable that controls
// how many times to retry the cleanup process.
CLEANUP_ATTEMPTS_FLAG = "IDOCK_CLEANUP_ATTEMPTS"
// SKIP_DOCKER_PULL_FLAG is the default environment variable that controls
// whether or not to skip the docker-compose pull step.
SKIP_DOCKER_PULL_FLAG = "IDOCK_SKIP_DOCKER_PULL"
)
var (
errTimedOut = fmt.Errorf("timed out")
)
// IDock is the main struct for the idock package.
type IDock struct {
// env variable names
verbosityFlag string
dockerMaxPullWaitFlag string
dockerMaxWaitFlag string
programMaxWaitFlag string
cleanupAttemptsFlag string
skipDockerPullFlag string
tcpPortMaxWait time.Duration
dockerComposeFile string
dockerTCPPorts []int
dockerMaxWait time.Duration
dockerMaxPullWait time.Duration
skipDockerPull bool
afterDocker func(context.Context, *IDock)
program func()
programTCPPorts []int
programMaxWait time.Duration
afterProgram func(context.Context, *IDock)
cleanupAttempts int
localhost string
verbosity int
noDockerCompose bool
dockerStarted bool
}
// Option is an option interface for the IDock struct.
type Option interface {
apply(*IDock)
}
type optionFunc func(*IDock)
func (f optionFunc) apply(c *IDock) {
f(c)
}
func emptyProgram() {}
func emptyAfter(context.Context, *IDock) {}
// New creates a new IDock struct with the given options.
func New(opts ...Option) *IDock {
var c IDock
defaults := []Option{
VerbosityEnvarName(VERBOSITY_FLAG),
CleanupAttemptsEnvarName(CLEANUP_ATTEMPTS_FLAG),
DockerMaxWaitEnvarName(DOCKER_MAX_WAIT_FLAG),
ProgramMaxWaitEnvarName(PROGRAM_MAX_WAIT_FLAG),
DockerPullMaxWaitEnvarName(DOCKER_MAX_PULL_WAIT_FLAG),
SkipDockerPullEnvarName(SKIP_DOCKER_PULL_FLAG),
TCPPortMaxWait(10 * time.Millisecond),
DockerMaxWait(10 * time.Second),
DockerPullMaxWait(60 * time.Second),
ProgramMaxWait(10 * time.Second),
AfterDocker(nil),
Program(nil),
AfterProgram(nil),
Localhost("localhost"),
CleanupAttempts(3),
}
opts = append(defaults, opts...)
opts = append(opts, []Option{
verbosity(),
cleanupRetries(),
dockerMaxWait(),
programMaxWait(),
skipDockerPull(),
}...)
for _, opt := range opts {
opt.apply(&c)
}
return &c
}
// Verbosity gets the verbosity level. The verbosity level is set by the
// IDOCK_VERBOSITY environment variable, or by the Verbosity option. The
// environment variable takes precedence over the option. The default value is
// 0.
func (c *IDock) Verbosity() int {
return c.verbosity
}
// Start starts the docker-compose services and the program. It waits for the
// docker-compose services and the program to start before returning. If the
// docker-compose services or the program fail to start, then an error is
// returned.
func (c *IDock) Start() error {
ctx := context.Background()
err := c.startDocker(ctx)
if err != nil {
c.logf(0, "docker startup failed: %s\n", err)
return err
}
if c.afterDocker != nil {
start := time.Now()
c.afterDocker(ctx, c)
end := time.Now()
c.logf(1, "customization after docker took %s\n", end.Sub(start))
}
err = c.startProgram(ctx)
if err != nil {
c.logf(0, "program startup failed: %s\n", err)
return err
}
if c.afterProgram != nil {
start := time.Now()
c.afterProgram(ctx, c)
end := time.Now()
c.logf(1, "customization after program took %s\n", end.Sub(start))
}
return nil
}
// Stop stops the docker-compose services and cleans up any docker containers
// that were started. If force is set to a value greater than 0, then the
// cleanup process is attempted that many times before giving up, overwriting
// the value set by the CleanupAttempts option or the IDOCK_CLEANUP_ATTEMPTS
// environment variable.
func (c *IDock) Stop(force ...int) {
if len(force) > 0 && force[0] > 0 {
c.cleanupAttempts = force[0]
}
c.cleanup()
}
func (c *IDock) startDocker(ctx context.Context) error {
if c.dockerComposeFile == "" {
return nil
}
verbose := c.verbosity > 1
if c.skipDockerPull {
c.logf(1, "Skipping docker-compose pull\n")
} else {
cmd, err := dockerCompose(ctx, verbose, "-f", c.dockerComposeFile, "pull")
if err != nil {
return fmt.Errorf("error pulling docker images: %w", err)
}
cmd.WaitDelay = c.dockerMaxPullWait
dockerPullStart := time.Now()
c.logf(1, "Waiting for docker images to pull...\n")
err = cmd.Start()
if err != nil {
return fmt.Errorf("error pulling docker images: %w", err)
}
err = cmd.Wait()
if err != nil {
c.logf(0, "docker-compose pull failed: %s\n", err)
return err
}
c.logf(1, "docker-compose pull took %s\n", time.Since(dockerPullStart))
}
args := []string{"-f", c.dockerComposeFile, "up", "-d"}
if verbose {
args = append([]string{"--verbose"}, args...)
}
ctx, cancel := context.WithTimeout(ctx, c.dockerMaxWait)
defer cancel()
cmd, err := dockerCompose(ctx, verbose, args...)
if err != nil {
return err
}
cmd.WaitDelay = c.dockerMaxWait
dockerStart := time.Now()
err = cmd.Start()
if err != nil {
return err
}
c.dockerStarted = true
c.logf(1, "Waiting for services to start...\n")
err = cmd.Wait()
if err != nil {
c.logf(0, "docker-compose services failed to start: %s\n", err)
return err
}
err = c.waitForPorts(ctx, c.dockerTCPPorts)
if err != nil {
c.logf(1, "docker-compose services took too long to start (%s)\n", c.dockerMaxWait)
return err
}
dockerReady := time.Now()
c.logf(1, "docker-compose services took %s to start\n", dockerReady.Sub(dockerStart))
return nil
}
func (c *IDock) cleanup() {
c.logf(1, "Cleaning up...\n")
if !c.dockerStarted {
return
}
if c.cleanupAttempts < 1 {
c.logf(0, "Docker container left intact. To cleanup run:\n")
c.logf(0, "docker-compose -f %s down --remove-orphans\n", c.dockerComposeFile)
return
}
args := []string{"-f", c.dockerComposeFile, "down", "--remove-orphans"}
cmd, err := dockerCompose(context.Background(), c.verbosity > 1, args...)
if err == nil {
for i := 0; i < c.cleanupAttempts; i++ {
err := cmd.Run()
if err == nil {
return
}
c.logf(1, "Failed to clean up docker-compose services on try %d: %s\n", i+1, err)
}
}
fmt.Printf("Failed to clean up docker services. Please run `docker-compose down --remove-orphans` manually\n")
}
func (c *IDock) startProgram(ctx context.Context) error {
start := time.Now()
ctx, cancel := context.WithTimeout(ctx, c.programMaxWait)
defer cancel()
go c.program()
err := c.waitForPorts(ctx, c.programTCPPorts)
end := time.Now()
if err != nil {
c.logf(1, "program took too long to start: %s\n", end.Sub(start))
return err
}
c.logf(1, "program startup took %s\n", end.Sub(start))
return nil
}
func (c *IDock) isPortOpen(ctx context.Context, port int) error {
var d net.Dialer
address := fmt.Sprintf("%s:%d", c.localhost, port)
conn, err := d.DialContext(ctx, "tcp", address)
if err != nil {
return errTimedOut
}
conn.Close()
return nil
}
func (c *IDock) waitForPorts(ctx context.Context, ports []int) error {
var wg sync.WaitGroup
results := make(chan int, len(ports))
for _, port := range ports {
wg.Add(1)
go func(ctx context.Context, port int) {
for {
sub, cancel := context.WithTimeout(ctx, c.tcpPortMaxWait)
err := c.isPortOpen(sub, port)
if err == nil {
results <- port
cancel()
wg.Done()
return
}
// timing out while checking is not fatal unless the
// root context is done.
if errors.Is(err, errTimedOut) && ctx.Err() == nil {
cancel()
// Give the service a chance to start.
time.Sleep(c.tcpPortMaxWait)
continue
}
results <- (-1 * port)
cancel()
wg.Done()
return
}
}(ctx, port)
}
wg.Wait()
close(results)
succeeded := make([]int, 0, len(ports))
failed := make([]int, 0, len(ports))
for result := range results {
if result < 0 {
failed = append(failed, (-1 * result))
} else {
succeeded = append(succeeded, result)
}
}
sort.Ints(succeeded)
for _, port := range succeeded {
c.logf(1, "Port %d started\n", port)
}
sort.Ints(failed)
for _, port := range failed {
c.logf(1, "Port %d failed to start\n", port)
}
if len(failed) > 0 {
return errTimedOut
}
c.logf(1, "All services are ready.\n")
return nil
}
// logf prints a message if the verbosity level is greater than or equal to the
// given level.
func (c *IDock) logf(level int, format string, a ...any) {
if c.verbosity >= level {
fmt.Printf(format, a...)
}
}
func dockerCompose(ctx context.Context, useStdout bool, args ...string) (*exec.Cmd, error) {
cmd := exec.CommandContext(ctx, "docker-compose", args...)
if cmd == nil {
return nil, errors.New("failed to create docker-compose command")
}
// Some systems don't have docker-compose installed, so try to use the docker-compose
// binary from the docker image instead.
if errors.Is(cmd.Err, exec.ErrNotFound) {
args = append([]string{"compose"}, args...)
cmd = exec.CommandContext(ctx, "docker", args...)
if cmd == nil {
return nil, errors.New("failed to create docker-compose command")
}
}
if cmd.Err != nil {
return nil, cmd.Err
}
cmd.Stderr = os.Stderr
if useStdout {
cmd.Stdout = os.Stdout
}
return cmd, nil
}