Merge pull request #1068 from aluzzardi/fix-localdirs

Fix local directory error handling
This commit is contained in:
Sam Alba 2021-10-20 17:38:36 -07:00 committed by GitHub
commit bc3e8658ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 106 additions and 126 deletions

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings" "strings"
"sync" "sync"
@ -116,14 +115,10 @@ func (c *Client) buildfn(ctx context.Context, st *state.State, env *environment.
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
// Scan local dirs to grant access // Scan local dirs to grant access
localdirs := env.LocalDirs() localdirs, err := env.LocalDirs()
for label, dir := range localdirs {
abs, err := filepath.Abs(dir)
if err != nil { if err != nil {
return err return err
} }
localdirs[label] = abs
}
// buildkit auth provider (registry) // buildkit auth provider (registry)
auth := solver.NewRegistryAuthProvider() auth := solver.NewRegistryAuthProvider()

View File

@ -3,6 +3,7 @@ package environment
import ( import (
"context" "context"
"fmt" "fmt"
"path/filepath"
"cuelang.org/go/cue" "cuelang.org/go/cue"
cueflow "cuelang.org/go/tools/flow" cueflow "cuelang.org/go/tools/flow"
@ -75,10 +76,11 @@ func (e *Environment) Computed() *compiler.Value {
// and return all referenced directory names. // and return all referenced directory names.
// This is used by clients to grant access to local directories when they are referenced // This is used by clients to grant access to local directories when they are referenced
// by user-specified scripts. // by user-specified scripts.
func (e *Environment) LocalDirs() map[string]string { func (e *Environment) LocalDirs() (map[string]string, error) {
dirs := map[string]string{} dirs := map[string]string{}
localdirs := func(code *compiler.Value) {
Analyze( localdirs := func(code *compiler.Value) error {
return Analyze(
func(op *compiler.Value) error { func(op *compiler.Value) error {
do, err := op.Lookup("do").String() do, err := op.Lookup("do").String()
if err != nil { if err != nil {
@ -91,7 +93,12 @@ func (e *Environment) LocalDirs() map[string]string {
if err != nil { if err != nil {
return err return err
} }
dirs[dir] = dir abs, err := filepath.Abs(dir)
if err != nil {
return err
}
dirs[dir] = abs
return nil return nil
}, },
code, code,
@ -101,10 +108,10 @@ func (e *Environment) LocalDirs() map[string]string {
// FIXME: use a common `flow` instance to avoid rescanning the tree. // FIXME: use a common `flow` instance to avoid rescanning the tree.
src := compiler.NewValue() src := compiler.NewValue()
if err := src.FillPath(cue.MakePath(), e.plan); err != nil { if err := src.FillPath(cue.MakePath(), e.plan); err != nil {
return nil return nil, err
} }
if err := src.FillPath(cue.MakePath(), e.input); err != nil { if err := src.FillPath(cue.MakePath(), e.input); err != nil {
return nil return nil, err
} }
flow := cueflow.New( flow := cueflow.New(
&cueflow.Config{}, &cueflow.Config{},
@ -112,11 +119,12 @@ func (e *Environment) LocalDirs() map[string]string {
newTaskFunc(noOpRunner), newTaskFunc(noOpRunner),
) )
for _, t := range flow.Tasks() { for _, t := range flow.Tasks() {
v := compiler.Wrap(t.Value()) if err := localdirs(compiler.Wrap(t.Value())); err != nil {
localdirs(v.Lookup("#up")) return nil, err
}
} }
return dirs return dirs, nil
} }
// Up missing values in environment configuration, and write them to state. // Up missing values in environment configuration, and write them to state.

View File

@ -113,7 +113,7 @@ func ops(code *compiler.Value) ([]*compiler.Value, error) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
return nil, fmt.Errorf("not executable: %s", source) return nil, fmt.Errorf("not executable: %s (%s)", source, code.Path().String())
} }
return ops, nil return ops, nil
} }
@ -121,7 +121,9 @@ func ops(code *compiler.Value) ([]*compiler.Value, error) {
func Analyze(fn func(*compiler.Value) error, code *compiler.Value) error { func Analyze(fn func(*compiler.Value) error, code *compiler.Value) error {
ops, err := ops(code) ops, err := ops(code)
if err != nil { if err != nil {
return err // Ignore CUE errors when analyzing. This might be because the value is
// not concrete since static analysis runs before pipelines are executed.
return nil
} }
for _, op := range ops { for _, op := range ops {
if err := analyzeOp(fn, op); err != nil { if err := analyzeOp(fn, op); err != nil {

View File

@ -205,9 +205,6 @@ import (
} }
} }
"mount": { "mount": {
if ssh == _|_ {
"/var/run/docker.sock": from: "docker.sock"
}
if ssh != _|_ { if ssh != _|_ {
if ssh.key != _|_ { if ssh.key != _|_ {
"/key": secret: ssh.key "/key": secret: ssh.key

View File

@ -144,6 +144,9 @@ setup() {
# Copy deployment to sandbox # Copy deployment to sandbox
copy_to_sandbox kubernetes-deployment kubernetes copy_to_sandbox kubernetes-deployment kubernetes
# Query
dagger --project "$DAGGER_SANDBOX" -e kubernetes-deployment query
# Set kubeconfig # Set kubeconfig
dagger --project "$DAGGER_SANDBOX" -e kubernetes-deployment input text TestKubeconfig -f "$HOME"/.kube/config dagger --project "$DAGGER_SANDBOX" -e kubernetes-deployment input text TestKubeconfig -f "$HOME"/.kube/config

View File

@ -1,23 +1,22 @@
package testing package testing
import "alpha.dagger.io/dagger/op"
A: { A: {
result: string result: string
#up: [ #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "alpine" ref: "alpine"
}, },
{ op.#Exec & {
do: "exec"
args: ["sh", "-c", """ args: ["sh", "-c", """
echo '{"result": "from A"}' > /tmp/out echo '{"result": "from A"}' > /tmp/out
""", """,
] ]
dir: "/" dir: "/"
}, },
{ op.#Export & {
do: "export"
// Source path in the container // Source path in the container
source: "/tmp/out" source: "/tmp/out"
format: "json" format: "json"
@ -29,20 +28,17 @@ B: {
result: string result: string
#up: [ #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "alpine" ref: "alpine"
}, },
{ op.#Exec & {
do: "exec"
args: ["sh", "-c", """ args: ["sh", "-c", """
echo "{\\"result\\": \\"dependency \(A.result)\\"}" > /tmp/out echo "{\\"result\\": \\"dependency \(A.result)\\"}" > /tmp/out
""", """,
] ]
dir: "/" dir: "/"
}, },
{ op.#Export & {
do: "export"
// Source path in the container // Source path in the container
source: "/tmp/out" source: "/tmp/out"
format: "json" format: "json"

View File

@ -1,23 +1,21 @@
package testing package testing
import "alpha.dagger.io/dagger/op"
A: { A: {
result: string result: string
#up: [ #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "alpine" ref: "alpine"
}, },
{ op.#Exec & {
do: "exec"
args: ["sh", "-c", """ args: ["sh", "-c", """
echo '{"result": "from A"}' > /tmp/out echo '{"result": "from A"}' > /tmp/out
""", """,
] ]
dir: "/"
}, },
{ op.#Export & {
do: "export"
// Source path in the container // Source path in the container
source: "/tmp/out" source: "/tmp/out"
format: "json" format: "json"
@ -29,12 +27,10 @@ B: {
result: string result: string
#up: [ #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "alpine" ref: "alpine"
}, },
{ op.#Exec & {
do: "exec"
env: DATA: A.result env: DATA: A.result
args: ["sh", "-c", """ args: ["sh", "-c", """
echo "{\\"result\\": \\"dependency $DATA\\"}" > /tmp/out echo "{\\"result\\": \\"dependency $DATA\\"}" > /tmp/out
@ -42,8 +38,7 @@ B: {
] ]
dir: "/" dir: "/"
}, },
{ op.#Export & {
do: "export"
// Source path in the container // Source path in the container
source: "/tmp/out" source: "/tmp/out"
format: "json" format: "json"

View File

@ -1,25 +1,25 @@
package testing package testing
import "encoding/json" import (
"encoding/json"
"alpha.dagger.io/dagger/op"
)
A: { A: {
string string
#up: [ #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "alpine" ref: "alpine"
}, },
{ op.#Exec & {
do: "exec"
args: ["sh", "-c", """ args: ["sh", "-c", """
echo '{"hello": "world"}' > /tmp/out echo '{"hello": "world"}' > /tmp/out
""", """,
] ]
dir: "/" dir: "/"
}, },
{ op.#Export & {
do: "export"
// Source path in the container // Source path in the container
source: "/tmp/out" source: "/tmp/out"
format: "string" format: "string"
@ -33,20 +33,17 @@ B: {
result: string result: string
#up: [ #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "alpine" ref: "alpine"
}, },
{ op.#Exec & {
do: "exec"
args: ["sh", "-c", """ args: ["sh", "-c", """
echo "{\\"result\\": \\"unmarshalled.hello=\(unmarshalled.hello)\\"}" > /tmp/out echo "{\\"result\\": \\"unmarshalled.hello=\(unmarshalled.hello)\\"}" > /tmp/out
""", """,
] ]
dir: "/" dir: "/"
}, },
{ op.#Export & {
do: "export"
// Source path in the container // Source path in the container
source: "/tmp/out" source: "/tmp/out"
format: "json" format: "json"

View File

@ -1,25 +1,24 @@
package testing package testing
import "alpha.dagger.io/dagger/op"
X1=in: string | *"default input" X1=in: string | *"default input"
test: { test: {
string string
#up: [ #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "alpine" ref: "alpine"
}, },
{ op.#Exec & {
do: "exec"
args: ["sh", "-c", """ args: ["sh", "-c", """
echo -n "received: \(X1)" > /out echo -n "received: \(X1)" > /out
"""] """]
// XXX Blocked by https://github.com/blocklayerhq/dagger/issues/19 // XXX Blocked by https://github.com/blocklayerhq/dagger/issues/19
dir: "/" dir: "/"
}, },
{ op.#Export & {
do: "export"
source: "/out" source: "/out"
format: "string" format: "string"
}, },

View File

@ -1,25 +1,24 @@
package testing package testing
import "alpha.dagger.io/dagger/op"
X1=in: string X1=in: string
test: { test: {
string string
#up: [ #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "alpine" ref: "alpine"
}, },
{ op.#Exec & {
do: "exec"
args: ["sh", "-c", """ args: ["sh", "-c", """
echo -n "received: \(X1)" > /out echo -n "received: \(X1)" > /out
"""] """]
// XXX Blocked by https://github.com/blocklayerhq/dagger/issues/19 // XXX Blocked by https://github.com/blocklayerhq/dagger/issues/19
dir: "/" dir: "/"
}, },
{ op.#Export & {
do: "export"
source: "/out" source: "/out"
format: "string" format: "string"
}, },

View File

@ -1,17 +1,19 @@
package testing package testing
import "alpha.dagger.io/dagger/op"
new_prop: "lala" new_prop: "lala"
#new_def: "lala" #new_def: "lala"
new_prop_too: string new_prop_too: string
#new_def_too: string #new_def_too: string
#up: [{ #up: [
do: "fetch-container" op.#FetchContainer & {
ref: "busybox" ref: "busybox"
}, },
{ op.#Exec & {
do: "exec"
args: ["true"] args: ["true"]
dir: "/" dir: "/"
}] },
]

View File

@ -1,5 +1,7 @@
package testing package testing
import "alpha.dagger.io/dagger/op"
foo: { foo: {
new_prop: "lala" new_prop: "lala"
#new_def: "lala" #new_def: "lala"
@ -7,13 +9,13 @@ foo: {
new_prop_too: string new_prop_too: string
#new_def_too: string #new_def_too: string
#up: [{ #up: [
do: "fetch-container" op.#FetchContainer & {
ref: "busybox" ref: "busybox"
}, },
{ op.#Exec & {
do: "exec"
args: ["true"] args: ["true"]
dir: "/" dir: "/"
}] },
]
} }

View File

@ -1,12 +1,12 @@
package testing package testing
import "alpha.dagger.io/dagger/op"
#up: [ #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "busybox" ref: "busybox"
}, },
{ op.#Exec & {
do: "exec"
args: ["true"] args: ["true"]
dir: "/" dir: "/"
}, },

View File

@ -1,15 +1,14 @@
package def package def
import "alpha.dagger.io/dagger/op"
#dang: string #dang: string
#up: [ #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "alpine" ref: "alpine"
}, },
{ op.#Exec & {
do: "exec"
dir: "/"
args: ["sh", "-c", """ args: ["sh", "-c", """
echo success echo success
"""] """]

View File

@ -1,15 +1,14 @@
package nonoptional package nonoptional
import "alpha.dagger.io/dagger/op"
dang: string dang: string
#up: [ #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "alpine" ref: "alpine"
}, },
{ op.#Exec & {
do: "exec"
dir: "/"
args: ["sh", "-c", """ args: ["sh", "-c", """
echo "This test SHOULD fail, because this SHOULD be executed" echo "This test SHOULD fail, because this SHOULD be executed"
exit 1 exit 1

View File

@ -1,15 +1,14 @@
package optional package optional
import "alpha.dagger.io/dagger/op"
dang?: string dang?: string
#up: [ #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "alpine" ref: "alpine"
}, },
{ op.#Exec & {
do: "exec"
dir: "/"
args: ["sh", "-c", """ args: ["sh", "-c", """
echo success echo success
"""] """]

View File

@ -44,20 +44,16 @@ TestExportList: {
[...string] [...string]
#up: [ #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "alpine" ref: "alpine"
}, },
{ op.#Exec & {
do: "exec"
args: ["sh", "-c", """ args: ["sh", "-c", """
echo '["milk", "pumpkin pie", "eggs", "juice"]' > /tmp/out echo '["milk", "pumpkin pie", "eggs", "juice"]' > /tmp/out
""", """,
] ]
dir: "/"
}, },
{ op.#Export & {
do: "export"
// Source path in the container // Source path in the container
source: "/tmp/out" source: "/tmp/out"
format: "json" format: "json"

View File

@ -27,22 +27,17 @@ TestExportList: {
[...string] [...string]
#up: [ #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "alpine" ref: "alpine"
}, },
{ op.#Exec & {
do: "exec"
args: ["sh", "-c", """ args: ["sh", "-c", """
echo "--- # Shopping list echo "--- # Shopping list
[milk, pumpkin pie, eggs, juice]" > /tmp/out [milk, pumpkin pie, eggs, juice]" > /tmp/out
""", """,
] ]
// XXX Blocked by https://github.com/blocklayerhq/dagger/issues/19
dir: "/"
}, },
{ op.#Export & {
do: "export"
// Source path in the container // Source path in the container
source: "/tmp/out" source: "/tmp/out"
format: "yaml" format: "yaml"
@ -51,19 +46,16 @@ TestExportList: {
} }
TestExportMap: #up: [ TestExportMap: #up: [
{ op.#FetchContainer & {
do: "fetch-container"
ref: "alpine" ref: "alpine"
}, },
{ op.#Exec & {
do: "exec"
args: ["sh", "-c", """ args: ["sh", "-c", """
echo something: something > /tmp/out echo something: something > /tmp/out
""", """,
] ]
}, },
{ op.#Export & {
do: "export"
// Source path in the container // Source path in the container
source: "/tmp/out" source: "/tmp/out"
format: "yaml" format: "yaml"