2021-11-25 01:22:33 +01:00
|
|
|
package plan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"cuelang.org/go/cue"
|
|
|
|
cueflow "cuelang.org/go/tools/flow"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"go.dagger.io/dagger/compiler"
|
2022-01-12 01:36:23 +01:00
|
|
|
"go.dagger.io/dagger/pkg"
|
2021-11-25 01:22:33 +01:00
|
|
|
"go.dagger.io/dagger/plan/task"
|
|
|
|
"go.dagger.io/dagger/plancontext"
|
|
|
|
"go.dagger.io/dagger/solver"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
)
|
|
|
|
|
2022-03-03 03:33:15 +01:00
|
|
|
var (
|
|
|
|
ActionSelector = cue.Str("actions")
|
2022-03-08 01:02:42 +01:00
|
|
|
ClientSelector = cue.Str("client")
|
2022-02-22 22:04:54 +01:00
|
|
|
)
|
|
|
|
|
2021-11-25 01:22:33 +01:00
|
|
|
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-02-22 22:04:54 +01:00
|
|
|
action *Action
|
2021-11-25 01:22:33 +01:00
|
|
|
}
|
|
|
|
|
2022-01-04 19:54:50 +01:00
|
|
|
type Config struct {
|
2021-12-23 16:09:01 +01:00
|
|
|
Args []string
|
|
|
|
With []string
|
|
|
|
Target string
|
2022-01-04 19:54:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-03-07 23:09:39 +01:00
|
|
|
_, cueModExists := pkg.GetCueModParent()
|
|
|
|
if !cueModExists {
|
|
|
|
return nil, fmt.Errorf("dagger project not found. Run `dagger project init`")
|
2021-11-25 01:22:33 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-22 22:04:54 +01:00
|
|
|
p.fillAction()
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-22 22:04:54 +01:00
|
|
|
func (p *Plan) Action() *Action {
|
|
|
|
return p.action
|
|
|
|
}
|
|
|
|
|
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(
|
2022-01-20 02:19:37 +01:00
|
|
|
&cueflow.Config{
|
|
|
|
FindHiddenTasks: true,
|
|
|
|
},
|
2021-12-23 20:23:52 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-03 03:33:15 +01:00
|
|
|
// Do executes an action in the plan
|
|
|
|
func (p *Plan) Do(ctx context.Context, path cue.Path, s solver.Solver) error {
|
2021-11-25 01:22:33 +01:00
|
|
|
ctx, span := otel.Tracer("dagger").Start(ctx, "plan.Up")
|
|
|
|
defer span.End()
|
|
|
|
|
2022-03-03 03:33:15 +01:00
|
|
|
r := NewRunner(p.context, path, s)
|
|
|
|
return r.Run(ctx, p.source)
|
2021-11-25 01:22:33 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 22:04:54 +01:00
|
|
|
func (p *Plan) fillAction() {
|
|
|
|
cfg := &cueflow.Config{
|
|
|
|
FindHiddenTasks: true,
|
2022-03-03 03:33:15 +01:00
|
|
|
Root: cue.MakePath(ActionSelector),
|
2022-02-22 22:04:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
flow := cueflow.New(
|
|
|
|
cfg,
|
|
|
|
p.source.Cue(),
|
|
|
|
noOpRunner,
|
|
|
|
)
|
|
|
|
|
2022-03-09 03:34:47 +01:00
|
|
|
p.action = &Action{
|
|
|
|
Name: ActionSelector.String(),
|
|
|
|
Hidden: false,
|
|
|
|
Path: cue.MakePath(ActionSelector),
|
|
|
|
Children: []*Action{},
|
|
|
|
}
|
|
|
|
|
2022-03-03 03:33:15 +01:00
|
|
|
actions := p.source.LookupPath(cue.MakePath(ActionSelector))
|
2022-03-09 19:21:49 +01:00
|
|
|
if !actions.Exists() {
|
2022-03-09 03:34:47 +01:00
|
|
|
return
|
2022-02-22 22:04:54 +01:00
|
|
|
}
|
2022-03-09 03:34:47 +01:00
|
|
|
for _, cg := range actions.Doc() {
|
|
|
|
p.action.Comment += cg.Text()
|
2022-02-22 22:04:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tasks := flow.Tasks()
|
|
|
|
|
|
|
|
for _, t := range tasks {
|
|
|
|
var q []cue.Selector
|
|
|
|
prevAction := p.action
|
|
|
|
for _, s := range t.Path().Selectors() {
|
|
|
|
q = append(q, s)
|
|
|
|
path := cue.MakePath(q...)
|
|
|
|
a := prevAction.FindByPath(path)
|
|
|
|
if a == nil {
|
|
|
|
v := p.Source().LookupPath(path)
|
|
|
|
childComment := ""
|
|
|
|
for _, cg := range v.Doc() {
|
|
|
|
childComment += cg.Text()
|
|
|
|
}
|
|
|
|
|
|
|
|
a = &Action{
|
2022-03-09 03:34:47 +01:00
|
|
|
Name: s.String(),
|
|
|
|
Hidden: s.PkgPath() != "",
|
|
|
|
Path: path,
|
|
|
|
Comment: childComment,
|
|
|
|
Children: []*Action{},
|
2022-02-22 22:04:54 +01:00
|
|
|
}
|
|
|
|
prevAction.AddChild(a)
|
|
|
|
}
|
|
|
|
prevAction = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|