check_scenarios_load_checkout.py now merges the base config the way molecule's CLI does (its own lookup of .config/molecule/config.yml) and fails a scenario whose merged prerun is not false, naming the base config. Before, it built Config with no base config, so a scenario that turned prerun back on, or a moved base config, slowed every run and nothing failed. Closes #452 Assisted-by: Claude Code
115 lines
4.4 KiB
Python
115 lines
4.4 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. Also checks that molecule's prerun, its install of the collection, is off
|
|
once the base config is merged as molecule's CLI merges it. The rule it holds is in
|
|
.gitea/workflows/AGENTS.md (Molecule). Exit 1 when a scenario would load another copy, or none,
|
|
or has prerun on.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
from molecule.config import Config
|
|
from molecule.shell import LOCAL_CONFIG_SEARCH
|
|
from molecule.util import lookup_config_file
|
|
|
|
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)"
|
|
)
|
|
PRERUN = (
|
|
"prerun must be off, so molecule does not install mortis.kamino again: set `prerun: false`"
|
|
" in the base config {base} and do not turn it on in the scenario (.gitea/workflows/AGENTS.md, Molecule)"
|
|
)
|
|
|
|
|
|
def scenario_config(scenario):
|
|
"""Molecule's config of `scenario` as `molecule converge` builds it there, and its base config."""
|
|
parts = scenario.split("/")
|
|
cwd = os.path.join(CHECKOUT, *parts[:2]) if parts[0] == "roles" else CHECKOUT
|
|
# molecule takes the project and the base config from the working directory; set it afresh
|
|
os.chdir(cwd)
|
|
base = lookup_config_file(LOCAL_CONFIG_SEARCH)
|
|
config = Config(
|
|
os.path.relpath(os.path.join(CHECKOUT, scenario, "molecule.yml")),
|
|
args={"base_config": [base] if base else []},
|
|
command_args={"subcommand": "converge"},
|
|
)
|
|
return config, base
|
|
|
|
|
|
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)
|
|
config, base = scenario_config(scenario)
|
|
# molecule's worker reads this key to decide whether to install the collection
|
|
if config.config_data["prerun"] is not False:
|
|
failed = True
|
|
base = (
|
|
base or os.path.join(CHECKOUT, LOCAL_CONFIG_SEARCH) + " (not found)"
|
|
)
|
|
print(f"::error::{scenario}: " + PRERUN.format(base=base))
|
|
paths = [os.path.join(scenario_dir, "collections")]
|
|
paths += collection_paths(dict(config.provisioner.env), 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())
|