simplify: remove compute.go

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-02-02 11:03:12 -08:00
parent 6e31193d64
commit 167edf4a76
2 changed files with 34 additions and 87 deletions

View File

@ -20,6 +20,7 @@ import (
// buildkit // buildkit
bk "github.com/moby/buildkit/client" bk "github.com/moby/buildkit/client"
_ "github.com/moby/buildkit/client/connhelper/dockercontainer" // import the container connection driver _ "github.com/moby/buildkit/client/connhelper/dockercontainer" // import the container connection driver
bkgw "github.com/moby/buildkit/frontend/gateway/client"
// docker output // docker output
"github.com/containerd/console" "github.com/containerd/console"
@ -28,15 +29,11 @@ import (
const ( const (
defaultBuildkitHost = "docker-container://buildkitd" defaultBuildkitHost = "docker-container://buildkitd"
bkUpdaterKey = "updater"
bkInputKey = "input"
) )
// A dagger client // A dagger client
type Client struct { type Client struct {
c *bk.Client c *bk.Client
localdirs map[string]string
} }
func NewClient(ctx context.Context, host string) (*Client, error) { func NewClient(ctx context.Context, host string) (*Client, error) {
@ -64,19 +61,6 @@ func (c *Client) Compute(ctx context.Context, env *Env) (o *Value, err error) {
err = cueErr(err) err = cueErr(err)
} }
}() }()
// Scan local dirs to grant access
localdirs, err := env.LocalDirs(ctx)
if err != nil {
return nil, errors.Wrap(err, "scan local dirs")
}
for label, dir := range localdirs {
abs, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
localdirs[label] = abs
}
c.localdirs = localdirs
// FIXME: merge this into env output. // FIXME: merge this into env output.
out, err := env.Compiler().EmptyStruct() out, err := env.Compiler().EmptyStruct()
@ -136,22 +120,22 @@ func (c *Client) Compute(ctx context.Context, env *Env) (o *Value, err error) {
func (c *Client) buildfn(ctx context.Context, env *Env, ch chan *bk.SolveStatus, w io.WriteCloser) error { func (c *Client) buildfn(ctx context.Context, env *Env, ch chan *bk.SolveStatus, w io.WriteCloser) error {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
// Serialize input and updater // Scan local dirs to grant access
input, err := env.Input().SourceString() localdirs, err := env.LocalDirs(ctx)
if err != nil { if err != nil {
return errors.Wrap(err, "serialize env input") return errors.Wrap(err, "scan local dirs")
} }
updater, err := env.Updater().Value().SourceString() for label, dir := range localdirs {
abs, err := filepath.Abs(dir)
if err != nil { if err != nil {
return errors.Wrap(err, "serialize updater script") return err
} }
localdirs[label] = abs
}
// Setup solve options // Setup solve options
opts := bk.SolveOpt{ opts := bk.SolveOpt{
FrontendAttrs: map[string]string{ LocalDirs: localdirs,
bkInputKey: input,
bkUpdaterKey: updater,
},
LocalDirs: c.localdirs,
// FIXME: catch output & return as cue value // FIXME: catch output & return as cue value
Exports: []bk.ExportEntry{ Exports: []bk.ExportEntry{
{ {
@ -162,12 +146,33 @@ func (c *Client) buildfn(ctx context.Context, env *Env, ch chan *bk.SolveStatus,
}, },
}, },
} }
// Call buildkit solver // Call buildkit solver
lg.Debug(). lg.Debug().
Interface("localdirs", opts.LocalDirs). Interface("localdirs", opts.LocalDirs).
Interface("attrs", opts.FrontendAttrs). Interface("attrs", opts.FrontendAttrs).
Msg("spawning buildkit job") Msg("spawning buildkit job")
resp, err := c.c.Build(ctx, opts, "", Compute, ch)
resp, err := c.c.Build(ctx, opts, "", func(ctx context.Context, c bkgw.Client) (*bkgw.Result, error) {
s := NewSolver(c)
if err := env.Update(ctx, s); err != nil {
return nil, err
}
lg.Debug().Msg("computing env")
// Compute output overlay
if err := env.Compute(ctx, s); err != nil {
return nil, err
}
lg.Debug().Msg("exporting env")
// Export env to a cue directory
outdir, err := env.Export(s.Scratch())
if err != nil {
return nil, err
}
// Wrap cue directory in buildkit result
return outdir.Result(ctx)
}, ch)
if err != nil { if err != nil {
return errors.Wrap(bkCleanError(err), "buildkit solve") return errors.Wrap(bkCleanError(err), "buildkit solve")
} }

View File

@ -1,58 +0,0 @@
package dagger
import (
"context"
"fmt"
cueerrors "cuelang.org/go/cue/errors"
bkgw "github.com/moby/buildkit/frontend/gateway/client"
"github.com/rs/zerolog/log"
)
// Buildkit compute entrypoint (BK calls if "solve" or "build")
// Use by wrapping in a buildkit client Build call, or buildkit frontend.
func Compute(ctx context.Context, c bkgw.Client) (r *bkgw.Result, err error) {
lg := log.Ctx(ctx)
// FIXME: wrap errors to avoid crashing buildkit Build()
// with cue error types (why??)
defer func() {
if err != nil {
err = fmt.Errorf("%s", cueerrors.Details(err, nil))
}
}()
s := NewSolver(c)
// Retrieve updater script form client
var updater interface{}
if o, exists := c.BuildOpts().Opts[bkUpdaterKey]; exists {
updater = o
}
env, err := NewEnv()
if err != nil {
return nil, err
}
if err := env.SetUpdater(updater); err != nil {
return nil, err
}
if err := env.Update(ctx, s); err != nil {
return nil, err
}
if input, exists := c.BuildOpts().Opts["input"]; exists {
if err := env.SetInput(input); err != nil {
return nil, err
}
}
lg.Debug().Msg("computing env")
// Compute output overlay
if err := env.Compute(ctx, s); err != nil {
return nil, err
}
lg.Debug().Msg("exporting env")
// Export env to a cue directory
outdir, err := env.Export(s.Scratch())
if err != nil {
return nil, err
}
// Wrap cue directory in buildkit result
return outdir.Result(ctx)
}