2018-04-18 19:36:26 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2018-04-19 13:07:27 -04:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2018-04-18 19:36:26 -04:00
|
|
|
"io"
|
2018-05-18 22:56:22 -04:00
|
|
|
"io/ioutil"
|
2018-04-18 19:36:26 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
"github.com/containerd/console"
|
|
|
|
"github.com/docker/cli/cli"
|
2018-04-18 19:36:26 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
2018-05-18 22:56:22 -04:00
|
|
|
"github.com/docker/cli/cli/command/image/build"
|
2018-04-18 19:36:26 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2018-04-19 13:07:27 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2018-04-18 19:36:26 -04:00
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
2018-04-19 13:07:27 -04:00
|
|
|
controlapi "github.com/moby/buildkit/api/services/control"
|
|
|
|
"github.com/moby/buildkit/client"
|
2018-04-18 19:36:26 -04:00
|
|
|
"github.com/moby/buildkit/session/auth/authprovider"
|
|
|
|
"github.com/moby/buildkit/session/filesync"
|
|
|
|
"github.com/moby/buildkit/util/appcontext"
|
2018-04-19 13:07:27 -04:00
|
|
|
"github.com/moby/buildkit/util/progress/progressui"
|
2018-04-18 19:36:26 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/tonistiigi/fsutil"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
2018-05-18 20:52:09 -04:00
|
|
|
const uploadRequestRemote = "upload-request"
|
|
|
|
|
2018-05-23 02:15:11 -04:00
|
|
|
var errDockerfileConflict = errors.New("ambiguous Dockerfile source: both stdin and flag correspond to Dockerfiles")
|
|
|
|
|
2018-06-09 13:25:32 -04:00
|
|
|
//nolint: gocyclo
|
2018-04-18 19:36:26 -04:00
|
|
|
func runBuildBuildKit(dockerCli command.Cli, options buildOptions) error {
|
|
|
|
ctx := appcontext.Context()
|
|
|
|
|
|
|
|
s, err := trySession(dockerCli, options.context)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
return errors.Errorf("buildkit not supported by daemon")
|
|
|
|
}
|
|
|
|
|
2018-05-21 21:44:30 -04:00
|
|
|
var (
|
2018-05-23 02:15:11 -04:00
|
|
|
remote string
|
|
|
|
body io.Reader
|
|
|
|
dockerfileName = options.dockerfileName
|
|
|
|
dockerfileReader io.ReadCloser
|
|
|
|
dockerfileDir string
|
|
|
|
contextDir string
|
2018-05-21 21:44:30 -04:00
|
|
|
)
|
2018-05-23 02:15:11 -04:00
|
|
|
|
2018-04-18 19:36:26 -04:00
|
|
|
switch {
|
|
|
|
case options.contextFromStdin():
|
2018-05-22 19:23:15 -04:00
|
|
|
if options.dockerfileFromStdin() {
|
|
|
|
return errStdinConflict
|
|
|
|
}
|
2018-05-18 22:56:22 -04:00
|
|
|
rc, isArchive, err := build.DetectArchiveReader(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if isArchive {
|
|
|
|
body = rc
|
|
|
|
remote = uploadRequestRemote
|
|
|
|
} else {
|
2018-05-23 02:15:11 -04:00
|
|
|
if options.dockerfileName != "" {
|
|
|
|
return errDockerfileConflict
|
2018-05-18 22:56:22 -04:00
|
|
|
}
|
2018-05-23 02:15:11 -04:00
|
|
|
dockerfileReader = rc
|
2018-05-18 22:56:22 -04:00
|
|
|
remote = clientSessionRemote
|
2018-05-23 02:15:11 -04:00
|
|
|
// TODO: make fssync handle empty contextdir
|
|
|
|
contextDir, _ = ioutil.TempDir("", "empty-dir")
|
|
|
|
defer os.RemoveAll(contextDir)
|
2018-05-18 22:56:22 -04:00
|
|
|
}
|
2018-04-18 19:36:26 -04:00
|
|
|
case isLocalDir(options.context):
|
2018-05-23 02:15:11 -04:00
|
|
|
contextDir = options.context
|
2018-05-21 21:44:30 -04:00
|
|
|
if options.dockerfileFromStdin() {
|
2018-05-23 02:15:11 -04:00
|
|
|
dockerfileReader = os.Stdin
|
|
|
|
} else if options.dockerfileName != "" {
|
|
|
|
dockerfileName = filepath.Base(options.dockerfileName)
|
|
|
|
dockerfileDir = filepath.Dir(options.dockerfileName)
|
|
|
|
} else {
|
|
|
|
dockerfileDir = options.context
|
2018-05-21 21:44:30 -04:00
|
|
|
}
|
2018-05-23 02:15:11 -04:00
|
|
|
remote = clientSessionRemote
|
|
|
|
case urlutil.IsGitURL(options.context):
|
|
|
|
remote = options.context
|
|
|
|
case urlutil.IsURL(options.context):
|
|
|
|
remote = options.context
|
|
|
|
default:
|
|
|
|
return errors.Errorf("unable to prepare context: path %q not found", options.context)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dockerfileReader != nil {
|
|
|
|
dockerfileName = build.DefaultDockerfileName
|
|
|
|
dockerfileDir, err = build.WriteTempDockerfile(dockerfileReader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dockerfileDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dockerfileDir != "" {
|
2018-05-18 22:56:22 -04:00
|
|
|
s.Allow(filesync.NewFSSyncProvider([]filesync.SyncedDir{
|
|
|
|
{
|
|
|
|
Name: "context",
|
2018-05-23 02:15:11 -04:00
|
|
|
Dir: contextDir,
|
2018-05-18 22:56:22 -04:00
|
|
|
Map: resetUIDAndGID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "dockerfile",
|
2018-05-21 21:44:30 -04:00
|
|
|
Dir: dockerfileDir,
|
2018-05-18 22:56:22 -04:00
|
|
|
},
|
|
|
|
}))
|
2018-04-18 19:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
s.Allow(authprovider.NewDockerAuthProvider())
|
|
|
|
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
eg.Go(func() error {
|
2018-04-19 13:07:27 -04:00
|
|
|
return s.Run(context.TODO(), dockerCli.Client().DialSession)
|
2018-04-18 19:36:26 -04:00
|
|
|
})
|
|
|
|
|
2018-06-13 18:35:15 -04:00
|
|
|
buildID := stringid.GenerateRandomID()
|
2018-05-18 20:52:09 -04:00
|
|
|
if body != nil {
|
|
|
|
eg.Go(func() error {
|
|
|
|
buildOptions := types.ImageBuildOptions{
|
|
|
|
Version: types.BuilderBuildKit,
|
|
|
|
BuildID: uploadRequestRemote + ":" + buildID,
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := dockerCli.Client().ImageBuild(context.Background(), body, buildOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-09 13:25:32 -04:00
|
|
|
eg.Go(func() error {
|
2018-04-18 19:36:26 -04:00
|
|
|
defer func() { // make sure the Status ends cleanly on build errors
|
|
|
|
s.Close()
|
|
|
|
}()
|
|
|
|
|
2018-06-09 13:25:32 -04:00
|
|
|
buildOptions := imageBuildOptions(dockerCli, options)
|
|
|
|
buildOptions.Version = types.BuilderBuildKit
|
|
|
|
buildOptions.Dockerfile = dockerfileName
|
|
|
|
//buildOptions.AuthConfigs = authConfigs // handled by session
|
|
|
|
buildOptions.RemoteContext = remote
|
|
|
|
buildOptions.SessionID = s.ID()
|
|
|
|
buildOptions.BuildID = buildID
|
|
|
|
return doBuild(ctx, eg, dockerCli, options, buildOptions)
|
|
|
|
})
|
|
|
|
|
|
|
|
return eg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func doBuild(ctx context.Context, eg *errgroup.Group, dockerCli command.Cli, options buildOptions, buildOptions types.ImageBuildOptions) (finalErr error) {
|
|
|
|
response, err := dockerCli.Client().ImageBuild(context.Background(), nil, buildOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
eg.Go(func() error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return dockerCli.Client().BuildCancel(context.TODO(), buildOptions.BuildID)
|
|
|
|
case <-done:
|
2018-04-18 19:36:26 -04:00
|
|
|
}
|
2018-06-09 13:25:32 -04:00
|
|
|
return nil
|
|
|
|
})
|
2018-04-18 19:36:26 -04:00
|
|
|
|
2018-06-09 13:25:32 -04:00
|
|
|
t := newTracer()
|
|
|
|
ssArr := []*client.SolveStatus{}
|
|
|
|
|
|
|
|
displayStatus := func(displayCh chan *client.SolveStatus) {
|
|
|
|
var c console.Console
|
|
|
|
out := os.Stderr
|
2018-06-13 16:26:16 -04:00
|
|
|
// TODO: Handle interactive output in non-interactive environment.
|
|
|
|
consoleOpt := options.console.Value()
|
|
|
|
if cons, err := console.ConsoleFromFile(out); err == nil && (consoleOpt == nil || *consoleOpt) {
|
2018-06-09 13:25:32 -04:00
|
|
|
c = cons
|
2018-04-18 19:36:26 -04:00
|
|
|
}
|
2018-06-09 13:25:32 -04:00
|
|
|
// not using shared context to not disrupt display but let is finish reporting errors
|
|
|
|
eg.Go(func() error {
|
|
|
|
return progressui.DisplaySolveStatus(context.TODO(), c, out, displayCh)
|
|
|
|
})
|
|
|
|
}
|
2018-04-18 19:36:26 -04:00
|
|
|
|
2018-06-09 13:25:32 -04:00
|
|
|
if options.quiet {
|
2018-04-19 13:07:27 -04:00
|
|
|
eg.Go(func() error {
|
2018-06-09 13:25:32 -04:00
|
|
|
// TODO: make sure t.displayCh closes
|
|
|
|
for ss := range t.displayCh {
|
|
|
|
ssArr = append(ssArr, ss)
|
|
|
|
}
|
|
|
|
<-done
|
|
|
|
// TODO: verify that finalErr is indeed set when error occurs
|
|
|
|
if finalErr != nil {
|
|
|
|
displayCh := make(chan *client.SolveStatus)
|
|
|
|
go func() {
|
|
|
|
for _, ss := range ssArr {
|
|
|
|
displayCh <- ss
|
|
|
|
}
|
|
|
|
close(displayCh)
|
|
|
|
}()
|
|
|
|
displayStatus(displayCh)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2018-06-09 13:25:32 -04:00
|
|
|
} else {
|
|
|
|
displayStatus(t.displayCh)
|
|
|
|
}
|
|
|
|
defer close(t.displayCh)
|
|
|
|
err = jsonmessage.DisplayJSONMessagesStream(response.Body, os.Stdout, dockerCli.Out().FD(), dockerCli.Out().IsTerminal(), t.write)
|
|
|
|
if err != nil {
|
|
|
|
if jerr, ok := err.(*jsonmessage.JSONError); ok {
|
|
|
|
// If no error code is set, default to 1
|
|
|
|
if jerr.Code == 0 {
|
|
|
|
jerr.Code = 1
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2018-06-09 13:25:32 -04:00
|
|
|
return cli.StatusError{Status: jerr.Message, StatusCode: jerr.Code}
|
2018-04-18 19:36:26 -04:00
|
|
|
}
|
2018-06-09 13:25:32 -04:00
|
|
|
}
|
2018-06-13 18:35:15 -04:00
|
|
|
return err
|
2018-04-18 19:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func resetUIDAndGID(s *fsutil.Stat) bool {
|
2018-06-13 18:35:15 -04:00
|
|
|
s.Uid = 0
|
|
|
|
s.Gid = 0
|
2018-04-18 19:36:26 -04:00
|
|
|
return true
|
|
|
|
}
|
2018-04-19 13:07:27 -04:00
|
|
|
|
|
|
|
type tracer struct {
|
|
|
|
displayCh chan *client.SolveStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTracer() *tracer {
|
|
|
|
return &tracer{
|
|
|
|
displayCh: make(chan *client.SolveStatus),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tracer) write(msg jsonmessage.JSONMessage) {
|
|
|
|
var resp controlapi.StatusResponse
|
|
|
|
|
2018-06-09 16:48:13 -04:00
|
|
|
if msg.ID != "moby.buildkit.trace" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
var dt []byte
|
2018-06-13 18:35:15 -04:00
|
|
|
// ignoring all messages that are not understood
|
2018-04-19 13:07:27 -04:00
|
|
|
if err := json.Unmarshal(*msg.Aux, &dt); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := (&resp).Unmarshal(dt); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s := client.SolveStatus{}
|
|
|
|
for _, v := range resp.Vertexes {
|
|
|
|
s.Vertexes = append(s.Vertexes, &client.Vertex{
|
|
|
|
Digest: v.Digest,
|
|
|
|
Inputs: v.Inputs,
|
|
|
|
Name: v.Name,
|
|
|
|
Started: v.Started,
|
|
|
|
Completed: v.Completed,
|
|
|
|
Error: v.Error,
|
|
|
|
Cached: v.Cached,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, v := range resp.Statuses {
|
|
|
|
s.Statuses = append(s.Statuses, &client.VertexStatus{
|
|
|
|
ID: v.ID,
|
|
|
|
Vertex: v.Vertex,
|
|
|
|
Name: v.Name,
|
|
|
|
Total: v.Total,
|
|
|
|
Current: v.Current,
|
|
|
|
Timestamp: v.Timestamp,
|
|
|
|
Started: v.Started,
|
|
|
|
Completed: v.Completed,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, v := range resp.Logs {
|
|
|
|
s.Logs = append(s.Logs, &client.VertexLog{
|
|
|
|
Vertex: v.Vertex,
|
|
|
|
Stream: int(v.Stream),
|
|
|
|
Data: v.Msg,
|
|
|
|
Timestamp: v.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.displayCh <- &s
|
|
|
|
}
|