2020-12-30 03:45:16 +01:00
|
|
|
package dagger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"context"
|
2021-02-18 00:27:40 +01:00
|
|
|
"errors"
|
2021-02-17 05:13:51 +01:00
|
|
|
"fmt"
|
2020-12-30 03:45:16 +01:00
|
|
|
"io"
|
|
|
|
"os"
|
2021-01-28 23:58:13 +01:00
|
|
|
"path/filepath"
|
2020-12-30 03:45:16 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2021-03-04 03:09:25 +01:00
|
|
|
"github.com/opentracing/opentracing-go"
|
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-03-12 22:00:11 +01:00
|
|
|
"github.com/moby/buildkit/client/llb"
|
2021-02-02 20:03:12 +01:00
|
|
|
bkgw "github.com/moby/buildkit/frontend/gateway/client"
|
2020-12-30 03:45:16 +01:00
|
|
|
|
|
|
|
// docker output
|
2021-03-03 01:14:53 +01:00
|
|
|
"dagger.io/go/pkg/buildkitd"
|
2021-02-24 00:39:06 +01:00
|
|
|
"dagger.io/go/pkg/progressui"
|
2021-02-07 08:36:21 +01:00
|
|
|
|
2021-02-17 22:13:39 +01:00
|
|
|
"dagger.io/go/dagger/compiler"
|
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 {
|
|
|
|
c *bk.Client
|
|
|
|
}
|
|
|
|
|
2021-01-30 02:16:22 +01:00
|
|
|
func NewClient(ctx context.Context, host string) (*Client, error) {
|
|
|
|
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{}
|
|
|
|
if span := opentracing.SpanFromContext(ctx); span != nil {
|
|
|
|
opts = append(opts, bk.WithTracer(span.Tracer()))
|
|
|
|
}
|
|
|
|
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{
|
|
|
|
c: c,
|
|
|
|
}, nil
|
2021-01-15 00:19:39 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 03:31:03 +01:00
|
|
|
// FIXME: return completed *Env, instead of *compiler.Value
|
2021-03-25 01:55:21 +01:00
|
|
|
func (c *Client) Up(ctx context.Context, env *Route) (*compiler.Value, 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-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())
|
|
|
|
return c.logSolveStatus(dispCtx, events)
|
|
|
|
})
|
2021-01-11 20:51:15 +01:00
|
|
|
|
2021-02-02 20:32:35 +01:00
|
|
|
// Spawn build function
|
|
|
|
outr, outw := io.Pipe()
|
|
|
|
eg.Go(func() error {
|
|
|
|
defer outw.Close()
|
|
|
|
return c.buildfn(gctx, env, events, outw)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Spawn output retriever
|
|
|
|
var (
|
2021-02-17 03:31:03 +01:00
|
|
|
out *compiler.Value
|
2021-02-02 20:32:35 +01:00
|
|
|
err error
|
|
|
|
)
|
2021-01-11 20:51:15 +01:00
|
|
|
eg.Go(func() error {
|
|
|
|
defer outr.Close()
|
2021-02-07 08:36:21 +01:00
|
|
|
out, err = c.outputfn(gctx, outr)
|
2021-02-02 20:32:35 +01:00
|
|
|
return err
|
2021-01-11 20:51:15 +01:00
|
|
|
})
|
2021-02-02 20:32:35 +01:00
|
|
|
|
2021-02-17 03:31:03 +01:00
|
|
|
return out, compiler.Err(eg.Wait())
|
2020-12-30 03:45:16 +01:00
|
|
|
}
|
|
|
|
|
2021-03-25 01:55:21 +01:00
|
|
|
func (c *Client) buildfn(ctx context.Context, env *Route, ch chan *bk.SolveStatus, w io.WriteCloser) 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-02-08 20:47:07 +01: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-01-11 20:51:15 +01:00
|
|
|
// Setup solve options
|
|
|
|
opts := bk.SolveOpt{
|
2021-02-02 20:03:12 +01:00
|
|
|
LocalDirs: localdirs,
|
2021-01-11 20:51:15 +01:00
|
|
|
// FIXME: catch output & return as cue value
|
|
|
|
Exports: []bk.ExportEntry{
|
|
|
|
{
|
|
|
|
Type: bk.ExporterTar,
|
|
|
|
Output: func(m map[string]string) (io.WriteCloser, error) {
|
|
|
|
return w, nil
|
2020-12-30 03:45:16 +01:00
|
|
|
},
|
|
|
|
},
|
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) {
|
|
|
|
s := NewSolver(c.c, gw, ch)
|
2021-02-02 20:03:12 +01:00
|
|
|
|
2021-02-24 01:37:45 +01:00
|
|
|
lg.Debug().Msg("loading configuration")
|
2021-02-02 20:03:12 +01:00
|
|
|
if err := env.Update(ctx, s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-24 01:37:45 +01:00
|
|
|
|
2021-02-02 20:03:12 +01:00
|
|
|
// Compute output overlay
|
2021-02-24 01:37:45 +01:00
|
|
|
lg.Debug().Msg("computing env")
|
2021-03-25 01:55:21 +01:00
|
|
|
if err := env.Up(ctx, s, nil); 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
|
|
|
// Export env to a cue directory
|
2021-03-12 22:00:11 +01:00
|
|
|
// FIXME: this should be elsewhere
|
2021-02-24 01:37:45 +01:00
|
|
|
lg.Debug().Msg("exporting env")
|
2021-03-12 22:00:11 +01:00
|
|
|
span, _ := opentracing.StartSpanFromContext(ctx, "Env.Export")
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
st := llb.Scratch().File(
|
|
|
|
llb.Mkfile("state.cue", 0600, env.State().JSON()),
|
|
|
|
llb.WithCustomName("[internal] serializing state to JSON"),
|
|
|
|
)
|
|
|
|
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-02-17 05:13:51 +01:00
|
|
|
return fmt.Errorf("buildkit solve: %w", bkCleanError(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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read tar export stream from buildkit Build(), and extract cue output
|
2021-02-17 03:31:03 +01:00
|
|
|
func (c *Client) outputfn(ctx context.Context, r io.Reader) (*compiler.Value, error) {
|
2021-01-14 02:38:16 +01:00
|
|
|
lg := log.Ctx(ctx)
|
|
|
|
|
2021-02-02 20:32:35 +01:00
|
|
|
// FIXME: merge this into env output.
|
2021-02-19 23:04:40 +01:00
|
|
|
out := compiler.EmptyStruct()
|
2021-02-02 20:32:35 +01:00
|
|
|
|
2021-01-11 20:51:15 +01:00
|
|
|
tr := tar.NewReader(r)
|
|
|
|
for {
|
|
|
|
h, err := tr.Next()
|
2021-02-18 00:27:40 +01:00
|
|
|
if errors.Is(err, io.EOF) {
|
2021-01-11 20:51:15 +01:00
|
|
|
break
|
2020-12-30 03:45:16 +01:00
|
|
|
}
|
2021-01-05 09:37:29 +01:00
|
|
|
if err != nil {
|
2021-02-17 05:13:51 +01:00
|
|
|
return nil, fmt.Errorf("read tar stream: %w", err)
|
2021-01-05 09:37:29 +01:00
|
|
|
}
|
2021-01-14 02:38:16 +01:00
|
|
|
|
|
|
|
lg := lg.
|
|
|
|
With().
|
|
|
|
Str("file", h.Name).
|
|
|
|
Logger()
|
|
|
|
|
2021-01-11 20:51:15 +01:00
|
|
|
if !strings.HasSuffix(h.Name, ".cue") {
|
2021-01-14 02:38:16 +01:00
|
|
|
lg.Debug().Msg("skipping non-cue file from exporter tar stream")
|
2021-01-11 20:51:15 +01:00
|
|
|
continue
|
2020-12-30 03:45:16 +01:00
|
|
|
}
|
2021-01-14 02:38:16 +01:00
|
|
|
lg.Debug().Msg("outputfn: compiling & merging")
|
2021-01-11 20:51:15 +01:00
|
|
|
|
2021-02-17 03:31:03 +01:00
|
|
|
v, err := compiler.Compile(h.Name, tr)
|
2020-12-30 03:45:16 +01:00
|
|
|
if err != nil {
|
2021-02-02 20:32:35 +01:00
|
|
|
return nil, err
|
2020-12-30 03:45:16 +01:00
|
|
|
}
|
2021-01-11 20:51:15 +01:00
|
|
|
if err := out.Fill(v); err != nil {
|
2021-02-17 05:13:51 +01:00
|
|
|
return nil, fmt.Errorf("%s: %w", h.Name, err)
|
2020-12-30 03:45:16 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-02 20:32:35 +01:00
|
|
|
return out, nil
|
2020-12-30 03:45:16 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 00:39:06 +01:00
|
|
|
func (c *Client) logSolveStatus(ctx context.Context, ch chan *bk.SolveStatus) error {
|
|
|
|
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)]
|
|
|
|
}
|
|
|
|
|
|
|
|
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().
|
|
|
|
Msg(fmt.Sprintf("#%d %s\n", index, name))
|
|
|
|
lg.
|
|
|
|
Debug().
|
|
|
|
Msg(fmt.Sprintf("#%d %s\n", index, v.Digest))
|
|
|
|
},
|
|
|
|
func(v *bk.Vertex, format string, a ...interface{}) {
|
|
|
|
component, _ := parseName(v)
|
|
|
|
lg := log.
|
|
|
|
Ctx(ctx).
|
|
|
|
With().
|
|
|
|
Str("component", component).
|
|
|
|
Logger()
|
|
|
|
|
|
|
|
lg.
|
|
|
|
Debug().
|
|
|
|
Msg(fmt.Sprintf(format, a...))
|
|
|
|
},
|
|
|
|
func(v *bk.Vertex, stream int, partial bool, format string, a ...interface{}) {
|
|
|
|
component, _ := parseName(v)
|
|
|
|
lg := log.
|
|
|
|
Ctx(ctx).
|
|
|
|
With().
|
|
|
|
Str("component", component).
|
|
|
|
Logger()
|
|
|
|
|
|
|
|
switch stream {
|
|
|
|
case 1:
|
|
|
|
lg.
|
|
|
|
Info().
|
|
|
|
Msg(fmt.Sprintf(format, a...))
|
|
|
|
case 2:
|
|
|
|
lg.
|
|
|
|
Error().
|
|
|
|
Msg(fmt.Sprintf(format, a...))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-12-30 03:45:16 +01:00
|
|
|
}
|