Skip to content

Commit

Permalink
core: #Mount supports inline file
Browse files Browse the repository at this point in the history
It was really painful to mount a simple file using `type: fs` because it
creates some verbose intermediate step.
With this update, it is not possible to directly mount a file as string.

Signed-off-by: Vasek - Tom C <[email protected]>
  • Loading branch information
TomChv committed May 16, 2022
1 parent 5c78a0e commit 209f6d5
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/rs/zerolog v1.26.1
github.com/sergi/go-diff v1.2.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.11.0
Expand Down
4 changes: 4 additions & 0 deletions pkg/dagger.io/dagger/core/exec.cue
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ import "dagger.io/dagger"
uid: int | *0
gid: int | *0
mask: int | *0o400
} | {
type: "file"
contents: string
permissions: *0o644 | int
}
}

Expand Down
44 changes: 41 additions & 3 deletions plan/task/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"net"
"strings"

Expand Down Expand Up @@ -88,9 +89,9 @@ func (t *execTask) Run(ctx context.Context, pctx *plancontext.Context, s *solver
}

// Fill result
fs := pctx.FS.New(result)
resultFS := pctx.FS.New(result)
return compiler.NewValue().FillFields(map[string]interface{}{
"output": fs.MarshalCUE(),
"output": resultFS.MarshalCUE(),
"exit": 0,
})
}
Expand Down Expand Up @@ -144,7 +145,7 @@ func (t *asyncExecTask) Run(ctx context.Context, pctx *plancontext.Context, s *s
type stopAsyncExecTask struct {
}

func (t *stopAsyncExecTask) Run(ctx context.Context, pctx *plancontext.Context, s *solver.Solver, v *compiler.Value) (*compiler.Value, error) {
func (t *stopAsyncExecTask) Run(ctx context.Context, _ *plancontext.Context, s *solver.Solver, v *compiler.Value) (*compiler.Value, error) {
ctrID, err := v.LookupPath(cue.MakePath(cue.Str("input"), cue.Hid("_id", pkg.DaggerPackage))).String()
if err != nil {
return nil, err
Expand Down Expand Up @@ -401,6 +402,28 @@ func parseMount(pctx *plancontext.Context, v *compiler.Value) (mount, error) {
},
}, nil

case "file":
contents, err := v.Lookup("contents").String()
if err != nil {
return mount{}, err
}

opts := struct {
Permissions uint32
}{}

if err := v.Decode(&opts); err != nil {
return mount{}, err
}

return mount{
dest: dest,
fileMount: &fileMount{
contents: contents,
permissions: opts.Permissions,
},
}, nil

case "":
return mount{}, errors.New("no mount type specified")
default:
Expand All @@ -416,6 +439,7 @@ type mount struct {
socketMount *socketMount
fsMount *fsMount
secretMount *secretMount
fileMount *fileMount
}

func (m mount) runOpt() (llb.RunOption, error) {
Expand Down Expand Up @@ -461,6 +485,15 @@ func (m mount) runOpt() (llb.RunOption, error) {
llb.SecretID(m.secretMount.id),
llb.SecretFileOpt(int(m.secretMount.uid), int(m.secretMount.gid), int(m.secretMount.mask)),
), nil
case m.fileMount != nil:
return llb.AddMount(
m.dest,
llb.Scratch().File(llb.Mkfile(
"/file",
fs.FileMode(m.fileMount.permissions),
[]byte(m.fileMount.contents))),
llb.SourcePath("/file"),
), nil
}
return nil, fmt.Errorf("no mount type set")
}
Expand Down Expand Up @@ -545,3 +578,8 @@ type secretMount struct {
gid uint32
mask uint32
}

type fileMount struct {
contents string
permissions uint32
}
5 changes: 5 additions & 0 deletions tests/tasks.bats
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ setup() {
"$DAGGER" "do" -p ./mount_socket.cue verify
}

@test "task: #Exec mount file" {
cd ./tasks/exec
"$DAGGER" "do" -p ./mount_file.cue test
}

@test "task: #Exec user" {
cd ./tasks/exec
"$DAGGER" "do" -p ./user.cue test
Expand Down
37 changes: 37 additions & 0 deletions tests/tasks/exec/mount_file.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"dagger.io/dagger"
"dagger.io/dagger/core"
)

dagger.#Plan & {
actions: {
image: core.#Pull & {
source: "alpine:3.15.0@sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3"
}

test: {
write: core.#Exec & {
input: image.output
mounts: file: {
dest: "/test.txt"
contents: "hello world"
}
args: [
"sh", "-c", """
ls
cat /test.txt >> /result.txt
""",
]
}

verify: core.#ReadFile & {
input: write.output
path: "/result.txt"
} & {
contents: "hello world"
}
}
}
}

0 comments on commit 209f6d5

Please sign in to comment.