forked from moby/buildkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
206 lines (187 loc) · 4.98 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
package main
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"os"
"strings"
"github.com/containerd/console"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/appcontext"
"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. e.g. /dev/null. Defaults to /tmp/buildctlXXXXXXXXX.",
},
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.StringFlag{
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 ioutil.TempFile("", "buildctl")
}
func build(clicontext *cli.Context) error {
c, err := resolveClient(clicontext)
if err != nil {
return err
}
traceFile, err := openTraceFile(clicontext)
if err != nil {
return err
}
defer traceFile.Close()
traceEnc := json.NewEncoder(traceFile)
logrus.Infof("tracing logs to %s", traceFile.Name())
ch := make(chan *client.SolveStatus)
displayCh := make(chan *client.SolveStatus)
eg, ctx := errgroup.WithContext(appcontext.Context())
exporterAttrs, err := attrMap(clicontext.StringSlice("exporter-opt"))
if err != nil {
return errors.Wrap(err, "invalid exporter-opt")
}
frontendAttrs, err := attrMap(clicontext.StringSlice("frontend-opt"))
if err != nil {
return errors.Wrap(err, "invalid frontend-opt")
}
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 {
return c.Solve(ctx, def, client.SolveOpt{
Exporter: clicontext.String("exporter"),
ExporterAttrs: exporterAttrs,
LocalDirs: localDirs,
Frontend: clicontext.String("frontend"),
FrontendAttrs: frontendAttrs,
ExportCache: clicontext.String("export-cache"),
ImportCache: clicontext.String("import-cache"),
}, ch)
})
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 {
if !clicontext.Bool("no-progress") {
if c, err := console.ConsoleFromFile(os.Stderr); err == nil {
// not using shared context to not disrupt display but let is finish reporting errors
return progressui.DisplaySolveStatus(context.TODO(), c, displayCh)
}
}
for s := range displayCh {
for _, v := range s.Vertexes {
logrus.Debugf("vertex: %s %s %v %v", v.Digest, v.Name, v.Started, v.Completed)
}
for _, s := range s.Statuses {
logrus.Debugf("status: %s %s %d", s.Vertex, s.ID, s.Current)
}
for _, l := range s.Logs {
logrus.Debugf("log: %s\n%s", l.Vertex, l.Data)
}
}
return nil
})
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
}