Files

127 lines
4.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Deploys the terminal homepage on this host from its clone in
# /srv/security-command.org. The same run covers the first setup and every
# update.
#
# Usage: sudo ./deploy.sh [--no-pull]
# --no-pull deploy the checked out commit without pulling (allows local changes)
set -euo pipefail
cd "$(dirname "$(readlink -f "$0")")"
pull=1
for arg in "$@"; do
case $arg in
--no-pull) pull=0 ;;
*) echo "unknown option: $arg" >&2; exit 2 ;;
esac
done
if [[ $EUID -ne 0 ]]; then
echo "run as root" >&2
exit 1
fi
# Git refuses repositories owned by another user, so it runs as the owner.
repo_owner=$(stat -c %U .)
as_owner() { sudo -u "$repo_owner" "$@"; }
if [[ $pull == 1 ]]; then
if [[ -n $(as_owner git status --porcelain) ]]; then
echo "the working tree has local changes; commit them or use --no-pull" >&2
exit 1
fi
as_owner git pull --ff-only
fi
if [[ ! -f .env ]]; then
as_owner cp deploy/env.example .env
read -rp "Absolute path of the content directory: " content_dir
as_owner sed -i "s|^CONTENT_DIR=.*|CONTENT_DIR=${content_dir}|" .env
fi
set -a
source .env
set +a
if [[ ! -d ${CONTENT_DIR:-} || ${CONTENT_DIR} != /* ]]; then
echo "CONTENT_DIR in .env must be an absolute path to a directory" >&2
exit 1
fi
# Jobs run as the owner of the content directory, which must not be root.
jobs_user=$(stat -c %u:%g "$CONTENT_DIR")
if [[ ${jobs_user%%:*} == 0 ]]; then
echo "CONTENT_DIR is owned by root; give it to the user that maintains the content, jobs run as that user" >&2
exit 1
fi
registry_host=${REGISTRY%%/*}
if ! grep -qs "\"${registry_host}\"" ~/.docker/config.json; then
echo "log in to the registry first: docker login ${registry_host} (token with read:package)" >&2
exit 1
fi
deploy/host/install-sandbox-host.sh
sandbox_docker() { DOCKER_HOST=unix:///run/docker-sandbox/docker.sock docker "$@"; }
# Images are tagged with the commit they were built from. Right after a push,
# CI may still be building them, so pulling is retried for a while.
pull_image() {
local engine=$1 image=$2 deadline=$((SECONDS + 900))
until "$engine" pull --quiet "$image" >/dev/null; do
if ((SECONDS >= deadline)); then
echo "gave up waiting for ${image}" >&2
return 1
fi
echo "waiting for CI to publish ${image}"
sleep 20
done
}
commit=$(as_owner git rev-parse HEAD)
sandbox_tag="${REGISTRY}/homepage-sandbox:${commit}"
spawner_tag="${REGISTRY}/homepage-spawner:${commit}"
backend_tag="${REGISTRY}/homepage-backend:${commit}"
jobs_tag="${REGISTRY}/homepage-jobs:${commit}"
pull_image sandbox_docker "$sandbox_tag"
pull_image docker "$spawner_tag"
pull_image docker "$backend_tag"
pull_image docker "$jobs_tag"
digest() { "$1" image inspect --format '{{index .RepoDigests 0}}' "$2"; }
# The spawner runs as 10001 and shares group 10100 with the backend, which
# runs as 10002 and writes the status file that sandboxes read. The jobs
# service writes the job file and the triggers the backend reads.
install -d -m 0755 runtime
install -d -o 10001 -g 10100 -m 0750 runtime/spawner
install -d -o 10002 -g 10002 -m 0755 runtime/status
install -d -o "${jobs_user%%:*}" -g "${jobs_user##*:}" -m 0755 config jobs runtime/jobs runtime/jobs/triggers
cat > runtime/deploy.env <<EOF
SANDBOX_IMAGE=$(digest sandbox_docker "$sandbox_tag")
SPAWNER_IMAGE=$(digest docker "$spawner_tag")
BACKEND_IMAGE=$(digest docker "$backend_tag")
JOBS_IMAGE=$(digest docker "$jobs_tag")
JOBS_USER=${jobs_user}
SANDBOX_GROUP_ID=$(getent group sandbox-spawner | cut -d: -f3)
STATUS_DIR=${PWD}/runtime/status
EOF
set -a
source runtime/deploy.env
set +a
IMAGE=$SANDBOX_IMAGE deploy/host/verify-sandbox.sh
compose() {
docker compose --project-directory "$PWD" -f deploy/compose.yml \
--env-file .env --env-file runtime/deploy.env "$@"
}
if ! compose up -d --remove-orphans --wait --wait-timeout 120; then
compose ps
echo "the services did not become healthy; check: docker logs security-command-backend-1" >&2
exit 1
fi
echo "deployed ${commit}"
echo "jobs: docker exec security-command-jobs-1 jobctl list"