Files
computer-playbook/roles/sys-svc-container/files/shell/reconcile_runtime.sh
kevinveenbirkenbachandClaude Opus 5 575a5a87ac refactor(layout): sort shipped scripts by language and split the unit suite
Every script a role ships now lives under `files/<language>/`, and the unit
suite is split the same way: `tests/unit/python/` beside new sibling trees for
the other languages. `tests/lint/filesystem/test_role_files_layout.py` keeps it
that way; directories whose shape is dictated from outside carry a `.nocheck`
naming the constraint (Docker build contexts, WordPress mu-plugins, the Joomla
extension, Odoo addons, Playwright suites, the `files/test/test.sh` harness
that `test-e2e-cli` resolves generically).

This cannot be applied in halves. Moving a file and repointing what names it
are the same change: 14 references resolve only at deploy time, so the tree is
green either before or after, never between.

Those 14 were found by static resolution over every reference class, not by the
test suite -- a full `make quality-high` was green while all of them were
broken:

  * 7 `src:` values built from a role variable holding a bare filename
    (svc-bkp-remote-2-local, decidim, espocrm, friendica, joomla, pixelfed x2).
    The variable doubles as the destination name inside the container, so the
    language segment belongs at the call site, not in the variable.
  * `sys-service/tasks/04_base.yml` resolved every consumer's systemd script
    through `first_found(..., errors='strict')` over `files/script.{sh,py}`.
    18 roles ship exactly that and have no template fallback: the play would
    have died on each.
  * `sys-front-inj-javascript` fell back to `<role>/files/javascript.js`; the
    interface now reads `files/javascript/javascript.js`.
  * `sys-front-inj-logout/templates/head_sub.j2` built the cache-busting mtime
    path through `path_absolute(... 'files', ...)`.
  * 4 `ansible.builtin.script:` invocations naming a bare command
    (pull.py, prune.sh, set_postgres_superuser_password.sh, ganesha-serve.sh).
  * `dev-python/files/shell/install.sh` pointed at a sibling role's script.

Depth-dependent paths inside the moved trees were bumped accordingly: 59
`parents[N]` computations, 12 relative climbs, and the per-role recover CLI,
whose path `cli/administration/recover/recoverers.py` still assembled from the
pre-move location.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-22 01:31:09 +02:00

83 lines
2.4 KiB
Bash

#!/usr/bin/env bash
#
# Reap container scopes that outlived a containerd restart, then resync dockerd.
#
# Prints RECONCILED when it acted, UNCHANGED when the runtime was consistent.
set -euo pipefail
STOP_TIMEOUT=120
RESTART_TIMEOUT=120
ACTIVE_TIMEOUT=60
# Param: $1 unit name
# Prints the unit's ActiveEnterTimestampMonotonic, or nothing for a unit systemd
# does not know, which answers 0.
started() {
local value
value="$(systemctl show --property=ActiveEnterTimestampMonotonic --value "$1")"
case "${value}" in
'' | 0) return 0 ;;
*) printf '%s' "${value}" ;;
esac
}
containerd_started="$(started containerd.service)"
if [ -z "${containerd_started}" ]; then
echo "UNCHANGED: no running containerd to compare against"
exit 0
fi
scopes="$(systemctl list-units 'docker-*.scope' --state=active --no-legend --plain | awk '{print $1}')"
orphans=()
while read -r scope; do
[ -n "${scope}" ] || continue
scope_started="$(started "${scope}")"
[ -n "${scope_started}" ] || continue
if [ "${scope_started}" -lt "${containerd_started}" ]; then
orphans+=("${scope}")
fi
done <<<"${scopes}"
if [ "${#orphans[@]}" -eq 0 ]; then
echo "UNCHANGED: containerd restarted but left no orphaned container scope"
exit 0
fi
reaped=()
for scope in "${orphans[@]}"; do
rc=0
timeout "${STOP_TIMEOUT}" systemctl stop "${scope}" || rc=$?
if [ "${rc}" -eq 5 ]; then
continue
fi
if [ "${rc}" -ne 0 ]; then
echo "FAILURE: could not stop ${scope} (rc ${rc})" >&2
exit 1
fi
if [ "$(systemctl is-active "${scope}" || true)" = "active" ]; then
echo "FAILURE: ${scope} is still active after being stopped" >&2
exit 1
fi
reaped+=("${scope}")
done
if [ "${#reaped[@]}" -gt 0 ]; then
timeout "${RESTART_TIMEOUT}" systemctl restart docker.service || true # nocheck: shell-or-true -- a restart rc says nothing about dockerd being up; the is-active poll below is the authority and fatals
for _ in $(seq "${ACTIVE_TIMEOUT}"); do
[ "$(systemctl is-active docker.service || true)" = "active" ] && break
sleep 1
done
if [ "$(systemctl is-active docker.service || true)" != "active" ]; then
echo "FAILURE: docker.service is not running after the reap; this node cannot start containers" >&2
exit 1
fi
fi
if [ "${#reaped[@]}" -eq 0 ]; then
echo "UNCHANGED: every orphaned scope had already gone away"
exit 0
fi
echo "RECONCILED: ${#reaped[@]} orphaned container scope(s) reaped, dockerd restarted"