A reader of the time limit needs to know its limits: on expiry only molecule is signalled, so its ansible-playbook and workers run until the run container ends, and the run-label cleanup step removes anything they recreate. The default's reasoning holds at the default 2 lanes, and the variable is for local runs and tests only. The timeout test now runs on one lane, so beta running proves the stalled lane was freed. The workflows rule gives the limit its reason and names the two tests that hold it. Refs #474 Assisted-by: Claude Code
189 lines
7.6 KiB
Bash
Executable File
189 lines
7.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# The Molecule workflow's "Run the scenarios" step: `molecule test` for each scenario directory on
|
|
# stdin, MOLECULE_LANES (default 2, also when empty) at a time, longest first, each with its own
|
|
# log and ANSIBLE_HOME under <work-dir>/<n>/ (no collection is installed there:
|
|
# .config/molecule/config.yml sets prerun false). A scenario still running after
|
|
# MOLECULE_SCENARIO_TIMEOUT seconds (default 1800, also when empty) is stopped and fails; the
|
|
# variable is for local runs and tests: the workflow does not pass it into the ci container.
|
|
# Exit 1 on a failure.
|
|
#
|
|
# scripts/run_molecule_scenarios.sh <work-dir> < scenario-dirs
|
|
set -euo pipefail
|
|
|
|
# 2: a full run took 34 min at 2 lanes and 35 at 4, so more lanes only slow each scenario.
|
|
lanes="${MOLECULE_LANES:-2}"
|
|
if ! [[ "$lanes" =~ ^[1-9][0-9]*$ ]]; then
|
|
echo "MOLECULE_LANES must be a positive integer, not '$lanes'" >&2
|
|
exit 2
|
|
fi
|
|
# 1800: the longest scenario takes about 8.5 min, and at the default 2 lanes two hung ones plus the
|
|
# rest still end inside the job's 90 min, so a hang fails on its own with its log printed.
|
|
limit="${MOLECULE_SCENARIO_TIMEOUT:-1800}"
|
|
if ! [[ "$limit" =~ ^[1-9][0-9]*$ ]]; then
|
|
echo "MOLECULE_SCENARIO_TIMEOUT must be a positive integer, not '$limit'" >&2
|
|
exit 2
|
|
fi
|
|
if [ "$#" -ne 1 ]; then
|
|
echo "usage: $0 <work-dir> < scenario-dirs" >&2
|
|
exit 2
|
|
fi
|
|
work="$1"
|
|
|
|
label_of() {
|
|
case "$1" in
|
|
extensions/*) echo "${1##*/}" ;;
|
|
*) local role="${1#roles/}"; echo "${role%%/*}/${1##*/}" ;;
|
|
esac
|
|
}
|
|
|
|
# Removes the scenario's containers by the platform names in its molecule.yml, expanded by molecule's
|
|
# interpolator, in place of molecule's destroy, which the scenarios leave out: its driver waits about
|
|
# 5.6 s per scenario for the deletion (about 84 s over 15), and the whole destroy phase measured about
|
|
# 103 s a full run. The ci image has no docker CLI: python3 is the one molecule runs on, with PyYAML
|
|
# and the Docker SDK of molecule's Docker driver.
|
|
remove_instances() {
|
|
python3 -I - "$1/molecule.yml" <<'EOF'
|
|
import os
|
|
import sys
|
|
|
|
import docker
|
|
import yaml
|
|
from molecule.interpolation import Interpolator, TemplateWithDefaults
|
|
|
|
with open(sys.argv[1]) as f:
|
|
text = Interpolator(TemplateWithDefaults, os.environ).interpolate(f.read())
|
|
names = [p["name"] for p in yaml.safe_load(text)["platforms"]]
|
|
if not names:
|
|
sys.exit("no platform names in " + sys.argv[1])
|
|
containers = docker.from_env().containers
|
|
for name in names:
|
|
for container in containers.list(all=True, filters={"name": f"^/{name}$"}):
|
|
if container.name == name:
|
|
container.remove(force=True)
|
|
EOF
|
|
}
|
|
|
|
# Runs scenario $2 into $work/$1/. Its outcome is the rc file, which a lane that died lacks.
|
|
run_one() {
|
|
local out="$work/$1" dir="$2" start=$SECONDS rc=0
|
|
mkdir -p "$out/ansible"
|
|
(
|
|
export ANSIBLE_HOME="$out/ansible"
|
|
# a role scenario runs from its role's directory, as molecule expects
|
|
case "$dir" in roles/*) cd "${dir%/molecule/*}" || exit 1 ;; esac
|
|
# --foreground keeps timeout and molecule in this script's process group, so a cancel that
|
|
# kills the group still reaches them. On expiry it signals molecule alone: its ansible-playbook
|
|
# and workers run on until the run container ends, and the workflow's "Remove this run's
|
|
# containers" step removes any container they recreate late.
|
|
timeout --foreground --kill-after=30 "$limit" molecule test -s "${dir##*/}"
|
|
) >"$out/log" 2>&1 </dev/null || rc=$?
|
|
# 124 after TERM, 137 when the KILL of --kill-after was needed
|
|
if { [ "$rc" -eq 124 ] || [ "$rc" -eq 137 ]; } && [ "$((SECONDS - start))" -ge "$limit" ]; then
|
|
echo "timed out after $limit s" >>"$out/log"
|
|
fi
|
|
if ! remove_instances "$dir" >>"$out/log" 2>&1; then
|
|
echo "could not remove the containers of $dir" >>"$out/log"
|
|
if [ "$rc" -eq 0 ]; then rc=1; fi
|
|
fi
|
|
echo "$((SECONDS - start))" >"$out/seconds"
|
|
echo "$rc" >"$out/rc"
|
|
echo "finished $(label_of "$dir"): exit $rc after $((SECONDS - start)) s"
|
|
}
|
|
|
|
dirs=()
|
|
while IFS= read -r dir; do
|
|
if [ -n "$dir" ]; then dirs+=("$dir"); fi
|
|
done
|
|
mkdir -p "$work"
|
|
|
|
# Seconds each scenario took in the last full run at the default lane count, so the longest start
|
|
# first and none started late sets the end of the run. Keyed by scenario directory, not label:
|
|
# refresh from a full run's `finished <label>: exit 0 after S s` lines, each label mapped back to
|
|
# its directory. A scenario missing here is assumed long and starts first.
|
|
declare -A last_seconds=(
|
|
[roles/system_baseline/molecule/default]=568
|
|
[roles/ufw/molecule/default]=525
|
|
[roles/traefik/molecule/default]=484
|
|
[roles/gitea_runner/molecule/default]=398
|
|
[roles/docker/molecule/default]=352
|
|
[roles/cloudflared/molecule/default]=305
|
|
[roles/tailscale/molecule/default]=220
|
|
[roles/fail2ban/molecule/default]=161
|
|
[roles/gitea/molecule/default]=137
|
|
[roles/systemd_hardening/molecule/default]=134
|
|
[roles/_binary_update/molecule/default]=132
|
|
[roles/nfs_client/molecule/default]=126
|
|
[roles/user_management/molecule/default]=115
|
|
[roles/pihole/molecule/default]=112
|
|
[roles/sshd/molecule/default]=65
|
|
[roles/pve_lxc/molecule/default]=44
|
|
[roles/_binary_update/molecule/check_mode]=42
|
|
[roles/network_interfaces/molecule/default]=36
|
|
[extensions/molecule/image-smoke]=25
|
|
[extensions/molecule/image-smoke-2604]=21
|
|
)
|
|
# The indices of dirs in start order; ties keep the selection order (a stable sort).
|
|
mapfile -t start_order < <(
|
|
for n in "${!dirs[@]}"; do printf '%s %s\n' "${last_seconds[${dirs[$n]}]:-999999}" "$n"; done |
|
|
sort -s -k1,1nr | cut -d' ' -f2
|
|
)
|
|
|
|
# A cancelled job must not leave lanes behind that recreate containers after the next run's
|
|
# cleanup. A signal to this script stops each lane's tree so it cannot fork, then terminates it,
|
|
# with the whole group of any descendant that leads one: ansible's workers detach into their own.
|
|
# A kill of the step's process group reaches the lanes and molecule, not those detached workers.
|
|
kill_tree() {
|
|
local child leader=""
|
|
kill -STOP "$1" 2>/dev/null || return 0
|
|
# never a group this script or its caller is in: only one a descendant leads
|
|
if [ "$(ps -o pgid= -p "$1" | tr -d ' ')" = "$1" ] && [ "$1" != "$own_group" ]; then
|
|
leader=1
|
|
kill -STOP -- "-$1" 2>/dev/null || true
|
|
fi
|
|
for child in $(ps -o pid= --ppid "$1"); do kill_tree "$child"; done
|
|
if [ -n "$leader" ]; then
|
|
kill -TERM -- "-$1" 2>/dev/null || true
|
|
kill -CONT -- "-$1" 2>/dev/null || true
|
|
fi
|
|
kill -TERM "$1" 2>/dev/null || true
|
|
kill -CONT "$1" 2>/dev/null || true
|
|
}
|
|
stop_lanes() {
|
|
# a second signal must not abort the cleanup and leave a lane stopped
|
|
trap '' TERM INT HUP
|
|
local pid
|
|
for pid in $(jobs -rp); do kill_tree "$pid"; done
|
|
wait || true
|
|
exit "$1"
|
|
}
|
|
own_group="$(ps -o pgid= -p "$$" | tr -d ' ')"
|
|
trap 'stop_lanes 143' TERM
|
|
trap 'stop_lanes 130' INT
|
|
trap 'stop_lanes 129' HUP
|
|
|
|
for n in "${start_order[@]}"; do
|
|
while [ "$(jobs -rp | wc -l)" -ge "$lanes" ]; do wait -n || true; done
|
|
run_one "$n" "${dirs[$n]}" </dev/null &
|
|
done
|
|
wait
|
|
|
|
failed=""
|
|
ran=0
|
|
times=""
|
|
for n in "${!dirs[@]}"; do
|
|
label="$(label_of "${dirs[$n]}")"
|
|
seconds="$(cat "$work/$n/seconds" 2>/dev/null || echo '?')"
|
|
times="$times $label: $seconds s"$'\n'
|
|
echo "::group::molecule test: $label ($seconds s)"
|
|
cat "$work/$n/log" 2>/dev/null || true
|
|
echo "::endgroup::"
|
|
if [ -f "$work/$n/rc" ]; then
|
|
ran=$((ran + 1))
|
|
if [ "$(cat "$work/$n/rc")" != 0 ]; then failed="$failed $label"; fi
|
|
fi
|
|
done
|
|
printf 'Scenarios run: %s\n%s' "$ran" "$times"
|
|
# A lane that died without a result must not pass quietly.
|
|
test "$ran" -eq "${#dirs[@]}" || { echo "::error::ran $ran of ${#dirs[@]} selected scenarios"; exit 1; }
|
|
test -z "$failed" || { echo "::error::Failed:$failed"; exit 1; }
|