forked from moby/buildkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
263 lines (243 loc) · 6.92 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
package main
import (
"context"
"encoding/json"
"io"
"os"
"strings"
"github.com/containerd/console"
"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/solver/pb"
"github.com/moby/buildkit/util/progress/progressui"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"golang.org/x/sync/errgroup"
)
var buildCommand = cli.Command{
Name: "build",
Usage: "build",
Action: build,
Flags: []cli.Flag{
cli.StringFlag{
Name: "exporter",
Usage: "Define exporter for build result",
},
cli.StringSliceFlag{
Name: "exporter-opt",
Usage: "Define custom options for exporter",
},
cli.BoolFlag{
Name: "no-progress",
Usage: "Don't show interactive progress",
},
cli.StringFlag{
Name: "trace",
Usage: "Path to trace file. Defaults to no tracing.",
},
cli.StringSliceFlag{
Name: "local",
Usage: "Allow build access to the local directory",
},
cli.StringFlag{
Name: "frontend",
Usage: "Define frontend used for build",
},
cli.StringSliceFlag{
Name: "frontend-opt",
Usage: "Define custom options for frontend",
},
cli.BoolFlag{
Name: "no-cache",
Usage: "Disable cache for all the vertices. Frontend is not supported.",
},
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",
},
},
}
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")
}
if clicontext.Bool("no-cache") {
for _, dt := range def.Def {
var op pb.Op
if err := (&op).Unmarshal(dt); err != nil {
return nil, errors.Wrap(err, "failed to parse llb proto op")
}
dgst := digest.FromBytes(dt)
opMetadata, ok := def.Metadata[dgst]
if !ok {
opMetadata = llb.OpMetadata{}
}
llb.IgnoreCache(&opMetadata)
def.Metadata[dgst] = opMetadata
}
}
return def, nil
}
func openTraceFile(clicontext *cli.Context) (*os.File, error) {
if traceFileName := clicontext.String("trace"); traceFileName != "" {
return os.OpenFile(traceFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
}
return nil, nil
}
func build(clicontext *cli.Context) error {
c, err := resolveClient(clicontext)
if err != nil {
return err
}
traceFile, err := openTraceFile(clicontext)
if err != nil {
return err
}
var traceEnc *json.Encoder
if traceFile != nil {
defer traceFile.Close()
traceEnc = json.NewEncoder(traceFile)
logrus.Infof("tracing logs to %s", traceFile.Name())
}
ch := make(chan *client.SolveStatus)
eg, ctx := errgroup.WithContext(commandContext(clicontext))
solveOpt := client.SolveOpt{
Exporter: clicontext.String("exporter"),
// ExporterAttrs is set later
// LocalDirs is set later
Frontend: clicontext.String("frontend"),
// FrontendAttrs is set later
ExportCache: clicontext.String("export-cache"),
ImportCache: clicontext.StringSlice("import-cache"),
Session: []session.Attachable{authprovider.NewDockerAuthProvider()},
}
solveOpt.ExporterAttrs, err = attrMap(clicontext.StringSlice("exporter-opt"))
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")
}
solveOpt.FrontendAttrs, err = attrMap(clicontext.StringSlice("frontend-opt"))
if err != nil {
return errors.Wrap(err, "invalid frontend-opt")
}
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(clicontext.StringSlice("local"))
if err != nil {
return errors.Wrap(err, "invalid local")
}
var def *llb.Definition
if clicontext.String("frontend") == "" {
def, err = read(os.Stdin, clicontext)
if err != nil {
return err
}
} else {
if clicontext.Bool("no-cache") {
return errors.New("no-cache is not supported for frontends")
}
}
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
if traceEnc != nil {
displayCh = make(chan *client.SolveStatus)
eg.Go(func() error {
defer close(displayCh)
for s := range ch {
if err := traceEnc.Encode(s); err != nil {
logrus.Error(err)
}
displayCh <- s
}
return nil
})
}
eg.Go(func() error {
var c console.Console
if !clicontext.Bool("no-progress") {
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
}
}