2017-06-09 01:16:19 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2017-07-11 17:12:12 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-06-29 22:31:08 +00:00
|
|
|
"time"
|
2017-06-09 01:16:19 +00:00
|
|
|
|
2017-06-22 20:15:46 +00:00
|
|
|
controlapi "github.com/moby/buildkit/api/services/control"
|
|
|
|
"github.com/moby/buildkit/client/llb"
|
2017-10-03 23:13:35 +00:00
|
|
|
"github.com/moby/buildkit/identity"
|
2017-07-11 17:12:12 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
|
|
|
"github.com/moby/buildkit/session/filesync"
|
|
|
|
"github.com/moby/buildkit/session/grpchijack"
|
|
|
|
"github.com/moby/buildkit/solver/pb"
|
2018-01-05 23:17:35 +00:00
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
2017-06-09 01:16:19 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-19 01:05:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-06-14 00:15:55 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-06-09 01:16:19 +00:00
|
|
|
)
|
|
|
|
|
2017-07-20 22:13:00 +00:00
|
|
|
type SolveOpt struct {
|
2018-03-27 04:16:32 +00:00
|
|
|
Exporter string
|
|
|
|
ExporterAttrs map[string]string
|
|
|
|
ExporterOutput io.WriteCloser // for ExporterOCI and ExporterDocker
|
|
|
|
ExporterOutputDir string // for ExporterLocal
|
|
|
|
LocalDirs map[string]string
|
|
|
|
SharedKey string
|
|
|
|
Frontend string
|
|
|
|
FrontendAttrs map[string]string
|
|
|
|
ExportCache string
|
|
|
|
ImportCache string
|
2018-04-17 22:55:35 +00:00
|
|
|
Session []session.Attachable
|
2017-07-20 22:13:00 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 04:59:34 +00:00
|
|
|
// Solve calls Solve on the controller.
|
|
|
|
// def must be nil if (and only if) opt.Frontend is set.
|
2018-05-03 00:35:07 +00:00
|
|
|
func (c *Client) Solve(ctx context.Context, def *llb.Definition, opt SolveOpt, statusChan chan *SolveStatus) (*SolveResponse, error) {
|
2017-07-11 17:12:12 +00:00
|
|
|
defer func() {
|
|
|
|
if statusChan != nil {
|
|
|
|
close(statusChan)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-10-02 04:59:34 +00:00
|
|
|
if opt.Frontend == "" && def == nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, errors.New("invalid empty definition")
|
2017-10-02 04:59:34 +00:00
|
|
|
}
|
|
|
|
if opt.Frontend != "" && def != nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, errors.Errorf("invalid definition for frontend %s", opt.Frontend)
|
2017-06-09 01:16:19 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 20:42:53 +00:00
|
|
|
syncedDirs, err := prepareSyncedDirs(def, opt.LocalDirs)
|
2017-07-20 22:13:00 +00:00
|
|
|
if err != nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, err
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 23:13:35 +00:00
|
|
|
ref := identity.NewID()
|
2017-06-14 00:15:55 +00:00
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
2017-06-29 22:31:08 +00:00
|
|
|
statusContext, cancelStatus := context.WithCancel(context.Background())
|
2017-07-11 17:12:12 +00:00
|
|
|
defer cancelStatus()
|
|
|
|
|
2018-01-05 23:17:35 +00:00
|
|
|
if span := opentracing.SpanFromContext(ctx); span != nil {
|
|
|
|
statusContext = opentracing.ContextWithSpan(statusContext, span)
|
|
|
|
}
|
|
|
|
|
2018-01-06 16:54:10 +00:00
|
|
|
s, err := session.NewSession(statusContext, defaultSessionName(), opt.SharedKey)
|
2017-07-11 17:12:12 +00:00
|
|
|
if err != nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to create session")
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 22:13:00 +00:00
|
|
|
if len(syncedDirs) > 0 {
|
|
|
|
s.Allow(filesync.NewFSSyncProvider(syncedDirs))
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 22:55:35 +00:00
|
|
|
for _, a := range opt.Session {
|
|
|
|
s.Allow(a)
|
|
|
|
}
|
2017-10-15 06:49:55 +00:00
|
|
|
|
2017-12-13 00:59:06 +00:00
|
|
|
switch opt.Exporter {
|
|
|
|
case ExporterLocal:
|
2018-03-27 04:16:32 +00:00
|
|
|
if opt.ExporterOutput != nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, errors.New("output file writer is not supported by local exporter")
|
2018-03-27 04:16:32 +00:00
|
|
|
}
|
2018-03-29 05:27:52 +00:00
|
|
|
if opt.ExporterOutputDir == "" {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, errors.New("output directory is required for local exporter")
|
2017-08-03 22:24:02 +00:00
|
|
|
}
|
2018-03-29 05:27:52 +00:00
|
|
|
s.Allow(filesync.NewFSSyncTargetDir(opt.ExporterOutputDir))
|
2017-12-18 00:20:19 +00:00
|
|
|
case ExporterOCI, ExporterDocker:
|
2018-03-27 04:16:32 +00:00
|
|
|
if opt.ExporterOutputDir != "" {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, errors.Errorf("output directory %s is not supported by %s exporter", opt.ExporterOutputDir, opt.Exporter)
|
2018-03-27 04:16:32 +00:00
|
|
|
}
|
2018-03-29 05:27:52 +00:00
|
|
|
if opt.ExporterOutput == nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, errors.Errorf("output file writer is required for %s exporter", opt.Exporter)
|
2018-03-27 04:16:32 +00:00
|
|
|
}
|
2018-03-29 05:27:52 +00:00
|
|
|
s.Allow(filesync.NewFSSyncTarget(opt.ExporterOutput))
|
2018-03-27 04:16:32 +00:00
|
|
|
default:
|
|
|
|
if opt.ExporterOutput != nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, errors.Errorf("output file writer is not supported by %s exporter", opt.Exporter)
|
2018-03-27 04:16:32 +00:00
|
|
|
}
|
|
|
|
if opt.ExporterOutputDir != "" {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, errors.Errorf("output directory %s is not supported by %s exporter", opt.ExporterOutputDir, opt.Exporter)
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
2017-08-03 22:24:02 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 17:12:12 +00:00
|
|
|
eg.Go(func() error {
|
2018-01-06 16:54:10 +00:00
|
|
|
return s.Run(statusContext, grpchijack.Dialer(c.controlClient()))
|
2017-07-11 17:12:12 +00:00
|
|
|
})
|
2017-06-29 22:31:08 +00:00
|
|
|
|
2018-05-03 00:35:07 +00:00
|
|
|
var res *SolveResponse
|
2017-06-14 00:15:55 +00:00
|
|
|
eg.Go(func() error {
|
2017-06-29 22:31:08 +00:00
|
|
|
defer func() { // make sure the Status ends cleanly on build errors
|
|
|
|
go func() {
|
|
|
|
<-time.After(3 * time.Second)
|
|
|
|
cancelStatus()
|
|
|
|
}()
|
2017-07-11 17:12:12 +00:00
|
|
|
logrus.Debugf("stopping session")
|
|
|
|
s.Close()
|
2017-06-29 22:31:08 +00:00
|
|
|
}()
|
2017-10-05 20:42:53 +00:00
|
|
|
var pbd *pb.Definition
|
|
|
|
if def != nil {
|
|
|
|
pbd = def.ToPB()
|
|
|
|
}
|
2018-05-03 00:35:07 +00:00
|
|
|
resp, err := c.controlClient().Solve(ctx, &controlapi.SolveRequest{
|
2017-07-10 20:03:38 +00:00
|
|
|
Ref: ref,
|
2017-10-05 20:42:53 +00:00
|
|
|
Definition: pbd,
|
2017-07-20 22:13:00 +00:00
|
|
|
Exporter: opt.Exporter,
|
|
|
|
ExporterAttrs: opt.ExporterAttrs,
|
2017-07-14 18:05:54 +00:00
|
|
|
Session: s.ID(),
|
2017-08-25 20:08:18 +00:00
|
|
|
Frontend: opt.Frontend,
|
|
|
|
FrontendAttrs: opt.FrontendAttrs,
|
2017-10-13 18:54:26 +00:00
|
|
|
Cache: controlapi.CacheOptions{
|
|
|
|
ExportRef: opt.ExportCache,
|
|
|
|
ImportRef: opt.ImportCache,
|
|
|
|
},
|
2017-06-14 00:15:55 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to solve")
|
|
|
|
}
|
2018-05-03 00:35:07 +00:00
|
|
|
res = &SolveResponse{
|
|
|
|
ExporterResponse: resp.ExporterResponse,
|
|
|
|
}
|
2017-06-14 00:15:55 +00:00
|
|
|
return nil
|
2017-06-09 01:16:19 +00:00
|
|
|
})
|
2017-06-14 00:15:55 +00:00
|
|
|
|
|
|
|
eg.Go(func() error {
|
2017-06-29 22:31:08 +00:00
|
|
|
stream, err := c.controlClient().Status(statusContext, &controlapi.StatusRequest{
|
2017-06-14 00:15:55 +00:00
|
|
|
Ref: ref,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to get status")
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
resp, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.Wrap(err, "failed to receive status")
|
|
|
|
}
|
2017-06-15 23:34:42 +00:00
|
|
|
s := SolveStatus{}
|
|
|
|
for _, v := range resp.Vertexes {
|
|
|
|
s.Vertexes = append(s.Vertexes, &Vertex{
|
|
|
|
Digest: v.Digest,
|
|
|
|
Inputs: v.Inputs,
|
|
|
|
Name: v.Name,
|
|
|
|
Started: v.Started,
|
|
|
|
Completed: v.Completed,
|
2017-06-29 22:31:08 +00:00
|
|
|
Error: v.Error,
|
2017-07-06 20:15:54 +00:00
|
|
|
Cached: v.Cached,
|
2017-06-15 23:34:42 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, v := range resp.Statuses {
|
|
|
|
s.Statuses = append(s.Statuses, &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,
|
|
|
|
})
|
|
|
|
}
|
2017-06-19 05:24:41 +00:00
|
|
|
for _, v := range resp.Logs {
|
|
|
|
s.Logs = append(s.Logs, &VertexLog{
|
|
|
|
Vertex: v.Vertex,
|
|
|
|
Stream: int(v.Stream),
|
|
|
|
Data: v.Msg,
|
|
|
|
Timestamp: v.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
2017-06-15 23:34:42 +00:00
|
|
|
if statusChan != nil {
|
|
|
|
statusChan <- &s
|
|
|
|
}
|
2017-06-14 00:15:55 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-05-03 00:35:07 +00:00
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
2017-06-09 01:16:19 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 20:42:53 +00:00
|
|
|
func prepareSyncedDirs(def *llb.Definition, localDirs map[string]string) ([]filesync.SyncedDir, error) {
|
2017-07-20 22:13:00 +00:00
|
|
|
for _, d := range localDirs {
|
|
|
|
fi, err := os.Stat(d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not find %s", d)
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return nil, errors.Errorf("%s not a directory", d)
|
|
|
|
}
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
2017-07-20 22:13:00 +00:00
|
|
|
dirs := make([]filesync.SyncedDir, 0, len(localDirs))
|
2017-10-05 20:42:53 +00:00
|
|
|
if def == nil {
|
2017-08-25 20:08:18 +00:00
|
|
|
for name, d := range localDirs {
|
|
|
|
dirs = append(dirs, filesync.SyncedDir{Name: name, Dir: d})
|
|
|
|
}
|
2017-10-05 20:42:53 +00:00
|
|
|
} else {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
if src := op.GetSource(); src != nil {
|
|
|
|
if strings.HasPrefix(src.Identifier, "local://") { // TODO: just make a type property
|
|
|
|
name := strings.TrimPrefix(src.Identifier, "local://")
|
|
|
|
d, ok := localDirs[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("local directory %s not enabled", name)
|
|
|
|
}
|
|
|
|
dirs = append(dirs, filesync.SyncedDir{Name: name, Dir: d}) // TODO: excludes
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-20 22:13:00 +00:00
|
|
|
return dirs, nil
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 22:13:00 +00:00
|
|
|
func defaultSessionName() string {
|
|
|
|
wd, err := os.Getwd()
|
2017-07-11 17:12:12 +00:00
|
|
|
if err != nil {
|
2017-07-20 22:13:00 +00:00
|
|
|
return "unknown"
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
2017-07-20 22:13:00 +00:00
|
|
|
return filepath.Base(wd)
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|