-
Notifications
You must be signed in to change notification settings - Fork 290
/
docker_compose.go
340 lines (279 loc) · 8.73 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
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
package tiltfile
import (
"context"
"fmt"
"io/ioutil"
"path"
"path/filepath"
"reflect"
"strings"
"github.com/docker/distribution/reference"
"github.com/pkg/errors"
"github.com/windmilleng/tilt/internal/dockercompose"
"github.com/windmilleng/tilt/internal/dockerfile"
"github.com/windmilleng/tilt/internal/model"
"go.starlark.net/starlark"
"gopkg.in/yaml.v2"
)
// dcResourceSet represents a single docker-compose config file and all its associated services
type dcResourceSet struct {
configPath string
services []*dcService
}
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 configPath string
err := starlark.UnpackArgs(fn.Name(), args, kwargs, "configPath", &configPath)
if err != nil {
return nil, err
}
configPath = s.absPath(configPath)
if err != nil {
return nil, err
}
services, err := parseDCConfig(s.ctx, s.dcCli, configPath)
if err != nil {
return nil, err
}
if !s.dc.Empty() {
return starlark.None, fmt.Errorf("already have a docker-compose resource declared (%s), cannot declare another (%s)", s.dc.configPath, configPath)
}
s.dc = dcResourceSet{configPath: configPath, services: services}
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
if err := starlark.UnpackArgs(fn.Name(), args, kwargs,
"name", &name,
"image", &imageVal, // in future this will be optional
); 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:
return nil, fmt.Errorf("must specify an image arg (string or fast_build)")
case starlark.String:
imageRefAsStr = string(imageVal)
case *fastBuild:
imageRefAsStr = imageVal.img.ref.String()
default:
return nil, fmt.Errorf("image arg must be a string or fast_build; got %T", imageVal)
}
svc, err := s.getDCService(name)
if err != nil {
return nil, err
}
normalized, err := reference.ParseNormalizedNamed(imageRefAsStr)
if err != nil {
return nil, err
}
svc.ImageRef = normalized
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, "; "))
}
// Go representations of docker-compose.yml
// (Add fields as we need to support more things)
type dcConfig struct {
Services map[string]dcServiceConfig
}
type dcServiceConfig struct {
RawYAML []byte // We store this to diff against when docker-compose.yml is edited to see if the manifest has changed
Build dcBuildConfig `yaml:"build"`
Volumes Volumes
}
type Volumes struct {
Volumes []Volume
}
type Volume struct {
Source string
}
func (v *Volumes) UnmarshalYAML(unmarshal func(interface{}) error) error {
var sliceType []interface{}
err := unmarshal(&sliceType)
if err != nil {
return errors.Wrap(err, "unmarshalling volumes")
}
for _, volume := range sliceType {
// Volumes syntax documented here: https://docs.docker.com/compose/compose-file/#volumes
// This implementation far from comprehensive. It will silently ignore:
// 1. "short" syntax using volume keys instead of paths
// 2. all "long" syntax volumes
// Ideally, we'd let the user know we didn't handle their case, but getting a ctx here is not easy
switch a := volume.(type) {
case string:
parts := strings.Split(a, ":")
v.Volumes = append(v.Volumes, Volume{Source: parts[0]})
}
}
return nil
}
// We use a custom Unmarshal method here so that we can store the RawYAML in addition
// to unmarshaling the fields we care about into structs.
func (c *dcConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
aux := struct {
Services map[string]interface{} `yaml:"services"`
}{}
err := unmarshal(&aux)
if err != nil {
return err
}
if c.Services == nil {
c.Services = make(map[string]dcServiceConfig)
}
for k, v := range aux.Services {
b, err := yaml.Marshal(v) // so we can unmarshal it again
if err != nil {
return err
}
svcConf := &dcServiceConfig{}
err = yaml.Unmarshal(b, svcConf)
if err != nil {
return err
}
svcConf.RawYAML = b
c.Services[k] = *svcConf
}
return nil
}
type dcBuildConfig struct {
Context string `yaml:"context"`
Dockerfile string `yaml:"dockerfile"`
}
// A docker-compose service, according to Tilt.
type dcService struct {
Name string
Context string
DfPath string
// these are the host machine paths that DC will mount from the local volume into the container
// https://docs.docker.com/compose/compose-file/#volumes
MountedLocalDirs []string
// Ref of an image described via docker_build || fast_build call
// (explicitly linked to this service via dc_service call)
ImageRef reference.Named
// Currently just use these to diff against when config files are edited to see if manifest has changed
ServiceConfig []byte
DfContents []byte
}
func (c dcConfig) GetService(name string) (dcService, error) {
svcConfig, ok := c.Services[name]
if !ok {
return dcService{}, fmt.Errorf("no service %s found in config", name)
}
df := svcConfig.Build.Dockerfile
if df == "" && svcConfig.Build.Context != "" {
// We only expect a Dockerfile if there's a build context specified.
df = "Dockerfile"
}
var mountedLocalDirs []string
for _, v := range svcConfig.Volumes.Volumes {
mountedLocalDirs = append(mountedLocalDirs, v.Source)
}
dfPath := filepath.Join(svcConfig.Build.Context, df)
svc := dcService{
Name: name,
Context: svcConfig.Build.Context,
DfPath: dfPath,
MountedLocalDirs: mountedLocalDirs,
ServiceConfig: svcConfig.RawYAML,
}
if dfPath != "" {
dfContents, err := ioutil.ReadFile(dfPath)
if err != nil {
return svc, err
}
svc.DfContents = dfContents
}
return svc, nil
}
func svcNames(ctx context.Context, dcc dockercompose.DockerComposeClient, configPath string) ([]string, error) {
servicesText, err := dcc.Services(ctx, configPath)
if err != nil {
return nil, err
}
serviceNames := strings.Split(servicesText, "\n")
var result []string
for _, name := range serviceNames {
if name == "" {
continue
}
result = append(result, name)
}
return result, nil
}
func parseDCConfig(ctx context.Context, dcc dockercompose.DockerComposeClient, configPath string) ([]*dcService, error) {
configOut, err := dcc.Config(ctx, configPath)
if err != nil {
return nil, err
}
config := dcConfig{}
err = yaml.Unmarshal([]byte(configOut), &config)
if err != nil {
return nil, err
}
svcNames, err := svcNames(ctx, dcc, configPath)
if err != nil {
return nil, err
}
var services []*dcService
for _, name := range svcNames {
svc, err := config.GetService(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, dcConfigPath string) (manifest model.Manifest,
configFiles []string, err error) {
dcInfo := model.DockerComposeTarget{
ConfigPath: dcConfigPath,
YAMLRaw: service.ServiceConfig,
DfRaw: service.DfContents,
}
m := model.Manifest{
Name: model.ManifestName(service.Name),
}.WithDeployTarget(dcInfo)
if service.DfPath == "" {
// DC service may not have Dockerfile -- e.g. may be just an image that we pull and run.
// So, don't parse a non-existent Dockerfile for mount info.
return m, nil, nil
}
df := dockerfile.Dockerfile(service.DfContents)
mounts, err := df.DeriveMounts(service.Context)
if err != nil {
return model.Manifest{}, nil, err
}
dcInfo.Mounts = mounts
paths := []string{path.Dir(service.DfPath), path.Dir(dcConfigPath)}
for _, mount := range mounts {
paths = append(paths, mount.LocalPath)
}
dcInfo = dcInfo.WithDockerignores(dockerignoresForPaths(append(paths, path.Dir(s.filename.path))))
localPaths := []localPath{s.filename}
for _, p := range paths {
localPaths = append(localPaths, s.localPathFromString(p))
}
dcInfo = dcInfo.WithRepos(reposForPaths(localPaths)).
WithTiltFilename(s.filename.path).
WithIgnoredLocalDirectories(service.MountedLocalDirs)
m = m.WithDeployTarget(dcInfo)
return m, []string{service.DfPath}, nil
}