dagger.#FS support

- Implement dagger.#FS support
- Migrate `context.imports` to dagger.#FS
- Backward compat: dagger.#FS can be passed in lieu of a
  dagger.#Artifact
- For instance, an import (`dagger.#FS`) can be passed to the current
  `yarn.#Package` implementation

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2021-11-30 12:48:09 -08:00
parent 6bedfb7c63
commit 0aea10d23e
23 changed files with 359 additions and 135 deletions

View File

@@ -3,7 +3,6 @@ package plan
import (
"context"
"fmt"
"path/filepath"
"strings"
"time"
@@ -35,10 +34,16 @@ func Load(ctx context.Context, path, pkg string) (*Plan, error) {
return nil, err
}
return &Plan{
p := &Plan{
context: plancontext.New(),
source: v,
}, nil
}
if err := p.registerLocalDirs(); err != nil {
return nil, err
}
return p, nil
}
func (p *Plan) Context() *plancontext.Context {
@@ -49,30 +54,26 @@ func (p *Plan) Source() *compiler.Value {
return p.source
}
// LocalDirectories scans the context for local imports.
// registerLocalDirectories scans the context for local imports.
// BuildKit requires to known the list of directories ahead of time.
func (p *Plan) LocalDirectories() (map[string]string, error) {
dirs := map[string]string{}
func (p *Plan) registerLocalDirs() error {
imports, err := p.source.Lookup("context.imports").Fields()
if err != nil {
return nil, err
return err
}
for _, v := range imports {
dir, err := v.Value.Lookup("path").String()
if err != nil {
return nil, err
}
abs, err := filepath.Abs(dir)
if err != nil {
return nil, err
return err
}
dirs[dir] = abs
p.context.LocalDirs.Register(&plancontext.LocalDir{
Path: dir,
})
}
return dirs, nil
return nil
}
// Up executes the plan

View File

@@ -2,9 +2,9 @@ package task
import (
"context"
"fmt"
"os"
"cuelang.org/go/cue"
"github.com/moby/buildkit/client/llb"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver"
@@ -17,21 +17,64 @@ func init() {
type importTask struct {
}
func (c importTask) Run(ctx context.Context, pctx *plancontext.Context, _ solver.Solver, v *compiler.Value) (*compiler.Value, error) {
var dir *plancontext.Directory
func (c importTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) {
var dir struct {
Path string
Include []string
Exclude []string
}
if err := v.Decode(&dir); err != nil {
return nil, err
}
// Check that directory exists
if _, err := os.Stat(dir.Path); os.IsNotExist(err) {
return nil, fmt.Errorf("%q dir doesn't exist", dir.Path)
opts := []llb.LocalOption{
withCustomName(v, "Local %s", dir.Path),
// Without hint, multiple `llb.Local` operations on the
// same path get a different digest.
llb.SessionID(s.SessionID()),
llb.SharedKeyHint(dir.Path),
}
id := pctx.Directories.Register(dir)
return compiler.Compile("", fmt.Sprintf(
`fs: #up: [{do: "local", id: %q}]`,
id,
))
if len(dir.Include) > 0 {
opts = append(opts, llb.IncludePatterns(dir.Include))
}
// Excludes .dagger directory by default
excludePatterns := []string{"**/.dagger/"}
if len(dir.Exclude) > 0 {
excludePatterns = dir.Exclude
}
opts = append(opts, llb.ExcludePatterns(excludePatterns))
// FIXME: Remove the `Copy` and use `Local` directly.
//
// Copy'ing is a costly operation which should be unnecessary.
// However, using llb.Local directly breaks caching sometimes for unknown reasons.
st := llb.Scratch().File(
llb.Copy(
llb.Local(
dir.Path,
opts...,
),
"/",
"/",
),
withCustomName(v, "Local %s [copy]", dir.Path),
)
result, err := s.Solve(ctx, st, pctx.Platform.Get())
if err != nil {
return nil, err
}
id := pctx.FS.Register(&plancontext.FS{
Result: result,
})
return compiler.NewValueWithContent(id,
cue.Str("fs"),
cue.Hid("_fs", "alpha.dagger.io/dagger"),
cue.Str("id"),
)
}

View File

@@ -40,9 +40,8 @@ func (c secretEnvTask) Run(ctx context.Context, pctx *plancontext.Context, _ sol
PlainText: env,
})
out := compiler.NewValue()
if err := out.FillPath(cue.ParsePath("contents.id"), id); err != nil {
return nil, err
}
return out, nil
return compiler.NewValueWithContent(id,
cue.Str("contents"),
cue.Str("id"),
)
}

View File

@@ -39,9 +39,8 @@ func (c secretFileTask) Run(ctx context.Context, pctx *plancontext.Context, _ so
PlainText: string(data),
})
out := compiler.NewValue()
if err := out.FillPath(cue.ParsePath("contents.id"), id); err != nil {
return nil, err
}
return out, nil
return compiler.NewValueWithContent(id,
cue.Str("contents"),
cue.Str("id"),
)
}

14
plan/task/util.go Normal file
View File

@@ -0,0 +1,14 @@
package task
import (
"fmt"
"github.com/moby/buildkit/client/llb"
"go.dagger.io/dagger/compiler"
)
func withCustomName(v *compiler.Value, format string, a ...interface{}) llb.ConstraintsOpt {
prefix := fmt.Sprintf("@%s@", v.Path().String())
name := fmt.Sprintf(format, a...)
return llb.WithCustomName(prefix + " " + name)
}