2021-05-26 01:30:49 +02:00
|
|
|
package environment
|
2021-03-23 08:22:50 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-25 01:55:21 +01:00
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2021-03-23 08:22:50 +01:00
|
|
|
|
2021-03-25 01:55:21 +01:00
|
|
|
"cuelang.org/go/cue"
|
|
|
|
cueflow "cuelang.org/go/tools/flow"
|
2021-05-26 01:53:26 +02:00
|
|
|
"go.dagger.io/dagger/compiler"
|
|
|
|
"go.dagger.io/dagger/solver"
|
|
|
|
"go.dagger.io/dagger/state"
|
|
|
|
"go.dagger.io/dagger/stdlib"
|
2021-03-24 23:03:05 +01:00
|
|
|
|
2021-03-25 01:55:21 +01:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/opentracing/opentracing-go/ext"
|
|
|
|
otlog "github.com/opentracing/opentracing-go/log"
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-03-24 23:03:05 +01:00
|
|
|
)
|
|
|
|
|
2021-04-27 20:59:04 +02:00
|
|
|
type Environment struct {
|
2021-05-07 23:45:15 +02:00
|
|
|
state *state.State
|
2021-04-09 03:09:10 +02:00
|
|
|
|
|
|
|
// Layer 1: plan configuration
|
|
|
|
plan *compiler.Value
|
|
|
|
|
|
|
|
// Layer 2: user inputs
|
|
|
|
input *compiler.Value
|
|
|
|
|
|
|
|
// Layer 3: computed values
|
|
|
|
computed *compiler.Value
|
2021-03-24 18:37:50 +01:00
|
|
|
}
|
|
|
|
|
2021-05-26 01:30:49 +02:00
|
|
|
func New(st *state.State) (*Environment, error) {
|
2021-05-07 23:39:12 +02:00
|
|
|
e := &Environment{
|
2021-04-09 03:09:10 +02:00
|
|
|
state: st,
|
|
|
|
|
|
|
|
plan: compiler.NewValue(),
|
|
|
|
input: compiler.NewValue(),
|
|
|
|
computed: compiler.NewValue(),
|
2021-03-25 01:55:21 +01:00
|
|
|
}
|
2021-03-24 18:37:50 +01:00
|
|
|
|
2021-03-25 01:55:21 +01:00
|
|
|
// Prepare inputs
|
2021-05-17 20:12:46 +02:00
|
|
|
for key, input := range st.Inputs {
|
2021-05-26 03:56:16 +02:00
|
|
|
v, err := input.Compile(key, st)
|
2021-03-25 01:55:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-17 20:12:46 +02:00
|
|
|
if key == "" {
|
2021-05-07 23:39:12 +02:00
|
|
|
err = e.input.FillPath(cue.MakePath(), v)
|
2021-03-25 01:55:21 +01:00
|
|
|
} else {
|
2021-05-17 20:12:46 +02:00
|
|
|
err = e.input.FillPath(cue.ParsePath(key), v)
|
2021-03-25 01:55:21 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 23:39:12 +02:00
|
|
|
return e, nil
|
2021-03-24 18:37:50 +01:00
|
|
|
}
|
|
|
|
|
2021-05-07 23:39:12 +02:00
|
|
|
func (e *Environment) Name() string {
|
|
|
|
return e.state.Name
|
2021-03-27 00:44:13 +01:00
|
|
|
}
|
|
|
|
|
2021-05-07 23:39:12 +02:00
|
|
|
func (e *Environment) Plan() *compiler.Value {
|
|
|
|
return e.plan
|
2021-04-09 03:09:10 +02:00
|
|
|
}
|
|
|
|
|
2021-05-07 23:39:12 +02:00
|
|
|
func (e *Environment) Input() *compiler.Value {
|
|
|
|
return e.input
|
2021-04-09 03:09:10 +02:00
|
|
|
}
|
|
|
|
|
2021-05-07 23:39:12 +02:00
|
|
|
func (e *Environment) Computed() *compiler.Value {
|
|
|
|
return e.computed
|
2021-03-24 18:37:50 +01:00
|
|
|
}
|
|
|
|
|
2021-03-31 04:40:44 +02:00
|
|
|
// LoadPlan loads the plan
|
2021-05-26 01:30:49 +02:00
|
|
|
func (e *Environment) LoadPlan(ctx context.Context, s solver.Solver) error {
|
2021-04-27 20:59:04 +02:00
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "environment.LoadPlan")
|
2021-03-25 01:55:21 +01:00
|
|
|
defer span.Finish()
|
2021-03-23 23:41:26 +01:00
|
|
|
|
2021-06-15 18:49:57 +02:00
|
|
|
planSource, err := e.state.Plan.Source().Compile("", e.state)
|
2021-03-25 01:55:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-24 18:37:50 +01:00
|
|
|
|
2021-05-05 01:26:56 +02:00
|
|
|
p := NewPipeline(planSource, s).WithCustomName("[internal] source")
|
2021-03-25 01:55:21 +01:00
|
|
|
// execute updater script
|
2021-05-05 01:26:56 +02:00
|
|
|
if err := p.Run(ctx); err != nil {
|
2021-03-25 01:55:21 +01:00
|
|
|
return err
|
|
|
|
}
|
2021-03-24 18:37:50 +01:00
|
|
|
|
2021-03-25 01:55:21 +01:00
|
|
|
// Build a Cue config by overlaying the source with the stdlib
|
|
|
|
sources := map[string]fs.FS{
|
|
|
|
stdlib.Path: stdlib.FS,
|
|
|
|
"/": p.FS(),
|
|
|
|
}
|
2021-06-15 18:49:57 +02:00
|
|
|
args := []string{}
|
|
|
|
if pkg := e.state.Plan.Package; pkg != "" {
|
|
|
|
args = append(args, pkg)
|
|
|
|
}
|
|
|
|
plan, err := compiler.Build(sources, args...)
|
2021-03-25 01:55:21 +01:00
|
|
|
if err != nil {
|
2021-05-25 21:50:12 +02:00
|
|
|
return fmt.Errorf("plan config: %w", compiler.Err(err))
|
2021-03-25 01:55:21 +01:00
|
|
|
}
|
2021-05-07 23:39:12 +02:00
|
|
|
e.plan = plan
|
2021-03-24 18:37:50 +01:00
|
|
|
|
2021-04-07 02:43:12 +02:00
|
|
|
return nil
|
2021-03-23 23:41:26 +01:00
|
|
|
}
|
|
|
|
|
2021-04-27 20:59:04 +02:00
|
|
|
// Scan all scripts in the environment for references to local directories (do:"local"),
|
2021-03-25 01:55:21 +01:00
|
|
|
// and return all referenced directory names.
|
|
|
|
// This is used by clients to grant access to local directories when they are referenced
|
|
|
|
// by user-specified scripts.
|
2021-05-07 23:39:12 +02:00
|
|
|
func (e *Environment) LocalDirs() map[string]string {
|
2021-03-25 01:55:21 +01:00
|
|
|
dirs := map[string]string{}
|
2021-05-05 01:26:56 +02:00
|
|
|
localdirs := func(code *compiler.Value) {
|
2021-03-25 01:55:21 +01:00
|
|
|
Analyze(
|
|
|
|
func(op *compiler.Value) error {
|
2021-03-31 22:40:46 +02:00
|
|
|
do, err := op.Lookup("do").String()
|
2021-03-25 01:55:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if do != "local" {
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-31 22:40:46 +02:00
|
|
|
dir, err := op.Lookup("dir").String()
|
2021-03-25 01:55:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dirs[dir] = dir
|
|
|
|
return nil
|
|
|
|
},
|
2021-05-05 01:26:56 +02:00
|
|
|
code,
|
2021-03-25 01:55:21 +01:00
|
|
|
)
|
|
|
|
}
|
2021-04-27 20:59:04 +02:00
|
|
|
// 1. Scan the environment state
|
2021-03-25 01:55:21 +01:00
|
|
|
// FIXME: use a common `flow` instance to avoid rescanning the tree.
|
2021-05-01 03:05:37 +02:00
|
|
|
src := compiler.NewValue()
|
2021-05-07 23:39:12 +02:00
|
|
|
if err := src.FillPath(cue.MakePath(), e.plan); err != nil {
|
2021-05-01 03:05:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-07 23:39:12 +02:00
|
|
|
if err := src.FillPath(cue.MakePath(), e.input); err != nil {
|
2021-05-01 03:05:37 +02:00
|
|
|
return nil
|
2021-04-07 02:43:12 +02:00
|
|
|
}
|
|
|
|
flow := cueflow.New(
|
|
|
|
&cueflow.Config{},
|
2021-05-01 03:05:37 +02:00
|
|
|
src.Cue(),
|
|
|
|
newTaskFunc(noOpRunner),
|
2021-04-07 02:43:12 +02:00
|
|
|
)
|
2021-03-25 01:55:21 +01:00
|
|
|
for _, t := range flow.Tasks() {
|
2021-05-01 03:05:37 +02:00
|
|
|
v := compiler.Wrap(t.Value())
|
2021-04-02 23:58:12 +02:00
|
|
|
localdirs(v.Lookup("#up"))
|
2021-03-25 01:55:21 +01:00
|
|
|
}
|
2021-03-23 08:22:50 +01:00
|
|
|
|
2021-03-31 04:40:44 +02:00
|
|
|
// 2. Scan the plan
|
2021-06-15 18:49:57 +02:00
|
|
|
plan, err := e.state.Plan.Source().Compile("", e.state)
|
2021-03-26 22:11:54 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2021-03-25 01:55:21 +01:00
|
|
|
}
|
2021-03-31 04:40:44 +02:00
|
|
|
localdirs(plan)
|
2021-03-25 01:55:21 +01:00
|
|
|
return dirs
|
2021-03-23 08:22:50 +01:00
|
|
|
}
|
2021-03-23 23:27:16 +01:00
|
|
|
|
2021-05-27 18:45:24 +02:00
|
|
|
// prepare initializes the Environment with inputs and plan code
|
|
|
|
func (e *Environment) prepare(ctx context.Context) (*compiler.Value, error) {
|
|
|
|
span, _ := opentracing.StartSpanFromContext(ctx, "environment.Prepare")
|
2021-03-25 01:55:21 +01:00
|
|
|
defer span.Finish()
|
|
|
|
|
2021-04-07 02:43:12 +02:00
|
|
|
// Reset the computed values
|
2021-05-07 23:39:12 +02:00
|
|
|
e.computed = compiler.NewValue()
|
2021-03-25 01:55:21 +01:00
|
|
|
|
2021-05-01 03:05:37 +02:00
|
|
|
src := compiler.NewValue()
|
2021-05-07 23:39:12 +02:00
|
|
|
if err := src.FillPath(cue.MakePath(), e.plan); err != nil {
|
2021-05-27 18:45:24 +02:00
|
|
|
return nil, err
|
2021-05-01 03:05:37 +02:00
|
|
|
}
|
2021-05-07 23:39:12 +02:00
|
|
|
if err := src.FillPath(cue.MakePath(), e.input); err != nil {
|
2021-05-27 18:45:24 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return src, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Up missing values in environment configuration, and write them to state.
|
|
|
|
func (e *Environment) Up(ctx context.Context, s solver.Solver) error {
|
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx, "environment.Up")
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
// Set user inputs and plan code
|
|
|
|
src, err := e.prepare(ctx)
|
|
|
|
if err != nil {
|
2021-04-07 02:43:12 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-03-25 01:55:21 +01:00
|
|
|
|
|
|
|
// Orchestrate execution with cueflow
|
2021-04-07 02:43:12 +02:00
|
|
|
flow := cueflow.New(
|
2021-04-08 03:41:44 +02:00
|
|
|
&cueflow.Config{},
|
2021-05-01 03:05:37 +02:00
|
|
|
src.Cue(),
|
2021-05-07 23:39:12 +02:00
|
|
|
newTaskFunc(newPipelineRunner(e.computed, s)),
|
2021-04-07 02:43:12 +02:00
|
|
|
)
|
2021-03-25 01:55:21 +01:00
|
|
|
if err := flow.Run(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-07 02:43:12 +02:00
|
|
|
return nil
|
2021-03-23 08:22:50 +01:00
|
|
|
}
|
2021-03-23 23:27:16 +01:00
|
|
|
|
2021-03-23 23:41:26 +01:00
|
|
|
type DownOpts struct{}
|
2021-03-23 08:22:50 +01:00
|
|
|
|
2021-05-07 23:39:12 +02:00
|
|
|
func (e *Environment) Down(ctx context.Context, _ *DownOpts) error {
|
2021-03-25 01:55:21 +01:00
|
|
|
panic("NOT IMPLEMENTED")
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:41:26 +01:00
|
|
|
type QueryOpts struct{}
|
2021-03-25 01:55:21 +01:00
|
|
|
|
2021-05-01 03:05:37 +02:00
|
|
|
func newTaskFunc(runner cueflow.RunnerFunc) cueflow.TaskFunc {
|
2021-03-25 01:55:21 +01:00
|
|
|
return func(flowVal cue.Value) (cueflow.Runner, error) {
|
2021-05-01 03:05:37 +02:00
|
|
|
v := compiler.Wrap(flowVal)
|
2021-03-25 01:55:21 +01:00
|
|
|
if !isComponent(v) {
|
|
|
|
// No compute script
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return runner, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func noOpRunner(t *cueflow.Task) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:30:49 +02:00
|
|
|
func newPipelineRunner(computed *compiler.Value, s solver.Solver) cueflow.RunnerFunc {
|
2021-03-25 01:55:21 +01:00
|
|
|
return cueflow.RunnerFunc(func(t *cueflow.Task) error {
|
|
|
|
ctx := t.Context()
|
|
|
|
lg := log.
|
|
|
|
Ctx(ctx).
|
|
|
|
With().
|
|
|
|
Str("component", t.Path().String()).
|
|
|
|
Logger()
|
|
|
|
ctx = lg.WithContext(ctx)
|
|
|
|
span, ctx := opentracing.StartSpanFromContext(ctx,
|
|
|
|
fmt.Sprintf("compute: %s", t.Path().String()),
|
|
|
|
)
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
lg.
|
|
|
|
Info().
|
|
|
|
Msg("computing")
|
|
|
|
for _, dep := range t.Dependencies() {
|
|
|
|
lg.
|
|
|
|
Debug().
|
|
|
|
Str("dependency", dep.Path().String()).
|
|
|
|
Msg("dependency detected")
|
|
|
|
}
|
2021-05-01 03:05:37 +02:00
|
|
|
v := compiler.Wrap(t.Value())
|
2021-05-05 01:26:56 +02:00
|
|
|
p := NewPipeline(v, s)
|
|
|
|
err := p.Run(ctx)
|
2021-03-25 01:55:21 +01:00
|
|
|
if err != nil {
|
|
|
|
span.LogFields(otlog.String("error", err.Error()))
|
|
|
|
ext.Error.Set(span, true)
|
|
|
|
|
|
|
|
// FIXME: this should use errdefs.IsCanceled(err)
|
|
|
|
if strings.Contains(err.Error(), "context canceled") {
|
|
|
|
lg.
|
|
|
|
Error().
|
|
|
|
Dur("duration", time.Since(start)).
|
|
|
|
Msg("canceled")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lg.
|
|
|
|
Error().
|
|
|
|
Dur("duration", time.Since(start)).
|
|
|
|
Err(err).
|
|
|
|
Msg("failed")
|
|
|
|
return err
|
|
|
|
}
|
2021-04-08 03:41:44 +02:00
|
|
|
|
|
|
|
// Mirror the computed values in both `Task` and `Result`
|
2021-04-09 03:09:10 +02:00
|
|
|
if p.Computed().IsEmptyStruct() {
|
2021-04-08 03:41:44 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 03:09:10 +02:00
|
|
|
if err := t.Fill(p.Computed().Cue()); err != nil {
|
2021-04-08 03:41:44 +02:00
|
|
|
lg.
|
|
|
|
Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("failed to fill task")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge task value into output
|
2021-04-09 03:09:10 +02:00
|
|
|
if err := computed.FillPath(t.Path(), p.Computed()); err != nil {
|
2021-04-08 03:41:44 +02:00
|
|
|
lg.
|
|
|
|
Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("failed to fill task result")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-25 01:55:21 +01:00
|
|
|
lg.
|
|
|
|
Info().
|
|
|
|
Dur("duration", time.Since(start)).
|
|
|
|
Msg("completed")
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2021-04-19 22:44:21 +02:00
|
|
|
|
2021-05-27 18:45:24 +02:00
|
|
|
func (e *Environment) ScanInputs(ctx context.Context, mergeUserInputs bool) ([]*compiler.Value, error) {
|
|
|
|
src := e.plan
|
|
|
|
|
|
|
|
if mergeUserInputs {
|
|
|
|
// Set user inputs and plan code
|
|
|
|
var err error
|
|
|
|
src, err = e.prepare(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 16:22:38 +02:00
|
|
|
return ScanInputs(ctx, src), nil
|
2021-04-19 22:44:21 +02:00
|
|
|
}
|
2021-05-31 15:24:42 +02:00
|
|
|
|
|
|
|
func (e *Environment) ScanOutputs(ctx context.Context) ([]*compiler.Value, error) {
|
|
|
|
src, err := e.prepare(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-01 14:14:45 +02:00
|
|
|
if e.state.Computed != "" {
|
|
|
|
computed, err := compiler.DecodeJSON("", []byte(e.state.Computed))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-31 15:24:42 +02:00
|
|
|
|
2021-06-01 14:14:45 +02:00
|
|
|
if err := src.FillPath(cue.MakePath(), computed); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-31 15:24:42 +02:00
|
|
|
}
|
|
|
|
|
2021-06-02 16:22:38 +02:00
|
|
|
return ScanOutputs(ctx, src), nil
|
2021-05-31 15:24:42 +02:00
|
|
|
}
|