mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 14:30:52 +00:00
feat(ci/variant-bundles): storage-aware job split and explicit variant CSV
Open a new CI runner bundle as soon as it would exceed INFINITO_VARIANT_BUNDLE_SIZE variants OR INFINITO_VARIANT_BUNDLE_MAX_STORAGE (default 400GB) cumulative min_storage, so storage-heavy variants are not stacked onto one runner that then runs too long. Every job now carries its explicit variant CSV in the name; empty only for roles without variants. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
d6e646e458
commit
bc76fe0dff
3 files changed
+213
-13
No files matched your search
+123
-10
@@ -1,12 +1,18 @@
|
||||
"""Expand a discovered app-id list into CI deploy-matrix entries.
|
||||
|
||||
A role whose matrix-deploy declares more variants than a single runner should
|
||||
iterate (``INFINITO_VARIANT_BUNDLE_SIZE``, default 3) is split into bundles of
|
||||
consecutive variant indices, one runner per bundle — e.g. a 5-variant role
|
||||
becomes ``0,1,2`` and ``3,4``. Roles that fit one runner stay a single entry.
|
||||
iterate is split into bundles of consecutive variant indices, one runner per
|
||||
bundle — e.g. a 5-variant role becomes ``0,1,2`` and ``3,4``. A new bundle opens
|
||||
as soon as the current one would exceed ``INFINITO_VARIANT_BUNDLE_SIZE`` variants
|
||||
(default 3) OR ``INFINITO_VARIANT_BUNDLE_MAX_STORAGE`` cumulative ``min_storage``
|
||||
(default 400GB), so storage-heavy variants are not stacked onto one runner that
|
||||
would then run too long. Roles that fit one runner stay a single entry but still
|
||||
carry their full ``0,…,N-1`` variant CSV, so every job name shows the variants it
|
||||
covers.
|
||||
|
||||
Each entry is ``{"apps": <id>, "variant": "<csv>", "variant_slug": "<dashed>"}``;
|
||||
an empty ``variant`` means full-matrix mode. The ``variant`` slice is threaded
|
||||
``variant`` is empty only for a role that declares no variants (a plain base
|
||||
deploy). The ``variant`` slice is threaded
|
||||
through to ``cli.administration.deploy.development`` via the ``variant``
|
||||
environment variable (consumed by ``--variant``), so a runner only iterates the
|
||||
rounds in its bundle. ``variant_slug`` is a comma-free copy for artifact/job
|
||||
@@ -18,15 +24,28 @@ from __future__ import annotations
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from humanfriendly import parse_size
|
||||
|
||||
from utils.cache.applications import get_variants
|
||||
from utils.roles.applications.services.registry import (
|
||||
build_service_registry_from_applications,
|
||||
load_applications_from_roles_dir,
|
||||
)
|
||||
from utils.roles.applications.services.resources import (
|
||||
aggregate,
|
||||
collect_role_resources,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Iterable, Mapping, Sequence
|
||||
from typing import Any
|
||||
|
||||
DEFAULT_BUNDLE_SIZE = 3
|
||||
DEFAULT_MAX_STORAGE = "400GB"
|
||||
ROLES_DIR = Path(__file__).resolve().parents[2] / "roles"
|
||||
|
||||
|
||||
def resolve_bundle_size(raw: str | None = None) -> int:
|
||||
@@ -46,6 +65,58 @@ def resolve_bundle_size(raw: str | None = None) -> int:
|
||||
return size
|
||||
|
||||
|
||||
def resolve_max_storage(raw: str | None = None) -> int | None:
|
||||
if raw is None:
|
||||
raw = os.environ.get("INFINITO_VARIANT_BUNDLE_MAX_STORAGE")
|
||||
value = (raw or "").strip()
|
||||
if not value:
|
||||
value = DEFAULT_MAX_STORAGE
|
||||
if value.lower() in ("0", "off", "none"):
|
||||
return None
|
||||
try:
|
||||
return int(parse_size(value))
|
||||
except Exception:
|
||||
raise ValueError(
|
||||
"INFINITO_VARIANT_BUNDLE_MAX_STORAGE must be a size like '400GB', "
|
||||
f"got {value!r}"
|
||||
) from None
|
||||
|
||||
|
||||
def bundle_indices(
|
||||
count: int,
|
||||
bundle_size: int,
|
||||
storages: Sequence[int | None] | None = None,
|
||||
max_storage_bytes: int | None = None,
|
||||
) -> list[list[int]]:
|
||||
"""Greedily pack variant indices into bundles, opening a new bundle as soon
|
||||
as the current one would exceed ``bundle_size`` variants OR
|
||||
``max_storage_bytes`` cumulative ``min_storage``; both counters reset per
|
||||
bundle. With no storage cap this matches ``chunk_indices``. A single variant
|
||||
that alone exceeds the storage cap still forms its own bundle."""
|
||||
bundles: list[list[int]] = []
|
||||
current: list[int] = []
|
||||
current_storage = 0
|
||||
for index in range(count):
|
||||
size = 0
|
||||
if storages is not None and index < len(storages):
|
||||
size = storages[index] or 0
|
||||
over_count = len(current) >= bundle_size
|
||||
over_storage = (
|
||||
max_storage_bytes is not None
|
||||
and bool(current)
|
||||
and current_storage + size > max_storage_bytes
|
||||
)
|
||||
if current and (over_count or over_storage):
|
||||
bundles.append(current)
|
||||
current = []
|
||||
current_storage = 0
|
||||
current.append(index)
|
||||
current_storage += size
|
||||
if current:
|
||||
bundles.append(current)
|
||||
return bundles
|
||||
|
||||
|
||||
def chunk_indices(count: int, bundle_size: int) -> list[list[int]]:
|
||||
return [
|
||||
list(range(start, min(start + bundle_size, count)))
|
||||
@@ -71,20 +142,55 @@ def expand_apps(
|
||||
apps: Iterable[str],
|
||||
variants_per_app: Mapping[str, Sequence[Any]],
|
||||
bundle_size: int = DEFAULT_BUNDLE_SIZE,
|
||||
*,
|
||||
storages_per_app: Mapping[str, Sequence[int | None]] | None = None,
|
||||
max_storage_bytes: int | None = None,
|
||||
) -> list[dict[str, str]]:
|
||||
entries: list[dict[str, str]] = []
|
||||
for app in apps:
|
||||
count = variant_count(variants_per_app, app)
|
||||
if count <= bundle_size:
|
||||
variant_list = variants_per_app.get(app) or []
|
||||
if not variant_list:
|
||||
entries.append({"apps": app, "variant": "", "variant_slug": ""})
|
||||
continue
|
||||
entries.extend(
|
||||
_entry(app, ",".join(map(str, chunk)))
|
||||
for chunk in chunk_indices(count, bundle_size)
|
||||
storages = (storages_per_app or {}).get(app)
|
||||
bundles = bundle_indices(
|
||||
len(variant_list), bundle_size, storages, max_storage_bytes
|
||||
)
|
||||
entries.extend(_entry(app, ",".join(map(str, chunk))) for chunk in bundles)
|
||||
return entries
|
||||
|
||||
|
||||
def app_variant_storages(
|
||||
apps: Iterable[str],
|
||||
variants_per_app: Mapping[str, Sequence[Any]],
|
||||
roles_dir: Path = ROLES_DIR,
|
||||
) -> dict[str, list[int | None]]:
|
||||
"""Per-variant total ``min_storage`` (bytes) for each app, via the shared
|
||||
resource collection (same machinery as the ``ressources`` CLI / budget
|
||||
lint). Unknown apps and variants without storage yield ``None`` entries."""
|
||||
applications = load_applications_from_roles_dir(roles_dir)
|
||||
registry = build_service_registry_from_applications(applications)
|
||||
result: dict[str, list[int | None]] = {}
|
||||
for app in apps:
|
||||
sizes: list[int | None] = []
|
||||
for variant_config in variants_per_app.get(app) or []:
|
||||
scoped = dict(applications)
|
||||
scoped[app] = variant_config or {}
|
||||
rows: list[dict[str, Any]] = []
|
||||
collect_role_resources(
|
||||
role_name=app,
|
||||
applications=scoped,
|
||||
service_registry=registry,
|
||||
visited=set(),
|
||||
rows=rows,
|
||||
warnings=[],
|
||||
dedup=True,
|
||||
)
|
||||
sizes.append(aggregate(rows).get("min_storage_bytes"))
|
||||
result[app] = sizes
|
||||
return result
|
||||
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
argv = list(sys.argv[1:] if argv is None else argv)
|
||||
raw = argv[0] if argv else sys.stdin.read()
|
||||
@@ -94,7 +200,14 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||
f"variant_bundles: expected a JSON array of app ids, got "
|
||||
f"{type(apps).__name__}"
|
||||
)
|
||||
entries = expand_apps(apps, get_variants(), resolve_bundle_size())
|
||||
variants_per_app = get_variants()
|
||||
entries = expand_apps(
|
||||
apps,
|
||||
variants_per_app,
|
||||
resolve_bundle_size(),
|
||||
storages_per_app=app_variant_storages(apps, variants_per_app),
|
||||
max_storage_bytes=resolve_max_storage(),
|
||||
)
|
||||
print(json.dumps(entries))
|
||||
return 0
|
||||
|
||||
|
||||
Reference in new issue
Block a user