mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-08 19:56:53 +00:00
DEPLOYMENT_MODE was compared against a quoted literal 219 times across 110
files. IS_COMPOSE_MODE and IS_SWARM_MODE say the same thing once, and
test_deployment_mode_booleans keeps it that way: only the file that defines
them may still compare.
Both are real booleans under this ansible, measured before the swap because
the alternative is silent: a variable that templates to the string "False" is
truthy in Jinja, which is why IS_STACK_HOST is written `| bool` at every use.
These need no filter.
What had to learn the names:
* utils/annotations/task_gate.py, which several lints ask whether a task is
compose-only gated;
* four compose-template lints that recognise their gate by matching the
expression;
* the fixtures that stand in for the play's variables. They derive the
booleans from the mode now instead of carrying them, or a test that
overrides DEPLOYMENT_MODE keeps the old pair and asserts the wrong
branch.
Each replacement was checked rather than trusted: the 206 conditions that
changed in place were evaluated old against new through the Templar under
compose, swarm and host, with no disagreement, and no changed line turned out
to be anything but a condition swap. `!= 'swarm'` became `not IS_SWARM_MODE`
rather than the shorter IS_COMPOSE_MODE, because only the literal translation
stays correct should the mode ever be host.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
101 lines
3.6 KiB
Python
101 lines
3.6 KiB
Python
"""Detect Ansible task-level `when:` gates that exempt compose-only
|
|
shell calls from swarm-compat lints.
|
|
|
|
A task whose `when:` evaluates compose-only at deploy time (e.g.
|
|
`when: not IS_SWARM_MODE` or `when: IS_COMPOSE_MODE`)
|
|
never reaches a swarm host, so `compose <verb>` / `chdir: directories.instance`
|
|
inside that task body is legitimate. This module gives the lints a
|
|
heuristic to recognise that gate without parsing YAML.
|
|
|
|
The detection is line-based: the task block is the span between the
|
|
nearest `- name:` (at indent N) above the offending line and the next
|
|
top-level structural marker (`- name:` at indent <= N, or end of
|
|
file). Within that span we look for any `when:` clause whose textual
|
|
value names the compose-only boolean.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Sequence
|
|
|
|
_TASK_NAME_RE = re.compile(
|
|
r"^(?P<indent>\s*)-\s+(?:name|block|include_tasks|import_tasks|hosts)\s*:"
|
|
)
|
|
_WHEN_RE = re.compile(r"^\s*when\s*:\s*(?P<expr>.+?)\s*$")
|
|
_COMPOSE_ONLY_EXPR = re.compile(r"not\s+IS_SWARM_MODE|IS_COMPOSE_MODE")
|
|
_FILE_COMPOSE_ONLY_HEADER = re.compile(
|
|
r"^\s*#\s*include-gated\s*:\s*when\s*:\s*(?:not\s+IS_SWARM_MODE|IS_COMPOSE_MODE)"
|
|
)
|
|
|
|
|
|
def is_file_compose_only_by_header(lines: Sequence[str]) -> bool:
|
|
"""True iff the file declares a top-of-file marker
|
|
|
|
# include-gated: when: not IS_SWARM_MODE
|
|
|
|
that documents the entire file is meant to be `include_tasks:` from
|
|
a parent that already carries the compose-only `when:` guard. The
|
|
marker MUST sit in the first 5 lines.
|
|
"""
|
|
return any(_FILE_COMPOSE_ONLY_HEADER.search(raw) for raw in lines[:5])
|
|
|
|
|
|
def _task_block_bounds(lines: Sequence[str], idx: int) -> tuple[int, int]:
|
|
"""Return (start, end_exclusive) of the task block containing *idx*.
|
|
|
|
*start* is the index of the nearest `- name:` (or `- block:`/`- include_tasks:`)
|
|
above *idx*. *end* is the index of the next sibling `- name:` at the same
|
|
indent, or `len(lines)` if none.
|
|
"""
|
|
start = -1
|
|
indent = ""
|
|
for i in range(idx, -1, -1):
|
|
m = _TASK_NAME_RE.match(lines[i])
|
|
if m:
|
|
start = i
|
|
indent = m.group("indent")
|
|
break
|
|
if start < 0:
|
|
return (0, len(lines))
|
|
|
|
for j in range(start + 1, len(lines)):
|
|
m = _TASK_NAME_RE.match(lines[j])
|
|
if m and len(m.group("indent")) <= len(indent):
|
|
return (start, j)
|
|
return (start, len(lines))
|
|
|
|
|
|
def is_task_compose_only_gated(lines: Sequence[str], idx: int) -> bool:
|
|
"""True iff the task body containing *idx* — or any enclosing parent
|
|
`- block:` / `- name:` block — carries a `when:` clause whose textual
|
|
expression evaluates compose-only at deploy time. Walks upwards to
|
|
recognise block-level gates: a single `when: IS_COMPOSE_MODE`
|
|
on a parent `- block:` covers every child task underneath.
|
|
"""
|
|
cur_idx = idx
|
|
while True:
|
|
start, end = _task_block_bounds(lines, cur_idx)
|
|
for k in range(start, end):
|
|
m = _WHEN_RE.match(lines[k])
|
|
if m and _COMPOSE_ONLY_EXPR.search(m.group("expr")):
|
|
return True
|
|
if start <= 0:
|
|
return False
|
|
marker = _TASK_NAME_RE.match(lines[start])
|
|
if not marker:
|
|
return False
|
|
cur_indent = len(marker.group("indent"))
|
|
parent_idx = None
|
|
for i in range(start - 1, -1, -1):
|
|
m = _TASK_NAME_RE.match(lines[i])
|
|
if m and len(m.group("indent")) < cur_indent:
|
|
parent_idx = i
|
|
break
|
|
if parent_idx is None:
|
|
return False
|
|
cur_idx = parent_idx
|