Add deploy script and CI workflow for sandboxed homepage deployment.

This commit is contained in:
Overlord committed 2026-10-12 23:20:04 +02:00
1 parent 0737b28897
commit 571f43457d
5 files changed
+140 -10

No files matched your search

+26
View File
@@ -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"
+1
View File
@@ -5,3 +5,4 @@
*.md *.md
!README.md !README.md
/runtime/
Executable
+87
View File
@@ -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 <<EOF
SANDBOX_IMAGE=${sandbox_image}
SANDBOX_GROUP_ID=$(getent group sandbox-spawner | cut -d: -f3)
EOF
IMAGE=$sandbox_image deploy/host/verify-sandbox.sh
echo "deployed ${commit}"
+5
View File
@@ -0,0 +1,5 @@
# Absolute host path of the homepage content. It is mounted read only.
CONTENT_DIR=
# Container registry of the SecurityCommand organization.
REGISTRY=git.security-command.org/securitycommand
+21 -10
View File
@@ -1,7 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Prepares the host for sandbox sessions: installs gVisor, creates the # Prepares the host for sandbox sessions: installs gVisor, creates the
# spawner account and starts the dedicated sandbox Docker daemon. # spawner group and runs the dedicated sandbox Docker daemon.
# Run as root from the repository root. Safe to run repeatedly. # Called by deploy.sh as root. Safe to run repeatedly: the sandbox daemon
# only restarts when its configuration changed, so live sessions survive.
set -euo pipefail set -euo pipefail
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
@@ -23,16 +24,26 @@ if ! command -v runsc >/dev/null; then
fi fi
getent group sandbox-spawner >/dev/null || groupadd --system sandbox-spawner 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 changed=0
install -m 0644 "$repo_root/deploy/systemd/sandbox.slice" /etc/systemd/system/sandbox.slice install_if_changed() {
install -m 0644 "$repo_root/deploy/systemd/docker-sandbox.service" /etc/systemd/system/docker-sandbox.service if ! cmp -s "$1" "$2"; then
install -D -m 0644 "$1" "$2"
changed=1
fi
}
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 systemctl daemon-reload
fi
systemctl start sandbox.slice systemctl start sandbox.slice
systemctl enable docker-sandbox.service systemctl enable --quiet docker-sandbox.service
if [[ $changed == 1 ]]; then
systemctl restart docker-sandbox.service systemctl restart docker-sandbox.service
else
echo "sandbox daemon is up: DOCKER_HOST=unix:///run/docker-sandbox/docker.sock docker info" systemctl start docker-sandbox.service
fi