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:
1 parent
12fe748bdd
commit
bb57077fb3
3 files changed
+96
-9
No files matched your search
+4
-2
@@ -15,7 +15,9 @@
|
||||
- Third-party material needs an SPDX header, a `REUSE.toml` annotation (it
|
||||
overrides file headers) and an entry in `NOTICE`.
|
||||
- Set up the tooling once per clone with `scripts/dev-setup.sh`: it creates a virtualenv with
|
||||
the pinned tools (`requirements-dev.txt`) and installs git hooks that find them. Then
|
||||
the pinned tools (`requirements-dev.txt`), installs the pinned collections the roles call
|
||||
(`requirements.yml`, into `.ansible/collections`, so local lint resolves the versions CI does)
|
||||
and installs git hooks that find both. Then
|
||||
`pre-commit run --all-files` before pushing.
|
||||
- Never commit credentials, tokens or key material.
|
||||
- Releases are cut by the maintainer: bump `version` in `galaxy.yml`, add a
|
||||
@@ -25,7 +27,7 @@
|
||||
|
||||
## Dependency and image updates
|
||||
|
||||
- `requirements-dev.txt`, `.pre-commit-config.yaml` and `images/**` are inputs of the CI and
|
||||
- `requirements-dev.txt`, `requirements.yml`, `.pre-commit-config.yaml` and `images/**` are inputs of the CI and
|
||||
molecule images. A pull request that changes one needs a higher `version:` for that image in
|
||||
`.gitea/workflows/image.yml` (`python3 scripts/bump_image_versions.py` makes the edit; the
|
||||
`Check image versions were bumped` step enforces it). After the merge, `image.yml` builds the
|
||||
|
||||
+29
-6
@@ -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."
|
||||
@@ -28,7 +28,8 @@ if [ "$1" = "-m" ] && [ "$2" = "venv" ]; then
|
||||
cp "$STUB_DIR/python" "$3/bin/python"
|
||||
cp "$STUB_DIR/pip" "$3/bin/pip"
|
||||
cp "$STUB_DIR/pre-commit" "$3/bin/pre-commit"
|
||||
chmod +x "$3/bin/python" "$3/bin/pip" "$3/bin/pre-commit"
|
||||
cp "$STUB_DIR/ansible-galaxy" "$3/bin/ansible-galaxy"
|
||||
chmod +x "$3/bin/python" "$3/bin/pip" "$3/bin/pre-commit" "$3/bin/ansible-galaxy"
|
||||
exit 0
|
||||
fi
|
||||
exit 2
|
||||
@@ -36,6 +37,13 @@ exit 2
|
||||
|
||||
PIP_STUB = "#!/bin/sh\nexit 0\n"
|
||||
|
||||
# Records the collections path and the arguments of each call, one line per call.
|
||||
GALAXY_STUB = """#!/bin/sh
|
||||
printf '%s|%s\\n' "${ANSIBLE_COLLECTIONS_PATH:-}" "$*" >> "$STUB_DIR/galaxy.log"
|
||||
if [ "${STUB_GALAXY_FAIL:-}" = "1" ]; then echo "stub galaxy failure" >&2; exit 1; fi
|
||||
exit 0
|
||||
"""
|
||||
|
||||
# Mimics `pre-commit install`: refuses when core.hooksPath has any value, otherwise writes
|
||||
# the two hooks into the repository's hooks directory. The hook body prints PATH.
|
||||
PRECOMMIT_STUB = """#!/bin/sh
|
||||
@@ -67,6 +75,7 @@ class DevSetupTest(unittest.TestCase):
|
||||
("python", PYTHON_STUB),
|
||||
("pip", PIP_STUB),
|
||||
("pre-commit", PRECOMMIT_STUB),
|
||||
("ansible-galaxy", GALAXY_STUB),
|
||||
):
|
||||
path = self.stubs / name
|
||||
path.write_text(body)
|
||||
@@ -101,6 +110,7 @@ class DevSetupTest(unittest.TestCase):
|
||||
(repo / "scripts").mkdir(parents=True)
|
||||
shutil.copy(SCRIPT, repo / "scripts" / "dev-setup.sh")
|
||||
(repo / "requirements-dev.txt").write_text("")
|
||||
(repo / "requirements.yml").write_text("---\ncollections: []\n")
|
||||
self.git(repo, "init", "--quiet")
|
||||
return repo
|
||||
|
||||
@@ -136,8 +146,57 @@ class DevSetupTest(unittest.TestCase):
|
||||
for name in ("pre-commit", "commit-msg"):
|
||||
text = self.hook(repo, name).read_text()
|
||||
self.assertIn("# kamino-dev-setup: virtualenv first on PATH", text)
|
||||
self.assertIn("# kamino-dev-setup: collections from this clone", text)
|
||||
self.assertIn(f"export ANSIBLE_HOME={repo / '.ansible'}", text)
|
||||
self.assertIn(
|
||||
f"export ANSIBLE_COLLECTIONS_PATH={repo / '.ansible' / 'collections'}",
|
||||
text,
|
||||
)
|
||||
self.assertEqual(self.hooks_z(repo), b"")
|
||||
|
||||
def galaxy_calls(self) -> list[str]:
|
||||
return (self.stubs / "galaxy.log").read_text().splitlines()
|
||||
|
||||
def test_collections_are_installed_from_requirements_into_the_clone(self) -> None:
|
||||
repo = self.make_repo()
|
||||
self.assertEqual(self.run_setup(repo).returncode, 0)
|
||||
target = repo / ".ansible" / "collections"
|
||||
# The path is scoped to the target for the install, so a copy elsewhere is not skipped.
|
||||
self.assertEqual(
|
||||
self.galaxy_calls(),
|
||||
[f"{target}|collection install -r requirements.yml -p {target}"],
|
||||
)
|
||||
|
||||
def test_failed_collection_install_fails_the_setup(self) -> None:
|
||||
repo = self.make_repo()
|
||||
result = self.run_setup(repo, STUB_GALAXY_FAIL="1")
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertIn("stub galaxy failure", result.stderr)
|
||||
self.assertNotIn("kamino-dev-setup", self.hook(repo).read_text())
|
||||
|
||||
def test_hook_of_the_previous_version_gains_only_the_collections_path(
|
||||
self,
|
||||
) -> None:
|
||||
repo = self.make_repo()
|
||||
self.assertEqual(self.run_setup(repo).returncode, 0)
|
||||
hook = self.hook(repo)
|
||||
# What the previous version of the script wrote: the PATH block, no collections block.
|
||||
lines = hook.read_text().splitlines()
|
||||
old = [
|
||||
line
|
||||
for line in lines
|
||||
if "collections from this clone" not in line
|
||||
and "ANSIBLE_COLLECTIONS_PATH" not in line
|
||||
and "ANSIBLE_HOME" not in line
|
||||
]
|
||||
hook.write_text("\n".join(old) + "\n")
|
||||
self.assertEqual(self.run_setup(repo).returncode, 0)
|
||||
text = hook.read_text()
|
||||
self.assertEqual(text.count("kamino-dev-setup: virtualenv first"), 1)
|
||||
self.assertEqual(text.count("kamino-dev-setup: collections from this clone"), 1)
|
||||
self.assertEqual(text.count("export ANSIBLE_COLLECTIONS_PATH="), 1)
|
||||
self.assertEqual(text.count("export ANSIBLE_HOME="), 1)
|
||||
|
||||
def test_rerun_is_idempotent(self) -> None:
|
||||
repo = self.make_repo()
|
||||
self.assertEqual(self.run_setup(repo).returncode, 0)
|
||||
@@ -146,6 +205,9 @@ class DevSetupTest(unittest.TestCase):
|
||||
self.assertEqual(second_run.returncode, 0, second_run.stderr)
|
||||
self.assertEqual(self.hook(repo).read_text(), first)
|
||||
self.assertEqual(first.count("kamino-dev-setup: virtualenv first"), 1)
|
||||
self.assertEqual(
|
||||
first.count("kamino-dev-setup: collections from this clone"), 1
|
||||
)
|
||||
|
||||
def test_own_absolute_hooks_path_is_lifted_and_restored(self) -> None:
|
||||
repo = self.make_repo()
|
||||
|
||||
Reference in new issue
Block a user