engine: Task PreRun Hook
Tasks now have a PreRun hook that gets called before buildkit kicks in. Allows to support local access without special casing. Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
parent
06b05746b8
commit
2982a0dda0
57
plan/plan.go
57
plan/plan.go
@ -2,9 +2,7 @@ package plan
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -43,11 +41,11 @@ func Load(ctx context.Context, args ...string) (*Plan, error) {
|
|||||||
source: v,
|
source: v,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.registerLocalDirs(); err != nil {
|
if err := p.configPlatform(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.configPlatform(); err != nil {
|
if err := p.prepare(ctx); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,26 +85,39 @@ func (p *Plan) configPlatform() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// registerLocalDirectories scans the context for local imports.
|
// prepare executes the pre-run hooks of tasks
|
||||||
// BuildKit requires to known the list of directories ahead of time.
|
func (p *Plan) prepare(ctx context.Context) error {
|
||||||
func (p *Plan) registerLocalDirs() error {
|
flow := cueflow.New(
|
||||||
imports, err := p.source.Lookup("inputs.directories").Fields()
|
&cueflow.Config{},
|
||||||
if err != nil {
|
p.source.Cue(),
|
||||||
return err
|
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
|
||||||
|
}
|
||||||
|
|
||||||
for _, v := range imports {
|
return cueflow.RunnerFunc(func(t *cueflow.Task) error {
|
||||||
dir, err := v.Value.Lookup("path").AbsPath()
|
ctx := t.Context()
|
||||||
if err != nil {
|
lg := log.Ctx(ctx).With().Str("task", t.Path().String()).Logger()
|
||||||
return err
|
ctx = lg.WithContext(ctx)
|
||||||
}
|
|
||||||
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
|
|
||||||
return fmt.Errorf("path %q does not exist", dir)
|
|
||||||
}
|
|
||||||
p.context.LocalDirs.Add(dir)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Up executes the plan
|
// Up executes the plan
|
||||||
@ -175,7 +186,7 @@ func newRunner(pctx *plancontext.Context, s solver.Solver, computed *compiler.Va
|
|||||||
} else {
|
} else {
|
||||||
lg.Error().Dur("duration", time.Since(start)).Err(err).Str("state", string(environment.StateFailed)).Msg(string(environment.StateFailed))
|
lg.Error().Dur("duration", time.Since(start)).Err(err).Str("state", string(environment.StateFailed)).Msg(string(environment.StateFailed))
|
||||||
}
|
}
|
||||||
return err
|
return fmt.Errorf("%s: %w", t.Path().String(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
lg.Info().Dur("duration", time.Since(start)).Str("state", string(environment.StateCompleted)).Msg(string(environment.StateCompleted))
|
lg.Info().Dur("duration", time.Since(start)).Str("state", string(environment.StateCompleted)).Msg(string(environment.StateCompleted))
|
||||||
|
@ -2,6 +2,9 @@ package task
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/moby/buildkit/client/llb"
|
"github.com/moby/buildkit/client/llb"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@ -17,6 +20,20 @@ func init() {
|
|||||||
type inputDirectoryTask struct {
|
type inputDirectoryTask struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *inputDirectoryTask) PreRun(ctx context.Context, pctx *plancontext.Context, v *compiler.Value) error {
|
||||||
|
path, err := v.Lookup("path").AbsPath()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
||||||
|
return fmt.Errorf("path %q does not exist", path)
|
||||||
|
}
|
||||||
|
pctx.LocalDirs.Add(path)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *inputDirectoryTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) {
|
func (c *inputDirectoryTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) {
|
||||||
path, err := v.Lookup("path").AbsPath()
|
path, err := v.Lookup("path").AbsPath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -29,6 +29,12 @@ type Task interface {
|
|||||||
Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error)
|
Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PreRunner interface {
|
||||||
|
Task
|
||||||
|
|
||||||
|
PreRun(ctx context.Context, pctx *plancontext.Context, v *compiler.Value) error
|
||||||
|
}
|
||||||
|
|
||||||
// Register a task type and initializer
|
// Register a task type and initializer
|
||||||
func Register(typ string, f NewFunc) {
|
func Register(typ string, f NewFunc) {
|
||||||
tasks.Store(typ, f)
|
tasks.Store(typ, f)
|
||||||
|
@ -62,7 +62,7 @@ setup() {
|
|||||||
cd "$TESTDIR"
|
cd "$TESTDIR"
|
||||||
run "$DAGGER" --europa up ./plan/inputs/directories/conflicting_values.cue
|
run "$DAGGER" --europa up ./plan/inputs/directories/conflicting_values.cue
|
||||||
assert_failure
|
assert_failure
|
||||||
assert_output --partial 'failed to up environment: actions.verify.contents: conflicting values "local directory" and "local dfsadf"'
|
assert_output --partial 'conflicting values "local directory" and "local dfsadf"'
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "plan/inputs/secrets exec" {
|
@test "plan/inputs/secrets exec" {
|
||||||
|
Reference in New Issue
Block a user