build(dev): install the pinned collections in dev-setup

dev-setup.sh installs requirements.yml into .ansible/collections of the clone and
exports ANSIBLE_HOME and ANSIBLE_COLLECTIONS_PATH from the hook scripts, so local
ansible-lint resolves the collection versions CI does and not the control host's
~/.ansible (#118, part of #80). The install scopes the path to its target so a
copy elsewhere is not skipped as already installed. A hook written by the previous
version gains only the new block.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNmHMc85bomfevwUDG
This commit is contained in:
Defame1297andClaude Sonnet 5.5 committed 2026-11-14 13:52:09 +00:00
1 parent 12fe748bdd
commit bb57077fb3
3 files changed
+96 -9

No files matched your search

+29 -6
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env bash
# Sets up a local checkout for development: a virtualenv with the pinned tools
# (requirements-dev.txt, the versions CI runs) and git hooks that find them.
# (requirements-dev.txt, the versions CI runs), the pinned collections the roles call
# (requirements.yml) and git hooks that find both.
#
# scripts/dev-setup.sh # KAMINO_PYTHON=python3.13 scripts/dev-setup.sh to pick one
#
@@ -16,8 +17,11 @@
# Some hooks of .pre-commit-config.yaml are `language: system` (REUSE, changelog lint,
# ansible-lint): they run their tools by name, so the tools must be on PATH when git
# runs the hook. `pre-commit install` does not arrange that, so this script puts the
# virtualenv first on PATH inside the installed hook scripts. It is idempotent; run it
# again after changing requirements-dev.txt or the hook types.
# virtualenv first on PATH inside the installed hook scripts. It also points ANSIBLE_HOME
# at .ansible/ of this clone and ANSIBLE_COLLECTIONS_PATH at the collections installed into
# it, so ansible-lint resolves the same collection versions as CI (the ci image bakes
# requirements.yml) and never the ones in the control host's ~/.ansible. It is idempotent;
# run it again after changing requirements-dev.txt, requirements.yml or the hook types.
set -euo pipefail
# ansible-core 2.22 (requirements-dev.txt) needs Python 3.13+. Renovate raises that pin, so
@@ -119,7 +123,16 @@ fi
.venv/bin/pip install --quiet --disable-pip-version-check -r requirements-dev.txt
.venv/bin/pre-commit install --install-hooks
# The collections of requirements.yml, exactly as pinned, into this clone (.ansible/ is ignored by
# git and by the Collection build). ANSIBLE_COLLECTIONS_PATH is set to the target for the install:
# a copy found elsewhere (~/.ansible/collections) would be skipped as already installed.
collections="$root/.ansible/collections"
mkdir -p "$collections"
ANSIBLE_COLLECTIONS_PATH="$collections" .venv/bin/ansible-galaxy collection install \
-r requirements.yml -p "$collections"
marker="# kamino-dev-setup: virtualenv first on PATH"
collections_marker="# kamino-dev-setup: collections from this clone"
# default_install_hook_types in .pre-commit-config.yaml names these; keep in step.
for hook in pre-commit commit-msg; do
file="$(git -C "$root" rev-parse --path-format=absolute --git-path "hooks/$hook")"
@@ -127,12 +140,22 @@ for hook in pre-commit commit-msg; do
echo "error: $file was not installed by 'pre-commit install'; check default_install_hook_types in .pre-commit-config.yaml." >&2
exit 1
fi
# Each block is added on its own, so a hook installed by an older version of this script
# (PATH only) gains the collections path without a second PATH line.
add=()
if ! grep -qxF "$marker" "$file"; then
# %q shell-quotes the path: a repository path with spaces, quotes or $(...) is data.
add+=("$marker" "export PATH=$(printf '%q' "$root/.venv/bin"):\"\$PATH\"")
fi
if ! grep -qxF "$collections_marker" "$file"; then
add+=("$collections_marker" "export ANSIBLE_HOME=$(printf '%q' "$root/.ansible")")
add+=("export ANSIBLE_COLLECTIONS_PATH=$(printf '%q' "$collections")")
fi
if [ "${#add[@]}" -gt 0 ]; then
tmp="$(mktemp)"
{
head -n 1 "$file"
# %q shell-quotes the path: a repository path with spaces, quotes or $(...) is data.
printf '%s\n' "$marker" "export PATH=$(printf '%q' "$root/.venv/bin"):\"\$PATH\""
printf '%s\n' "${add[@]}"
tail -n +2 "$file"
} > "$tmp"
cat "$tmp" > "$file" # keeps the file mode of the hook
@@ -140,4 +163,4 @@ for hook in pre-commit commit-msg; do
fi
done
echo "Done: git commit now runs the hooks with the tools in $root/.venv/bin."
echo "Done: git commit now runs the hooks with the tools in $root/.venv/bin and the collections in $collections."