-
-
Notifications
You must be signed in to change notification settings - Fork 511
/
compose_local.go
398 lines (329 loc) · 10.8 KB
/
compose_local.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
package testcontainers
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"gopkg.in/yaml.v3"
"github.com/testcontainers/testcontainers-go/wait"
)
var (
_ ComposeVersion = (*composeVersion1)(nil)
_ ComposeVersion = (*composeVersion2)(nil)
)
type ComposeVersion interface {
Format(parts ...string) string
}
type composeVersion1 struct {
}
func (c composeVersion1) Format(parts ...string) string {
return strings.Join(parts, "_")
}
type composeVersion2 struct {
}
func (c composeVersion2) Format(parts ...string) string {
return strings.Join(parts, "-")
}
// LocalDockerCompose represents a Docker Compose execution using local binary
// docker-compose or docker-compose.exe, depending on the underlying platform
type LocalDockerCompose struct {
ComposeVersion
*LocalDockerComposeOptions
Executable string
ComposeFilePaths []string
absComposeFilePaths []string
Identifier string
Cmd []string
Env map[string]string
Services map[string]interface{}
waitStrategySupplied bool
WaitStrategyMap map[waitService]wait.Strategy
}
type (
// LocalDockerComposeOptions defines options applicable to LocalDockerCompose
LocalDockerComposeOptions struct {
Logger Logging
}
// LocalDockerComposeOption defines a common interface to modify LocalDockerComposeOptions
// These options can be passed to NewLocalDockerCompose in a variadic way to customize the returned LocalDockerCompose instance
LocalDockerComposeOption interface {
ApplyToLocalCompose(opts *LocalDockerComposeOptions)
}
// LocalDockerComposeOptionsFunc is a shorthand to implement the LocalDockerComposeOption interface
LocalDockerComposeOptionsFunc func(opts *LocalDockerComposeOptions)
)
func (f LocalDockerComposeOptionsFunc) ApplyToLocalCompose(opts *LocalDockerComposeOptions) {
f(opts)
}
// Down executes docker-compose down
func (dc *LocalDockerCompose) Down() ExecError {
return executeCompose(dc, []string{"down", "--remove-orphans", "--volumes"})
}
func (dc *LocalDockerCompose) getDockerComposeEnvironment() map[string]string {
environment := map[string]string{}
composeFileEnvVariableValue := ""
for _, abs := range dc.absComposeFilePaths {
composeFileEnvVariableValue += abs + string(os.PathListSeparator)
}
environment[envProjectName] = dc.Identifier
environment[envComposeFile] = composeFileEnvVariableValue
return environment
}
func (dc *LocalDockerCompose) containerNameFromServiceName(service, separator string) string {
return dc.Identifier + separator + service
}
func (dc *LocalDockerCompose) applyStrategyToRunningContainer() error {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return err
}
cli.NegotiateAPIVersion(context.Background())
for k := range dc.WaitStrategyMap {
containerName := dc.containerNameFromServiceName(k.service, "_")
composeV2ContainerName := dc.containerNameFromServiceName(k.service, "-")
f := filters.NewArgs(
filters.Arg("name", containerName),
filters.Arg("name", composeV2ContainerName),
filters.Arg("name", k.service))
containerListOptions := types.ContainerListOptions{Filters: f, All: true}
containers, err := cli.ContainerList(context.Background(), containerListOptions)
if err != nil {
return fmt.Errorf("error %w occured while filtering the service %s: %d by name and published port", err, k.service, k.publishedPort)
}
if len(containers) == 0 {
return fmt.Errorf("service with name %s not found in list of running containers", k.service)
}
// The length should always be a list of 1, since we are matching one service name at a time
if l := len(containers); l > 1 {
return fmt.Errorf("expecting only one running container for %s but got %d", k.service, l)
}
container := containers[0]
strategy := dc.WaitStrategyMap[k]
dockerProvider, err := NewDockerProvider(WithLogger(dc.Logger))
if err != nil {
return fmt.Errorf("unable to create new Docker Provider: %w", err)
}
dockercontainer := &DockerContainer{ID: container.ID, WaitingFor: strategy, provider: dockerProvider, logger: dc.Logger}
err = strategy.WaitUntilReady(context.Background(), dockercontainer)
if err != nil {
return fmt.Errorf("Unable to apply wait strategy %v to service %s due to %w", strategy, k.service, err)
}
}
return nil
}
// Invoke invokes the docker compose
func (dc *LocalDockerCompose) Invoke() ExecError {
return executeCompose(dc, dc.Cmd)
}
// WaitForService sets the strategy for the service that is to be waited on
func (dc *LocalDockerCompose) WaitForService(service string, strategy wait.Strategy) DockerCompose {
dc.waitStrategySupplied = true
dc.WaitStrategyMap[waitService{service: service}] = strategy
return dc
}
// WithCommand assigns the command
func (dc *LocalDockerCompose) WithCommand(cmd []string) DockerCompose {
dc.Cmd = cmd
return dc
}
// WithEnv assigns the environment
func (dc *LocalDockerCompose) WithEnv(env map[string]string) DockerCompose {
dc.Env = env
return dc
}
// WithExposedService sets the strategy for the service that is to be waited on. If multiple strategies
// are given for a single service running on different ports, both strategies will be applied on the same container
func (dc *LocalDockerCompose) WithExposedService(service string, port int, strategy wait.Strategy) DockerCompose {
dc.waitStrategySupplied = true
dc.WaitStrategyMap[waitService{service: service, publishedPort: port}] = strategy
return dc
}
// determineVersion checks which version of docker-compose is installed
// depending on the version services names are composed in a different way
func (dc *LocalDockerCompose) determineVersion() error {
execErr := executeCompose(dc, []string{"version", "--short"})
if err := execErr.Error; err != nil {
return err
}
components := bytes.Split(execErr.StdoutOutput, []byte("."))
if componentsLen := len(components); componentsLen != 3 {
return fmt.Errorf("expected 3 version components in %s", execErr.StdoutOutput)
}
majorVersion, err := strconv.ParseInt(string(components[0]), 10, 8)
if err != nil {
return err
}
switch majorVersion {
case 1:
dc.ComposeVersion = composeVersion1{}
case 2:
dc.ComposeVersion = composeVersion2{}
default:
return fmt.Errorf("unexpected compose version %d", majorVersion)
}
return nil
}
// validate checks if the files to be run in the compose are valid YAML files, setting up
// references to all services in them
func (dc *LocalDockerCompose) validate() error {
type compose struct {
Services map[string]interface{}
}
for _, abs := range dc.absComposeFilePaths {
c := compose{}
yamlFile, err := os.ReadFile(abs)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, &c)
if err != nil {
return err
}
if dc.Services == nil {
dc.Services = c.Services
} else {
for k, v := range c.Services {
dc.Services[k] = v
}
}
}
return nil
}
// ExecError is super struct that holds any information about an execution error, so the client code
// can handle the result
type ExecError struct {
Command []string
StdoutOutput []byte
StderrOutput []byte
Error error
Stdout error
Stderr error
}
// execute executes a program with arguments and environment variables inside a specific directory
func execute(
dirContext string, environment map[string]string, binary string, args []string) ExecError {
var errStdout, errStderr error
cmd := exec.Command(binary, args...)
cmd.Dir = dirContext
cmd.Env = os.Environ()
for key, value := range environment {
cmd.Env = append(cmd.Env, key+"="+value)
}
stdoutIn, _ := cmd.StdoutPipe()
stderrIn, _ := cmd.StderrPipe()
stdout := newCapturingPassThroughWriter(os.Stdout)
stderr := newCapturingPassThroughWriter(os.Stderr)
err := cmd.Start()
if err != nil {
execCmd := []string{"Starting command", dirContext, binary}
execCmd = append(execCmd, args...)
return ExecError{
// add information about the CMD and arguments used
Command: execCmd,
StdoutOutput: stdout.Bytes(),
StderrOutput: stderr.Bytes(),
Error: err,
Stderr: errStderr,
Stdout: errStdout,
}
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
_, errStdout = io.Copy(stdout, stdoutIn)
wg.Done()
}()
_, errStderr = io.Copy(stderr, stderrIn)
wg.Wait()
err = cmd.Wait()
execCmd := []string{"Reading std", dirContext, binary}
execCmd = append(execCmd, args...)
return ExecError{
Command: execCmd,
StdoutOutput: stdout.Bytes(),
StderrOutput: stderr.Bytes(),
Error: err,
Stderr: errStderr,
Stdout: errStdout,
}
}
func executeCompose(dc *LocalDockerCompose, args []string) ExecError {
if which(dc.Executable) != nil {
return ExecError{
Command: []string{dc.Executable},
Error: fmt.Errorf("Local Docker Compose not found. Is %s on the PATH?", dc.Executable),
}
}
environment := dc.getDockerComposeEnvironment()
for k, v := range dc.Env {
environment[k] = v
}
var cmds []string
pwd := "."
if len(dc.absComposeFilePaths) > 0 {
pwd, _ = filepath.Split(dc.absComposeFilePaths[0])
for _, abs := range dc.absComposeFilePaths {
cmds = append(cmds, "-f", abs)
}
} else {
cmds = append(cmds, "-f", "docker-compose.yml")
}
cmds = append(cmds, args...)
execErr := execute(pwd, environment, dc.Executable, cmds)
err := execErr.Error
if err != nil {
args := strings.Join(dc.Cmd, " ")
return ExecError{
Command: []string{dc.Executable},
Error: fmt.Errorf("Local Docker compose exited abnormally whilst running %s: [%v]. %s", dc.Executable, args, err.Error()),
}
}
if dc.waitStrategySupplied {
// If the wait strategy has been executed once for all services during startup , disable it so that it is not invoked while tearing down
dc.waitStrategySupplied = false
if err := dc.applyStrategyToRunningContainer(); err != nil {
return ExecError{
Error: fmt.Errorf("one or more wait strategies could not be applied to the running containers: %w", err),
}
}
}
return execErr
}
// capturingPassThroughWriter is a writer that remembers
// data written to it and passes it to w
type capturingPassThroughWriter struct {
buf bytes.Buffer
w io.Writer
}
// newCapturingPassThroughWriter creates new capturingPassThroughWriter
func newCapturingPassThroughWriter(w io.Writer) *capturingPassThroughWriter {
return &capturingPassThroughWriter{
w: w,
}
}
func (w *capturingPassThroughWriter) Write(d []byte) (int, error) {
w.buf.Write(d)
return w.w.Write(d)
}
// Bytes returns bytes written to the writer
func (w *capturingPassThroughWriter) Bytes() []byte {
b := w.buf.Bytes()
if b == nil {
b = []byte{}
}
return b
}
// Which checks if a binary is present in PATH
func which(binary string) error {
_, err := exec.LookPath(binary)
return err
}