Files
computer-playbook/scripts/tests/deploy/swarm/utils/clean/lab_subnet.sh
T
kevinveenbirkenbachandClaude Opus 5 dc54e6d83f fix(swarm): stop the shell helpers from swallowing their own failures
Every one of these call sites ended in `|| true`, so a failure left no trace
and the surrounding script reported a clean run.

service_converged.sh needed the `|| true` for a reason that had nothing to do
with the command: under `set -o pipefail`, `head -10` closes the pipe and awk
dies of SIGPIPE, which pipefail then reports as the pipeline's status. That
made a genuine `container service ps` failure indistinguishable from a normal
truncation. Capping inside awk (`n++ < 10`) removes the SIGPIPE, so the command
status can be tested on its own and a real failure now prints why. give_up and
report_failed_tasks carried a byte-identical copy of that block, so the fix
lifts it into one report_tasks helper rather than applying it twice.

The swarm-clean scripts keep going after a failed step on purpose, since the
host layer below them clears what survives. They now say what did not work
before continuing, instead of continuing silently.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-28 13:20:26 +02:00

64 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Reclaim whatever occupies THIS worktree's swarm lab subnet.
#
# Required env: INFINITO_SWARM_LAB_SUBNET, INFINITO_SWARM_LAB_NET_NAME.
# Exit codes: 0 the subnet is free, 1 a network survived removal.
set -uo pipefail
_here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
_repo="$(cd "${_here}/../../../../../.." && pwd)"
# shellcheck source=scripts/meta/env/load.sh
source "${_repo}/scripts/meta/env/load.sh"
: "${INFINITO_SWARM_LAB_SUBNET:?INFINITO_SWARM_LAB_SUBNET must be set}"
: "${INFINITO_SWARM_LAB_NET_NAME:?INFINITO_SWARM_LAB_NET_NAME must be set}"
_occupants=""
for _net in $(docker network ls --format '{{.Name}}' | grep -E "${INFINITO_SWARM_LAB_NET_NAME}" || true); do
_subnet="$(docker network inspect "${_net}" \
--format '{{range .IPAM.Config}}{{.Subnet}}{{end}}' 2>/dev/null)"
if [ "${_subnet}" = "${INFINITO_SWARM_LAB_SUBNET}" ]; then
_occupants="${_occupants} ${_net}"
fi
done
if [ -z "${_occupants// /}" ]; then
echo ">>> lab-reclaim: ${INFINITO_SWARM_LAB_SUBNET} is free"
exit 0
fi
DIR_VAR_LIB="$(python3 -c "import yaml,sys;print(yaml.safe_load(open(sys.argv[1]))['DIR_VAR_LIB'])" \
"${_repo}/group_vars/all/05_paths.yml")"
_rc=0
for _net in ${_occupants}; do
_nodes="$(docker network inspect "${_net}" \
--format '{{range .Containers}}{{.Name}} {{end}}' 2>/dev/null)"
echo ">>> lab-reclaim: ${_net} holds ${INFINITO_SWARM_LAB_SUBNET}:${_nodes:+ ${_nodes}}"
if [ -n "${_nodes// /}" ]; then
# shellcheck disable=SC2086
if ! timeout 300 bash "${_here}/../unmount/nfs_mounts.sh" ${_nodes}; then
echo ">>> lab-reclaim: nfs detach reported failures on ${_net}; continuing to removal"
fi
fi
bash "${_here}/../unmount/host_state.sh" "${DIR_VAR_LIB}"
for _node in ${_nodes}; do
timeout 30 docker exec "${_node}" systemctl stop nfs-ganesha >/dev/null 2>&1
timeout 30 docker kill "${_node}" >/dev/null 2>&1
timeout 15 docker network disconnect -f "${_net}" "${_node}" >/dev/null 2>&1
timeout 30 docker rm -f "${_node}" >/dev/null 2>&1
done
timeout 30 docker network rm "${_net}" >/dev/null 2>&1
timeout 30 docker volume rm "${_net%-"${INFINITO_SWARM_LAB_NET_NAME}"}_nfs-export" >/dev/null 2>&1
if docker network inspect "${_net}" >/dev/null 2>&1; then
echo "!!! lab-reclaim: '${_net}' survived removal; clear with 'make swarm-clean'." >&2
_rc=1
fi
done
exit "${_rc}"