diff --git a/.gitignore b/.gitignore index 6af3a0c..96be220 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ __pycache__/ # internal markdown files *.md +!deploy/README.md /HANDOFF.md /DISCOVERIES.md /GOING-PUBLIC.md diff --git a/deploy/README.md b/deploy/README.md new file mode 100644 index 0000000..c1566cb --- /dev/null +++ b/deploy/README.md @@ -0,0 +1,31 @@ +# Deployment helpers + +`update.sh` fetches the configured branch, fast-forwards only, validates changed +JavaScript, and restarts the server only when a loaded server file changed. It +runs as root, but uses `BURROW_USER` for Git operations. + +## systemd + +The existing `burrow-update.service` and `burrow-update.timer` check for an +update every two minutes. + +## OpenRC + +The OpenRC service keeps a small foreground loop alive and runs the same +updater every two minutes. It does not require cron. + +```sh +install -m 755 deploy/burrow-update.openrc /etc/init.d/burrow-update +install -m 644 deploy/burrow-update.conf /etc/conf.d/burrow-update +rc-update add burrow-update default +rc-service burrow-update start +``` + +Uncomment and change values in `/etc/conf.d/burrow-update` when the server is +not installed at `/home/aj/ajc-private-server`, runs as a different user, or +uses a differently named OpenRC service. `BURROW_UPDATE_INTERVAL` is a positive +number of seconds and defaults to `120`. + +`update.sh` selects OpenRC automatically when run by OpenRC. Set +`BURROW_INIT=openrc` when running it manually or from another supervisor. Set +`BURROW_INIT=systemd` to force systemd. diff --git a/deploy/burrow-update.conf b/deploy/burrow-update.conf new file mode 100644 index 0000000..1e09575 --- /dev/null +++ b/deploy/burrow-update.conf @@ -0,0 +1,9 @@ +# Copy to /etc/conf.d/burrow-update to override these values for OpenRC. +# This file is intentionally safe to source as a shell configuration file. + +# BURROW_DIR=/home/aj/ajc-private-server +# BURROW_USER=aj +# BURROW_SERVICE=burrow +# BURROW_BRANCH=main +# BURROW_UPDATE_INTERVAL=120 +# BURROW_INIT=openrc diff --git a/deploy/burrow-update.openrc b/deploy/burrow-update.openrc new file mode 100644 index 0000000..3d907dd --- /dev/null +++ b/deploy/burrow-update.openrc @@ -0,0 +1,17 @@ +#!/sbin/openrc-run +# Install as /etc/init.d/burrow-update (see deploy/README.md). + +description="Update the Burrow server from GitHub" +supervisor=supervise-daemon +command=/bin/bash +command_args="${BURROW_DIR:-/home/aj/ajc-private-server}/deploy/update-loop.sh" +pidfile="/run/${RC_SVCNAME}.pid" +stopgroup=yes + +# update.sh uses this to choose rc-service when it restarts Burrow. +: "${BURROW_INIT:=openrc}" +export BURROW_INIT + +depend() { + need net +} diff --git a/deploy/update-loop.sh b/deploy/update-loop.sh new file mode 100644 index 0000000..a954cd2 --- /dev/null +++ b/deploy/update-loop.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Runs update.sh repeatedly for init systems without a timer facility. +set -u + +interval=${BURROW_UPDATE_INTERVAL:-120} +case "$interval" in + ''|*[!0-9]*) + echo "BURROW_UPDATE_INTERVAL must be a positive number of seconds" >&2 + exit 2 + ;; +esac + +if [ "$interval" -eq 0 ]; then + echo "BURROW_UPDATE_INTERVAL must be greater than zero" >&2 + exit 2 +fi + +script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) + +while :; do + # A failed fetch or rejected update should not stop future update attempts. + "$script_dir/update.sh" || echo "burrow update failed; will retry in $interval seconds" >&2 + sleep "$interval" +done diff --git a/deploy/update.sh b/deploy/update.sh index 9ec74ca..6c02e94 100644 --- a/deploy/update.sh +++ b/deploy/update.sh @@ -1,15 +1,35 @@ #!/usr/bin/env bash -# brings the server to origin's main and restarts it when the change needs it. -# run as root by burrow-update.timer; git itself runs as the server's user. +# Brings the server to origin's main and restarts it when the change needs it. +# Run as root by either burrow-update.timer or the OpenRC updater service; git +# itself runs as the server's user. set -euo pipefail dir=${BURROW_DIR:-/home/aj/ajc-private-server} user=${BURROW_USER:-aj} service=${BURROW_SERVICE:-burrow} branch=${BURROW_BRANCH:-main} +init=${BURROW_INIT:-} git() { runuser -u "$user" -- git -C "$dir" "$@"; } +restart_service() { + case "$init" in + systemd) systemctl restart "$service" ;; + openrc) rc-service "$service" restart ;; + "") + if [ -d /run/openrc ] || [ -n "${RC_SVCNAME:-}" ]; then + rc-service "$service" restart + else + systemctl restart "$service" + fi + ;; + *) + echo "unknown BURROW_INIT value: $init (expected systemd or openrc)" >&2 + return 2 + ;; + esac +} + git fetch --quiet origin "$branch" old=$(git rev-parse HEAD) new=$(git rev-parse "origin/$branch") @@ -33,7 +53,7 @@ done # pipefail read as nothing to restart for. changed=$(git diff --name-only "$old" "$new") if grep -qvE '^(public/|client-updates/|test/|tools/|deploy/|[^/]+\.md$|\.gitignore$)' <<<"$changed"; then - systemctl restart "$service" + restart_service echo "updated $old -> $new, restarted $service" else echo "updated $old -> $new, no restart needed"