2021-11-25 01:22:33 +01:00
|
|
|
package plan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"cuelang.org/go/cue"
|
|
|
|
cueflow "cuelang.org/go/tools/flow"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"go.dagger.io/dagger/compiler"
|
|
|
|
"go.dagger.io/dagger/environment"
|
|
|
|
"go.dagger.io/dagger/plan/task"
|
|
|
|
"go.dagger.io/dagger/plancontext"
|
|
|
|
"go.dagger.io/dagger/solver"
|
|
|
|
"go.dagger.io/dagger/state"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Plan struct {
|
2022-01-04 19:54:50 +01:00
|
|
|
config Config
|
|
|
|
|
2021-11-25 01:22:33 +01:00
|
|
|
context *plancontext.Context
|
|
|
|
source *compiler.Value
|
|
|
|
}
|
|
|
|
|
2022-01-04 19:54:50 +01:00
|
|
|
type Config struct {
|
|
|
|
Args []string
|
|
|
|
With []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func Load(ctx context.Context, cfg Config) (*Plan, error) {
|
|
|
|
log.Ctx(ctx).Debug().Interface("args", cfg.Args).Msg("loading plan")
|
2021-12-08 23:28:02 +01:00
|
|
|
|
2021-12-09 20:17:19 +01:00
|
|
|
// FIXME: universe vendoring
|
2021-12-08 00:10:55 +01:00
|
|
|
if err := state.VendorUniverse(ctx, ""); err != nil {
|
2021-11-25 01:22:33 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-04 19:54:50 +01:00
|
|
|
v, err := compiler.Build("", nil, cfg.Args...)
|
2021-11-25 01:22:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-04 19:54:50 +01:00
|
|
|
for i, param := range cfg.With {
|
|
|
|
log.Ctx(ctx).Debug().Interface("with", param).Msg("compiling overlay")
|
|
|
|
paramV, err := compiler.Compile(fmt.Sprintf("with%v", i), param)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Ctx(ctx).Debug().Interface("with", param).Msg("filling overlay")
|
2022-01-04 22:40:34 +01:00
|
|
|
fillErr := v.FillPath(cue.MakePath(), paramV)
|
2022-01-04 19:54:50 +01:00
|
|
|
if fillErr != nil {
|
|
|
|
return nil, fillErr
|
2022-01-04 18:23:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-30 21:48:09 +01:00
|
|
|
p := &Plan{
|
2022-01-04 19:54:50 +01:00
|
|
|
config: cfg,
|
2021-11-25 01:22:33 +01:00
|
|
|
context: plancontext.New(),
|
|
|
|
source: v,
|
2021-11-30 21:48:09 +01:00
|
|
|
}
|
|
|
|
|
2021-12-23 20:23:52 +01:00
|
|
|
if err := p.configPlatform(); err != nil {
|
2021-11-30 21:48:09 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-23 20:23:52 +01:00
|
|
|
if err := p.prepare(ctx); err != nil {
|
2021-12-23 15:27:03 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-30 21:48:09 +01:00
|
|
|
return p, nil
|
2021-11-25 01:22:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plan) Context() *plancontext.Context {
|
|
|
|
return p.context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plan) Source() *compiler.Value {
|
|
|
|
return p.source
|
|
|
|
}
|
|
|
|
|
2021-12-23 15:27:03 +01:00
|
|
|
// configPlatform load the platform specified in the context
|
|
|
|
// Buildkit will then run every operation using that platform
|
|
|
|
// If platform is not define, context keep default platform
|
|
|
|
func (p *Plan) configPlatform() error {
|
|
|
|
platformField := p.source.Lookup("platform")
|
|
|
|
|
|
|
|
// Ignore if platform is not set in `#Plan`
|
|
|
|
if !platformField.Exists() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert platform to string
|
|
|
|
platform, err := platformField.String()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set platform to context
|
|
|
|
err = p.context.Platform.Set(platform)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-23 20:23:52 +01:00
|
|
|
// prepare executes the pre-run hooks of tasks
|
|
|
|
func (p *Plan) prepare(ctx context.Context) error {
|
|
|
|
flow := cueflow.New(
|
|
|
|
&cueflow.Config{},
|
|
|
|
p.source.Cue(),
|
|
|
|
func(flowVal cue.Value) (cueflow.Runner, error) {
|
|
|
|
v := compiler.Wrap(flowVal)
|
|
|
|
t, err := task.Lookup(v)
|
|
|
|
if err != nil {
|
|
|
|
// Not a task
|
|
|
|
if err == task.ErrNotTask {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r, ok := t.(task.PreRunner)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-11-25 01:22:33 +01:00
|
|
|
|
2021-12-23 20:23:52 +01:00
|
|
|
return cueflow.RunnerFunc(func(t *cueflow.Task) error {
|
|
|
|
ctx := t.Context()
|
|
|
|
lg := log.Ctx(ctx).With().Str("task", t.Path().String()).Logger()
|
|
|
|
ctx = lg.WithContext(ctx)
|
2021-11-25 01:22:33 +01:00
|
|
|
|
2021-12-23 20:23:52 +01:00
|
|
|
if err := r.PreRun(ctx, p.context, compiler.Wrap(t.Value())); err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", t.Path().String(), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}), nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return flow.Run(ctx)
|
2021-11-25 01:22:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Up executes the plan
|
2021-12-21 15:17:51 +01:00
|
|
|
func (p *Plan) Up(ctx context.Context, s solver.Solver) (*compiler.Value, error) {
|
2021-11-25 01:22:33 +01:00
|
|
|
ctx, span := otel.Tracer("dagger").Start(ctx, "plan.Up")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
computed := compiler.NewValue()
|
|
|
|
|
|
|
|
flow := cueflow.New(
|
|
|
|
&cueflow.Config{},
|
|
|
|
p.source.Cue(),
|
|
|
|
newRunner(p.context, s, computed),
|
|
|
|
)
|
|
|
|
if err := flow.Run(ctx); err != nil {
|
2021-12-21 15:17:51 +01:00
|
|
|
return nil, err
|
2021-11-25 01:22:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if src, err := computed.Source(); err == nil {
|
|
|
|
log.Ctx(ctx).Debug().Str("computed", string(src)).Msg("computed values")
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: canceling the context makes flow return `nil`
|
|
|
|
// Check explicitly if the context is canceled.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-12-21 15:17:51 +01:00
|
|
|
return nil, ctx.Err()
|
2021-11-25 01:22:33 +01:00
|
|
|
default:
|
2021-12-21 15:17:51 +01:00
|
|
|
return computed, nil
|
2021-11-25 01:22:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRunner(pctx *plancontext.Context, s solver.Solver, computed *compiler.Value) cueflow.TaskFunc {
|
|
|
|
return func(flowVal cue.Value) (cueflow.Runner, error) {
|
|
|
|
v := compiler.Wrap(flowVal)
|
|
|
|
r, err := task.Lookup(v)
|
|
|
|
if err != nil {
|
|
|
|
// Not a task
|
|
|
|
if err == task.ErrNotTask {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper around `task.Run` that handles logging, tracing, etc.
|
|
|
|
return cueflow.RunnerFunc(func(t *cueflow.Task) error {
|
|
|
|
ctx := t.Context()
|
2021-11-25 01:58:24 +01:00
|
|
|
lg := log.Ctx(ctx).With().Str("task", t.Path().String()).Logger()
|
2021-11-25 01:22:33 +01:00
|
|
|
ctx = lg.WithContext(ctx)
|
2021-12-09 20:17:19 +01:00
|
|
|
ctx, span := otel.Tracer("dagger").Start(ctx, fmt.Sprintf("up: %s", t.Path().String()))
|
2021-11-25 01:22:33 +01:00
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
lg.Info().Str("state", string(environment.StateComputing)).Msg(string(environment.StateComputing))
|
|
|
|
|
|
|
|
// Debug: dump dependencies
|
|
|
|
for _, dep := range t.Dependencies() {
|
|
|
|
lg.Debug().Str("dependency", dep.Path().String()).Msg("dependency detected")
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
result, err := r.Run(ctx, pctx, s, compiler.Wrap(t.Value()))
|
|
|
|
if err != nil {
|
|
|
|
// FIXME: this should use errdefs.IsCanceled(err)
|
|
|
|
if strings.Contains(err.Error(), "context canceled") {
|
|
|
|
lg.Error().Dur("duration", time.Since(start)).Str("state", string(environment.StateCanceled)).Msg(string(environment.StateCanceled))
|
|
|
|
} else {
|
|
|
|
lg.Error().Dur("duration", time.Since(start)).Err(err).Str("state", string(environment.StateFailed)).Msg(string(environment.StateFailed))
|
|
|
|
}
|
2021-12-23 20:23:52 +01:00
|
|
|
return fmt.Errorf("%s: %w", t.Path().String(), err)
|
2021-11-25 01:22:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lg.Info().Dur("duration", time.Since(start)).Str("state", string(environment.StateCompleted)).Msg(string(environment.StateCompleted))
|
|
|
|
|
2021-12-09 20:17:19 +01:00
|
|
|
// If the result is not concrete (e.g. empty value), there's nothing to merge.
|
2021-11-25 01:22:33 +01:00
|
|
|
if !result.IsConcrete() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if src, err := result.Source(); err == nil {
|
|
|
|
lg.Debug().Str("result", string(src)).Msg("merging task result")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mirror task result in both `flow.Task` and `computed`
|
|
|
|
if err := t.Fill(result.Cue()); err != nil {
|
|
|
|
lg.Error().Err(err).Msg("failed to fill task")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge task value into computed
|
|
|
|
if err := computed.FillPath(t.Path(), result); err != nil {
|
|
|
|
lg.Error().Err(err).Msg("failed to fill plan")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
}
|