diff --git a/nixos/hosts/asxpio-ops/apps.nix b/nixos/hosts/asxpio-ops/apps.nix new file mode 100644 index 0000000..4449755 --- /dev/null +++ b/nixos/hosts/asxpio-ops/apps.nix @@ -0,0 +1,38 @@ +# The app stacks (asxpio, datacenterbingo, blog, czsk, vhpbot) deploy +# themselves from their own repositories: CI copies a compose file and a +# .env to /opt/ and runs `docker compose up -d` as user deploy. +# +# Their compose files still expect the Docker-era storage stack: a +# network named "storage" and the hosts "postgres" and "garage" on it. +# Postgres and Garage are native services now, so every container on +# this host resolves those two names to the host, and the network exists +# empty. Nothing in the app repositories has to change for the move; +# dropping the storage network from their compose files is a later +# clean-up. +{ config, pkgs, ... }: +let + hosts = pkgs.writeText "container-hosts" '' + 10.10.0.10 postgres garage + ''; +in +{ + virtualisation.containers.containersConf.settings.containers.base_hosts_file = "${hosts}"; + # The API service (the socket the Docker CLI and Traefik use) reads it + # at start only + systemd.services.podman.restartTriggers = [ hosts ]; + + # `docker compose up --build` (vhpbot-prod builds on the host) starts + # BuildKit in a privileged container + asxpio.security.containers."buildx_buildkit_.*" = [ "privileged" ]; + + systemd.services.podman-network-storage = { + description = "Podman network storage (empty; see apps.nix)"; + wantedBy = [ "multi-user.target" ]; + path = [ config.virtualisation.podman.package ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + }; + script = "podman network exists storage || podman network create storage"; + }; +} diff --git a/nixos/hosts/asxpio-ops/default.nix b/nixos/hosts/asxpio-ops/default.nix index 176e6e0..217a176 100644 --- a/nixos/hosts/asxpio-ops/default.nix +++ b/nixos/hosts/asxpio-ops/default.nix @@ -4,6 +4,7 @@ { lib, ... }: { imports = [ + ./apps.nix ./crawler.nix ./forgejo.nix ./garage.nix diff --git a/nixos/migration/ops-data.sh b/nixos/migration/ops-data.sh index 4541521..557f1cf 100755 --- a/nixos/migration/ops-data.sh +++ b/nixos/migration/ops-data.sh @@ -46,6 +46,32 @@ 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 + 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 @@ -63,4 +89,16 @@ echo "== Forgejo: hooks and SSH keys for the new paths" forgejo-admin regenerate hooks forgejo-admin regenerate keys -echo "== Done. Check: systemctl --failed; garage status; a git clone over SSH and HTTPS" +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" diff --git a/nixos/modules/monitoring-agent.nix b/nixos/modules/monitoring-agent.nix index e6dec05..eee9df9 100644 --- a/nixos/modules/monitoring-agent.nix +++ b/nixos/modules/monitoring-agent.nix @@ -244,9 +244,9 @@ in pkgs.wireguard-tools pkgs.iproute2 pkgs.coreutils - config.virtualisation.podman.package - "/run/current-system/sw" # docker, the alias for podman + pkgs.docker-client # `docker ps` / `docker inspect`, against Podman ]; + environment.DOCKER_HOST = "unix:///run/podman/podman.sock"; serviceConfig = { Type = "oneshot"; ExecStart = "${pkgs.python3}/bin/python3 ${./security-metrics.py} ${securityConfig}"; diff --git a/nixos/modules/podman.nix b/nixos/modules/podman.nix index 5286e67..27fb33d 100644 --- a/nixos/modules/podman.nix +++ b/nixos/modules/podman.nix @@ -1,13 +1,16 @@ -# Containers, for what has no native NixOS service: Podman, no Docker. -# Declared containers (virtualisation.oci-containers) become systemd units -# named podman-.service. `docker` is an alias, and /run/docker.sock -# is Podman's Docker-compatible socket (CI runner, compose files). -{ ... }: +# Containers, for what has no native NixOS service: Podman, no Docker +# daemon. Declared containers (virtualisation.oci-containers) become +# systemd units named podman-.service. +# +# The `docker` command is the real Docker CLI (with compose and buildx) +# talking to rootful Podman's Docker-compatible socket, so the apps' CI +# deploys (`docker compose up -d`, `docker inspect`, `docker logs`) work +# unchanged, and root, deploy and the admins all see the same containers. +{ pkgs, ... }: { virtualisation.podman = { enable = true; - dockerCompat = true; - dockerSocket.enable = true; + dockerSocket.enable = true; # /run/docker.sock -> /run/podman/podman.sock defaultNetwork.settings.dns_enabled = true; # Every image update and CI job leaves an image behind autoPrune = { @@ -17,4 +20,12 @@ }; }; virtualisation.oci-containers.backend = "podman"; + + environment.systemPackages = [ pkgs.docker-client ]; + # Podman's DNS for container networks, on each bridge's gateway + asxpio.security.ports = [ + "tcp/53/aardvark-dns" + "udp/53/aardvark-dns" + ]; + environment.variables.DOCKER_HOST = "unix:///run/podman/podman.sock"; }