Files
computer-playbook/utils/roles/resource_validation.py
T
Environment TestandClaude Sonnet 4.6 9ea2ad6ff1 refactor(docker): introduce utils/docker/image_discovery as single SPOT for image enumeration
- Move iter_role_images + all helpers + ImageRef from cli/mirror/util.py and
  cli/mirror/model.py into utils/docker/image_discovery.py; add source_file field
  to ImageRef to distinguish config/main.yml vs vars/main.yml entries
- Delete cli/mirror/util.py; update all callers to import from utils.docker.image_discovery
- Move utils into subpackages: utils/docker/ (image_ref, paths_utils, hadolint) and
  utils/roles/ (dependency_resolver, resource_validation); update all imports
- Reorganise tests into subpackages: tests/lint/docker/dockerfile/, tests/integration/docker/,
  tests/unit/utils/docker/, tests/unit/utils/roles/; fix parents[N] offsets after relocation
- tests/lint/docker/test_image_versions.py now uses iter_role_images (same SPOT as mirror)
- Add vars/main.yml images.<name>.{image,version} support for non-application roles
  (test-e2e-playwright, sys-ctl-hlth-csp); eliminate hardcoded duplicates in defaults/tasks
- Add web-app-matrix matrix-chatgpt-bot and web-app-mini-qr mini-qr entries in config/main.yml;
  replace hardcoded image strings in vars/templates with lookup('config', ...) references
- Fix sync/__main__.py log line to use correct registry prefix for non-Docker-Hub images
- Update docs/contributing/code/role/dockerfile.md with image declaration rules and SPOT reference

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-09 19:32:31 +02:00

128 lines
3.7 KiB
Python

from __future__ import annotations
from pathlib import Path
from typing import Iterable, List, Any
import yaml
from humanfriendly import parse_size
from utils.entity_name_utils import get_entity_name
from utils.annotations.message import warning
def _repo_root() -> Path:
# utils/roles/resource_validation.py -> repo root = parents[2]
return Path(__file__).resolve().parents[2]
def _roles_root() -> Path:
return (_repo_root() / "roles").resolve()
def _deep_get(dct: dict, path: list[str]) -> Any:
cur: Any = dct
for key in path:
if not isinstance(cur, dict) or key not in cur:
return None
cur = cur[key]
return cur
def _load_yaml_file(path: Path) -> dict:
data = yaml.safe_load(path.read_text(encoding="utf-8"))
return data if isinstance(data, dict) else {}
def _parse_storage_to_gb(value: Any) -> float:
if isinstance(value, (int, float)):
return float(value)
size_bytes = parse_size(str(value))
return size_bytes / (1000**3)
def filter_roles_by_min_storage(
*,
role_names: Iterable[str],
required_storage: str | int | float,
emit_warnings: bool = False,
) -> List[str]:
roles_root_path = _roles_root()
out: List[str] = []
try:
required_gb = _parse_storage_to_gb(required_storage)
except Exception as exc:
raise ValueError(
f"Invalid required_storage value: {required_storage!r}"
) from exc
for role_name in role_names:
role_dir = (roles_root_path / role_name).resolve()
if not role_dir.is_dir():
if emit_warnings:
warning(
f"Role directory not found: {role_dir}",
title="min_storage validation",
)
continue
entity_name = get_entity_name(role_name)
if not entity_name:
if emit_warnings:
warning(
f"Could not derive entity_name from role_name '{role_name}'.",
title="min_storage validation",
)
continue
cfg_path = role_dir / "config" / "main.yml"
if not cfg_path.is_file():
continue
try:
cfg = _load_yaml_file(cfg_path)
except Exception as exc:
if emit_warnings:
warning(
f"Failed to parse YAML: {cfg_path} ({exc})",
title="min_storage validation",
)
continue
service_path = ["compose", "services", entity_name]
service_cfg = _deep_get(cfg, service_path)
if service_cfg is None or not isinstance(service_cfg, dict):
out.append(role_name)
continue
min_storage_val = service_cfg.get("min_storage")
if min_storage_val is None:
if emit_warnings:
warning(
f"Missing key compose.services.{entity_name}.min_storage in {cfg_path} (treating as 0GB)",
title="min_storage validation",
)
out.append(role_name)
continue
try:
min_storage_gb = _parse_storage_to_gb(min_storage_val)
except Exception as exc:
if emit_warnings:
warning(
f"Invalid min_storage value in {cfg_path}: {min_storage_val!r} ({exc})",
title="min_storage validation",
)
continue
if min_storage_gb <= required_gb:
out.append(role_name)
else:
if emit_warnings:
warning(
f"{role_name} requires {min_storage_gb:.1f}GB but runner provides only {required_gb:.1f}GB",
title="min_storage validation",
)
return out