-
Notifications
You must be signed in to change notification settings - Fork 290
/
client.go
221 lines (186 loc) · 6.36 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
package dockercompose
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/pkg/errors"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/internal/docker"
"github.com/windmilleng/tilt/pkg/logger"
"github.com/windmilleng/tilt/pkg/model"
)
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, error)
StreamEvents(ctx context.Context, configPaths []string) (<-chan string, error)
Config(ctx context.Context, configPaths []string) (string, error)
Services(ctx context.Context, configPaths []string) (string, error)
ContainerID(ctx context.Context, configPaths []string, serviceName model.TargetName) (container.ID, error)
}
type cmdDCClient struct {
env docker.Env
}
// 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),
}
}
func (c *cmdDCClient) Up(ctx context.Context, configPaths []string, serviceName model.TargetName, shouldBuild bool, stdout, stderr io.Writer) error {
var args []string
if logger.Get(ctx).Level() >= logger.VerboseLvl {
args = []string{"--verbose"}
}
for _, config := range configPaths {
args = append(args, "-f", config)
}
args = append(args, "up", "--no-deps", "-d")
if shouldBuild {
args = append(args, "--build")
} else {
// !shouldBuild implies that Tilt will take care of building, which implies that
// we should recreate container so that we pull the new image
// NOTE(maia): this is maybe the WRONG thing to do if we're deploying a service
// but none of the code changed (i.e. it was just a dockercompose.yml change)?
args = append(args, "--force-recreate")
}
args = append(args, serviceName.String())
cmd := c.dcCommand(ctx, args)
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 {
var args []string
if logger.Get(ctx).Level() >= 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, error) {
// TODO(maia): --since time
// (may need to implement with `docker log <cID>` instead since `d-c log` doesn't support `--since`
var args []string
for _, config := range configPaths {
args = append(args, "-f", config)
}
args = append(args, "logs", "-f", "-t", serviceName.String())
cmd := c.dcCommand(ctx, args)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, errors.Wrap(err, "making stdout pipe for `docker-compose logs`")
}
errBuf := bytes.Buffer{}
cmd.Stderr = &errBuf
err = cmd.Start()
if err != nil {
return nil, errors.Wrapf(err, "`docker-compose %s`",
strings.Join(args, " "))
}
go func() {
err = cmd.Wait()
if err != nil {
logger.Get(ctx).Debugf("cmd `docker-compose %s` exited with error: \"%v\" (stderr: %s)",
strings.Join(args, " "), err, errBuf.String())
}
}()
return stdout, nil
}
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) Config(ctx context.Context, configPaths []string) (string, error) {
return c.dcOutput(ctx, configPaths, "config")
}
func (c *cmdDCClient) Services(ctx context.Context, configPaths []string) (string, error) {
return c.dcOutput(ctx, configPaths, "config", "--services")
}
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
}
func (c *cmdDCClient) dcCommand(ctx context.Context, args []string) *exec.Cmd {
cmd := exec.CommandContext(ctx, "docker-compose", 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(errorMessage)
}
return strings.TrimSpace(string(output)), err
}
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(errorMessage)
}