main: the current setup only
Some checks failed
nixos / nixos (push) Has been cancelled

Both servers run NixOS. The Ansible code and the move itself leave main
for their own branches: `ansible` is the Debian/Ansible setup as it was
before the migration, `migration` keeps the plan and record, the data
scripts and everything else the move needed.

README and RESTORE describe the setup as it is now: NixOS, Podman,
sops, OpenTofu, the backup paths and hooks of the new layout. The
README carries the TODO list that MIGRATION.md had. The Forgejo
sanitising script stays, under hosts/rehearsal, with the rules for
rehearsing on restored production data in the NixOS README. Renovate
no longer looks at Ansible files.

Assisted-by: Claude:opus-5-5
This commit is contained in:
Sergei Poljanski 2026-11-17 10:08:40 +04:00
commit 6d9122eeff
Signed by: asxpi
GPG key ID: 4F8851660FA4121B
4 changed files with 135 additions and 34955 deletions

View file

@ -2,8 +2,8 @@
Each server's whole system, as one flake. A server is created by OpenTofu
(`../tofu`, OpenTofu), converts itself to NixOS on first boot, and gets its
configuration from here. Production is moving over from `../ansible`;
the plan and its state are in `../MIGRATION.md`.
configuration from here. How the servers moved here from Debian and
Ansible is on branch `migration`.
```
flake.nix inputs (nixpkgs 26.05, unstable for a few packages, sops-nix), hosts, dev shell
@ -17,7 +17,6 @@ modules/monitoring-agent.nix node, container and security metrics, logs to Loki
hosts/asxpio-ops/ Traefik, Forgejo, Postgres, Garage, monitoring, app stacks
hosts/asxpio-services/ runners, Minecraft, XMPP, Xray, SillyTavern, one file each
hosts/rehearsal/ overrides that turn the two hosts into a test pair
migration/ one-off scripts: Debian-era data into place, rehearsal clean-up
secrets/*.yaml sops-encrypted, one file per host
```
@ -191,12 +190,22 @@ the other way round for WireGuard); `nixos-rebuild switch --flake
.#rehearsal-<role> --target-host root@<ip> --build-host root@<ip>`;
reboot once.
With test data: `touch /var/lib/.data-in-place`. With a restored copy of
production: restore last night's snapshot to `/restore`, run
`migration/ops-data.sh /restore` or `services-data.sh /restore`, and on
ops `migration/rehearsal-sanitize-forgejo.sh` before the runner starts.
Read the rehearsal rules in `../MIGRATION.md` first: restored data
carries production credentials.
With test data: `touch /var/lib/.data-in-place`. With a restored copy
of production: restore that night's snapshot to `/` (RESTORE.md, "A whole
host"), and mind what the copy carries:
- Production credentials. Anything that would use them outward stays
off in `hosts/rehearsal/`: XMPP and the Telegram gateway, mail
(Forgejo, Grafana, crawler), the bucket replica, certificate pushes,
the interop runner, Minecraft's outward plugins (DiscordSRV,
ServerBackup, AutoUpdateGeyser).
- On ops, run `hosts/rehearsal/sanitize-forgejo.sh` before any runner
meets the data: it removes production's queued jobs, schedules,
Actions secrets, runners and webhooks.
- Start app containers by hand only, with mail pointed at nothing and
`INDEXNOW_KEY` empty; never vhpbot or asxpio-monitor.
- Limit SSH to your address with a Hetzner firewall, keep backup
credentials in `/run` only, and delete the servers when done.
## How the first boot works, and its limits

View file

@ -0,0 +1,33 @@
#!/usr/bin/env bash
# REHEARSAL HOSTS ONLY. Run after restoring production's Forgejo data and
# before any runner is started against it.
#
# A restored database still has production's queued jobs, its schedules
# and its Actions secrets (deploy keys, registry tokens). A runner would
# pick the jobs up and deploy to production from the test server. This
# cancels what is queued, removes the schedules and deletes every secret.
set -euo pipefail
case "$(hostname)" in
rehearsal-*) ;;
*) echo "refusing: $(hostname) is not a rehearsal host" >&2; exit 1 ;;
esac
db=/var/lib/forgejo/data/forgejo.db
systemctl stop forgejo.service
sqlite3 "$db" <<'SQL'
BEGIN;
DELETE FROM secret;
-- 3 = cancelled; 5 waiting, 6 running, 7 blocked
UPDATE action_run_job SET status = 3 WHERE status IN (5, 6, 7);
UPDATE action_run SET status = 3 WHERE status IN (5, 6, 7);
UPDATE action_task SET status = 3 WHERE status IN (5, 6, 7);
DELETE FROM action_schedule_spec;
DELETE FROM action_schedule;
DELETE FROM action_runner;
DELETE FROM webhook;
DELETE FROM push_mirror;
COMMIT;
SQL
systemctl start forgejo.service
echo "sanitized: no secrets, no queued jobs, no schedules, no runners, no webhooks"

View file

@ -1,124 +0,0 @@
#!/usr/bin/env bash
# asxpio-ops: move the data of the Debian/Docker layout to where the
# NixOS services keep it, then let them start.
#
# ops-data.sh <old root>
#
# <old root> is /old-root after an in-place conversion (the dumps then
# come from the last thing done before it: `restic-backup run`, which
# leaves nothing behind, so take them by hand into
# <old root>/var/backups/restic-stage), or the target of a restic restore.
# Moves within one filesystem, so it is quick; run it once, as root, on a
# host whose services are still waiting for /var/lib/.data-in-place.
set -euo pipefail
src=${1:?usage: ops-data.sh <old root>}
stage=$src/var/backups/restic-stage
[ ! -e /var/lib/.data-in-place ] || { echo "services already have data (/var/lib/.data-in-place exists)" >&2; exit 1; }
for f in "$stage/pg_dumpall.sql" "$stage/forgejo-gitea.db"; do
[ -e "$f" ] || { echo "missing: $f" >&2; exit 1; }
done
# Each part moves what is still under <old root> and skips what already
# moved, so an interrupted run can simply be started again.
echo "== Forgejo"
fj=/var/lib/forgejo
install -d -o git -g git -m 750 $fj $fj/data $fj/custom
if [ -d "$src/opt/forgejo/data/git/repositories" ]; then
rm -rf $fj/repositories
mv "$src/opt/forgejo/data/git/repositories" $fj/repositories
fi
[ ! -d "$src/opt/forgejo/data/git/lfs" ] || { rm -rf $fj/data/lfs; mv "$src/opt/forgejo/data/git/lfs" $fj/data/lfs; }
[ -d $fj/repositories ] || { echo "missing: Forgejo repositories" >&2; exit 1; }
for d in "$src"/opt/forgejo/data/gitea/data/*; do
[ -e "$d" ] || continue
case "$(basename "$d")" in
home | ssh | tmp | sessions | queues | indexers) ;; # rebuilt, or the container's own
*) rm -rf "$fj/data/$(basename "$d")"; mv "$d" $fj/data/ ;;
esac
done
[ ! -d "$src/opt/forgejo/data/gitea/public" ] || { rm -rf $fj/custom/public; mv "$src/opt/forgejo/data/gitea/public" $fj/custom/public; }
# The online copy taken by the backup hook, not the live file
cp "$stage/forgejo-gitea.db" $fj/data/forgejo.db
chown -R git:git $fj
echo "== Garage"
install -d -m 700 /var/lib/garage
for part in meta data; do
v="$src/var/lib/docker/volumes/storage_garage_$part/_data"
[ ! -d "$v" ] || { rm -rf /var/lib/garage/$part; mv "$v" /var/lib/garage/$part; }
[ -d /var/lib/garage/$part ] || { echo "missing: Garage $part" >&2; exit 1; }
done
echo "== Traefik"
install -d -o traefik -g traefik -m 700 /var/lib/traefik
if [ -f "$src/opt/traefik/letsencrypt/acme.json" ]; then
install -o traefik -g traefik -m 600 "$src/opt/traefik/letsencrypt/acme.json" /var/lib/traefik/acme.json
fi
echo "== Monitoring history (Docker volumes; not in the restic backup)"
mvvol() { # mvvol <volume> <dest> <owner>
if [ -d "$vols/$1/_data" ]; then
rm -rf "$2"; mkdir -p "$(dirname "$2")"
mv "$vols/$1/_data" "$2"; chown -R "$3" "$2"
else
echo " $1: not there (a restore, not a conversion); starting empty"
fi
}
vols=$src/var/lib/docker/volumes
mvvol monitoring_prometheus-data /var/lib/prometheus2/data prometheus:prometheus
mvvol monitoring_loki-data /var/lib/loki loki:loki
if [ -f "$vols/monitoring_grafana-data/_data/grafana.db" ]; then
install -d -o grafana -g grafana /var/lib/grafana/data
mv "$vols/monitoring_grafana-data/_data/grafana.db" /var/lib/grafana/data/grafana.db
chown grafana:grafana /var/lib/grafana/data/grafana.db
fi
echo "== App stacks (compose file and .env each; CI deploys them later)"
install -d -m 755 /opt
for app in asxpio datacenterbingo blog czsk vhpbot-prod vhpbot-dev; do
[ -d "$src/opt/$app" ] || continue
rm -rf "/opt/$app"; mv "$src/opt/$app" "/opt/$app"
chown -R deploy:deploy "/opt/$app"
done
[ ! -d "$src/root/backups" ] || { rm -rf /root/backups; mv "$src/root/backups" /root/backups; }
echo "== Starting"
touch /var/lib/.data-in-place
systemctl start postgresql.service
echo "== Postgres"
# The dump recreates the roles with their old passwords; postgres-roles
# then sets the ones from sops
runuser -u postgres -- psql -q postgres < "$stage/pg_dumpall.sql" > /var/log/pg-restore.log 2>&1 || true
echo "errors in /var/log/pg-restore.log: $(grep -c ERROR /var/log/pg-restore.log || true)"
systemctl restart postgres-roles.service
systemctl start garage.service garage-bootstrap.service forgejo.service traefik.service
echo "== Forgejo: SSH keys file for the new paths"
# The repositories' own hook files still name the container's binary;
# Forgejo 17 gives git its own hooks on every push, so they are unused.
forgejo-admin regenerate keys
echo "== App stacks: images again (Docker's image store does not carry over)"
case "$(hostname)" in
rehearsal-*)
# Restored .env files hold production credentials: vhpbot would log
# in as the production bot, asxpio-monitor probe the internet, the
# apps send mail. Start them by hand, sanitized (MIGRATION.md rules).
echo " rehearsal host: apps NOT started"
exit 0 ;;
esac
for app in asxpio datacenterbingo blog czsk vhpbot-prod vhpbot-dev; do
[ -f "/opt/$app/docker-compose.yml" ] || continue
echo " $app"
if grep -q "^ *build:" "/opt/$app/docker-compose.yml" && ! grep -q "image: *git" "/opt/$app/docker-compose.yml"; then
runuser -l deploy -c "cd /opt/$app && docker compose up -d --build" || echo " $app FAILED"
else
runuser -l deploy -c "cd /opt/$app && docker compose pull -q && docker compose up -d" || echo " $app FAILED"
fi
done
echo "== Done. Check: systemctl --failed; garage status; a git clone over SSH and HTTPS;"
echo " docker ps (as deploy): every app healthy; Grafana on gf.asxp.io"

View file

@ -1,60 +0,0 @@
#!/usr/bin/env bash
# asxpio-services: move the data of the Debian/Docker layout to where the
# NixOS services keep it, then let them start.
#
# services-data.sh <old root>
#
# <old root> is /old-root after an in-place conversion, or the target of
# a restic restore. Moves within one filesystem, so it is quick; run it
# once, as root, on a host whose services are still waiting for
# /var/lib/.data-in-place.
set -euo pipefail
src=${1:?usage: services-data.sh <old root>}
vols=$src/var/lib/docker/volumes
[ ! -e /var/lib/.data-in-place ] || { echo "services already have data (/var/lib/.data-in-place exists)" >&2; exit 1; }
for f in "$src/opt/minecraft/world" "$src/opt/sillytavern/data" "$src/opt/xmpp/state" "$vols/xmpp_database/_data"; do
[ -e "$f" ] || { echo "missing: $f" >&2; exit 1; }
done
echo "== Minecraft"
rm -rf /var/lib/minecraft
mv "$src/opt/minecraft" /var/lib/minecraft
rm -f /var/lib/minecraft/docker-compose.yml
chown -R minecraft:minecraft /var/lib/minecraft
echo "== SillyTavern"
install -d -o sillytavern -g sillytavern -m 750 /var/lib/SillyTavern
rm -rf /var/lib/SillyTavern/data
mv "$src/opt/sillytavern/data" /var/lib/SillyTavern/data
chown -R sillytavern:sillytavern /var/lib/SillyTavern
echo "== XMPP"
install -d -m 755 /opt/xmpp
rm -rf /opt/xmpp/state
mv "$src/opt/xmpp/state" /opt/xmpp/state
for v in xmpp_database xmpp_tackygram; do
podman volume exists $v || podman volume create $v >/dev/null
dst=$(podman volume inspect $v --format '{{.Mountpoint}}')
rm -rf "$dst"
mv "$vols/$v/_data" "$dst"
done
# The online copy taken by the backup hook, if this is a restore
stage=$src/var/backups/restic-stage/ejabberd.sqlite
db=$(podman volume inspect xmpp_database --format '{{.Mountpoint}}')/ejabberd.sqlite
if [ -f "$stage" ]; then
cp "$stage" "$db.new"
chown --reference="$db" "$db.new"
chmod --reference="$db" "$db.new"
mv "$db.new" "$db"
fi
echo "== Starting"
touch /var/lib/.data-in-place
systemctl start minecraft-server.service sillytavern.service
for u in podman-xmpp-ejabberd podman-xmpp-apushd podman-xmpp-tackygram; do
# Not enabled on a rehearsal host: it would be a second asxp.io
if systemctl is-enabled -q $u.service 2>/dev/null; then systemctl start $u.service; else echo "left stopped: $u"; fi
done
echo "== Done. Check: systemctl --failed; the Minecraft log reaches Done; SillyTavern on :8000; an XMPP login"