forked from crosbymichael/boss
/
build.go
360 lines (322 loc) · 9.09 KB
/
build.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
// stole from buildkit, tonis said it was ok
package main
import (
"context"
"fmt"
"io"
"os"
"strings"
"sync"
"text/tabwriter"
"time"
"github.com/containerd/console"
"github.com/containerd/containerd"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/cmd/ctr/commands/content"
"github.com/containerd/containerd/defaults"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/pkg/progress"
"github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth/authprovider"
"github.com/moby/buildkit/util/appdefaults"
"github.com/moby/buildkit/util/progress/progressui"
"github.com/opencontainers/image-spec/specs-go/v1"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"golang.org/x/sync/errgroup"
)
// sudo buildctl build --frontend=dockerfile.v0 --local context=. --local dockerfile=. --exporter=image --exporter-opt name=registry2
var buildCommand = cli.Command{
Name: "build",
Usage: "build",
Flags: []cli.Flag{
cli.StringFlag{
Name: "name",
Usage: "Name of the image to create",
},
cli.StringFlag{
Name: "export-cache",
Usage: "Reference to export build cache to",
},
cli.StringSliceFlag{
Name: "export-cache-opt",
Usage: "Define custom options for cache exporting",
},
cli.StringSliceFlag{
Name: "import-cache",
Usage: "Reference to import build cache from",
},
cli.BoolFlag{
Name: "push",
Usage: "push the resulting image",
},
},
Action: func(clix *cli.Context) error {
if err := build(clix); err != nil {
return err
}
if !clix.Bool("push") {
return nil
}
ref := clix.String("name")
ctx := namespaces.WithNamespace(context.Background(), clix.GlobalString("namespace"))
client, err := containerd.New(
defaults.DefaultAddress,
containerd.WithDefaultRuntime("io.containerd.runc.v1"),
)
if err != nil {
return err
}
defer client.Close()
return push(ctx, client, ref, clix)
},
}
func push(ctx context.Context, client *containerd.Client, ref string, clix *cli.Context) error {
var (
local = ref
desc v1.Descriptor
)
if ref == "" {
return errors.New("please provide a remote image reference to push")
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
img, err := client.ImageService().Get(ctx, local)
if err != nil {
return errors.Wrap(err, "unable to resolve image to manifest")
}
desc = img.Target
resolver, err := commands.GetResolver(ctx, clix)
if err != nil {
return err
}
ongoing := newPushJobs(commands.PushTracker)
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
log.G(ctx).WithField("image", ref).WithField("digest", desc.Digest).Debug("pushing")
jobHandler := images.HandlerFunc(func(ctx context.Context, desc v1.Descriptor) ([]v1.Descriptor, error) {
ongoing.add(remotes.MakeRefKey(ctx, desc))
return nil, nil
})
return client.Push(ctx, ref, desc,
containerd.WithResolver(resolver),
containerd.WithImageHandler(jobHandler),
)
})
errs := make(chan error)
go func() {
defer close(errs)
errs <- eg.Wait()
}()
var (
ticker = time.NewTicker(100 * time.Millisecond)
fw = progress.NewWriter(os.Stdout)
start = time.Now()
done bool
)
defer ticker.Stop()
for {
select {
case <-ticker.C:
fw.Flush()
tw := tabwriter.NewWriter(fw, 1, 8, 1, ' ', 0)
content.Display(tw, ongoing.status(), start)
tw.Flush()
if done {
fw.Flush()
return nil
}
case err := <-errs:
if err != nil {
return err
}
done = true
case <-ctx.Done():
done = true // allow ui to update once more
}
}
}
func read(r io.Reader, clicontext *cli.Context) (*llb.Definition, error) {
def, err := llb.ReadFrom(r)
if err != nil {
return nil, errors.Wrap(err, "failed to parse input")
}
return def, nil
}
func build(clicontext *cli.Context) error {
c, err := resolveClient(clicontext)
if err != nil {
return err
}
ch := make(chan *client.SolveStatus)
eg, ctx := errgroup.WithContext(commandContext(clicontext))
solveOpt := client.SolveOpt{
Exporter: "image",
// ExporterAttrs is set later
// LocalDirs is set later
Frontend: "dockerfile.v0",
// FrontendAttrs is set later
ExportCache: clicontext.String("export-cache"),
ImportCache: clicontext.StringSlice("import-cache"),
Session: []session.Attachable{authprovider.NewDockerAuthProvider()},
}
solveOpt.ExporterAttrs, err = attrMap(fmt.Sprintf("name=%s", clicontext.String("name")))
if err != nil {
return errors.Wrap(err, "invalid exporter-opt")
}
solveOpt.ExporterOutput, solveOpt.ExporterOutputDir, err = resolveExporterOutput(solveOpt.Exporter, solveOpt.ExporterAttrs["output"])
if err != nil {
return errors.Wrap(err, "invalid exporter-opt: output")
}
if solveOpt.ExporterOutput != nil || solveOpt.ExporterOutputDir != "" {
delete(solveOpt.ExporterAttrs, "output")
}
exportCacheAttrs, err := attrMap(clicontext.StringSlice("export-cache-opt")...)
if err != nil {
return errors.Wrap(err, "invalid export-cache-opt")
}
if len(exportCacheAttrs) == 0 {
exportCacheAttrs = map[string]string{"mode": "min"}
}
solveOpt.ExportCacheAttrs = exportCacheAttrs
solveOpt.LocalDirs, err = attrMap("context=.", "dockerfile=.")
if err != nil {
return errors.Wrap(err, "invalid local")
}
var def *llb.Definition
eg.Go(func() error {
resp, err := c.Solve(ctx, def, solveOpt, ch)
if err != nil {
return err
}
for k, v := range resp.ExporterResponse {
logrus.Debugf("solve response: %s=%s", k, v)
}
return err
})
displayCh := ch
eg.Go(func() error {
var c console.Console
if cf, err := console.ConsoleFromFile(os.Stderr); err == nil {
c = cf
}
// not using shared context to not disrupt display but let is finish reporting errors
return progressui.DisplaySolveStatus(context.TODO(), c, os.Stdout, displayCh)
})
return eg.Wait()
}
func attrMap(sl ...string) (map[string]string, error) {
m := map[string]string{}
for _, v := range sl {
parts := strings.SplitN(v, "=", 2)
if len(parts) != 2 {
return nil, errors.Errorf("invalid value %s", v)
}
m[parts[0]] = parts[1]
}
return m, nil
}
// resolveExporterOutput returns at most either one of io.WriteCloser (single file) or a string (directory path).
func resolveExporterOutput(exporter, output string) (io.WriteCloser, string, error) {
switch exporter {
case client.ExporterLocal:
if output == "" {
return nil, "", errors.New("output directory is required for local exporter")
}
return nil, output, nil
case client.ExporterOCI, client.ExporterDocker:
if output != "" {
fi, err := os.Stat(output)
if err != nil && !os.IsNotExist(err) {
return nil, "", errors.Wrapf(err, "invalid destination file: %s", output)
}
if err == nil && fi.IsDir() {
return nil, "", errors.Errorf("destination file is a directory")
}
w, err := os.Create(output)
return w, "", err
}
// if no output file is specified, use stdout
if _, err := console.ConsoleFromFile(os.Stdout); err == nil {
return nil, "", errors.Errorf("output file is required for %s exporter. refusing to write to console", exporter)
}
return os.Stdout, "", nil
default: // e.g. client.ExporterImage
if output != "" {
return nil, "", errors.Errorf("output %s is not supported by %s exporter", output, exporter)
}
return nil, "", nil
}
}
func commandContext(c *cli.Context) context.Context {
return context.Background()
}
func resolveClient(c *cli.Context) (*client.Client, error) {
opts := []client.ClientOpt{client.WithBlock()}
ctx := commandContext(c)
if span := opentracing.SpanFromContext(ctx); span != nil {
opts = append(opts, client.WithTracer(span.Tracer()))
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return client.New(ctx, appdefaults.Address, opts...)
}
type pushjobs struct {
jobs map[string]struct{}
ordered []string
tracker docker.StatusTracker
mu sync.Mutex
}
func newPushJobs(tracker docker.StatusTracker) *pushjobs {
return &pushjobs{
jobs: make(map[string]struct{}),
tracker: tracker,
}
}
func (j *pushjobs) add(ref string) {
j.mu.Lock()
defer j.mu.Unlock()
if _, ok := j.jobs[ref]; ok {
return
}
j.ordered = append(j.ordered, ref)
j.jobs[ref] = struct{}{}
}
func (j *pushjobs) status() []content.StatusInfo {
j.mu.Lock()
defer j.mu.Unlock()
statuses := make([]content.StatusInfo, 0, len(j.jobs))
for _, name := range j.ordered {
si := content.StatusInfo{
Ref: name,
}
status, err := j.tracker.GetStatus(name)
if err != nil {
si.Status = "waiting"
} else {
si.Offset = status.Offset
si.Total = status.Total
si.StartedAt = status.StartedAt
si.UpdatedAt = status.UpdatedAt
if status.Offset >= status.Total {
if status.UploadUUID == "" {
si.Status = "done"
} else {
si.Status = "committing"
}
} else {
si.Status = "uploading"
}
}
statuses = append(statuses, si)
}
return statuses
}