2021-05-26 01:30:49 +02:00
|
|
|
package client
|
2020-12-30 03:45:16 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-17 05:13:51 +01:00
|
|
|
"fmt"
|
2020-12-30 03:45:16 +01:00
|
|
|
"os"
|
2021-01-28 23:58:13 +01:00
|
|
|
"path/filepath"
|
2020-12-30 03:45:16 +01:00
|
|
|
"strings"
|
|
|
|
|
2021-07-02 14:26:35 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
2020-12-30 03:45:16 +01:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2021-01-14 02:38:16 +01:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2020-12-30 03:45:16 +01:00
|
|
|
// Cue
|
|
|
|
|
|
|
|
// buildkit
|
|
|
|
bk "github.com/moby/buildkit/client"
|
2021-01-08 17:16:00 +01:00
|
|
|
_ "github.com/moby/buildkit/client/connhelper/dockercontainer" // import the container connection driver
|
2021-04-09 03:09:10 +02:00
|
|
|
"github.com/moby/buildkit/client/llb"
|
2021-02-02 20:03:12 +01:00
|
|
|
bkgw "github.com/moby/buildkit/frontend/gateway/client"
|
2021-04-27 03:00:03 +02:00
|
|
|
"github.com/moby/buildkit/session"
|
2020-12-30 03:45:16 +01:00
|
|
|
|
|
|
|
// docker output
|
2021-05-26 01:53:26 +02:00
|
|
|
"go.dagger.io/dagger/util/buildkitd"
|
|
|
|
"go.dagger.io/dagger/util/progressui"
|
2021-02-07 08:36:21 +01:00
|
|
|
|
2021-05-26 01:53:26 +02:00
|
|
|
"go.dagger.io/dagger/compiler"
|
|
|
|
"go.dagger.io/dagger/environment"
|
|
|
|
"go.dagger.io/dagger/solver"
|
|
|
|
"go.dagger.io/dagger/state"
|
2020-12-30 03:45:16 +01:00
|
|
|
)
|
|
|
|
|
2021-01-28 23:58:13 +01:00
|
|
|
// A dagger client
|
2020-12-30 03:45:16 +01:00
|
|
|
type Client struct {
|
2021-04-15 01:21:18 +02:00
|
|
|
c *bk.Client
|
|
|
|
noCache bool
|
2020-12-30 03:45:16 +01:00
|
|
|
}
|
|
|
|
|
2021-05-26 01:30:49 +02:00
|
|
|
func New(ctx context.Context, host string, noCache bool) (*Client, error) {
|
2021-01-30 02:16:22 +01:00
|
|
|
if host == "" {
|
|
|
|
host = os.Getenv("BUILDKIT_HOST")
|
|
|
|
}
|
|
|
|
if host == "" {
|
2021-03-03 01:14:53 +01:00
|
|
|
h, err := buildkitd.Start(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
host = h
|
2021-01-30 02:16:22 +01:00
|
|
|
}
|
2021-03-04 03:09:25 +01:00
|
|
|
opts := []bk.ClientOpt{}
|
2021-07-02 14:26:35 +02:00
|
|
|
|
|
|
|
// FIXME: uncomment when next version of buildkit will be released
|
|
|
|
// if span := trace.SpanFromContext(ctx); span != nil {
|
|
|
|
// opts = append(opts, bk.WithTracerProvider(span.TracerProvider()))
|
|
|
|
// }
|
|
|
|
|
2021-03-04 03:09:25 +01:00
|
|
|
c, err := bk.New(ctx, host, opts...)
|
2021-01-30 02:16:22 +01:00
|
|
|
if err != nil {
|
2021-02-17 05:13:51 +01:00
|
|
|
return nil, fmt.Errorf("buildkit client: %w", err)
|
2021-01-30 02:16:22 +01:00
|
|
|
}
|
|
|
|
return &Client{
|
2021-04-15 01:21:18 +02:00
|
|
|
c: c,
|
|
|
|
noCache: noCache,
|
2021-01-30 02:16:22 +01:00
|
|
|
}, nil
|
2021-01-15 00:19:39 +01:00
|
|
|
}
|
|
|
|
|
2021-05-26 01:53:26 +02:00
|
|
|
type DoFunc func(context.Context, *environment.Environment, solver.Solver) error
|
2021-04-01 01:32:15 +02:00
|
|
|
|
2021-03-27 00:44:13 +01:00
|
|
|
// FIXME: return completed *Route, instead of *compiler.Value
|
2021-05-26 01:53:26 +02:00
|
|
|
func (c *Client) Do(ctx context.Context, state *state.State, fn DoFunc) (*environment.Environment, error) {
|
2021-01-28 23:58:13 +01:00
|
|
|
lg := log.Ctx(ctx)
|
2021-02-02 20:32:35 +01:00
|
|
|
eg, gctx := errgroup.WithContext(ctx)
|
2021-01-11 20:51:15 +01:00
|
|
|
|
2021-05-26 01:30:49 +02:00
|
|
|
environment, err := environment.New(state)
|
2021-04-02 23:45:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-02 20:10:47 +01:00
|
|
|
// Spawn print function
|
2021-02-24 00:39:06 +01:00
|
|
|
events := make(chan *bk.SolveStatus)
|
|
|
|
eg.Go(func() error {
|
|
|
|
// Create a background context so that logging will not be cancelled
|
|
|
|
// with the main context.
|
|
|
|
dispCtx := lg.WithContext(context.Background())
|
2021-05-29 11:18:01 +02:00
|
|
|
return c.logSolveStatus(dispCtx, state, events)
|
2021-02-24 00:39:06 +01:00
|
|
|
})
|
2021-01-11 20:51:15 +01:00
|
|
|
|
2021-02-02 20:32:35 +01:00
|
|
|
// Spawn build function
|
|
|
|
eg.Go(func() error {
|
2021-05-26 03:56:16 +02:00
|
|
|
return c.buildfn(gctx, state, environment, fn, events)
|
2021-02-02 20:32:35 +01:00
|
|
|
})
|
|
|
|
|
2021-04-27 20:59:04 +02:00
|
|
|
return environment, eg.Wait()
|
2020-12-30 03:45:16 +01:00
|
|
|
}
|
|
|
|
|
2021-05-26 03:56:16 +02:00
|
|
|
func (c *Client) buildfn(ctx context.Context, st *state.State, env *environment.Environment, fn DoFunc, ch chan *bk.SolveStatus) error {
|
2021-01-14 02:38:16 +01:00
|
|
|
lg := log.Ctx(ctx)
|
|
|
|
|
2021-02-02 20:03:12 +01:00
|
|
|
// Scan local dirs to grant access
|
2021-05-26 01:30:49 +02:00
|
|
|
localdirs := env.LocalDirs()
|
2021-02-02 20:03:12 +01:00
|
|
|
for label, dir := range localdirs {
|
|
|
|
abs, err := filepath.Abs(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
localdirs[label] = abs
|
2021-01-30 02:16:22 +01:00
|
|
|
}
|
2021-02-02 20:03:12 +01:00
|
|
|
|
2021-04-27 03:00:03 +02:00
|
|
|
// buildkit auth provider (registry)
|
2021-05-26 01:30:49 +02:00
|
|
|
auth := solver.NewRegistryAuthProvider()
|
2021-04-27 03:00:03 +02:00
|
|
|
|
2021-05-26 03:56:16 +02:00
|
|
|
// secrets
|
|
|
|
secrets := solver.NewSecretsProvider(st)
|
|
|
|
|
2021-01-11 20:51:15 +01:00
|
|
|
// Setup solve options
|
|
|
|
opts := bk.SolveOpt{
|
2021-02-02 20:03:12 +01:00
|
|
|
LocalDirs: localdirs,
|
2021-06-05 00:56:59 +02:00
|
|
|
Session: []session.Attachable{
|
|
|
|
auth,
|
|
|
|
secrets,
|
|
|
|
solver.NewDockerSocketProvider(),
|
|
|
|
},
|
2021-01-11 20:51:15 +01:00
|
|
|
}
|
2021-02-02 20:03:12 +01:00
|
|
|
|
2021-01-11 20:51:15 +01:00
|
|
|
// Call buildkit solver
|
2021-01-22 01:39:29 +01:00
|
|
|
lg.Debug().
|
|
|
|
Interface("localdirs", opts.LocalDirs).
|
|
|
|
Interface("attrs", opts.FrontendAttrs).
|
|
|
|
Msg("spawning buildkit job")
|
2021-02-02 20:03:12 +01:00
|
|
|
|
2021-03-12 01:41:19 +01:00
|
|
|
resp, err := c.c.Build(ctx, opts, "", func(ctx context.Context, gw bkgw.Client) (*bkgw.Result, error) {
|
2021-05-26 01:30:49 +02:00
|
|
|
s := solver.New(solver.Opts{
|
|
|
|
Control: c.c,
|
|
|
|
Gateway: gw,
|
|
|
|
Events: ch,
|
|
|
|
Auth: auth,
|
2021-05-26 03:56:16 +02:00
|
|
|
Secrets: secrets,
|
2021-05-26 01:30:49 +02:00
|
|
|
NoCache: c.noCache,
|
|
|
|
})
|
2021-02-02 20:03:12 +01:00
|
|
|
|
2021-02-24 01:37:45 +01:00
|
|
|
lg.Debug().Msg("loading configuration")
|
2021-05-26 01:30:49 +02:00
|
|
|
if err := env.LoadPlan(ctx, s); err != nil {
|
2021-02-02 20:03:12 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-24 01:37:45 +01:00
|
|
|
|
2021-02-02 20:03:12 +01:00
|
|
|
// Compute output overlay
|
2021-04-01 01:32:15 +02:00
|
|
|
if fn != nil {
|
2021-05-26 01:30:49 +02:00
|
|
|
if err := fn(ctx, env, s); err != nil {
|
2021-04-01 01:32:15 +02:00
|
|
|
return nil, compiler.Err(err)
|
|
|
|
}
|
2021-02-02 20:03:12 +01:00
|
|
|
}
|
2021-02-24 01:37:45 +01:00
|
|
|
|
2021-04-27 20:59:04 +02:00
|
|
|
// Export environment to a cue directory
|
2021-03-12 22:00:11 +01:00
|
|
|
// FIXME: this should be elsewhere
|
2021-04-27 20:59:04 +02:00
|
|
|
lg.Debug().Msg("exporting environment")
|
2021-07-02 14:26:35 +02:00
|
|
|
|
|
|
|
tr := otel.Tracer("client")
|
|
|
|
_, span := tr.Start(ctx, "environment.Export")
|
|
|
|
defer span.End()
|
2021-03-12 22:00:11 +01:00
|
|
|
|
2021-05-26 01:30:49 +02:00
|
|
|
computed := env.Computed().JSON().PrettyString()
|
2021-04-09 03:09:10 +02:00
|
|
|
st := llb.
|
|
|
|
Scratch().
|
|
|
|
File(
|
|
|
|
llb.Mkfile("computed.json", 0600, []byte(computed)),
|
|
|
|
llb.WithCustomName("[internal] serializing computed values"),
|
|
|
|
)
|
2021-04-01 01:32:15 +02:00
|
|
|
|
2021-03-12 22:00:11 +01:00
|
|
|
ref, err := s.Solve(ctx, st)
|
2021-02-02 20:03:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-12 22:00:11 +01:00
|
|
|
res := bkgw.NewResult()
|
|
|
|
res.SetRef(ref)
|
|
|
|
return res, nil
|
2021-02-02 20:03:12 +01:00
|
|
|
}, ch)
|
2021-01-11 20:51:15 +01:00
|
|
|
if err != nil {
|
2021-05-26 03:56:16 +02:00
|
|
|
return solver.CleanError(err)
|
2021-01-11 20:51:15 +01:00
|
|
|
}
|
|
|
|
for k, v := range resp.ExporterResponse {
|
|
|
|
// FIXME consume exporter response
|
2021-01-14 02:38:16 +01:00
|
|
|
lg.
|
|
|
|
Debug().
|
|
|
|
Str("key", k).
|
|
|
|
Str("value", v).
|
|
|
|
Msg("exporter response")
|
2021-01-11 20:51:15 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-29 11:18:01 +02:00
|
|
|
func (c *Client) logSolveStatus(ctx context.Context, st *state.State, ch chan *bk.SolveStatus) error {
|
2021-02-24 00:39:06 +01:00
|
|
|
parseName := func(v *bk.Vertex) (string, string) {
|
|
|
|
// Pattern: `@name@ message`. Minimal length is len("@X@ ")
|
|
|
|
if len(v.Name) < 2 || !strings.HasPrefix(v.Name, "@") {
|
|
|
|
return "", v.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixEndPos := strings.Index(v.Name[1:], "@")
|
|
|
|
if prefixEndPos == -1 {
|
|
|
|
return "", v.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
component := v.Name[1 : prefixEndPos+1]
|
|
|
|
return component, v.Name[prefixEndPos+3 : len(v.Name)]
|
|
|
|
}
|
|
|
|
|
2021-05-29 11:18:01 +02:00
|
|
|
// Just like sprintf, but redacts secrets automatically
|
|
|
|
secureSprintf := func(format string, a ...interface{}) string {
|
|
|
|
s := fmt.Sprintf(format, a...)
|
|
|
|
for _, i := range st.Inputs {
|
|
|
|
if i.Secret == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s = strings.ReplaceAll(s, i.Secret.PlainText(), "***")
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-02-24 00:39:06 +01:00
|
|
|
return progressui.PrintSolveStatus(ctx, ch,
|
|
|
|
func(v *bk.Vertex, index int) {
|
|
|
|
component, name := parseName(v)
|
|
|
|
lg := log.
|
|
|
|
Ctx(ctx).
|
|
|
|
With().
|
|
|
|
Str("component", component).
|
|
|
|
Logger()
|
|
|
|
|
|
|
|
lg.
|
|
|
|
Debug().
|
2021-05-29 11:18:01 +02:00
|
|
|
Msg(secureSprintf("#%d %s\n", index, name))
|
2021-02-24 00:39:06 +01:00
|
|
|
lg.
|
|
|
|
Debug().
|
2021-05-29 11:18:01 +02:00
|
|
|
Msg(secureSprintf("#%d %s\n", index, v.Digest))
|
2021-02-24 00:39:06 +01:00
|
|
|
},
|
|
|
|
func(v *bk.Vertex, format string, a ...interface{}) {
|
|
|
|
component, _ := parseName(v)
|
|
|
|
lg := log.
|
|
|
|
Ctx(ctx).
|
|
|
|
With().
|
|
|
|
Str("component", component).
|
|
|
|
Logger()
|
|
|
|
|
2021-05-29 11:18:01 +02:00
|
|
|
msg := secureSprintf(format, a...)
|
2021-02-24 00:39:06 +01:00
|
|
|
lg.
|
|
|
|
Debug().
|
2021-05-29 11:18:01 +02:00
|
|
|
Msg(msg)
|
2021-02-24 00:39:06 +01:00
|
|
|
},
|
|
|
|
func(v *bk.Vertex, stream int, partial bool, format string, a ...interface{}) {
|
|
|
|
component, _ := parseName(v)
|
|
|
|
lg := log.
|
|
|
|
Ctx(ctx).
|
|
|
|
With().
|
|
|
|
Str("component", component).
|
|
|
|
Logger()
|
|
|
|
|
2021-05-29 11:18:01 +02:00
|
|
|
msg := secureSprintf(format, a...)
|
2021-06-22 12:48:42 +02:00
|
|
|
lg.
|
|
|
|
Info().
|
|
|
|
Msg(msg)
|
2021-02-24 00:39:06 +01:00
|
|
|
},
|
|
|
|
)
|
2020-12-30 03:45:16 +01:00
|
|
|
}
|