Merge pull request #1704 from helderco/inputs-outputs-cleanup

Inputs/outputs cleanup
This commit is contained in:
Andrea Luzzardi
2022-03-08 16:16:15 -08:00
committed by GitHub
54 changed files with 101 additions and 1083 deletions

View File

@@ -1,98 +0,0 @@
package task
import (
"context"
"errors"
"fmt"
"os"
"github.com/moby/buildkit/client/llb"
"github.com/rs/zerolog/log"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver"
)
func init() {
Register("InputDirectory", func() Task { return &inputDirectoryTask{} })
}
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) {
path, err := v.Lookup("path").AbsPath()
if err != nil {
return nil, err
}
var dir struct {
Include []string
Exclude []string
}
if err := v.Decode(&dir); err != nil {
return nil, err
}
lg := log.Ctx(ctx)
lg.Debug().Str("path", path).Msg("loading local directory")
opts := []llb.LocalOption{
withCustomName(v, "Local %s", path),
// Without hint, multiple `llb.Local` operations on the
// same path get a different digest.
llb.SessionID(s.SessionID()),
llb.SharedKeyHint(path),
}
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(
path,
opts...,
),
"/",
"/",
),
withCustomName(v, "Local %s [copy]", path),
)
result, err := s.Solve(ctx, st, pctx.Platform.Get())
if err != nil {
return nil, err
}
fs := pctx.FS.New(result)
return compiler.NewValue().FillFields(map[string]interface{}{
"contents": fs.MarshalCUE(),
})
}

View File

@@ -1,49 +0,0 @@
package task
import (
"context"
"fmt"
"os"
"strings"
"github.com/rs/zerolog/log"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver"
)
func init() {
Register("InputSecretEnv", func() Task { return &inputSecretEnvTask{} })
}
type inputSecretEnvTask struct {
}
func (c *inputSecretEnvTask) Run(ctx context.Context, pctx *plancontext.Context, _ solver.Solver, v *compiler.Value) (*compiler.Value, error) {
lg := log.Ctx(ctx)
var secretEnv struct {
Envvar string
TrimSpace bool
}
if err := v.Decode(&secretEnv); err != nil {
return nil, err
}
lg.Debug().Str("envvar", secretEnv.Envvar).Str("trimSpace", fmt.Sprintf("%t", secretEnv.TrimSpace)).Msg("loading secret")
env := os.Getenv(secretEnv.Envvar)
if env == "" {
return nil, fmt.Errorf("environment variable %q not set", secretEnv.Envvar)
}
if secretEnv.TrimSpace {
env = strings.TrimSpace(env)
}
secret := pctx.Secrets.New(env)
return compiler.NewValue().FillFields(map[string]interface{}{
"contents": secret.MarshalCUE(),
})
}

View File

@@ -1,70 +0,0 @@
package task
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"github.com/rs/zerolog/log"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver"
)
func init() {
Register("InputSecretExec", func() Task { return &inputSecretExecTask{} })
}
type inputSecretExecTask struct {
}
func (c *inputSecretExecTask) Run(ctx context.Context, pctx *plancontext.Context, _ solver.Solver, v *compiler.Value) (*compiler.Value, error) {
var secretExec struct {
Command struct {
Name string
Args []string
}
TrimSpace bool
}
if err := v.Decode(&secretExec); err != nil {
return nil, err
}
lg := log.Ctx(ctx)
lg.Debug().Str("name", secretExec.Command.Name).Str("args", strings.Join(secretExec.Command.Args, " ")).Str("trimSpace", fmt.Sprintf("%t", secretExec.TrimSpace)).Msg("loading secret")
var err error
//#nosec G204: sec audited by @aluzzardi and @mrjones
cmd := exec.CommandContext(ctx, secretExec.Command.Name, secretExec.Command.Args...)
cmd.Env = os.Environ()
cmd.Dir, err = v.Lookup("command.name").Dirname()
if err != nil {
return nil, err
}
// sec audited by @aluzzardi and @mrjones
out, err := cmd.Output()
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
lg.Err(err).Msg(string(exitErr.Stderr))
}
return nil, err
}
plaintext := string(out)
if secretExec.TrimSpace {
plaintext = strings.TrimSpace(plaintext)
}
secret := pctx.Secrets.New(plaintext)
return compiler.NewValue().FillFields(map[string]interface{}{
"contents": secret.MarshalCUE(),
})
}

View File

@@ -1,49 +0,0 @@
package task
import (
"context"
"os"
"strings"
"github.com/rs/zerolog/log"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver"
)
func init() {
Register("InputSecretFile", func() Task { return &inputSecretFileTask{} })
}
type inputSecretFileTask struct {
}
func (c *inputSecretFileTask) Run(ctx context.Context, pctx *plancontext.Context, _ solver.Solver, v *compiler.Value) (*compiler.Value, error) {
lg := log.Ctx(ctx)
path, err := v.Lookup("path").AbsPath()
if err != nil {
return nil, err
}
lg.Debug().Str("path", path).Msg("loading secret")
fileBytes, err := os.ReadFile(path)
if err != nil {
return nil, err
}
plaintext := string(fileBytes)
trimSpace, err := v.Lookup("trimSpace").Bool()
if err != nil {
return nil, err
}
if trimSpace {
plaintext = strings.TrimSpace(plaintext)
}
secret := pctx.Secrets.New(plaintext)
return compiler.NewValue().FillFields(map[string]interface{}{
"contents": secret.MarshalCUE(),
})
}

View File

@@ -1,43 +0,0 @@
package task
import (
"context"
bk "github.com/moby/buildkit/client"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver"
)
func init() {
Register("OutputDirectory", func() Task { return &outputDirectoryTask{} })
}
type outputDirectoryTask struct {
}
func (c outputDirectoryTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) {
contents, err := pctx.FS.FromValue(v.Lookup("contents"))
if err != nil {
return nil, err
}
dest, err := v.Lookup("dest").AbsPath()
if err != nil {
return nil, err
}
st, err := contents.State()
if err != nil {
return nil, err
}
_, err = s.Export(ctx, st, nil, bk.ExportEntry{
Type: bk.ExporterLocal,
OutputDir: dest,
}, pctx.Platform.Get())
if err != nil {
return nil, err
}
return compiler.NewValue(), nil
}

View File

@@ -1,63 +0,0 @@
package task
import (
"context"
"fmt"
"io/fs"
"os"
"cuelang.org/go/cue"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver"
)
func init() {
Register("OutputFile", func() Task { return &outputFileTask{} })
}
type outputFileTask struct {
}
func (c outputFileTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) {
var contents []byte
var err error
switch kind := v.Lookup("contents").Kind(); kind {
case cue.StringKind:
var str string
str, err = v.Lookup("contents").String()
if err == nil {
contents = []byte(str)
}
case cue.BottomKind:
err = fmt.Errorf("contents is not set")
default:
err = fmt.Errorf("unhandled data type in contents: %s", kind)
}
if err != nil {
return nil, err
}
dest, err := v.Lookup("dest").AbsPath()
if err != nil {
return nil, err
}
perm := fs.FileMode(0644) // default permission
if v.Lookup("permissions").Exists() {
permissions, err := v.Lookup("permissions").Int64()
if err != nil {
return nil, err
}
perm = fs.FileMode(permissions)
}
err = os.WriteFile(dest, contents, perm)
if err != nil {
return nil, err
}
return compiler.NewValue(), nil
}

View File

@@ -1,58 +0,0 @@
package task
import (
"context"
"errors"
"cuelang.org/go/cue"
"github.com/rs/zerolog/log"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver"
)
func init() {
Register("ProxyEndpoint", func() Task { return &proxyEndpointTask{} })
}
type proxyEndpointTask struct {
}
func (c *proxyEndpointTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) {
var unix, npipe string
var stringErr error
unixV := v.Lookup("unix")
npipeV := v.Lookup("npipe")
if unixV.Exists() && unixV.IsConcrete() {
unix, stringErr = unixV.String()
}
if npipeV.Exists() && npipeV.IsConcrete() {
npipe, stringErr = npipeV.String()
}
if stringErr != nil {
return nil, stringErr
}
if unix == "" && npipe == "" {
return nil, errors.New("invalid service")
}
lg := log.Ctx(ctx)
if unix != "" {
lg.Debug().Str("unix", unix).Msg("loading service")
} else if npipe != "" {
lg.Debug().Str("npipe", npipe).Msg("loading service")
}
service := pctx.Services.New(unix, npipe)
out := compiler.NewValue()
if err := out.FillPath(cue.ParsePath("service"), service.MarshalCUE()); err != nil {
return nil, err
}
return out, nil
}