2021-11-04 08:05:24 +01:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"list"
|
|
|
|
|
|
|
|
"dagger.io/dagger"
|
2022-01-07 05:53:29 +01:00
|
|
|
"dagger.io/dagger/engine"
|
2021-11-04 08:05:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Run a command in a container
|
|
|
|
#Run: {
|
2022-02-09 07:16:13 +01:00
|
|
|
// Docker image to execute
|
|
|
|
input: #Image
|
2021-11-04 08:05:24 +01:00
|
|
|
|
|
|
|
always: bool | *false
|
|
|
|
|
|
|
|
// Filesystem mounts
|
|
|
|
mounts: [name=string]: engine.#Mount
|
|
|
|
|
|
|
|
// Expose network ports
|
2021-12-14 23:33:11 +01:00
|
|
|
// FIXME: investigate feasibility
|
2021-11-04 08:05:24 +01:00
|
|
|
ports: [name=string]: {
|
|
|
|
frontend: dagger.#Service
|
|
|
|
backend: {
|
|
|
|
protocol: *"tcp" | "udp"
|
|
|
|
address: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command to execute
|
2022-02-08 02:35:51 +01:00
|
|
|
command?: {
|
2021-11-04 08:05:24 +01:00
|
|
|
// Name of the command to execute
|
|
|
|
// Examples: "ls", "/bin/bash"
|
|
|
|
name: string
|
|
|
|
|
|
|
|
// Positional arguments to the command
|
|
|
|
// Examples: ["/tmp"]
|
|
|
|
args: [...string]
|
|
|
|
|
|
|
|
// Command-line flags represented in a civilized form
|
|
|
|
// Example: {"-l": true, "-c": "echo hello world"}
|
|
|
|
flags: [string]: (string | true)
|
|
|
|
|
|
|
|
_flatFlags: list.FlattenN([
|
|
|
|
for k, v in flags {
|
|
|
|
if (v & bool) != _|_ {
|
|
|
|
[k]
|
|
|
|
}
|
|
|
|
if (v & string) != _|_ {
|
|
|
|
[k, v]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
], 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Environment variables
|
|
|
|
// Example: {"DEBUG": "1"}
|
|
|
|
env: [string]: string
|
|
|
|
|
|
|
|
// Working directory for the command
|
|
|
|
// Example: "/src"
|
2022-01-27 21:21:08 +01:00
|
|
|
workdir: string
|
2021-11-04 08:05:24 +01:00
|
|
|
|
|
|
|
// Username or UID to ad
|
|
|
|
// User identity for this command
|
|
|
|
// Examples: "root", "0", "1002"
|
2022-01-27 20:15:32 +01:00
|
|
|
user: string
|
2021-11-04 08:05:24 +01:00
|
|
|
|
|
|
|
// Output fields
|
|
|
|
{
|
|
|
|
// Has the command completed?
|
|
|
|
completed: bool & (_exec.exit != _|_)
|
|
|
|
|
|
|
|
// Was completion successful?
|
|
|
|
success: bool & (_exec.exit == 0)
|
|
|
|
|
|
|
|
// Details on error, if any
|
|
|
|
error: {
|
|
|
|
// Error code
|
|
|
|
code: _exec.exit
|
|
|
|
|
|
|
|
// Error message
|
|
|
|
message: string | *null
|
|
|
|
}
|
|
|
|
|
2022-01-15 21:33:50 +01:00
|
|
|
export: {
|
|
|
|
rootfs: dagger.#FS & _exec.output
|
2021-11-04 08:05:24 +01:00
|
|
|
files: [path=string]: {
|
2022-01-15 21:33:50 +01:00
|
|
|
contents: string & _read.contents
|
|
|
|
_read: engine.#ReadFile & {
|
2021-11-04 08:05:24 +01:00
|
|
|
input: _exec.output
|
|
|
|
"path": path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
directories: [path=string]: {
|
2022-01-15 21:33:50 +01:00
|
|
|
contents: dagger.#FS & _subdir.output
|
|
|
|
_subdir: dagger.#Subdir & {
|
2021-11-04 08:05:24 +01:00
|
|
|
input: _exec.output
|
|
|
|
"path": path
|
2022-01-15 21:33:50 +01:00
|
|
|
}
|
2021-11-04 08:05:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 21:33:50 +01:00
|
|
|
// For compatibility with #Build
|
|
|
|
output: #Image & {
|
|
|
|
rootfs: _exec.output
|
2022-02-09 07:16:13 +01:00
|
|
|
config: input.config
|
2022-01-15 21:33:50 +01:00
|
|
|
}
|
|
|
|
|
2021-11-04 08:05:24 +01:00
|
|
|
// Actually execute the command
|
|
|
|
_exec: engine.#Exec & {
|
2022-02-09 07:16:13 +01:00
|
|
|
"input": input.rootfs
|
2022-01-27 20:15:32 +01:00
|
|
|
"always": always
|
|
|
|
"mounts": mounts
|
|
|
|
|
2022-02-08 02:35:51 +01:00
|
|
|
if command != _|_ {
|
|
|
|
args: [command.name] + command._flatFlags + command.args
|
2022-01-27 20:15:32 +01:00
|
|
|
}
|
2022-02-08 02:35:51 +01:00
|
|
|
if command == _|_ {
|
2022-01-27 20:15:32 +01:00
|
|
|
args: list.Concat([
|
2022-02-09 07:16:13 +01:00
|
|
|
if input.config.entrypoint != _|_ {
|
|
|
|
input.config.entrypoint
|
2022-01-27 20:15:32 +01:00
|
|
|
},
|
2022-02-09 07:16:13 +01:00
|
|
|
if input.config.cmd != _|_ {
|
|
|
|
input.config.cmd
|
2022-01-27 20:15:32 +01:00
|
|
|
},
|
|
|
|
])
|
|
|
|
}
|
|
|
|
"env": env
|
2022-02-09 07:16:13 +01:00
|
|
|
if input.config.env != _|_ {
|
|
|
|
for key, val in input.config.env {
|
2022-01-28 19:15:44 +01:00
|
|
|
if env[key] == _|_ {
|
|
|
|
env: "\(key)": val
|
2022-01-27 20:15:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 08:05:24 +01:00
|
|
|
"workdir": workdir
|
2022-02-09 07:16:13 +01:00
|
|
|
if workdir == _|_ && input.config.workdir != _|_ {
|
|
|
|
workdir: input.config.workdir
|
2022-01-27 20:15:32 +01:00
|
|
|
}
|
|
|
|
"user": user
|
2022-02-09 07:16:13 +01:00
|
|
|
if user == _|_ && input.config.user != _|_ {
|
|
|
|
user: input.config.user
|
2022-01-27 20:15:32 +01:00
|
|
|
}
|
2021-11-04 08:05:24 +01:00
|
|
|
}
|
|
|
|
}
|