bash.#Run: new and improved
* Script is written to filesystem, not inlined as argument * Pass script either as string, or directory+filename Signed-off-by: Solomon Hykes <solomon@dagger.io>
This commit is contained in:
@@ -2,25 +2,59 @@
|
||||
package bash
|
||||
|
||||
import (
|
||||
"dagger.io/dagger"
|
||||
"dagger.io/dagger/engine"
|
||||
|
||||
"universe.dagger.io/docker"
|
||||
)
|
||||
|
||||
// Run a bash command or script in a container
|
||||
// Run a bash script in a Docker container
|
||||
// Since this is a thin wrapper over docker.#Run, we embed it.
|
||||
// Whether to embed or wrap is a case-by-case decision, like in Go.
|
||||
#Run: {
|
||||
// Contents of the bash script
|
||||
script: string
|
||||
// The script to execute
|
||||
script: {
|
||||
// A directory containing one or more bash scripts
|
||||
directory: dagger.#FS
|
||||
|
||||
// Name of the file to execute
|
||||
filename: string
|
||||
|
||||
_directory: directory
|
||||
_filename: filename
|
||||
} | {
|
||||
// Script contents
|
||||
contents: string
|
||||
|
||||
_filename: "run.sh"
|
||||
_write: engine.#WriteFile & {
|
||||
input: engine.#Scratch
|
||||
path: _filename
|
||||
"contents": contents
|
||||
}
|
||||
_directory: _write.output
|
||||
}
|
||||
|
||||
// Arguments to the script
|
||||
args: [...string]
|
||||
|
||||
// Where in the container to mount the scripts directory
|
||||
_mountpoint: "/bash/scripts"
|
||||
|
||||
// FIXME: don't pass the script as argument: write to filesystme instead
|
||||
docker.#Run & {
|
||||
command: {
|
||||
name: "bash"
|
||||
name: "bash"
|
||||
"args": ["\(_mountpoint)/\(script._filename)"] + args
|
||||
// FIXME: make default flags overrideable
|
||||
flags: {
|
||||
"-c": script
|
||||
"--noprofile": true
|
||||
"--norc": true
|
||||
"-e": true
|
||||
"-o": "pipefail"
|
||||
"--norc": true
|
||||
"-e": true
|
||||
"-o": "pipefail"
|
||||
}
|
||||
}
|
||||
mounts: "Bash scripts": {
|
||||
contents: script._directory
|
||||
dest: _mountpoint
|
||||
}
|
||||
}
|
||||
}
|
||||
|
10
pkg/universe.dagger.io/bash/test/bash.bats
Normal file
10
pkg/universe.dagger.io/bash/test/bash.bats
Normal file
@@ -0,0 +1,10 @@
|
||||
setup() {
|
||||
load '../../bats_helpers'
|
||||
|
||||
common_setup
|
||||
}
|
||||
|
||||
@test "bash.#Run" {
|
||||
dagger up ./run-simple
|
||||
}
|
||||
|
3
pkg/universe.dagger.io/bash/test/run-simple/hello.sh
Normal file
3
pkg/universe.dagger.io/bash/test/run-simple/hello.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
echo Hello, world > /out.txt
|
87
pkg/universe.dagger.io/bash/test/run-simple/simple.cue
Normal file
87
pkg/universe.dagger.io/bash/test/run-simple/simple.cue
Normal file
@@ -0,0 +1,87 @@
|
||||
package bash
|
||||
|
||||
import (
|
||||
"dagger.io/dagger"
|
||||
|
||||
"universe.dagger.io/docker"
|
||||
"universe.dagger.io/alpine"
|
||||
)
|
||||
|
||||
dagger.#DAG & {
|
||||
actions: {
|
||||
"Run from source directory": {
|
||||
build: alpine.#Build & {
|
||||
packages: bash: _
|
||||
}
|
||||
run: #Run & {
|
||||
image: build.output
|
||||
script: {
|
||||
directory: loadScripts.output
|
||||
filename: "hello.sh"
|
||||
}
|
||||
export: files: "/out.txt": _
|
||||
}
|
||||
output: run.export.files."/out.txt".contents & "Hello, world\n"
|
||||
}
|
||||
|
||||
"Run from source directory with custom image": {
|
||||
debian: docker.#Pull & {
|
||||
source: "index.docker.io/debian"
|
||||
}
|
||||
run: #Run & {
|
||||
image: debian.output
|
||||
export: files: "/out.txt": _
|
||||
script: {
|
||||
directory: loadScripts.output
|
||||
filename: "hello.sh"
|
||||
}
|
||||
}
|
||||
output: run.export.files."/out.txt".contents & "Hello, world\n"
|
||||
}
|
||||
|
||||
"Run from string": {
|
||||
run: #Run & {
|
||||
script: contents: "echo 'Hello, inlined world!' > /output.txt"
|
||||
export: files: "/output.txt": _
|
||||
}
|
||||
output: run.export.files."/output.txt".contents & "Hello, inlined world!\n"
|
||||
}
|
||||
|
||||
"Run from string with custom image": {
|
||||
debian: docker.#Pull & {
|
||||
source: "index.docker.io/debian"
|
||||
}
|
||||
run: #Run & {
|
||||
image: debian.output
|
||||
export: files: "/output.txt": _
|
||||
script: contents: "echo 'Hello, inlined world!' > /output.txt"
|
||||
}
|
||||
output: run.export.files."/output.txt".contents & "Hello, inlined world!\n"
|
||||
}
|
||||
|
||||
// Same thing but without bash.#Run
|
||||
control: {
|
||||
run: docker.#Run & {
|
||||
image: base.output
|
||||
command: {
|
||||
name: "sh"
|
||||
args: ["/bash/scripts/hello.sh"]
|
||||
}
|
||||
mounts: scripts: {
|
||||
contents: loadScripts.output
|
||||
dest: "/bash/scripts"
|
||||
}
|
||||
export: files: "/out.txt": _
|
||||
}
|
||||
output: run.export.files."/out.txt".contents & "Hello, world\n"
|
||||
base: docker.#Pull & {
|
||||
source: "alpine"
|
||||
}
|
||||
}
|
||||
|
||||
loadScripts: dagger.#Source & {
|
||||
path: "."
|
||||
include: ["*.sh"]
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user