iac/nixos/migration/ops-data.sh
Sergei Poljanski fa46d26f89
nixos: app stacks on Podman, unchanged
The apps keep deploying themselves: their CI copies a compose file and
a .env and runs docker compose as user deploy. `docker` is now the real
Docker CLI against rootful Podman's socket, so compose, inspect and
logs behave as before and every user sees the same containers.

Their compose files still expect the storage stack's network and the
hosts postgres and garage. On ops every container resolves those two
names to the host, where the native services run, and an empty storage
network exists, so nothing in the app repositories has to change for
the move itself.

Tried on the rehearsal pair with the real asxpio and datacenterbingo
images on the restored database and Garage (mail and IndexNow
disabled): both serve through Traefik, and a local compose build works.

One thing does need the app repositories: Podman ignores the
HEALTHCHECK of OCI-format images, which is what the CI builds, so the
deploy's wait for "healthy" would time out. Declaring the healthcheck
in each compose file fixes it, also under Docker; see MIGRATION.md.

ops-data.sh now also moves the monitoring history, Traefik's
certificates and the app directories, and pulls the images again.

Assisted-by: Claude:opus-5-5
2026-11-17 05:11:17 +04:00

104 lines
4.5 KiB
Shell
Executable file

#!/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" "$src/opt/forgejo/data/git/repositories"; do
[ -e "$f" ] || { echo "missing: $f" >&2; exit 1; }
done
echo "== Forgejo"
fj=/var/lib/forgejo
install -d -o git -g git -m 750 $fj $fj/data $fj/custom
mv "$src/opt/forgejo/data/git/repositories" $fj/repositories
mv "$src/opt/forgejo/data/git/lfs" $fj/data/lfs
for d in "$src"/opt/forgejo/data/gitea/data/*; do
case "$(basename "$d")" in
home | ssh | tmp | sessions | queues | indexers) ;; # rebuilt, or the container's own
*) mv "$d" $fj/data/ ;;
esac
done
[ ! -d "$src/opt/forgejo/data/gitea/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
mv "$src/var/lib/docker/volumes/storage_garage_meta/_data" /var/lib/garage/meta
mv "$src/var/lib/docker/volumes/storage_garage_data/_data" /var/lib/garage/data
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)"
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: hooks and SSH keys for the new paths"
forgejo-admin regenerate hooks
forgejo-admin regenerate keys
echo "== App stacks: images again (Docker's image store does not carry over)"
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"