Files
Kamino/scripts/check_scenarios_load_checkout.py
Defame1297 03b21a9aef
Build / build (pull_request) Successful in 15s
Lint / lint (pull_request) Successful in 1m7s
Molecule / molecule (pull_request) Successful in 15m19s
ci(molecule): select no scenario for a galaxy.yml change, guarded by a load check
Scenarios load mortis.kamino from the checkout, not the copy molecule installs from
galaxy.yml, so galaxy.yml goes on the selector's IGNORED list. A new check, run by
run_molecule_in_ci_image.sh before the scenarios, fails the run when the environment
molecule gives ansible-playbook would resolve the collection anywhere else.

Closes #355

Assisted-by: Claude Code
2026-11-24 15:56:27 +00:00

98 lines
3.5 KiB
Python

# SPDX-FileCopyrightText: Defame1297 and contributors
# SPDX-License-Identifier: GPL-3.0-or-later
"""Fail when a molecule scenario would load mortis.kamino from anywhere but this checkout.
python3 -I scripts/check_scenarios_load_checkout.py < scenario-dirs
Checks the environment molecule hands ansible-playbook, not only the inherited one: a scenario's
own env can change it. The rule it holds is in .gitea/workflows/AGENTS.md (Molecule). Exit 1 when
a scenario would load another copy, or none.
"""
import json
import os
import subprocess
import sys
import tempfile
from molecule.config import Config
CHECKOUT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
RULE = (
"scenarios must load mortis.kamino from the checkout ({checkout}), not molecule's installed"
" copy: the scenario selector ignores galaxy.yml on that rule"
" (.gitea/workflows/AGENTS.md, Molecule)"
)
def playbook_env(scenario):
"""The environment molecule gives ansible-playbook in `scenario`, as a role scenario runs it."""
parts = scenario.split("/")
cwd = os.path.join(CHECKOUT, *parts[:2]) if parts[0] == "roles" else CHECKOUT
# molecule takes the project from the working directory; each call sets it afresh
os.chdir(cwd)
config = Config(
os.path.relpath(os.path.join(CHECKOUT, scenario, "molecule.yml")),
args={},
command_args={"subcommand": "converge"},
)
return dict(config.provisioner.env)
def collection_paths(env, cwd):
"""COLLECTIONS_PATHS as ansible-config resolves it in `env`, in search order."""
env = dict(env)
# molecule writes its ansible.cfg only when a step runs; the environment outranks it anyway
if not os.path.isfile(env.get("ANSIBLE_CONFIG", "")):
env.pop("ANSIBLE_CONFIG", None)
out = subprocess.run(
["ansible-config", "dump", "-t", "base", "--format", "json"],
cwd=cwd,
env=env,
check=True,
capture_output=True,
text=True,
).stdout
(entry,) = [e for e in json.loads(out) if e.get("name") == "COLLECTIONS_PATHS"]
return entry["value"]
def loaded_copy(paths):
"""The mortis.kamino directory the first of `paths` holding one gives, or None."""
for path in paths:
# a path may name the ansible_collections directory itself
base = (
path
if os.path.basename(path) == "ansible_collections"
else os.path.join(path, "ansible_collections")
)
found = os.path.join(base, "mortis", "kamino")
if os.path.isdir(found):
return found
return None
def main():
failed = False
with tempfile.TemporaryDirectory(prefix="kamino-loads-") as home:
# molecule's config writes its scratch directories under ANSIBLE_HOME
os.environ.setdefault("ANSIBLE_HOME", home)
for scenario in filter(None, sys.stdin.read().splitlines()):
scenario_dir = os.path.join(CHECKOUT, scenario)
paths = [os.path.join(scenario_dir, "collections")]
paths += collection_paths(playbook_env(scenario), scenario_dir)
found = loaded_copy(paths)
if found and os.path.realpath(found) == CHECKOUT:
continue
failed = True
print(
f"::error::{scenario}: "
+ RULE.format(checkout=CHECKOUT)
+ f"; it would load {found or 'no mortis.kamino on ' + ':'.join(paths)}"
)
return 1 if failed else 0
if __name__ == "__main__":
sys.exit(main())