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"
|
2017-10-15 06:49:55 +00:00
|
|
|
"github.com/moby/buildkit/session/auth"
|
2017-07-11 17:12:12 +00:00
|
|
|
"github.com/moby/buildkit/session/filesync"
|
|
|
|
"github.com/moby/buildkit/session/grpchijack"
|
|
|
|
"github.com/moby/buildkit/solver/pb"
|
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 {
|
|
|
|
Exporter string
|
|
|
|
ExporterAttrs map[string]string
|
|
|
|
LocalDirs map[string]string
|
|
|
|
SharedKey string
|
2017-08-25 20:08:18 +00:00
|
|
|
Frontend string
|
|
|
|
FrontendAttrs map[string]string
|
2017-10-13 18:54:26 +00:00
|
|
|
ExportCache string
|
|
|
|
ImportCache string
|
2017-07-20 22:13:00 +00:00
|
|
|
// Session string
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (c *Client) Solve(ctx context.Context, def *llb.Definition, opt SolveOpt, statusChan chan *SolveStatus) 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 {
|
|
|
|
return errors.New("invalid empty definition")
|
|
|
|
}
|
|
|
|
if opt.Frontend != "" && def != nil {
|
|
|
|
return 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 {
|
2017-07-11 17:12:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2017-07-20 22:13:00 +00:00
|
|
|
s, err := session.NewSession(defaultSessionName(), opt.SharedKey)
|
2017-07-11 17:12:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create session")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-15 06:49:55 +00:00
|
|
|
s.Allow(auth.NewDockerAuthProvider())
|
|
|
|
|
2017-08-03 22:24:02 +00:00
|
|
|
if opt.Exporter == ExporterLocal {
|
|
|
|
outputDir, ok := opt.ExporterAttrs[exporterLocalOutputDir]
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("output directory is required for local exporter")
|
|
|
|
}
|
|
|
|
s.Allow(filesync.NewFSSyncTarget(outputDir))
|
|
|
|
}
|
|
|
|
|
2017-07-11 17:12:12 +00:00
|
|
|
eg.Go(func() error {
|
|
|
|
return s.Run(ctx, grpchijack.Dialer(c.controlClient()))
|
|
|
|
})
|
2017-06-29 22:31:08 +00:00
|
|
|
|
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()
|
|
|
|
}
|
2017-06-14 00:15:55 +00:00
|
|
|
_, 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")
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return eg.Wait()
|
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
|
|
|
}
|