mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-11 21:26:44 +00:00
New lint guard
==============
tests/lint/repository/test_no_direct_yaml_calls.py AST-walks the
production tree (utils/, cli/, plugins/, filter_plugins/,
lookup_plugins/, roles/, scripts/, tasks/) and flags any direct
`yaml.safe_load` / `yaml.safe_dump` / `yaml.load` / `yaml.dump` /
`yaml.safe_load_all` / `yaml.dump_all` call. Aliased imports are
tracked: `import yaml as Y` ⇒ `Y.safe_load(...)` is flagged;
`from yaml import safe_load` ⇒ bare `safe_load(...)` is flagged.
Per-line opt-out: `# noqa: direct-yaml` on the call's physical line
(or any line spanned by a multi-line call). No central exemption
list — every legitimate exception lives next to its call with a
human-readable rationale in the surrounding comment.
Cache module additions
======================
* `dump_yaml_str(data)` — symmetric to `safe_dump(data)` with no file
path. Used for stdout output and filter return values where the
cache cannot help but the API needs to stay consistent.
* `load_yaml_str(text)` — symmetric to `safe_load(text)`. Used when
YAML comes from an HTTP body, a mutated buffer, or a mock-patched
source whose content does not correspond to a stable on-disk file.
Migrated production call sites (33 → 0 direct-yaml violations)
==============================================================
* utils/{entity_name_utils, valid_deploy_id, invokable, service_registry,
domains/list, applications/{config, in_group_deps},
docker/image/discovery, meta/{port_bands, scan},
roles/{dependency_resolver, meta_lookup, resource_validation}}
* cli/{build/{graph, inventory/full/command}, create/inventory/{host_vars,
mirror_overrides}, fix/vars_main_files, meta/{applications/in_group_deps,
callorder}, mirror/resolver, validate/inventory}
* plugins/{filter/{compose_volumes, get_all_application_ids, get_role,
invokable_paths, role_path_by_app_id}, lookup/applications_current_play}
* roles/{svc-db-postgres/filter_plugins/split_postgres_connections,
web-app-bigbluebutton/filter_plugins/compose_mods,
web-app-dashboard/lookup_plugins/docker_cards}
Behaviour adjustments
=====================
* utils/roles/dependency_resolver — `safe_load_all` (multi-doc) →
single-doc `load_yaml_any` wrapped in a list. Ansible task files are
single-doc by convention; the downstream multi-doc loop stays intact
for forward compatibility.
* roles/sys-svc-compose-ca/files/compose_ca.py — KEEPS direct
yaml.safe_load / yaml.safe_dump (annotated with noqa). The script is
copied to the target host via `ansible.builtin.copy` and the target
has no `utils/` package on PYTHONPATH.
noqa-annotated direct-yaml call sites (with rationale)
======================================================
* utils/cache/yaml.py — IS the cache; would recurse.
* utils/handler/yaml.py — vault-aware Loader subclass that wraps
\!vault scalars in VaultScalar; cache-by-path collapses the wrapping.
* cli/encrypt/inventory/__main__.py — vault-aware Dumper subclass that
emits \!vault literal blocks; the cached dump_yaml writes plain YAML
which is the wrong output here.
* roles/sys-svc-compose-ca/files/compose_ca.py — runtime-deployed.
* tasks/utils/migrate_meta_layout.py — once-off migration script.
Validation
==========
* Unit tests: 1573 passed.
* Lint: 56 passed (incl. new test_no_direct_yaml_calls).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
155 lines
5.5 KiB
Python
155 lines
5.5 KiB
Python
"""Walk the role tree to collect occupied ports and subnets per req-009.
|
|
|
|
Used by:
|
|
* `cli meta ports suggest` — gap-first port allocation suggestions
|
|
* `cli meta networks suggest` — gap-first subnet allocation suggestions
|
|
* `tests/lint/...` — collision/band checks
|
|
|
|
Reads only ``roles/*/meta/{services,server}.yml`` files.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ipaddress
|
|
from pathlib import Path
|
|
from typing import Dict, Iterable, List, Tuple
|
|
|
|
from utils.cache.yaml import load_yaml_any
|
|
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[2]
|
|
ROLES_DIR = REPO_ROOT / "roles"
|
|
|
|
|
|
def _load_yaml(path: Path):
|
|
if not path.is_file():
|
|
return None
|
|
data = load_yaml_any(str(path), default_if_missing=None)
|
|
if data in (None, {}):
|
|
return None
|
|
return data
|
|
|
|
|
|
def iter_role_dirs() -> Iterable[Path]:
|
|
if not ROLES_DIR.is_dir():
|
|
return []
|
|
return sorted(p for p in ROLES_DIR.iterdir() if p.is_dir())
|
|
|
|
|
|
def iter_port_assignments() -> Iterable[Tuple[str, str, str, str, int]]:
|
|
"""Yield ``(role, entity, scope, category, port)`` for every single-int port.
|
|
|
|
`inter` is intentionally excluded — internal container ports live in
|
|
per-container network namespaces and never collide.
|
|
"""
|
|
for role_dir in iter_role_dirs():
|
|
services = _load_yaml(role_dir / "meta" / "services.yml")
|
|
if not isinstance(services, dict):
|
|
continue
|
|
for entity_name, entity in services.items():
|
|
if not isinstance(entity, dict):
|
|
continue
|
|
ports = entity.get("ports")
|
|
if not isinstance(ports, dict):
|
|
continue
|
|
for scope_name in ("local", "public"):
|
|
scope_map = ports.get(scope_name)
|
|
if not isinstance(scope_map, dict):
|
|
continue
|
|
for category, value in scope_map.items():
|
|
if category == "relay":
|
|
continue
|
|
if isinstance(value, int):
|
|
yield (
|
|
role_dir.name,
|
|
entity_name,
|
|
scope_name,
|
|
category,
|
|
value,
|
|
)
|
|
|
|
|
|
def iter_relay_ranges() -> Iterable[Tuple[str, str, int, int]]:
|
|
"""Yield ``(role, entity, start, end)`` for every public relay range."""
|
|
for role_dir in iter_role_dirs():
|
|
services = _load_yaml(role_dir / "meta" / "services.yml")
|
|
if not isinstance(services, dict):
|
|
continue
|
|
for entity_name, entity in services.items():
|
|
if not isinstance(entity, dict):
|
|
continue
|
|
ports = entity.get("ports")
|
|
if not isinstance(ports, dict):
|
|
continue
|
|
public = ports.get("public")
|
|
if not isinstance(public, dict):
|
|
continue
|
|
relay = public.get("relay")
|
|
if not isinstance(relay, dict):
|
|
continue
|
|
start = relay.get("start")
|
|
end = relay.get("end")
|
|
if isinstance(start, int) and isinstance(end, int):
|
|
yield (role_dir.name, entity_name, start, end)
|
|
|
|
|
|
def iter_subnets() -> Iterable[Tuple[str, ipaddress.IPv4Network]]:
|
|
"""Yield ``(role, subnet)`` for every role that declares a local subnet."""
|
|
for role_dir in iter_role_dirs():
|
|
server = _load_yaml(role_dir / "meta" / "server.yml")
|
|
if not isinstance(server, dict):
|
|
continue
|
|
networks = server.get("networks")
|
|
if not isinstance(networks, dict):
|
|
continue
|
|
local = networks.get("local")
|
|
if not isinstance(local, dict):
|
|
continue
|
|
subnet_str = local.get("subnet")
|
|
if not isinstance(subnet_str, str):
|
|
continue
|
|
try:
|
|
yield role_dir.name, ipaddress.IPv4Network(subnet_str.strip())
|
|
except (ipaddress.AddressValueError, ValueError):
|
|
continue
|
|
|
|
|
|
def occupied_ports_for(scope: str, category: str) -> List[int]:
|
|
"""Return the sorted, de-duplicated list of host-bound ports in use for
|
|
``<scope>.<category>``.
|
|
"""
|
|
seen: set[int] = set()
|
|
for _role, _entity, scope_name, cat, port in iter_port_assignments():
|
|
if scope_name == scope and cat == category:
|
|
seen.add(port)
|
|
return sorted(seen)
|
|
|
|
|
|
def occupied_relay_ranges() -> List[Tuple[int, int]]:
|
|
"""Return the sorted list of ``(start, end)`` relay ranges in use."""
|
|
return sorted({(s, e) for _r, _e, s, e in iter_relay_ranges()})
|
|
|
|
|
|
def occupied_subnets(prefix_length: int) -> List[ipaddress.IPv4Network]:
|
|
"""Return all currently occupied subnets at the requested prefix length."""
|
|
return sorted(
|
|
{net for _role, net in iter_subnets() if net.prefixlen == prefix_length},
|
|
key=lambda n: int(n.network_address),
|
|
)
|
|
|
|
|
|
def host_bound_port_set() -> Dict[int, List[Tuple[str, str, str, str]]]:
|
|
"""Build the flat host-bound port map per req-009 lint rule.
|
|
|
|
Returns ``{port: [(role, entity, scope, category), ...]}``. Single-int
|
|
`local`/`public` categories AND every integer in each relay span are
|
|
included; ``inter`` is skipped.
|
|
"""
|
|
out: Dict[int, List[Tuple[str, str, str, str]]] = {}
|
|
for role, entity, scope, category, port in iter_port_assignments():
|
|
out.setdefault(port, []).append((role, entity, scope, category))
|
|
for role, entity, start, end in iter_relay_ranges():
|
|
for port in range(start, end + 1):
|
|
out.setdefault(port, []).append((role, entity, "public", "relay"))
|
|
return out
|