Files
computer-playbook/plugins/lookup/depends_on.py
T
kevinveenbirkenbach 5bbd0ff307 feat(plugins/depends_on): mode-aware lookup + migrate every *compose*.yml.j2 to it
The compose/swarm split on `depends_on:` shape (map form with
`condition:` vs. list form, per `docker stack deploy` constraint)
was being hand-coded as a `{% if DEPLOYMENT_MODE == 'swarm' %}` gate
around every block. Repetitive, easy to forget, and templates that
did remember it had to repeat the same six-line pattern per service.

The new `depends_on` lookup encapsulates the shape switch:

    {{ lookup('depends_on', {SVC: 'service_healthy', …}) }}     # mapping
    {{ lookup('depends_on', [SVC_A, SVC_B]) }}                  # all default

- Compose -> map form with explicit `condition:` per entry.
- Swarm   -> list form (just names; swarm ignores depends_on
  ordering semantically but accepts the syntax).
- Mapping `None` value uses the default condition (`service_started`,
  matching Docker Compose's behaviour when no condition is given).
- Plain list of names uses the default for every entry.

The output is shaped so the call site reads naturally at the
service-member column:

      some_service:
        image: …
        {{ lookup('depends_on', {DB: 'service_healthy'}) }}     # at col 4
        volumes: …

Concretely the lookup emits line 1 WITHOUT leading whitespace and
lines 2+ with the `indent` kwarg (default 4) plus the YAML-relative
nested indent. Jinja substitution applies the template's leading
spaces to line 1 only, so the column-4 call placement makes
`depends_on:` land at column 4 and the children at 6 / 8. Lines 2+
carry their own indent because Jinja does not propagate template-side
leading whitespace to continuation lines.

Plugin + tests:

- plugins/lookup/depends_on.py: ~190 lines. Mapping / list / empty
  input handling, default_condition kwarg, condition validation
  against the closed set `{service_started, service_healthy,
  service_completed_successfully}`, DEPLOYMENT_MODE resolution via
  templar with `mode=` test-only override, indent kwarg semantics
  documented in the module docstring.
- tests/unit/plugins/lookup/test_depends_on.py: 27 cases covering
  TestInputNormalisation (mapping / list / empty / typo / single
  string rejection / non-string sequence rejection / unsupported
  type rejection / arity errors), TestModeBehaviour (compose / swarm
  / unknown-falls-to-compose / missing-DEPLOYMENT_MODE-falls-to-compose
  / mode-kwarg override), TestOutputFormatting (line-1 unindented
  exact-string assertion, indent=0 edge case, indent=6 edge case,
  insertion-order stability for diff-stable output).

Lint:

- tests/lint/ansible/jinja/test_compose_template_uses_depends_on_lookup.py:
  rule key `compose-depends-on-must-use-lookup`. Flags three patterns
  in any `*compose*.yml.j2` under `roles/`:
    1. literal `depends_on:` keyword line
    2. flow-form `depends_on: [a, b]`
    3. literal `condition: service_*` line
  The pre-existing `lookup('container_depends_on', application_id)`
  is intentionally not flagged: its call form contains no literal
  `depends_on:` token in source. Scan target broadened to
  `name.endswith('.yml.j2') and 'compose' in name` so
  `compose.override.yml.j2` and `compose-inits.yml.j2` are covered
  alongside `compose.yml.j2` and `*.compose.yml.j2`.

Migration (43 depends_on blocks across 17 templates):

- erpnext (6): configurator + backend + websocket + scheduler +
  queue-short + queue-long + frontend chains.
- mailu (8): imap + smtp + oletools + antispam + antivirus + webdav +
  fetchmail + webmail.
- zammad (4): rails + 3 worker shapes.
- shopware (4): web + worker + scheduler + opensearch, init-gated.
- jitsi (3): three single-key dependencies on JITSI_PROSODY_CONTAINER.
- kix (3): backend + frontend + proxy.
- bigbluebutton/compose.override.yml.j2 (2): bbb-web + etherpad,
  inside the existing BBB_COLLABORA_ENABLED gate.
- moodle (2): nginx + cron, both depending on moodle.
- opentalk (2): application + recorder.
- taiga (2): events + gateway.
- matrix (1): loop-driven via `{% set deps = {} %} +
  __setitem__` escape hatch over MATRIX_BRIDGES.
- matomo (1): bootstrap service.
- nextcloud (1): whiteboard sidecar.
- openproject (1): proxy.
- funkwhale (1): front.
- magento (1): nginx.
- flowise (1): postgres + redis.

Pre-existing `lookup('container_depends_on', application_id)` calls
(erpnext, nextcloud, moodle, zammad, taiga, matrix, etc.) are left
untouched -- that lookup auto-detects DB / redis from the application
config and emits its own block; it is the app-aware counterpart to
this structural lookup.

Every call placement repositioned to column 4 (standard
compose-service-member indent) so the new line-1-unindented semantic
renders correctly. Eight templates needed the column-4 reposition
(via a mechanical one-shot script that was deleted after).

`make test` green; new lint + 27 unit tests green; independent
re-grep across all `*compose*.yml.j2` finds zero residual raw
`depends_on:` / `condition:` lines.
2026-06-14 19:08:45 +02:00

219 lines
7.7 KiB
Python

"""Lookup `depends_on`: emit a mode-aware ``depends_on:`` block.
Compose accepts the long form ``<svc>: { condition: service_X }`` and
``docker stack deploy`` rejects it with *Additional property condition
is not allowed*. The two modes therefore need different YAML shapes:
* compose -> map form with explicit ``condition:`` per entry
* swarm -> list form (just service names; swarm ignores depends_on
ordering semantically but accepts the syntax)
Hand-writing the ``{% if DEPLOYMENT_MODE == 'swarm' %} … {% else %} …
{% endif %}`` per service is repetitive and easy to get wrong (forgot
gate, wrong condition default, inconsistent indent). This lookup
emits the whole block and switches on ``DEPLOYMENT_MODE`` itself.
USAGE
# One dependency with an explicit condition.
{{ lookup('depends_on', {ERPNEXT_CONFIGURATOR_CONTAINER:
'service_completed_successfully'}) }}
# Multiple dependencies, mixed conditions.
{{ lookup('depends_on', {DB_CONTAINER: 'service_healthy',
INIT_CONTAINER: 'service_completed_successfully'}) }}
# Multiple dependencies, all defaulting to ``service_started``
# (the Docker Compose default when no condition is given).
{{ lookup('depends_on', [SVC_A, SVC_B]) }}
# Mix: dict value ``None`` means "use the default condition".
{{ lookup('depends_on', {SVC_A: 'service_healthy', SVC_B: None}) }}
KWARGS
indent (int, default 4)
Number of spaces every line BUT the first one is prefixed with.
The first line has NO leading whitespace - the caller places
``{{ lookup('depends_on', …) }}`` at the desired column in the
template, and Jinja substitution turns that template-side
leading whitespace into line 1's indent. Lines 2+ then carry
``indent`` extra spaces (on top of the YAML-relative indent)
so they align under line 1.
Concretely: writing `` {{ lookup('depends_on', {…}) }}``
(call placed at column 4 in the template) with the default
``indent=4`` renders as::
<prev line>
depends_on:
db:
condition: service_healthy
<next line>
``indent=0`` collapses everything to the left edge - useful
when the lookup is rendered into top-level YAML (no service
wrapper).
default_condition (str, default ``service_started``)
Condition used for entries whose value is ``None`` (dict) or
for plain-name entries in a list term.
mode (str, optional)
Override ``DEPLOYMENT_MODE`` (used by unit tests). When
unset, the lookup reads ``DEPLOYMENT_MODE`` from the templar
variables. An unknown value falls back to compose.
EMPTY INPUT
An empty mapping or list returns the empty string so the surrounding
template stays valid YAML (no orphan ``depends_on:`` keyword).
"""
from __future__ import annotations
import contextlib
import textwrap
from collections.abc import Mapping, Sequence
from typing import Any
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from utils.cache.yaml import dump_yaml_str
_DEFAULT_CONDITION = "service_started"
_VALID_CONDITIONS = frozenset(
{"service_started", "service_healthy", "service_completed_successfully"}
)
def _normalise_entries(raw: Any, default_condition: str) -> dict[str, str]:
"""Turn the user-facing term into ``{service_name: condition}``.
Accepts a ``Mapping`` (``None`` values mean "use default") or a
non-string ``Sequence`` of plain service names.
"""
if raw is None:
return {}
if isinstance(raw, Mapping):
out: dict[str, str] = {}
for name, condition in raw.items():
key = str(name).strip()
if not key:
raise AnsibleError(
"depends_on: dependency name must be a non-empty string"
)
if condition is None or (
isinstance(condition, str) and not condition.strip()
):
out[key] = default_condition
else:
out[key] = str(condition).strip()
return out
if isinstance(raw, str):
raise AnsibleError(
"depends_on: expected a mapping or list of service names; "
"got a single string. Wrap it in a list."
)
if isinstance(raw, Sequence):
out = {}
for name in raw:
key = str(name).strip()
if not key:
raise AnsibleError(
"depends_on: dependency name must be a non-empty string"
)
out[key] = default_condition
return out
raise AnsibleError(
"depends_on: first term must be a mapping {name: condition_or_None} "
f"or a list of service names; got {type(raw).__name__}"
)
def _validate_conditions(entries: dict[str, str]) -> None:
bad = [
(name, cond) for name, cond in entries.items() if cond not in _VALID_CONDITIONS
]
if bad:
bad_str = ", ".join(f"{name}={cond!r}" for name, cond in bad)
raise AnsibleError(
f"depends_on: invalid condition(s): {bad_str}. "
f"Allowed: {sorted(_VALID_CONDITIONS)}."
)
def _resolve_mode(variables: dict[str, Any], templar: Any, override: str | None) -> str:
if override is not None:
return str(override).strip()
raw = variables.get("DEPLOYMENT_MODE", "compose")
if templar is not None:
with contextlib.suppress(Exception):
raw = templar.template(raw)
return str(raw).strip()
def _render(entries: dict[str, str], mode: str, indent: int) -> str:
if not entries:
return ""
if mode == "swarm":
payload: dict[str, object] = {"depends_on": list(entries.keys())}
else:
payload = {
"depends_on": {name: {"condition": cond} for name, cond in entries.items()}
}
body = dump_yaml_str(payload).rstrip()
lines = body.splitlines()
if indent <= 0 or len(lines) <= 1:
return body
# Line 1 stays unindented so the caller can place
# `{{ lookup('depends_on', …) }}` at their preferred template
# column and the surrounding leading whitespace becomes the line-1
# indent at render time. Jinja does not propagate that leading
# whitespace to continuation lines, so we bake it into lines 2+
# ourselves.
return lines[0] + "\n" + textwrap.indent("\n".join(lines[1:]), " " * indent)
class LookupModule(LookupBase):
def run(
self,
terms: list[Any] | None,
variables: dict[str, Any] | None = None,
**kwargs: Any,
) -> list[str]:
if not terms or len(terms) != 1:
raise AnsibleError(
"depends_on: expected exactly 1 positional term "
"(mapping or list of service names)"
)
default_condition = str(
kwargs.get("default_condition", _DEFAULT_CONDITION)
).strip()
if default_condition not in _VALID_CONDITIONS:
raise AnsibleError(
f"depends_on: default_condition={default_condition!r} is not a "
f"valid compose condition. Allowed: {sorted(_VALID_CONDITIONS)}."
)
try:
indent = int(kwargs.get("indent", 4))
except (TypeError, ValueError) as exc:
raise AnsibleError(f"depends_on: indent must be an int: {exc}") from exc
entries = _normalise_entries(terms[0], default_condition=default_condition)
_validate_conditions(entries)
vars_ = variables or getattr(self._templar, "available_variables", {}) or {}
templar = getattr(self, "_templar", None)
mode = _resolve_mode(vars_, templar, override=kwargs.get("mode"))
return [_render(entries, mode, indent)]