-
Notifications
You must be signed in to change notification settings - Fork 303
/
docker.go
418 lines (349 loc) · 9.98 KB
/
docker.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package tiltfile
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/docker/distribution/reference"
"go.starlark.net/starlark"
"github.com/windmilleng/tilt/internal/dockerfile"
"github.com/windmilleng/tilt/internal/model"
"github.com/windmilleng/tilt/internal/ospath"
)
type dockerImage struct {
baseDockerfilePath localPath
baseDockerfile dockerfile.Dockerfile
ref reference.Named
mounts []mount
steps []model.Step
entrypoint string
cachePaths []string
hotReload bool
staticDockerfilePath localPath
staticDockerfile dockerfile.Dockerfile
staticBuildPath localPath
staticBuildArgs model.DockerBuildArgs
// Whether this has been matched up yet to a deploy resource.
matched bool
}
func (s *tiltfileState) dockerBuild(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var dockerRef string
var contextVal, dockerfilePathVal, buildArgs, cacheVal, dockerfileContentsVal starlark.Value
if err := starlark.UnpackArgs(fn.Name(), args, kwargs,
"ref", &dockerRef,
"context", &contextVal,
"build_args?", &buildArgs,
"dockerfile?", &dockerfilePathVal,
"dockerfile_contents?", &dockerfileContentsVal,
"cache?", &cacheVal,
); err != nil {
return nil, err
}
ref, err := reference.ParseNormalizedNamed(dockerRef)
if err != nil {
return nil, fmt.Errorf("Argument 1 (ref): can't parse %q: %v", dockerRef, err)
}
if contextVal == nil {
return nil, fmt.Errorf("Argument 2 (context): empty but is required")
}
context, err := s.localPathFromSkylarkValue(contextVal)
if err != nil {
return nil, err
}
var sba map[string]string
if buildArgs != nil {
d, ok := buildArgs.(*starlark.Dict)
if !ok {
return nil, fmt.Errorf("Argument 3 (build_args): expected dict, got %T", buildArgs)
}
sba, err = skylarkStringDictToGoMap(d)
if err != nil {
return nil, fmt.Errorf("Argument 3 (build_args): %v", err)
}
}
dockerfilePath := context.join("Dockerfile")
var dockerfileContents string
if dockerfileContentsVal != nil && dockerfilePathVal != nil {
return nil, fmt.Errorf("Cannot specify both dockerfile and dockerfile_contents keyword arguments")
}
if dockerfileContentsVal != nil {
switch v := dockerfileContentsVal.(type) {
case *blob:
dockerfileContents = v.text
case starlark.String:
dockerfileContents = v.GoString()
default:
return nil, fmt.Errorf("Argument (dockerfile_contents): must be string or blob.")
}
} else if dockerfilePathVal != nil {
dockerfilePath, err = s.localPathFromSkylarkValue(dockerfilePathVal)
if err != nil {
return nil, err
}
bs, err := s.readFile(dockerfilePath)
if err != nil {
return nil, err
}
dockerfileContents = string(bs)
} else {
bs, err := s.readFile(dockerfilePath)
if err != nil {
return nil, err
}
dockerfileContents = string(bs)
}
cachePaths, err := s.cachePathsFromSkylarkValue(cacheVal)
if err != nil {
return nil, err
}
r := &dockerImage{
staticDockerfilePath: dockerfilePath,
staticDockerfile: dockerfile.Dockerfile(dockerfileContents),
staticBuildPath: context,
ref: ref,
staticBuildArgs: sba,
cachePaths: cachePaths,
}
err = s.buildIndex.addImage(r)
if err != nil {
return nil, err
}
return starlark.None, nil
}
func (s *tiltfileState) fastBuild(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var dockerRef, entrypoint string
var baseDockerfile starlark.Value
var cacheVal starlark.Value
err := starlark.UnpackArgs(fn.Name(), args, kwargs,
"ref", &dockerRef,
"base_dockerfile", &baseDockerfile,
"entrypoint?", &entrypoint,
"cache?", &cacheVal,
)
if err != nil {
return nil, err
}
baseDockerfilePath, err := s.localPathFromSkylarkValue(baseDockerfile)
if err != nil {
return nil, fmt.Errorf("Argument 2 (base_dockerfile): %v", err)
}
ref, err := reference.ParseNormalizedNamed(dockerRef)
if err != nil {
return nil, fmt.Errorf("Parsing %q: %v", dockerRef, err)
}
bs, err := s.readFile(baseDockerfilePath)
if err != nil {
return nil, err
}
df := dockerfile.Dockerfile(bs)
if err = df.ValidateBaseDockerfile(); err != nil {
return nil, err
}
cachePaths, err := s.cachePathsFromSkylarkValue(cacheVal)
if err != nil {
return nil, err
}
r := &dockerImage{
baseDockerfilePath: baseDockerfilePath,
baseDockerfile: df,
ref: ref,
entrypoint: entrypoint,
cachePaths: cachePaths,
}
err = s.buildIndex.addImage(r)
if err != nil {
return nil, err
}
fb := &fastBuild{s: s, img: r}
return fb, nil
}
func (s *tiltfileState) cachePathsFromSkylarkValue(val starlark.Value) ([]string, error) {
if val == nil {
return nil, nil
}
if val, ok := val.(starlark.Sequence); ok {
var result []string
it := val.Iterate()
defer it.Done()
var i starlark.Value
for it.Next(&i) {
str, ok := i.(starlark.String)
if !ok {
return nil, fmt.Errorf("cache param %v is a %T; must be a string", i, i)
}
result = append(result, string(str))
}
return result, nil
}
str, ok := val.(starlark.String)
if !ok {
return nil, fmt.Errorf("cache param %v is a %T; must be a string or a sequence of strings", val, val)
}
return []string{string(str)}, nil
}
type fastBuild struct {
s *tiltfileState
img *dockerImage
}
var _ starlark.Value = &fastBuild{}
func (b *fastBuild) String() string {
return fmt.Sprintf("fast_build(%q)", b.img.ref.Name())
}
func (b *fastBuild) Type() string {
return "fast_build"
}
func (b *fastBuild) Freeze() {}
func (b *fastBuild) Truth() starlark.Bool {
return true
}
func (b *fastBuild) Hash() (uint32, error) {
return 0, fmt.Errorf("unhashable type: fast_build")
}
const (
addN = "add"
runN = "run"
hotReloadN = "hot_reload"
)
func (b *fastBuild) Attr(name string) (starlark.Value, error) {
switch name {
case addN:
return starlark.NewBuiltin(name, b.add), nil
case runN:
return starlark.NewBuiltin(name, b.run), nil
case hotReloadN:
return starlark.NewBuiltin(name, b.hotReload), nil
default:
return starlark.None, nil
}
}
func (b *fastBuild) AttrNames() []string {
return []string{addN, runN}
}
func (b *fastBuild) hotReload(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if err := starlark.UnpackArgs(fn.Name(), args, kwargs); err != nil {
return nil, err
}
b.img.hotReload = true
return b, nil
}
func (b *fastBuild) add(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if len(b.img.steps) > 0 {
return nil, fmt.Errorf("fast_build(%q).add() called after .run(); must add all code before runs", b.img.ref.Name())
}
var src starlark.Value
var mountPoint string
if err := starlark.UnpackArgs(fn.Name(), args, kwargs, "src", &src, "dest", &mountPoint); err != nil {
return nil, err
}
m := mount{}
switch p := src.(type) {
case localPath:
m.src = p
case *gitRepo:
m.src = p.makeLocalPath("")
default:
return nil, fmt.Errorf("fast_build(%q).add(): invalid type for src. Got %s want gitRepo OR localPath", fn.Name(), src.Type())
}
m.mountPoint = mountPoint
b.img.mounts = append(b.img.mounts, m)
return b, nil
}
func (b *fastBuild) run(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var cmd string
var trigger starlark.Value
if err := starlark.UnpackArgs(fn.Name(), args, kwargs, "cmd", &cmd, "trigger?", &trigger); err != nil {
return nil, err
}
var triggers []string
switch trigger := trigger.(type) {
case *starlark.List:
l := trigger.Len()
triggers = make([]string, l)
for i := 0; i < l; i++ {
t := trigger.Index(i)
tStr, isStr := t.(starlark.String)
if !isStr {
return nil, badTypeErr(fn, starlark.String(""), t)
}
triggers[i] = string(tStr)
}
case starlark.String:
triggers = []string{string(trigger)}
}
step := model.ToStep(b.s.absWorkingDir(), model.ToShellCmd(cmd))
step.Triggers = triggers
b.img.steps = append(b.img.steps, step)
return b, nil
}
type mount struct {
src localPath
mountPoint string
}
func (s *tiltfileState) mountsToDomain(image *dockerImage) []model.Mount {
var result []model.Mount
for _, m := range image.mounts {
result = append(result, model.Mount{LocalPath: m.src.path, ContainerPath: m.mountPoint})
}
return result
}
func reposForPaths(paths []localPath) []model.LocalGitRepo {
var result []model.LocalGitRepo
repoSet := map[string]bool{}
for _, path := range paths {
repo := path.repo
if repo == nil || repoSet[repo.basePath] {
continue
}
repoSet[repo.basePath] = true
result = append(result, model.LocalGitRepo{
LocalPath: repo.basePath,
GitignoreContents: repo.gitignoreContents,
})
}
return result
}
func (s *tiltfileState) reposForImage(image *dockerImage) []model.LocalGitRepo {
var paths []localPath
for _, m := range image.mounts {
paths = append(paths, m.src)
}
paths = append(paths,
image.baseDockerfilePath,
image.staticDockerfilePath,
image.staticBuildPath,
s.filename)
return reposForPaths(paths)
}
func dockerignoresForPaths(paths []string) []model.Dockerignore {
var result []model.Dockerignore
dupeSet := map[string]bool{}
for _, path := range paths {
if path == "" || dupeSet[path] {
continue
}
dupeSet[path] = true
if !ospath.IsDir(path) {
continue
}
contents, err := ioutil.ReadFile(filepath.Join(path, ".dockerignore"))
if err != nil {
continue
}
result = append(result, model.Dockerignore{
LocalPath: path,
Contents: string(contents),
})
}
return result
}
func (s *tiltfileState) dockerignoresForImage(image *dockerImage) []model.Dockerignore {
var paths []string
for _, m := range image.mounts {
paths = append(paths, m.src.path)
repo := m.src.repo
if repo != nil {
paths = append(paths, repo.basePath)
}
}
paths = append(paths, image.staticBuildPath.path)
return dockerignoresForPaths(paths)
}