Files
computer-playbook/plugins/lookup/deployment.py
T
kevinveenbirkenbach 2c04cce617 Merge branch 'feature/updater' into feature/store-i18n
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	plugins/lookup/deployment.py
2026-10-25 09:32:11 +01:00

76 lines
2.3 KiB
Python

from __future__ import annotations
from typing import Any
from ansible.plugins.lookup import LookupBase
from utils.roles.validation.invokable import list_invokable_app_ids
_STATUS_CACHE: dict[tuple, dict[str, list[str]]] = {}
_EPHEMERAL_RUNTIMES = frozenset({"act", "github"})
_IMPLICIT_GROUPS = frozenset({"all", "ungrouped"})
def _reset_cache_for_tests() -> None:
_STATUS_CACHE.clear()
def _coerce_to_list(raw: Any) -> list[str]:
if raw is None:
return []
if isinstance(raw, str):
return [item.strip() for item in raw.split(",") if item.strip()]
return [str(item) for item in raw]
def running_apps(vars_: dict[str, Any]) -> list[str]:
"""Return the apps the operator asked for, the round's groups otherwise.
Args:
vars_: the task variables the lookup was called with.
"""
whitelist = _coerce_to_list(vars_.get("APPLICATIONS_WHITELIST"))
return whitelist or _coerce_to_list(vars_.get("group_names"))
class LookupModule(LookupBase):
def run(
self,
terms: list[Any],
variables: dict[str, Any] | None = None,
**kwargs: Any,
) -> list[dict[str, list[str]]]:
vars_ = variables or getattr(self._templar, "available_variables", {}) or {}
whitelist = _coerce_to_list(vars_.get("APPLICATIONS_WHITELIST"))
groups = _coerce_to_list(vars_.get("group_names"))
inventory = sorted(
name
for name, hosts in (vars_.get("groups") or {}).items()
if hosts and name not in _IMPLICIT_GROUPS
)
runtime = str(vars_.get("RUNTIME", "")).strip().lower()
key = (tuple(whitelist), tuple(groups), tuple(inventory), runtime)
cached = _STATUS_CACHE.get(key)
if cached is not None:
return [cached]
running = running_apps(vars_)
ephemeral = runtime in _EPHEMERAL_RUNTIMES
deployed = list(running) if ephemeral else list(groups)
result = {
"whitelist": whitelist,
"running": list(running),
"groups": groups,
"inventory": inventory,
"deployed": deployed,
"runtime": runtime,
"ephemeral": ephemeral,
"all": list(list_invokable_app_ids()),
}
_STATUS_CACHE[key] = result
return [result]