Files
computer-playbook/plugins/lookup/prometheus_integration_active.py
T
kevinveenbirkenbachandClaude Opus 4.7 c55ad69c41 refactor: drop requirement-file references from code, add doc rule + lint
Strip every `req <NNN>`, `req-<NNN>`, `Requirement <NNN>`,
`requirement-<NNN>`, and `docs/requirements/<NNN>-...md` mention from
non-Markdown source files. Comments anchored to requirement numbers
rot when requirements are renumbered or rewritten and conflate
present-state behaviour (which the comment should describe) with the
audit trail of how the code got there (which belongs in commit
messages, PR descriptions, and the requirement file itself).

* Document the rule in `docs/contributing/documentation.md` under
  "Comments". Code MUST describe the *why* in present-state language;
  contract enforcement lives in tests, not folklore.
* Clean 254 source files across `roles/`, `tests/`, `plugins/`,
  `cli/`, `scripts/`, `utils/`, `group_vars/`, and the repo-root
  `eslint.config.js`. Pure comment / docstring / log-message edits;
  no behavioural change.
* Add `tests/lint/repository/test_no_req_references_in_code.py` to
  enforce the rule going forward across `.js`, `.py`, `.yml`,
  `.yaml`, `.j2`, and `.sh` files. The legacy
  `test_no_stale_requirement_refs.py` is exempt as the only file
  whose purpose is to detect these very tokens.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 16:25:22 +02:00

70 lines
2.2 KiB
Python

from __future__ import annotations
from typing import Any
from ansible.plugins.lookup import LookupBase
from utils.cache.applications import get_merged_applications
class LookupModule(LookupBase):
"""
Return True when the prometheus monitoring block should be emitted for the
current application on the current host; False otherwise.
The condition satisfied:
1. 'web-app-prometheus' is in group_names (prometheus is deployed on this host), AND
2. Either the current application IS web-app-prometheus, OR it declares prometheus
as an enabled compose service dependency (services.prometheus.enabled: true).
Usage in a template:
{% if lookup('prometheus_integration_active', application_id) %}
...prometheus monitoring block...
{% endif %}
Pass 'application_id' as term 0 (string). 'applications' is obtained via
get_merged_applications — the same merged view that backs lookup('applications').
"""
def run(
self,
terms: list[Any],
variables: dict[str, Any] | None = None,
**kwargs: Any,
) -> list[bool]:
vars_ = variables or getattr(self._templar, "available_variables", {}) or {}
applications = get_merged_applications(
variables=vars_,
roles_dir=kwargs.get("roles_dir"),
templar=getattr(self, "_templar", None),
)
# application_id may be passed explicitly (term 0) or read from available_variables.
if terms and isinstance(terms[0], str):
application_id: str = terms[0]
else:
application_id = vars_.get("application_id", "")
group_names: list[str] = vars_.get("group_names", [])
if "web-app-prometheus" not in group_names:
return [False]
if application_id == "web-app-prometheus":
return [True]
try:
# Per services live at applications.<app>.services
# (no `compose.services` wrapper).
enabled = bool(
applications.get(application_id, {})
.get("services", {})
.get("prometheus", {})
.get("enabled", False)
)
except Exception:
enabled = False
return [enabled]