-
Notifications
You must be signed in to change notification settings - Fork 290
/
client.go
362 lines (315 loc) · 11.8 KB
/
client.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
package dockercompose
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"github.com/compose-spec/compose-go/loader"
"golang.org/x/mod/semver"
"github.com/compose-spec/compose-go/types"
"github.com/pkg/errors"
"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/internal/docker"
"github.com/tilt-dev/tilt/pkg/logger"
"github.com/tilt-dev/tilt/pkg/model"
compose "github.com/compose-spec/compose-go/cli"
)
// versionRegex handles both v1 and v2 version outputs, which have several variations.
// (See TestParseComposeVersionOutput for various cases.)
var versionRegex = regexp.MustCompile(`(?mi)^docker[ -]compose(?: version)?:? v?([^\s,]+),?(?: build ([a-z0-9-]+))?`)
type DockerComposeClient interface {
Up(ctx context.Context, configPaths []string, serviceName model.TargetName, shouldBuild bool, stdout, stderr io.Writer) error
Down(ctx context.Context, configPaths []string, stdout, stderr io.Writer) error
StreamLogs(ctx context.Context, configPaths []string, serviceName model.TargetName) io.ReadCloser
StreamEvents(ctx context.Context, configPaths []string) (<-chan string, error)
Project(ctx context.Context, configPaths []string) (*types.Project, error)
ContainerID(ctx context.Context, configPaths []string, serviceName model.TargetName) (container.ID, error)
Version(ctx context.Context) (canonicalVersion string, build string, err error)
}
type cmdDCClient struct {
env docker.Env
mu *sync.Mutex
composePath string
}
// TODO(dmiller): we might want to make this take a path to the docker-compose config so we don't
// have to keep passing it in.
func NewDockerComposeClient(env docker.LocalEnv) DockerComposeClient {
return &cmdDCClient{
env: docker.Env(env),
mu: &sync.Mutex{},
composePath: dcExecutablePath(),
}
}
func (c *cmdDCClient) Up(ctx context.Context, configPaths []string, serviceName model.TargetName, shouldBuild bool, stdout, stderr io.Writer) error {
var genArgs []string
// TODO(milas): this causes docker-compose to output a truly excessive amount of logging; it might
// make sense to hide it behind a special environment variable instead or something
if logger.Get(ctx).Level().ShouldDisplay(logger.VerboseLvl) {
genArgs = []string{"--verbose"}
}
for _, config := range configPaths {
genArgs = append(genArgs, "-f", config)
}
if shouldBuild {
var buildArgs = append([]string{}, genArgs...)
buildArgs = append(buildArgs, "build", serviceName.String())
cmd := c.dcCommand(ctx, buildArgs)
cmd.Stdout = stdout
cmd.Stderr = stderr
err := cmd.Run()
if err != nil {
return FormatError(cmd, nil, err)
}
}
// docker-compose up is not thread-safe, because network operations are non-atomic. See:
// https://github.com/tilt-dev/tilt/issues/2817
//
// docker-compose build can run in parallel fine, so we only want the mutex on the 'up' call.
//
// TODO(nick): It might make sense to use a CondVar so that we can log a message
// when we're waiting on another build...
c.mu.Lock()
defer c.mu.Unlock()
runArgs := append([]string{}, genArgs...)
runArgs = append(runArgs, "up", "--no-deps", "--no-build", "-d")
runArgs = append(runArgs, serviceName.String())
cmd := c.dcCommand(ctx, runArgs)
cmd.Stdout = stdout
cmd.Stderr = stderr
return FormatError(cmd, nil, cmd.Run())
}
func (c *cmdDCClient) Down(ctx context.Context, configPaths []string, stdout, stderr io.Writer) error {
// To be safe, we try not to run two docker-compose downs in parallel,
// because we know docker-compose up is not thread-safe.
c.mu.Lock()
defer c.mu.Unlock()
var args []string
if logger.Get(ctx).Level().ShouldDisplay(logger.VerboseLvl) {
args = []string{"--verbose"}
}
for _, config := range configPaths {
args = append(args, "-f", config)
}
args = append(args, "down")
cmd := c.dcCommand(ctx, args)
cmd.Stdout = stdout
cmd.Stderr = stderr
err := cmd.Run()
if err != nil {
return FormatError(cmd, nil, err)
}
return nil
}
func (c *cmdDCClient) StreamLogs(ctx context.Context, configPaths []string, serviceName model.TargetName) io.ReadCloser {
var args []string
for _, config := range configPaths {
args = append(args, "-f", config)
}
r, w := io.Pipe()
// NOTE: we can't practically remove "--no-color" due to the way that Docker Compose formats colorful lines; it
// will wrap the entire line (including the \n) with the color codes, so you end up with something like:
// \u001b[36mmyproject_my-container_1 exited with code 0\n\u001b[0m
// where the ANSI reset (\u001b[0m) is _AFTER_ the \n, which doesn't play nice with our log segment logic
// under some conditions - adding a final \n after stdout is closed would probably be sufficient given the
// current pattern of how Compose colorizes stuff, but it's really not worth the headache to find out
args = append(args, "logs", "--no-color", "--no-log-prefix", "--timestamps", "--follow", serviceName.String())
cmd := c.dcCommand(ctx, args)
cmd.Stdout = w
errBuf := bytes.Buffer{}
cmd.Stderr = &errBuf
go func() {
if cmdErr := cmd.Run(); cmdErr != nil {
_ = w.CloseWithError(fmt.Errorf("cmd `docker-compose %s` exited with error: \"%v\" (stderr: %s)",
strings.Join(args, " "), cmdErr, errBuf.String()))
} else {
_ = w.Close()
}
}()
return r
}
func (c *cmdDCClient) StreamEvents(ctx context.Context, configPaths []string) (<-chan string, error) {
ch := make(chan string)
var args []string
for _, config := range configPaths {
args = append(args, "-f", config)
}
args = append(args, "events", "--json")
cmd := c.dcCommand(ctx, args)
stdout, err := cmd.StdoutPipe()
if err != nil {
return ch, errors.Wrap(err, "making stdout pipe for `docker-compose events`")
}
err = cmd.Start()
if err != nil {
return ch, errors.Wrapf(err, "`docker-compose %s`",
strings.Join(args, " "))
}
go func() {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
ch <- scanner.Text()
}
if err := scanner.Err(); err != nil {
logger.Get(ctx).Infof("[DOCKER-COMPOSE WATCHER] scanning `events` output: %v", err)
}
err = cmd.Wait()
if err != nil {
logger.Get(ctx).Infof("[DOCKER-COMPOSE WATCHER] exited with error: %v", err)
}
}()
return ch, nil
}
func (c *cmdDCClient) Project(ctx context.Context, configPaths []string) (*types.Project, error) {
proj, err := c.loadProject(configPaths)
if err == nil {
return proj, nil
}
// HACK(milas): compose-go has known regressions with resolving variables during YAML loading
// if it fails, attempt to fallback to using the CLI to resolve the YAML and then parse
// it with compose-go
// see https://github.com/tilt-dev/tilt/issues/4795
proj, err = c.loadProjectCLI(ctx, configPaths)
if err != nil {
return nil, err
}
return proj, nil
}
func (c *cmdDCClient) ContainerID(ctx context.Context, configPaths []string, serviceName model.TargetName) (container.ID, error) {
id, err := c.dcOutput(ctx, configPaths, "ps", "-q", serviceName.String())
if err != nil {
return container.ID(""), err
}
return container.ID(id), nil
}
// Version runs `docker-compose version` and parses the output, returning the canonical version and build (if present).
//
// NOTE: The version subcommand was added in Docker Compose v1.4.0 (released 2015-08-04), so this won't work for
// truly ancient versions, but handles both v1 and v2.
func (c *cmdDCClient) Version(ctx context.Context) (string, string, error) {
cmd := c.dcCommand(ctx, []string{"version"})
stdout, err := cmd.Output()
if err != nil {
return "", "", FormatError(cmd, stdout, err)
}
return parseComposeVersionOutput(stdout)
}
func (c *cmdDCClient) loadProject(configPaths []string) (*types.Project, error) {
// NOTE: take care to keep relevant options in sync with FakeDCClient::Project() and cmdDCClient::loadProjectCLI()
// which work differently so cannot directly share options but need to behave similarly
opts, err := compose.NewProjectOptions(configPaths, compose.WithOsEnv, compose.WithResolvedPaths(true))
if err != nil {
return nil, err
}
proj, err := compose.ProjectFromOptions(opts)
if err != nil {
return nil, err
}
return proj, nil
}
func (c *cmdDCClient) loadProjectCLI(ctx context.Context, configPaths []string) (*types.Project, error) {
resolvedYAML, err := c.dcOutput(ctx, configPaths, "config")
if err != nil {
return nil, err
}
// docker-compose is very inconsistent about whether it fully resolves paths or not via CLI, both between
// v1 and v2 as well as even different releases within v2, so set the workdir and force the loader to resolve
// any relative paths
var workDir string
if len(configPaths) != 0 {
// from the compose Docs:
// > When you use multiple Compose files, all paths in the files are relative to the first configuration file specified with -f
// https://docs.docker.com/compose/reference/#use--f-to-specify-name-and-path-of-one-or-more-compose-files
workDir = filepath.Dir(configPaths[0])
}
return loader.Load(types.ConfigDetails{
WorkingDir: workDir,
ConfigFiles: []types.ConfigFile{
{
Content: []byte(resolvedYAML),
},
},
// no environment specified because the CLI call will already have resolved all variables
}, func(options *loader.Options) {
options.ResolvePaths = true
})
}
func dcExecutablePath() string {
v1Name := "docker-compose-v1"
if runtime.GOOS == "windows" {
v1Name += ".exe"
}
composePath, err := exec.LookPath(v1Name)
if err != nil {
composePath = "docker-compose"
}
return composePath
}
func (c *cmdDCClient) dcCommand(ctx context.Context, args []string) *exec.Cmd {
cmd := exec.CommandContext(ctx, c.composePath, args...)
cmd.Env = append(os.Environ(), c.env.AsEnviron()...)
return cmd
}
func (c *cmdDCClient) dcOutput(ctx context.Context, configPaths []string, args ...string) (string, error) {
var tempArgs []string
for _, config := range configPaths {
tempArgs = append(tempArgs, "-f", config)
}
args = append(tempArgs, args...)
cmd := c.dcCommand(ctx, args)
output, err := cmd.Output()
if err != nil {
errorMessage := fmt.Sprintf("command %q failed.\nerror: %v\nstdout: %q", cmd.Args, err, string(output))
if err, ok := err.(*exec.ExitError); ok {
errorMessage += fmt.Sprintf("\nstderr: '%v'", string(err.Stderr))
}
err = fmt.Errorf("%s", errorMessage)
}
return strings.TrimSpace(string(output)), err
}
// parseComposeVersionOutput parses the raw output of `docker-compose version` for both v1.x + v2.x Compose
// and returns the canonical semver + build (might be blank) or an error.
func parseComposeVersionOutput(stdout []byte) (string, string, error) {
// match 0: raw output
// match 1: version w/o leading v (required)
// match 2: build (optional)
m := versionRegex.FindSubmatch(bytes.TrimSpace(stdout))
if len(m) < 2 {
return "", "", fmt.Errorf("could not parse version from output: %q", string(stdout))
}
rawVersion := "v" + string(m[1])
canonicalVersion := semver.Canonical(rawVersion)
if canonicalVersion == "" {
return "", "", fmt.Errorf("invalid version: %q", rawVersion)
}
build := semver.Build(rawVersion)
if build != "" {
// prefer semver build if present, but strip off the leading `+`
// (currently, Docker Compose has not made use of this, preferring to list the build independently if at all)
build = strings.TrimPrefix(build, "+")
} else if len(m) > 2 {
// otherwise, fall back to regex match if possible
build = string(m[2])
}
return canonicalVersion, build, nil
}
func FormatError(cmd *exec.Cmd, stdout []byte, err error) error {
if err == nil {
return nil
}
errorMessage := fmt.Sprintf("command %q failed.\nerror: %v\n", cmd.Args, err)
if len(stdout) > 0 {
errorMessage += fmt.Sprintf("\nstdout: '%v'", string(stdout))
}
if err, ok := err.(*exec.ExitError); ok && len(err.Stderr) > 0 {
errorMessage += fmt.Sprintf("\nstderr: '%v'", string(err.Stderr))
}
return fmt.Errorf("%s", errorMessage)
}