2021-06-15 10:46:56 +02:00
|
|
|
// Docker container operations
|
2021-04-06 23:20:21 +02:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2021-06-23 16:31:42 +02:00
|
|
|
"alpha.dagger.io/dagger"
|
|
|
|
"alpha.dagger.io/dagger/op"
|
2021-04-06 23:20:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Build a Docker image from source, using included Dockerfile
|
2021-05-01 09:14:36 +02:00
|
|
|
#Build: {
|
2021-08-31 13:05:22 +02:00
|
|
|
source: dagger.#Input & {dagger.#Artifact}
|
2021-04-06 23:20:21 +02:00
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#DockerBuild & {
|
|
|
|
context: source
|
|
|
|
},
|
|
|
|
]
|
2021-05-01 09:14:36 +02:00
|
|
|
|
2021-04-06 23:20:21 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 08:53:04 +02:00
|
|
|
// Pull a docker container
|
|
|
|
#Pull: {
|
|
|
|
// Remote ref (example: "index.docker.io/alpine:latest")
|
2021-08-31 13:05:22 +02:00
|
|
|
from: dagger.#Input & {string}
|
2021-04-06 23:20:21 +02:00
|
|
|
|
|
|
|
#up: [
|
2021-05-06 08:53:04 +02:00
|
|
|
op.#FetchContainer & {ref: from},
|
2021-04-06 23:20:21 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2021-06-12 17:11:41 +02:00
|
|
|
// Push a docker image to a remote registry
|
2021-05-13 18:52:22 +02:00
|
|
|
#Push: {
|
2021-06-28 14:48:49 +02:00
|
|
|
// Remote target (example: "index.docker.io/alpine:latest")
|
2021-08-31 13:05:22 +02:00
|
|
|
target: dagger.#Input & {string}
|
2021-05-13 18:52:22 +02:00
|
|
|
|
2021-06-12 17:11:27 +02:00
|
|
|
// Image source
|
2021-08-31 13:05:22 +02:00
|
|
|
source: dagger.#Input & {dagger.#Artifact}
|
2021-05-13 18:52:22 +02:00
|
|
|
|
2021-06-18 22:01:16 +02:00
|
|
|
// Registry auth
|
2021-07-02 01:20:20 +02:00
|
|
|
auth?: {
|
2021-06-12 17:11:27 +02:00
|
|
|
// Username
|
2021-08-31 13:05:22 +02:00
|
|
|
username: dagger.#Input & {string}
|
2021-06-12 17:11:27 +02:00
|
|
|
|
|
|
|
// Password or secret
|
2021-08-31 13:05:22 +02:00
|
|
|
secret: dagger.#Input & {dagger.#Secret | string}
|
2021-06-12 17:11:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
push: #up: [
|
|
|
|
op.#Load & {from: source},
|
|
|
|
|
2021-06-18 22:01:16 +02:00
|
|
|
if auth != _|_ {
|
2021-06-12 17:11:27 +02:00
|
|
|
op.#DockerLogin & {
|
2021-06-28 14:48:49 +02:00
|
|
|
"target": target
|
2021-06-18 22:01:16 +02:00
|
|
|
username: auth.username
|
|
|
|
secret: auth.secret
|
2021-06-12 17:11:27 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-06-28 14:48:49 +02:00
|
|
|
op.#PushContainer & {ref: target},
|
2021-06-18 22:01:16 +02:00
|
|
|
|
|
|
|
op.#Subdir & {dir: "/dagger"},
|
2021-05-13 18:52:22 +02:00
|
|
|
]
|
2021-06-12 17:11:27 +02:00
|
|
|
|
2021-06-12 17:11:41 +02:00
|
|
|
// Image ref
|
2021-06-24 11:42:34 +02:00
|
|
|
ref: {
|
|
|
|
string
|
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#Load & {from: push},
|
|
|
|
|
|
|
|
op.#Export & {
|
|
|
|
source: "/image_ref"
|
|
|
|
},
|
|
|
|
]
|
2021-08-31 13:05:22 +02:00
|
|
|
} & dagger.#Output
|
2021-06-12 17:11:41 +02:00
|
|
|
|
|
|
|
// Image digest
|
2021-06-24 11:42:34 +02:00
|
|
|
digest: {
|
|
|
|
string
|
2021-06-12 17:11:41 +02:00
|
|
|
|
2021-06-24 11:42:34 +02:00
|
|
|
#up: [
|
|
|
|
op.#Load & {from: push},
|
2021-06-12 17:11:41 +02:00
|
|
|
|
2021-06-24 11:42:34 +02:00
|
|
|
op.#Export & {
|
|
|
|
source: "/image_digest"
|
|
|
|
},
|
|
|
|
]
|
2021-08-31 13:05:22 +02:00
|
|
|
} & dagger.#Output
|
2021-05-13 18:52:22 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 19:25:44 +02:00
|
|
|
#Run: {
|
2021-06-05 01:57:31 +02:00
|
|
|
// Connect to a remote SSH server
|
2021-06-07 22:29:42 +02:00
|
|
|
ssh: {
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh host
|
2021-08-31 13:05:22 +02:00
|
|
|
host: dagger.#Input & {string}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh user
|
2021-08-31 13:05:22 +02:00
|
|
|
user: dagger.#Input & {string}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh port
|
2021-08-31 13:05:22 +02:00
|
|
|
port: dagger.#Input & {*22 | int}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// private key
|
2021-08-31 13:05:22 +02:00
|
|
|
key: dagger.#Input & {dagger.#Secret}
|
2021-05-21 17:18:30 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// fingerprint
|
2021-08-31 13:05:22 +02:00
|
|
|
fingerprint?: dagger.#Input & {string}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh key passphrase
|
2021-08-31 13:05:22 +02:00
|
|
|
keyPassphrase?: dagger.#Input & {dagger.#Secret}
|
2021-06-05 01:57:31 +02:00
|
|
|
}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
|
|
|
// Image reference (e.g: nginx:alpine)
|
2021-08-31 13:05:22 +02:00
|
|
|
ref: dagger.#Input & {string}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
|
|
|
// Container name
|
2021-08-31 13:05:22 +02:00
|
|
|
name?: dagger.#Input & {string}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
|
|
|
// Image registry
|
|
|
|
registry?: {
|
2021-05-20 19:26:24 +02:00
|
|
|
target: string
|
2021-05-20 19:25:44 +02:00
|
|
|
username: string
|
|
|
|
secret: dagger.#Secret
|
2021-08-31 13:05:22 +02:00
|
|
|
} & dagger.#Input
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-11 19:01:33 +02:00
|
|
|
#command: #"""
|
2021-06-05 01:57:31 +02:00
|
|
|
# Run detach container
|
|
|
|
OPTS=""
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
if [ ! -z "$CONTAINER_NAME" ]; then
|
|
|
|
OPTS="$OPTS --name $CONTAINER_NAME"
|
|
|
|
fi
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
docker container run -d $OPTS "$IMAGE_REF"
|
|
|
|
"""#
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-11 19:01:33 +02:00
|
|
|
run: #Command & {
|
|
|
|
"ssh": ssh
|
|
|
|
command: #command
|
|
|
|
env: {
|
|
|
|
IMAGE_REF: ref
|
|
|
|
if name != _|_ {
|
|
|
|
CONTAINER_NAME: name
|
2021-05-20 19:25:44 +02:00
|
|
|
}
|
2021-06-11 19:01:33 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-20 19:25:44 +02:00
|
|
|
}
|
2021-05-06 08:53:04 +02:00
|
|
|
|
2021-04-06 23:20:21 +02:00
|
|
|
// Build a Docker image from the provided Dockerfile contents
|
2021-05-06 08:53:04 +02:00
|
|
|
// FIXME: incorporate into #Build
|
2021-04-06 23:20:21 +02:00
|
|
|
#ImageFromDockerfile: {
|
2021-06-15 10:46:56 +02:00
|
|
|
// Dockerfile passed as a string
|
2021-08-31 13:05:22 +02:00
|
|
|
dockerfile: dagger.#Input & {string}
|
2021-06-15 10:46:56 +02:00
|
|
|
|
|
|
|
// Build context
|
2021-08-31 13:05:22 +02:00
|
|
|
context: dagger.#Input & {dagger.#Artifact}
|
2021-04-06 23:20:21 +02:00
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#DockerBuild & {
|
|
|
|
"context": context
|
|
|
|
"dockerfile": dockerfile
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|