From c52fa349030c30405e539d0a04c62bc419b48b37 Mon Sep 17 00:00:00 2001 From: neuromaxer Date: Thu, 8 Oct 2026 16:19:12 +0200 Subject: [PATCH] Make --purge-data actually purge project data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit teardown.sh --purge-data deleted $APPX_DATA, which in container mode holds only the SQLite DB and TLS certs. Every project's files and session transcripts live in the builder-workspace volume and the inner app images in builder-podman-storage, neither of which the script touched — so "purge all data" left all project data on disk, and a later bootstrap silently inherited the old workspace. Split the two kinds of state: - The outer container is a PROCESS, so it is now removed unconditionally. It runs with `--restart unless-stopped`, which means the Docker daemon kept it (and the agent inside it) alive and resurrected it on reboot even after the appx service was gone. Removing it is not destructive — its state is in the volumes, and appx's EnsureRunning recreates it on next start. - The volumes are DATA, so they are removed only under --purge-data, after the container that mounts them is gone (docker volume rm refuses while a container still references a volume). Container and volume names are overridable per deployment, so they are read from appx.env alongside APPX_DATA before that file is deleted; assuming the defaults would have silently skipped a renamed deployment's volumes and left all its project data behind. When docker is absent, the volume step warns on stderr rather than reporting a successful purge. The non-purge path now also states where project data actually is, since "kept data: /var/lib/appx" previously implied that directory was all of it. Refs #7 Co-Authored-By: Claude Opus 5 (1M context) --- CLAUDE.md | 1 + deploy/teardown.sh | 90 ++++++++++++++++++++++++++++++++----- docs/readme/self-hosting.md | 21 ++++++++- 3 files changed, 100 insertions(+), 12 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 16fbb5f..978e938 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -153,6 +153,7 @@ Add or update tests when behavior changes, especially for server routes, databas - Deploy hosts must be **amd64**: the published agent-server image is amd64-only and is no longer built on the box. - The agent (agent-server + Pi) runs as an unprivileged uid **inside** the outer container, not as a host user; provider secrets reach it via the service env (`/etc/appx/secrets.env`, `root:root 0600`), forwarded into the container by name. - The Docker daemon (`--restart unless-stopped`) keeps the outer container alive across crash + reboot; appx's startup `EnsureRunning` re-attaches idempotently (never auto-recreates on drift). `appx.service` is ordered `After=docker.service`. +- `deploy/teardown.sh` always removes the outer container (a process — `--restart unless-stopped` would otherwise keep the agent running after appx is gone; appx recreates it from the volumes). `--purge-data` additionally removes `builder-workspace` and `builder-podman-storage`, which is where **all** project data lives — purging only `APPX_DATA` would leave every project behind and let a later bootstrap inherit the old workspace. Container and volume names are read from `appx.env` before it is deleted, so a renamed deployment is not skipped. - `appx` is in the `docker` group (root-equivalent — accepted on a dedicated box; Stage 5 scopes it down). It binds 443 via `CAP_NET_BIND_SERVICE`, not root. - Provider traffic from Pi goes through the Appx egress proxy (bridge gateway in container mode); loopback traffic to agent-server stays local. - The HSTS header includes `includeSubDomains`. Do not point appx at a shared domain that also hosts HTTP services on subdomains. diff --git a/deploy/teardown.sh b/deploy/teardown.sh index 9f8b9f8..610e145 100644 --- a/deploy/teardown.sh +++ b/deploy/teardown.sh @@ -7,9 +7,20 @@ # Must be run as root. Safe to run multiple times (idempotent). # # Shared build/runtime tools (go, node, pi, claude, uv) are intentionally left -# in place — they are not appx-specific. Pass --purge-data to also delete the -# data directory and the agent user's home (DESTRUCTIVE: removes all projects, -# the SQLite DB, TLS certs, and session transcripts). +# in place — they are not appx-specific. +# +# Pass --purge-data to also delete every piece of appx state (DESTRUCTIVE and +# IRREVERSIBLE): +# - the data directory (SQLite DB, TLS certs) and the legacy agent user's home +# - /etc/appx/secrets.env (provider credentials) +# - the outer container and BOTH of its named volumes: builder-workspace +# (every project's files and Pi session transcripts) and +# builder-podman-storage (inner app images and containers) +# +# The volumes are the part that is easy to miss: in container mode they, not the +# data directory, hold all project data, and nothing else in this script touches +# them. Without removing them, --purge-data leaves projects behind and a +# subsequent bootstrap silently inherits the old workspace. set -euo pipefail @@ -28,14 +39,23 @@ done ENV_FILE="/etc/appx/appx.env" -# Resolve the data dir from the env file (fall back to the default) before we -# delete the env file. +# Resolve everything we need from the env file BEFORE deleting it. The container +# and volume names are overridable per deployment, so read them rather than +# assuming the defaults — otherwise --purge-data would silently skip a renamed +# container's volumes and leave all project data behind. DATA_DIR="/var/lib/appx" +CONTAINER_NAME="builder-outer" +WORKSPACE_VOLUME="builder-workspace" +PODMAN_VOLUME="builder-podman-storage" if [ -f "$ENV_FILE" ]; then _APPX_DATA=$(grep '^APPX_DATA=' "$ENV_FILE" | cut -d= -f2- || true) - if [ -n "$_APPX_DATA" ]; then - DATA_DIR="${_APPX_DATA%/}" - fi + [ -n "$_APPX_DATA" ] && DATA_DIR="${_APPX_DATA%/}" + _CNAME=$(grep '^APPX_AGENT_CONTAINER_NAME=' "$ENV_FILE" | cut -d= -f2- || true) + [ -n "$_CNAME" ] && CONTAINER_NAME="$_CNAME" + _WSVOL=$(grep '^APPX_AGENT_WORKSPACE_VOLUME=' "$ENV_FILE" | cut -d= -f2- || true) + [ -n "$_WSVOL" ] && WORKSPACE_VOLUME="$_WSVOL" + _PDVOL=$(grep '^APPX_AGENT_PODMAN_VOLUME=' "$ENV_FILE" | cut -d= -f2- || true) + [ -n "$_PDVOL" ] && PODMAN_VOLUME="$_PDVOL" fi # --------------------------------------------------------------------------- @@ -56,14 +76,34 @@ pkill -u appx-agent -f '(^|/)agent-server( |$)|agent-server/dist/server\.js' 2>/ pkill -u appx -f '(^|/)appx( |$)' 2>/dev/null || true # --------------------------------------------------------------------------- -# 2. Binaries +# 2. Outer agent container +# --------------------------------------------------------------------------- + +# The container runs with `--restart unless-stopped`, so the Docker daemon keeps +# it (and the agent inside it) alive and brings it back on reboot even after the +# appx service is gone. Remove it unconditionally: it is a process, not data — +# its state lives in the named volumes, which survive here and are only deleted +# under --purge-data below. appx's EnsureRunning recreates it on next startup. +if command -v docker >/dev/null 2>&1; then + if docker inspect "$CONTAINER_NAME" >/dev/null 2>&1; then + docker rm -f "$CONTAINER_NAME" >/dev/null 2>&1 || true + echo "removed outer container: $CONTAINER_NAME (volumes kept)" + else + echo "no outer container named $CONTAINER_NAME" + fi +else + echo "docker not installed — skipping outer container removal" +fi + +# --------------------------------------------------------------------------- +# 3. Binaries # --------------------------------------------------------------------------- rm -f /usr/local/bin/appx /usr/local/bin/agent-server /usr/local/bin/opencode echo "removed binaries: /usr/local/bin/{appx,agent-server,opencode}" # --------------------------------------------------------------------------- -# 3. Users and groups +# 4. Users and groups # --------------------------------------------------------------------------- # userdel -r would remove home dirs; we manage data deletion explicitly below so @@ -83,7 +123,7 @@ for grp in projects appx-agent; do done # --------------------------------------------------------------------------- -# 4. Config + data +# 5. Config + data # --------------------------------------------------------------------------- rm -f "$ENV_FILE" @@ -108,5 +148,33 @@ else echo "kept data: $DATA_DIR and /home/appx-agent (re-run with --purge-data to delete)" fi +# The named volumes, NOT the data directory, hold all project data in container +# mode: builder-workspace has every project's files and Pi session transcripts, +# builder-podman-storage has the inner app images. They are removable only now +# that the container referencing them is gone (section 2) — `docker volume rm` +# refuses while a container still uses it. +if [ "$PURGE_DATA" -eq 1 ]; then + if command -v docker >/dev/null 2>&1; then + for vol in "$WORKSPACE_VOLUME" "$PODMAN_VOLUME"; do + if docker volume inspect "$vol" >/dev/null 2>&1; then + if docker volume rm "$vol" >/dev/null 2>&1; then + echo "purged volume: $vol" + else + # Almost always another container still mounting it. + echo "WARNING: could not remove volume $vol — still in use?" >&2 + echo " inspect with: docker ps -a --filter volume=$vol" >&2 + fi + else + echo "no volume named $vol" + fi + done + else + echo "WARNING: docker not installed — project data in the named volumes was NOT purged" >&2 + fi +else + echo "kept volumes: $WORKSPACE_VOLUME and $PODMAN_VOLUME (ALL project files and" + echo " sessions live here, not in $DATA_DIR; --purge-data deletes them)" +fi + echo "" echo "Teardown complete. Shared tools (go, node, pi, claude, uv) were left in place." diff --git a/docs/readme/self-hosting.md b/docs/readme/self-hosting.md index 5f00814..1a3adb2 100644 --- a/docs/readme/self-hosting.md +++ b/docs/readme/self-hosting.md @@ -199,4 +199,23 @@ docker logs -f builder-outer # agent-server (inside the outer container) | `deploy/agent-version.sh` | sourced, not run | Resolves `AGENT_VERSION` → `$AGENT_IMAGE` for the other scripts | | `deploy/appx.service` | systemd unit | `appx` unit (container mode; ordered after `docker.service`) | | `deploy/verify-installation.sh` | After any change | Full system verification | -| `deploy/teardown.sh` | Uninstall & cleanup | Reverse everything created by bootstrap.sh | +| `deploy/teardown.sh` | Uninstall & cleanup | Reverse everything created by bootstrap.sh (see below) | + +### Uninstalling + +```bash +sudo ./deploy/teardown.sh # keep all data +sudo ./deploy/teardown.sh --purge-data # DESTRUCTIVE: delete everything +``` + +Both remove the services, binaries, users, config, and the **outer container** — +the container runs with `--restart unless-stopped`, so leaving it would keep the +agent alive and restarting after appx is gone. Removing it is safe: its state is +in the named volumes, and appx recreates the container on next start. + +`--purge-data` additionally deletes the data directory *and* both named volumes: +`builder-workspace` (every project's files and Pi session transcripts) and +`builder-podman-storage` (inner app images). **This is where all project data +lives** — a purge that spared the volumes would leave every project behind and a +later bootstrap would silently inherit the old workspace. There is no undo, and +no backup is taken.