A full run executes 15 scenarios one after another, about 1100 s in total, with the slowest (traefik/default) at about 216 s. The scenarios share nothing on the runner's Docker daemon but the daemon itself: their container names are unique. So scripts/run_molecule_scenarios.sh now runs MOLECULE_LANES (default 4, a positive integer) of them at once inside the one job, with the slowest scenario as the floor; the saving will be measured in CI. Each scenario gets its own ANSIBLE_HOME under the job tree. With Runtime(isolated=False), ansible-compat uses ANSIBLE_HOME as molecule's cache dir: molecule's prerun installs the collection under test there with `ansible-galaxy collection install --force` (which first deletes the installed copy), and the scenario's ephemeral directory (inventory, ansible.cfg, state) lives under it. A shared one would let a scenario delete the copy another is running. Each scenario's output goes to its own log, printed afterwards in the selection order inside its own group with its wall time, plus a timing summary. The failure list, the ran-N-of-M guard, </dev/null stdin, the role-directory cwd and the per-scenario container removal are kept. A TERM, INT or HUP to the script stops each lane's whole process tree, including the process groups of ansible's detached workers (they call setsid), terminates it, ignores further signals until done, and exits 128 plus the first signal. A kill of the step's process group reaches the lanes and molecule but not ansible's detached workers. Either way a cancelled job leaves no molecule behind to recreate a container after the next run's cleanup. The logic moved from the workflow into a script so a unit test with stub molecule and docker can hold it; the test also fails a scenario whose platform name is not unique or not matched by the stale-container filter, which is what makes running side by side safe. Assisted-by: Claude Code:claude-opus-5-5
125 lines
4.5 KiB
Bash
Executable File
125 lines
4.5 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 4) at a time, each with its own log and ANSIBLE_HOME under
|
|
# <work-dir>/<n>/ (molecule reinstalls the collection there with --force). Exit 1 on a failure.
|
|
#
|
|
# scripts/run_molecule_scenarios.sh <work-dir> < scenario-dirs
|
|
set -euo pipefail
|
|
|
|
lanes="${MOLECULE_LANES:-4}"
|
|
if ! [[ "$lanes" =~ ^[1-9][0-9]*$ ]]; then
|
|
echo "MOLECULE_LANES must be a positive integer, not '$lanes'" >&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 literal platform names in its molecule.yml, 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.
|
|
# python3 is the molecule venv's, with PyYAML.
|
|
remove_instances() {
|
|
local names ids
|
|
local py='import sys, yaml; print(*(p["name"] for p in yaml.safe_load(open(sys.argv[1]))["platforms"]))'
|
|
names="$(python3 -I -c "$py" "$1/molecule.yml")" || return 1
|
|
[ -n "$names" ] || return 1
|
|
# shellcheck disable=SC2046,SC2086 # names and ids are one word each
|
|
ids="$(docker ps -aq $(printf -- '--filter name=^/%s$ ' $names))" || return 1
|
|
# shellcheck disable=SC2086
|
|
if [ -n "$ids" ]; then docker rm -f $ids >/dev/null || return 1; fi
|
|
}
|
|
|
|
# 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
|
|
molecule test -s "${dir##*/}"
|
|
) >"$out/log" 2>&1 </dev/null || rc=$?
|
|
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"
|
|
|
|
# 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 "${!dirs[@]}"; 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; }
|