Files
cc-ci-orchestrator/.claude/skills/upgrade-all/sweep-orphans.sh
T
notplantsandClaude Fable 5.1 48226dd78f opencode UI stack is ccci-opencode-ui, on the weekly sweep's keep-list; journal: first weekly run on the new host
The 2026-09-07 weekly run's step-0 orphan sweep removed the `opencode-ui`
swarm stack (the traefik route to the opencode web UI) because it was not
on sweep-orphans.sh's keep-list. Renamed to ccci-opencode-ui alongside the
other control-plane stacks and added to KEEP_RE.

Journal: the run itself (lasuite-docs #8, n8n #7 GREEN on the new Drone,
report week-2026-09-07 published) ran entirely on the new host.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FqkQq3CDmFWcQ7u1LzoyRz
2026-09-07 22:13:43 +00:00

108 lines
6.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Sweep orphaned test deployments + debug debris left by PREVIOUS cc-ci runs.
#
# Runs ON the cc-ci host (root) — pipe it in: ssh cc-ci 'bash -s' < sweep-orphans.sh
# Invoked at the START of /upgrade-all so a leaked stack/container/volume/process from a prior run
# (a teardown that crashed, a manual debug probe, a killed agent) does not contend for the shared
# Swarm or skew the survey. Idempotent + safe to run anytime: a no-op when the host is already clean.
#
# SAFE BY ALLOWLIST. It removes ONLY things NOT on the keep-list, so it can never take down infra or
# the warm canonicals. The keep-list (leading name prefix) is:
# - traefik, drone, backups : Swarm + CI infra
# - ccci-bridge / -dashboard / -reports / -opencode-ui: the cc-ci control plane (opencode-ui is
# the traefik route to the orchestrator's opencode web UI, deployed by the host's nix config —
# the 2026-09-07 run reaped it as an "orphan" before it was on this list)
# - warm-* : warm canonicals (idle persistent deps reused across runs;
# their retained volumes are spared too)
# Everything else deployed on the Swarm is a per-run test stack and is fair game.
set -uo pipefail
export PATH=/run/current-system/sw/bin:$PATH
KEEP_RE='^(traefik|drone|backups|ccci-(bridge|dashboard|reports|opencode-ui)|warm-)'
removed=0
echo "== orphan sweep: scanning (keep-list: infra + warm-* canonicals) =="
# 1) Orphan Swarm stacks — any deployed stack not on the keep-list is a leftover per-run test deploy
# (the harness deploys each recipe under its own per-run stack; a clean run tears it down).
mapfile -t STACKS < <(docker stack ls --format '{{.Name}}' 2>/dev/null)
for s in "${STACKS[@]}"; do
[ -z "$s" ] && continue
if printf '%s' "$s" | grep -Eq "$KEEP_RE"; then continue; fi
echo " orphan stack -> removing: $s"
docker stack rm "$s" >/dev/null 2>&1 || true
removed=$((removed + 1))
done
# wait (bounded) for removed stacks' services to drain so their volumes free up for step 3
for _ in $(seq 1 30); do
[ -z "$(docker service ls --format '{{.Name}}' 2>/dev/null | grep -Ev "$KEEP_RE")" ] && break
sleep 2
done
# 2) Orphan standalone containers — running/exited containers NOT managed by Swarm (no
# com.docker.swarm.service.id label) are debug `docker run` leftovers (e.g. the plausible
# clickhouse entrypoint probes). Remove only ones started > 30 min ago, so an in-flight manual
# probe started right before the run is spared.
now=$(date +%s)
for c in $(docker ps -aq 2>/dev/null); do
sid=$(docker inspect -f '{{ index .Config.Labels "com.docker.swarm.service.id" }}' "$c" 2>/dev/null)
[ -n "$sid" ] && continue # Swarm-managed → keep
started=$(docker inspect -f '{{.State.StartedAt}}' "$c" 2>/dev/null)
st=$(date -d "$started" +%s 2>/dev/null || echo "$now")
[ $((now - st)) -lt 1800 ] && continue # younger than 30 min → spare a fresh manual probe
echo " orphan container -> removing: $(docker inspect -f '{{.Name}} ({{.Config.Image}})' "$c" 2>/dev/null)"
docker rm -f "$c" >/dev/null 2>&1 || true
removed=$((removed + 1))
done
# 3) Leaked volumes — dangling (referenced by no container) volumes left by removed test stacks.
# Spare warm-* volumes: a warm canonical idles UNDEPLOYED with its data volume retained, so its
# volume is legitimately dangling and must NOT be pruned.
for v in $(docker volume ls -qf dangling=true 2>/dev/null); do
printf '%s' "$v" | grep -Eq "$KEEP_RE" && continue
docker volume rm "$v" >/dev/null 2>&1 && { echo " leaked volume -> removed: $v"; removed=$((removed + 1)); }
done
# 4) Orphan debug host-processes — reparented (ppid==1) `timeout … docker run …` wrappers left by
# manual recipe probes; they outlive their container and never self-reap. Kill by explicit PID
# (NEVER pkill -f, which would self-match this script's own command line).
for p in $(ps -eo pid=,ppid=,cmd= 2>/dev/null | awk '$2==1 && /timeout/ && /docker[[:space:]]+run/ {print $1}'); do
echo " orphan debug wrapper -> killing pid $p"
kill "$p" 2>/dev/null || true
removed=$((removed + 1))
done
# 5) Stray exited containers (debug one-shots) — best-effort prune.
docker container prune -f >/dev/null 2>&1 || true
# 6) Unused IMAGES — the one that actually took CI down. Every run pulls each recipe's images and
# nothing ever removed the old ones: on 2026-08-11 they had grown to 72GB (63GB of it unused),
# the root filesystem hit 100% under two concurrent runs, and the harness died at startup with
# `OSError: [Errno 28] No space left on device: '/var/lib/cc-ci-runs/<build>'`. Every !testme
# from build 1236 to 1242 failed that way — with no results.json, so the PR badges just read
# "failure" and looked like recipe regressions.
#
# Only prune above a threshold, so a healthy host keeps its layer cache and runs stay fast.
# `image prune -a` removes only images no container references, so anything deployed (infra +
# warm-* canonicals) is untouched; anything else is re-pulled on demand.
#
# Volumes are deliberately NOT pruned here — see the KEEP_RE guard in (3): warm-* canonicals are
# data-warm and their volumes are legitimately dangling between runs.
DISK_PRUNE_PCT="${DISK_PRUNE_PCT:-60}"
used_pct="$(df --output=pcent / 2>/dev/null | tail -1 | tr -dc '0-9')"
if [ -n "$used_pct" ] && [ "$used_pct" -ge "$DISK_PRUNE_PCT" ]; then
echo " disk ${used_pct}% >= ${DISK_PRUNE_PCT}% -> pruning unused images"
freed="$(docker image prune -af 2>/dev/null | awk '/Total reclaimed space/ {print $4, $5}')"
echo " reclaimed: ${freed:-0B}; disk now $(df -h / | tail -1 | awk '{print $5" used, "$4" free"}')"
else
echo " disk ${used_pct:-?}% < ${DISK_PRUNE_PCT}% -> keeping image cache"
fi
if [ "$removed" -eq 0 ]; then
echo "== orphan sweep: clean (nothing to remove) =="
else
echo "== orphan sweep: removed/killed $removed orphan(s) =="
fi
echo "-- surviving Swarm services (should be infra + warm-* only) --"
docker service ls --format '{{.Name}}' 2>/dev/null | sort