-
Notifications
You must be signed in to change notification settings - Fork 296
/
docker_compose.go
311 lines (257 loc) · 8.94 KB
/
docker_compose.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
package tiltfile
import (
"context"
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
"strings"
"github.com/docker/distribution/reference"
"github.com/pkg/errors"
"go.starlark.net/starlark"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/internal/dockercompose"
"github.com/windmilleng/tilt/internal/tiltfile/starkit"
"github.com/windmilleng/tilt/internal/tiltfile/value"
"github.com/windmilleng/tilt/pkg/model"
)
// dcResourceSet represents a single docker-compose config file and all its associated services
type dcResourceSet struct {
configPaths []string
services []*dcService
tiltfilePath string
}
func (dc dcResourceSet) Empty() bool { return reflect.DeepEqual(dc, dcResourceSet{}) }
func (s *tiltfileState) dockerCompose(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var configPathsValue starlark.Value
err := s.unpackArgs(fn.Name(), args, kwargs, "configPaths", &configPathsValue)
if err != nil {
return nil, err
}
pathSlice := starlarkValueOrSequenceToSlice(configPathsValue)
var configPaths []string
for _, v := range pathSlice {
path, err := value.ValueToAbsPath(thread, v)
if err != nil {
return nil, fmt.Errorf("docker_compose files must be a string or a sequence of strings; found a %T", v)
}
configPaths = append(configPaths, path)
}
var services []*dcService
tempServices, err := parseDCConfig(s.ctx, s.dcCli, configPaths)
services = append(services, tempServices...)
if err != nil {
return nil, err
}
if !s.dc.Empty() {
return starlark.None, fmt.Errorf("already have a docker-compose resource declared (%v), cannot declare another", s.dc.configPaths)
}
s.dc = dcResourceSet{
configPaths: configPaths,
services: services,
tiltfilePath: starkit.CurrentExecPath(thread),
}
return starlark.None, nil
}
// DCResource allows you to adjust specific settings on a DC resource that we assume
// to be defined in a `docker_compose.yml`
func (s *tiltfileState) dcResource(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var name string
var imageVal starlark.Value
var triggerMode triggerMode
var resourceDepsVal starlark.Sequence
if err := s.unpackArgs(fn.Name(), args, kwargs,
"name", &name,
// TODO(maia): if you docker_build('myimg') and dc.yml refers to 'myimg', we
// associate the docker_build with your dc resource automatically. What we
// CAN'T do is use the arg to dc_resource.image to OVERRIDE the image named
// in dc.yml, which we should probs be able to do?
// (If your dc.yml does NOT specify `Image`, DC will expect an image of name
// <directory>_<service>, and Tilt has no way of figuring this out yet, so
// can't auto-associate that image, you need to use dc_resource.)
"image?", &imageVal,
"trigger_mode?", &triggerMode,
"resource_deps?", &resourceDepsVal,
); err != nil {
return nil, err
}
if name == "" {
return nil, fmt.Errorf("dc_resource: `name` must not be empty")
}
var imageRefAsStr *string
switch imageVal := imageVal.(type) {
case nil: // optional arg, this is fine
case starlark.String:
s := string(imageVal)
imageRefAsStr = &s
default:
return nil, fmt.Errorf("image arg must be a string; got %T", imageVal)
}
svc, err := s.getDCService(name)
if err != nil {
return nil, err
}
svc.TriggerMode = triggerMode
if imageRefAsStr != nil {
normalized, err := container.ParseNamed(*imageRefAsStr)
if err != nil {
return nil, err
}
svc.imageRefFromUser = normalized
}
rds, err := value.SequenceToStringSlice(resourceDepsVal)
if err != nil {
return nil, errors.Wrapf(err, "%s: resource_deps", fn.Name())
}
svc.resourceDeps = rds
return starlark.None, nil
}
func (s *tiltfileState) getDCService(name string) (*dcService, error) {
allNames := make([]string, len(s.dc.services))
for i, svc := range s.dc.services {
if svc.Name == name {
return svc, nil
}
allNames[i] = svc.Name
}
return nil, fmt.Errorf("no Docker Compose service found with name '%s'. "+
"Found these instead:\n\t%s", name, strings.Join(allNames, "; "))
}
// A docker-compose service, according to Tilt.
type dcService struct {
Name string
BuildContext string
DfPath string
// these are the host machine paths that DC will sync from the local volume into the container
// https://docs.docker.com/compose/compose-file/#volumes
MountedLocalDirs []string
// RefSelector of the image associated with this service
// The user-provided image ref overrides the config-provided image ref
imageRefFromConfig reference.Named // from docker-compose.yml `Image` field
imageRefFromUser reference.Named // set via dc_resource
// Currently just use these to diff against when config files are edited to see if manifest has changed
ServiceConfig []byte
DfContents []byte
DependencyIDs []model.TargetID
PublishedPorts []int
TriggerMode triggerMode
resourceDeps []string
}
func (svc dcService) ImageRef() reference.Named {
if svc.imageRefFromUser != nil {
return svc.imageRefFromUser
}
return svc.imageRefFromConfig
}
func DockerComposeConfigToService(c dockercompose.Config, name string) (dcService, error) {
svcConfig, ok := c.Services[name]
if !ok {
return dcService{}, fmt.Errorf("no service %s found in config", name)
}
buildContext := svcConfig.Build.Context
dfPath := svcConfig.Build.Dockerfile
if buildContext != "" {
if dfPath == "" {
// We only expect a Dockerfile if there's a build context specified.
dfPath = "Dockerfile"
}
dfPath = filepath.Join(buildContext, dfPath)
}
var mountedLocalDirs []string
for _, v := range svcConfig.Volumes {
mountedLocalDirs = append(mountedLocalDirs, v.Source)
}
var publishedPorts []int
for _, portSpec := range svcConfig.Ports {
if portSpec.Published != 0 {
publishedPorts = append(publishedPorts, portSpec.Published)
}
}
svc := dcService{
Name: name,
BuildContext: buildContext,
DfPath: dfPath,
MountedLocalDirs: mountedLocalDirs,
ServiceConfig: svcConfig.RawYAML,
PublishedPorts: publishedPorts,
}
if svcConfig.Image != "" {
ref, err := container.ParseNamed(svcConfig.Image)
if err != nil {
// TODO(nick): This doesn't seem like the right place to report this
// error, but we don't really have a better way right now.
return dcService{}, fmt.Errorf("Error parsing image name %q: %v", ref, err)
} else {
svc.imageRefFromConfig = ref
}
}
if dfPath != "" {
dfContents, err := ioutil.ReadFile(dfPath)
if err != nil {
return svc, err
}
svc.DfContents = dfContents
}
return svc, nil
}
func parseDCConfig(ctx context.Context, dcc dockercompose.DockerComposeClient, configPaths []string) ([]*dcService, error) {
config, svcNames, err := dockercompose.ReadConfigAndServiceNames(ctx, dcc, configPaths)
if err != nil {
return nil, err
}
var services []*dcService
for _, name := range svcNames {
svc, err := DockerComposeConfigToService(config, name)
if err != nil {
return nil, errors.Wrapf(err, "getting service %s", name)
}
services = append(services, &svc)
}
return services, nil
}
func (s *tiltfileState) dcServiceToManifest(service *dcService, dcSet dcResourceSet) (manifest model.Manifest,
configFiles []string, err error) {
dcInfo := model.DockerComposeTarget{
ConfigPaths: dcSet.configPaths,
YAMLRaw: service.ServiceConfig,
DfRaw: service.DfContents,
}.WithDependencyIDs(service.DependencyIDs).
WithPublishedPorts(service.PublishedPorts).
WithIgnoredLocalDirectories(service.MountedLocalDirs)
um, err := starlarkTriggerModeToModel(s.triggerModeForResource(service.TriggerMode), true)
if err != nil {
return model.Manifest{}, nil, err
}
var mds []model.ManifestName
for _, md := range service.resourceDeps {
mds = append(mds, model.ManifestName(md))
}
m := model.Manifest{
Name: model.ManifestName(service.Name),
TriggerMode: um,
ResourceDependencies: mds,
}.WithDeployTarget(dcInfo)
if service.DfPath == "" {
// DC service may not have Dockerfile -- e.g. may be just an image that we pull and run.
return m, nil, nil
}
dcInfo = dcInfo.WithBuildPath(service.BuildContext)
paths := []string{filepath.Dir(service.DfPath)}
for _, configPath := range dcSet.configPaths {
paths = append(paths, filepath.Dir(configPath))
}
paths = append(paths, dcInfo.LocalPaths()...)
paths = append(paths, filepath.Dir(dcSet.tiltfilePath))
dcInfo = dcInfo.WithDockerignores(s.dockerignoresFromPathsAndContextFilters(paths, []string{}, []string{}))
localPaths := []string{dcSet.tiltfilePath}
for _, p := range paths {
if !filepath.IsAbs(p) {
return model.Manifest{}, nil, fmt.Errorf("internal error: path not resolved correctly! Please report to https://github.com/windmilleng/tilt/issues : %s", p)
}
localPaths = append(localPaths, p)
}
dcInfo = dcInfo.WithRepos(reposForPaths(localPaths)).
WithTiltFilename(dcSet.tiltfilePath)
m = m.WithDeployTarget(dcInfo)
return m, []string{service.DfPath}, nil
}