2021-06-11 09:50:27 +02:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
2021-06-23 16:31:42 +02:00
|
|
|
"alpha.dagger.io/alpine"
|
|
|
|
"alpha.dagger.io/dagger"
|
|
|
|
"alpha.dagger.io/dagger/op"
|
2021-06-11 09:50:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// A container image that can run any docker command
|
|
|
|
#Command: {
|
|
|
|
ssh?: {
|
|
|
|
// ssh host
|
|
|
|
host: string @dagger(input)
|
|
|
|
|
|
|
|
// ssh user
|
|
|
|
user: string @dagger(input)
|
|
|
|
|
|
|
|
// ssh port
|
|
|
|
port: *22 | int @dagger(input)
|
|
|
|
|
|
|
|
// private key
|
|
|
|
key: dagger.#Secret @dagger(input)
|
|
|
|
|
|
|
|
// fingerprint
|
|
|
|
fingerprint?: string @dagger(input)
|
|
|
|
|
|
|
|
// ssh key passphrase
|
|
|
|
keyPassphrase?: dagger.#Secret @dagger(input)
|
|
|
|
}
|
|
|
|
|
2021-11-22 08:12:47 +01:00
|
|
|
// Connect via DOCKER_HOST, supports tcp://
|
|
|
|
// TODO: Consider refactoring to support ssh:// & even file://
|
|
|
|
host?: string @dagger(input)
|
|
|
|
|
2021-06-11 09:50:27 +02:00
|
|
|
// Command to execute
|
2021-10-13 02:10:46 +02:00
|
|
|
command: string
|
2021-06-11 09:50:27 +02:00
|
|
|
|
|
|
|
// Environment variables shared by all commands
|
|
|
|
env: {
|
2021-10-13 02:10:46 +02:00
|
|
|
[string]: string
|
2021-06-11 09:50:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mount content from other artifacts
|
2021-12-03 13:10:08 +01:00
|
|
|
mount: [string]: from: dagger.#Artifact
|
|
|
|
|
|
|
|
// Mount secrets
|
|
|
|
secret: [string]: dagger.#Secret
|
2021-06-11 09:50:27 +02:00
|
|
|
|
|
|
|
// Mount persistent cache directories
|
2021-06-11 09:53:14 +02:00
|
|
|
cache: {
|
2021-10-13 02:10:46 +02:00
|
|
|
[string]: true
|
2021-06-11 09:53:14 +02:00
|
|
|
}
|
2021-06-11 09:50:27 +02:00
|
|
|
|
|
|
|
// Mount temporary directories
|
2021-06-11 09:53:14 +02:00
|
|
|
tmpfs: {
|
2021-10-13 02:10:46 +02:00
|
|
|
[string]: true
|
2021-06-11 09:53:14 +02:00
|
|
|
}
|
2021-06-11 09:50:27 +02:00
|
|
|
|
2021-09-22 02:19:00 +02:00
|
|
|
// Mount docker socket
|
|
|
|
socket?: dagger.#Stream @dagger(input)
|
|
|
|
|
2021-06-11 09:50:27 +02:00
|
|
|
// Additional packages to install
|
2021-06-11 09:53:14 +02:00
|
|
|
package: {
|
2021-10-13 02:10:46 +02:00
|
|
|
[string]: true | false | string
|
2021-06-11 09:53:14 +02:00
|
|
|
}
|
2021-06-11 09:50:27 +02:00
|
|
|
|
2021-06-17 17:12:24 +02:00
|
|
|
// Image registries
|
|
|
|
registries: [...{
|
|
|
|
target?: string
|
|
|
|
username: string
|
|
|
|
secret: dagger.#Secret
|
2021-10-13 02:10:46 +02:00
|
|
|
}]
|
2021-06-17 17:12:24 +02:00
|
|
|
|
|
|
|
// Copy contents from other artifacts
|
|
|
|
copy: [string]: from: dagger.#Artifact
|
|
|
|
|
|
|
|
// Write file in the container
|
|
|
|
files: [string]: string
|
|
|
|
|
2021-06-11 09:50:27 +02:00
|
|
|
// Setup docker client and then execute the user command
|
|
|
|
#code: #"""
|
|
|
|
# Setup ssh
|
|
|
|
if [ -n "$DOCKER_HOSTNAME" ]; then
|
|
|
|
export DOCKER_HOST="ssh://$DOCKER_USERNAME@$DOCKER_HOSTNAME:$DOCKER_PORT"
|
|
|
|
|
|
|
|
# Start ssh-agent
|
|
|
|
eval $(ssh-agent) > /dev/null
|
|
|
|
|
|
|
|
# Add key
|
|
|
|
if [ -f "/key" ]; then
|
|
|
|
message="$(ssh-keygen -y -f /key < /dev/null 2>&1)" || {
|
|
|
|
>&2 echo "$message"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# Save key
|
|
|
|
ssh-add /key > /dev/null
|
|
|
|
if [ "$?" != 0 ]; then
|
|
|
|
exit 1
|
2021-10-13 02:10:46 +02:00
|
|
|
fi
|
2021-06-11 09:50:27 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -z $FINGERPRINT ]]; then
|
|
|
|
mkdir -p "$HOME"/.ssh
|
|
|
|
|
|
|
|
# Add user's fingerprint to known hosts
|
|
|
|
echo "$FINGERPRINT" >> "$HOME"/.ssh/known_hosts
|
|
|
|
else
|
|
|
|
# Add host to known hosts
|
2022-01-25 20:17:18 +01:00
|
|
|
ssh -i /key -o "UserKnownHostsFile "$HOME"/.ssh/known_hosts" -o "StrictHostKeyChecking accept-new" -p "$DOCKER_PORT" "$DOCKER_USERNAME"@"$DOCKER_HOSTNAME" true > /dev/null 2>&1
|
2021-06-11 09:50:27 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Execute entrypoint
|
|
|
|
/bin/bash /entrypoint.sh
|
|
|
|
"""#
|
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#Load & {
|
|
|
|
from: alpine.#Image & {
|
2021-06-17 17:12:24 +02:00
|
|
|
"package": {
|
|
|
|
package
|
2021-06-11 09:50:27 +02:00
|
|
|
bash: true
|
|
|
|
"openssh-client": true
|
|
|
|
"docker-cli": true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-06-17 17:12:24 +02:00
|
|
|
for registry in registries {
|
|
|
|
op.#Exec & {
|
|
|
|
args: ["/bin/bash", "-c", #"""
|
2021-10-13 02:10:46 +02:00
|
|
|
echo "$TARGER_HOST" | docker login --username "$DOCKER_USERNAME" --password-stdin "$(cat /password)"
|
2021-06-17 17:12:24 +02:00
|
|
|
"""#,
|
|
|
|
]
|
|
|
|
env: {
|
|
|
|
TARGET_HOST: registry.target
|
|
|
|
DOCKER_USERNAME: registry.username
|
|
|
|
}
|
2021-11-15 17:26:34 +01:00
|
|
|
mount: "/password": secret: registry.secret
|
2021-06-17 17:12:24 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
for dest, content in files {
|
|
|
|
op.#WriteFile & {
|
|
|
|
"content": content
|
|
|
|
"dest": dest
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
for dest, src in copy {
|
|
|
|
op.#Copy & {
|
|
|
|
from: src.from
|
|
|
|
"dest": dest
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-06-11 09:50:27 +02:00
|
|
|
if ssh.keyPassphrase != _|_ {
|
|
|
|
op.#WriteFile & {
|
|
|
|
content: #"""
|
|
|
|
#!/bin/bash
|
|
|
|
cat /keyPassphrase
|
|
|
|
"""#
|
|
|
|
dest: "/get_keyPassphrase"
|
|
|
|
mode: 0o500
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Write wrapper
|
|
|
|
op.#WriteFile & {
|
|
|
|
content: #code
|
|
|
|
dest: "/setup.sh"
|
|
|
|
},
|
|
|
|
|
|
|
|
// Write entrypoint
|
|
|
|
op.#WriteFile & {
|
|
|
|
content: command
|
|
|
|
dest: "/entrypoint.sh"
|
|
|
|
},
|
|
|
|
|
|
|
|
op.#Exec & {
|
|
|
|
always: true
|
|
|
|
args: [
|
2021-06-17 17:12:24 +02:00
|
|
|
"/bin/bash",
|
2021-06-11 09:50:27 +02:00
|
|
|
"--noprofile",
|
|
|
|
"--norc",
|
|
|
|
"-eo",
|
|
|
|
"pipefail",
|
|
|
|
"/setup.sh",
|
|
|
|
]
|
|
|
|
"env": {
|
|
|
|
env
|
|
|
|
if ssh != _|_ {
|
|
|
|
DOCKER_HOSTNAME: ssh.host
|
|
|
|
DOCKER_USERNAME: ssh.user
|
|
|
|
DOCKER_PORT: strconv.FormatInt(ssh.port, 10)
|
|
|
|
if ssh.keyPassphrase != _|_ {
|
|
|
|
SSH_ASKPASS: "/get_keyPassphrase"
|
|
|
|
DISPLAY: "1"
|
|
|
|
}
|
|
|
|
if ssh.fingerprint != _|_ {
|
|
|
|
FINGERPRINT: ssh.fingerprint
|
|
|
|
}
|
|
|
|
}
|
2021-11-22 08:12:47 +01:00
|
|
|
if host != _|_ && ssh == _|_ {
|
|
|
|
DOCKER_HOST: host
|
|
|
|
}
|
2021-06-11 09:50:27 +02:00
|
|
|
}
|
|
|
|
"mount": {
|
|
|
|
if ssh != _|_ {
|
|
|
|
if ssh.key != _|_ {
|
|
|
|
"/key": secret: ssh.key
|
|
|
|
}
|
|
|
|
if ssh.keyPassphrase != _|_ {
|
|
|
|
"/keyPassphrase": secret: ssh.keyPassphrase
|
|
|
|
}
|
|
|
|
}
|
2021-09-22 02:19:00 +02:00
|
|
|
if socket != _|_ {
|
|
|
|
"/var/run/docker.sock": stream: socket
|
|
|
|
}
|
2021-06-11 09:50:27 +02:00
|
|
|
for dest, o in mount {
|
|
|
|
"\(dest)": o
|
|
|
|
}
|
2021-12-03 13:10:08 +01:00
|
|
|
for dest, s in secret {
|
|
|
|
"\(dest)": secret: s
|
|
|
|
}
|
2021-06-11 09:50:27 +02:00
|
|
|
for dest, _ in cache {
|
|
|
|
"\(dest)": "cache"
|
|
|
|
}
|
|
|
|
for dest, _ in tmpfs {
|
|
|
|
"\(dest)": "tmpfs"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
2021-06-11 09:53:14 +02:00
|
|
|
}
|