Pin molecule and its Docker driver in requirements-dev.txt and bake them, with the driver's collection, into the ci image. The Molecule job runs docker run on the pinned ci image with the Docker socket and the checkout mounted at the same path, as the runner user. A pull request whose pins differ from the image installs its own pins first, so a core bump runs its scenarios on the new core. gitea_runner no longer installs the molecule venv and collections. Closes #225 Assisted-by: Claude Code
133 lines
4.6 KiB
Bash
Executable File
133 lines
4.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 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.
|
|
# 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 sys
|
|
|
|
import docker
|
|
import yaml
|
|
|
|
names = [p["name"] for p in yaml.safe_load(open(sys.argv[1]))["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
|
|
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; }
|