From 571f43457d1031332bac9aa32703457d635fbac5 Mon Sep 17 00:00:00 2001 From: Overlord Date: Mon, 12 Oct 2026 23:20:04 +0200 Subject: [PATCH] Add deploy script and CI workflow for sandboxed homepage deployment. --- .gitea/workflows/images.yml | 26 +++++++++ .gitignore | 1 + deploy.sh | 87 +++++++++++++++++++++++++++++ deploy/env.example | 5 ++ deploy/host/install-sandbox-host.sh | 35 ++++++++---- 5 files changed, 142 insertions(+), 12 deletions(-) create mode 100644 .gitea/workflows/images.yml create mode 100755 deploy.sh create mode 100644 deploy/env.example diff --git a/.gitea/workflows/images.yml b/.gitea/workflows/images.yml new file mode 100644 index 0000000..58f19c5 --- /dev/null +++ b/.gitea/workflows/images.yml @@ -0,0 +1,26 @@ +name: images + +on: + push: + branches: [develop, master] + tags: ['v*'] + +env: + REGISTRY: git.security-command.org/securitycommand + +jobs: + sandbox: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Log in to the registry + run: > + echo "${{ secrets.REGISTRY_TOKEN }}" + | docker login "${REGISTRY%%/*}" -u "${{ secrets.REGISTRY_USER }}" --password-stdin + + - name: Build and push + run: | + image="$REGISTRY/homepage-sandbox" + docker build -t "$image:${{ gitea.sha }}" -t "$image:${{ gitea.ref_name }}" sandbox/ + docker push --all-tags "$image" diff --git a/.gitignore b/.gitignore index 95efc22..3f3a887 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ *.md !README.md +/runtime/ diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 0000000..57e4332 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,87 @@ +#!/usr/bin/env bash +# Deploys the terminal homepage on this host. Clone the repository into +# /srv/security-command.org and run this script for the first setup and for +# 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 + +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}" +pull_image sandbox_docker "$sandbox_tag" +sandbox_image=$(sandbox_docker image inspect --format '{{index .RepoDigests 0}}' "$sandbox_tag") + +install -d -m 0755 runtime +cat > runtime/deploy.env </dev/null; then fi getent group sandbox-spawner >/dev/null || groupadd --system sandbox-spawner -id sandbox-spawner >/dev/null 2>&1 \ - || useradd --system --gid sandbox-spawner --no-create-home --shell /usr/sbin/nologin sandbox-spawner -install -D -m 0644 "$repo_root/deploy/docker-sandbox/daemon.json" /etc/docker-sandbox/daemon.json -install -m 0644 "$repo_root/deploy/systemd/sandbox.slice" /etc/systemd/system/sandbox.slice -install -m 0644 "$repo_root/deploy/systemd/docker-sandbox.service" /etc/systemd/system/docker-sandbox.service +changed=0 +install_if_changed() { + if ! cmp -s "$1" "$2"; then + install -D -m 0644 "$1" "$2" + changed=1 + fi +} -systemctl daemon-reload +install_if_changed "$repo_root/deploy/docker-sandbox/daemon.json" /etc/docker-sandbox/daemon.json +install_if_changed "$repo_root/deploy/systemd/sandbox.slice" /etc/systemd/system/sandbox.slice +install_if_changed "$repo_root/deploy/systemd/docker-sandbox.service" /etc/systemd/system/docker-sandbox.service + +if [[ $changed == 1 ]]; then + systemctl daemon-reload +fi systemctl start sandbox.slice -systemctl enable docker-sandbox.service -systemctl restart docker-sandbox.service - -echo "sandbox daemon is up: DOCKER_HOST=unix:///run/docker-sandbox/docker.sock docker info" +systemctl enable --quiet docker-sandbox.service +if [[ $changed == 1 ]]; then + systemctl restart docker-sandbox.service +else + systemctl start docker-sandbox.service +fi