feat(roles): pull custom roles from inventory-declared sources

An inventory group web-app-cst-<name> with applications.<id>.custom: true and
a pinned source {url, version, path} is pulled into roles/ before the play,
read raw (Jinja, Ansible tags and credential URLs refused), placed through a
staging directory under an exclusive lock, and owned by a marker that names
its own directory, so a pull or roles-clean never replaces or removes a
directory it did not place and a failed pull leaves roles/ as it was.
Directory inventories, host lists and non-UTF-8 files behave as Ansible
reads them; generated test deploys pass --skip-pull. make roles-pull,
roles-clean and roles list, INFINITO_EXTRA_MOUNTS for local sources, the
web-app-cst category, the reserved prefix lint and a central
repository_files() that also yields the gitignored custom roles let every
lint scan them like in-tree roles. Supersedes the uncommitted 045 External
Roles draft and reuses its fetch, sync and mount design.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-10-29 00:02:58 +01:00
1 parent cb4d340de9
commit d92375922d
101 files changed
+3731 -248

No files matched your search

@@ -1,9 +1,43 @@
# cli/administration/deploy/dedicated/tests/test_command_ids.py
from __future__ import annotations
import unittest
from types import SimpleNamespace
from unittest import mock
from cli.administration.deploy.dedicated.command import _normalize_app_ids
from cli.administration.deploy.dedicated import command
from cli.administration.deploy.dedicated.command import (
_normalize_app_ids,
pull_custom_roles,
)
class TestPullCustomRoles(unittest.TestCase):
@mock.patch("cli.administration.deploy.dedicated.command.pull_or_exit")
def test_pulls_the_inventory_by_default(self, pull_or_exit):
pull_custom_roles(SimpleNamespace(inventory="inv/devices.yml", skip_pull=False))
pull_or_exit.assert_called_once_with("inv/devices.yml")
@mock.patch("cli.administration.deploy.dedicated.command.pull_or_exit")
def test_skip_pull_keeps_the_placed_roles(self, pull_or_exit):
pull_custom_roles(SimpleNamespace(inventory="inv/devices.yml", skip_pull=True))
pull_or_exit.assert_not_called()
def test_main_pulls_before_anything_reads_roles(self):
calls: list[str] = []
args = SimpleNamespace(inventory="inv/devices.yml", skip_pull=False)
with (
mock.patch.object(command, "parse_args", return_value=(args, [], {})),
mock.patch.object(
command, "pull_or_exit", side_effect=lambda _inv: calls.append("pull")
),
mock.patch.object(
command,
"run_from_args",
side_effect=lambda *_a: calls.append("deploy") or 0,
),
):
self.assertEqual(0, command.main([]))
self.assertEqual(["pull", "deploy"], calls)
class TestNormalizeAppIds(unittest.TestCase):
@@ -7,6 +7,7 @@ from cli.administration.deploy.dedicated import command as dedicated_command
class TestDedicatedCommandPassthrough(unittest.TestCase):
@unittest.mock.patch("cli.administration.deploy.dedicated.command.pull_or_exit")
@unittest.mock.patch(
"cli.administration.deploy.dedicated.command.validate_application_ids"
)
@@ -29,6 +30,7 @@ class TestDedicatedCommandPassthrough(unittest.TestCase):
mock_load_modes_from_yaml,
mock_run_ansible_playbook,
mock_validate_application_ids,
_mock_pull_or_exit,
):
mock_load_modes_from_yaml.return_value = []
mock_add_dynamic_mode_args.return_value = {}
@@ -15,6 +15,7 @@ class TestDedicatedMainParser(unittest.TestCase):
self.assertIn("limit", opts)
self.assertIn("password_file", opts)
self.assertIn("skip_build", opts)
self.assertIn("skip_pull", opts)
self.assertIn("id", opts)
self.assertIn("verbose", opts)
self.assertIn("diff", opts)
@@ -0,0 +1,34 @@
from __future__ import annotations
import unittest
from types import SimpleNamespace
from unittest import mock
from cli.administration.deploy.development.deploy.run import _run_deploy
class TestRunDeploy(unittest.TestCase):
def deploy_cmd(self) -> list[str]:
compose = mock.Mock()
compose.exec.return_value = SimpleNamespace(returncode=0)
_run_deploy(
compose,
deploy_ids=["web-app-cst-demo"],
debug=False,
passthrough=[],
inventory_dir="/etc/inventories/dev",
container_name="infinito",
)
return compose.exec.call_args.args[0]
def test_deploys_the_generated_inventory(self):
cmd = self.deploy_cmd()
self.assertEqual("/etc/inventories/dev/devices.yml", cmd[3])
self.assertEqual(["--id", "web-app-cst-demo"], cmd[-2:])
def test_generated_inventory_keeps_the_placed_custom_roles(self):
self.assertIn("--skip-pull", self.deploy_cmd())
if __name__ == "__main__":
unittest.main()
@@ -8,7 +8,9 @@ because an unsatisfied `:?` guard inside one of them aborts the whole stack.
from __future__ import annotations
import os
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from cli.administration.deploy.development.env import compose_file_args
@@ -23,6 +25,7 @@ _LOCAL_PRIMARY = {
"INFINITO_CACHE_STACK": "",
"INFINITO_GHCR_MIRROR": "",
"INFINITO_PUBLISH_PORTS": "",
"INFINITO_EXTRA_MOUNTS": "",
}
_WORKTREE = {
@@ -99,6 +102,35 @@ class TestComposeFileArgs(unittest.TestCase):
compose_file_args()[-2:], ["-f", "compose/noports.override.yml"]
)
def test_extra_mounts_append_their_generated_override(self) -> None:
with (
tempfile.TemporaryDirectory() as mount,
patch.dict(
os.environ,
{**_LOCAL_PRIMARY, "INFINITO_EXTRA_MOUNTS": mount},
clear=False,
),
patch(
"cli.administration.deploy.development.extra_mounts.write_mounts_override",
return_value="build/compose/extra-mounts.override.yml",
) as write,
):
args = compose_file_args()
self.assertEqual(write.call_args.args[1], [Path(mount)])
self.assertEqual(args[-2:], ["-f", "build/compose/extra-mounts.override.yml"])
def test_an_entry_that_is_no_directory_stops_the_stack_naming_it(self) -> None:
with (
patch.dict(
os.environ,
{**_LOCAL_PRIMARY, "INFINITO_EXTRA_MOUNTS": "/nonexistent/gui"},
clear=False,
),
self.assertRaises(SystemExit) as ctx,
):
compose_file_args()
self.assertIn("/nonexistent/gui", str(ctx.exception.code))
if __name__ == "__main__": # pragma: no cover
unittest.main()
@@ -0,0 +1,65 @@
from __future__ import annotations
import tempfile
import unittest
from pathlib import Path
from cli.administration.deploy.development.extra_mounts import (
OVERRIDE,
parse_mounts,
write_mounts_override,
)
from utils.cache.yaml import load_yaml
class TestParseMounts(unittest.TestCase):
def test_existing_absolute_directories_are_returned(self) -> None:
with (
tempfile.TemporaryDirectory() as first,
tempfile.TemporaryDirectory() as second,
):
self.assertEqual(
parse_mounts(f"{first}, {second},"), [Path(first), Path(second)]
)
def test_empty_value_mounts_nothing(self) -> None:
self.assertEqual(parse_mounts(""), [])
def test_relative_entry_is_rejected_naming_it(self) -> None:
with self.assertRaises(SystemExit) as ctx:
parse_mounts("relative/dir")
self.assertIn("relative/dir", str(ctx.exception.code))
def test_missing_directory_is_rejected_naming_it(self) -> None:
with tempfile.TemporaryDirectory() as tmp, self.assertRaises(SystemExit) as ctx:
parse_mounts(str(Path(tmp) / "absent"))
self.assertIn("absent", str(ctx.exception.code))
class TestWriteMountsOverride(unittest.TestCase):
def test_each_directory_is_bound_read_only_at_its_own_path(self) -> None:
with tempfile.TemporaryDirectory() as repo:
relative = write_mounts_override(Path(repo), [Path("/srv/gui")])
override = load_yaml(Path(repo) / relative)
self.assertEqual(relative, OVERRIDE)
self.assertEqual(
override,
{
"services": {
"infinito": {
"volumes": [
{
"type": "bind",
"source": "/srv/gui",
"target": "/srv/gui",
"read_only": True,
}
]
}
}
},
)
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,36 @@
from __future__ import annotations
import unittest
from types import SimpleNamespace
from unittest import mock
from cli.administration.deploy.swarm import command
class TestSwarmMain(unittest.TestCase):
def test_custom_roles_are_pulled_before_the_closure_reads_roles(self):
calls: list[str] = []
args = SimpleNamespace(id=["web-app-cst-demo"], inventory="inv/devices.yml")
def closure(ids, _inventory):
calls.append("closure")
return ids
with (
mock.patch.object(command, "parse_args", return_value=(args, [], {})),
mock.patch.object(
command,
"pull_custom_roles",
side_effect=lambda _args: calls.append("pull"),
),
mock.patch.object(command, "swarm_deploy_targets", side_effect=closure),
mock.patch.object(command, "run_from_args", return_value=0) as run,
):
self.assertEqual(0, command.main([]))
self.assertEqual(["pull", "closure"], calls)
run.assert_called_once_with(args, [], {})
if __name__ == "__main__":
unittest.main()
@@ -82,6 +82,35 @@ class TestEnsureImage(unittest.TestCase):
)
class TestRunInContainer(unittest.TestCase):
@unittest.mock.patch("subprocess.run")
@unittest.mock.patch(
"cli.administration.deploy.container.command.docker_exec",
return_value=subprocess.CompletedProcess(args=[], returncode=0),
)
@unittest.mock.patch(
"cli.administration.deploy.container.command.start_ci_container",
return_value="infinito-ci-test",
)
def test_generated_inventory_keeps_the_placed_custom_roles(
self, _start, mock_docker_exec, _run
):
deploy_container.run_in_container(
image="infinito:latest",
build=False,
rebuild=False,
no_cache=False,
inventory_dir="/tmp/inventory",
inventory_args=[],
deploy_args=["--debug"],
)
deploy_cmd = mock_docker_exec.call_args_list[-1].args[1]
self.assertIn("cli.administration.deploy.dedicated", deploy_cmd)
self.assertIn("--skip-pull", deploy_cmd)
self.assertEqual("--debug", deploy_cmd[-1])
class TestMain(unittest.TestCase):
@unittest.mock.patch("cli.administration.deploy.container.command.run_in_container")
def test_main_requires_forwarded_args(self, mock_run_in_container):