Files
computer-playbook/cli/contributing/mirror/providers.py
T
kevinveenbirkenbachandClaude Opus 4.7 a26bfc4e94 refactor(cli): reorg admin/contributing tree + per-folder help system
Module moves (with imports, paths, argv literals, scripts, docs, and
parents[N] formulas updated in lockstep):

- cli/deploy             -> cli/administration/deploy
- cli/validate/inventory -> cli/administration/inventory/validate
- cli/vault              -> cli/administration/secrets/vault
- cli/administration/inventory/encrypt -> cli/administration/secrets/seal
- cli/mirror             -> cli/contributing/mirror

The new cli/administration/secrets cluster collocates the whole-file
ansible-vault wrapper next to the inventory-aware per-field sealer.
The user-facing CLI subcommand paths follow the package layout, e.g.
"infinito administration deploy dedicated <inv>" and
"infinito administration inventory validate <inv>".

Help system redesign in cli/core/{discovery,help,app}.py:

- Discovery now distinguishes runnable commands (directories with
  __main__.py) from category folders (directories that only group
  further commands). Empty support packages are filtered out.
- Each category folder MAY carry a README.md; its H1 heading is used
  as the title and its first paragraph as the one-line description in
  the help output.
- "infinito" / "infinito --help" prints only the top-level entries
  (one level deep). "infinito <category>" navigates into a folder and
  shows its sub-folders and commands one level deep, with the README
  title as the page heading and the invocation as a dim subtitle.
- New "--tree [N]" flag walks the tree under the (optional) path
  prefix recursively. N caps the depth (1-based); omit N for an
  unbounded walk. The output uses Unicode tree connectors, an emoji
  per entry (folder for categories, gear for commands), a globally
  aligned description column, and multi-line wrapped descriptions
  with tree-continuity bars on continuation lines.
- Category and command listings use the full invocation
  ("infinito meta callorder") rather than the bare leaf name, so the
  output is copy-pasteable.

Convention doc + 17 CLI READMEs:

- New docs/contributing/artefact/files/cli/readme_md.md describes the
  H1+paragraph layout the help system reads, and how it differs from
  role/docs READMEs.
- README.md added for cli/{administration, build, contributing, meta}
  and their immediate sub-folders, plus cli/meta/roles/applications
  and its resolution sub-folder. Each follows the "title with trailing
  emoji + single descriptive paragraph" convention.

Mirror resolver/sync description strings (the help system reads them
from argparse --help output) added so they no longer surface as
"options:" in the global index. cli/mirror/cleanup already had a
description and is unchanged.

Fixes uncovered by the move:

- import unittest.mock added to four dedicated-deploy tests so
  unittest.mock.patch resolves under stdlib unittest discovery
  (Python 3.13 no longer surfaces unittest.mock through "import
  unittest" alone).
- parents[N] index in the new
  cli/administration/inventory/validate/__init__.py corrected from
  3 to 4 to match the deeper layout.
- test_paths.INVENTORY_VALIDATOR_PATH assertion and test_runner
  substring assertions updated to match the new validator path.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 20:17:09 +02:00

184 lines
5.6 KiB
Python

from __future__ import annotations
import subprocess
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import argparse
from .model import ImageRef
class RegistryProvider(ABC):
@abstractmethod
def image_base(self, image: ImageRef) -> str:
pass
@abstractmethod
def mirror(self, image: ImageRef) -> None:
pass
@abstractmethod
def tag_exists(self, image: ImageRef) -> bool:
pass
class GHCRProvider(RegistryProvider):
def __init__(self, namespace: str, repository: str, prefix: str = "mirror") -> None:
self.namespace = namespace.lower()
self.repository = repository.lower()
self.prefix = prefix.strip("/")
@classmethod
def add_args(cls, parser: argparse.ArgumentParser) -> None:
"""Register --ghcr-namespace, --ghcr-repository, --ghcr-prefix on *parser*."""
parser.add_argument("--ghcr-namespace", required=True)
parser.add_argument("--ghcr-repository", required=True)
parser.add_argument("--ghcr-prefix", default="mirror")
@classmethod
def from_args(cls, args: argparse.Namespace) -> GHCRProvider:
"""Construct a GHCRProvider from parsed CLI args."""
return cls(args.ghcr_namespace, args.ghcr_repository, args.ghcr_prefix)
def image_base(self, image: ImageRef) -> str:
return f"ghcr.io/{self.namespace}/{self.repository}/{self.prefix}/{image.registry}/{image.name}"
def tag_exists(self, image: ImageRef) -> bool:
"""
Return True if the destination tag already exists in GHCR.
Uses: skopeo inspect docker://<dest>
Exit code:
- 0 => exists
- !=0 => does not exist OR cannot be accessed (auth/network)
"""
dest = f"{self.image_base(image)}:{image.version}"
r = subprocess.run(
["skopeo", "inspect", f"docker://{dest}"],
check=False,
capture_output=True,
text=True,
)
return r.returncode == 0
def _run_copy(self, *, src: str, dest: str, extra: list[str] | None = None) -> None:
cmd = [
"skopeo",
"copy",
"--all",
"--retry-times",
"5",
"--dest-precompute-digests",
]
if extra:
cmd += extra
cmd += [src, f"docker://{dest}"]
subprocess.run(
cmd,
check=True,
capture_output=True,
text=True,
)
def _looks_like_blob_reuse_problem(self, e: subprocess.CalledProcessError) -> bool:
out = (e.stdout or "") + "\n" + (e.stderr or "")
s = out.lower()
return (
"reuse blob" in s
or "blob mount" in s
or "mount blob" in s
or ("failed to mount" in s and ("403" in s or "401" in s))
or "denied:" in s
or "unauthorized" in s
)
def mirror(self, image: ImageRef) -> None:
dest = f"{self.image_base(image)}:{image.version}"
src = f"docker://{image.source}"
try:
# Fast path
self._run_copy(src=src, dest=dest)
except subprocess.CalledProcessError as e:
# Always print skopeo output for debugging
output = (e.stdout or "") + (e.stderr or "")
if output.strip():
print(output, flush=True)
# Fallback: force recompress (avoids cross-repo blob reuse)
if self._looks_like_blob_reuse_problem(e):
self._run_copy(
src=src,
dest=dest,
extra=[
"--dest-compress-format",
"gzip",
"--dest-compress-level",
"1",
"--dest-force-compress-format",
],
)
return
raise
class GiteaProvider(RegistryProvider):
def __init__(self, registry: str, namespace: str, prefix: str = "mirror") -> None:
self.registry = registry.rstrip("/")
self.namespace = namespace
self.prefix = prefix.strip("/")
def image_base(self, image: ImageRef) -> str:
return f"{self.registry}/{self.namespace}/{self.prefix}/{image.registry}/{image.name}"
def tag_exists(self, image: ImageRef) -> bool:
"""
Return True if the destination tag already exists in the target registry.
Uses: skopeo inspect docker://<dest>
Exit code:
- 0 => exists
- !=0 => does not exist OR cannot be accessed (auth/network)
"""
dest = f"{self.image_base(image)}:{image.version}"
r = subprocess.run(
["skopeo", "inspect", f"docker://{dest}"],
check=False,
capture_output=True,
text=True,
)
return r.returncode == 0
def mirror(self, image: ImageRef) -> None:
dest = f"{self.image_base(image)}:{image.version}"
src = f"docker://{image.source}"
cmd = [
"skopeo",
"copy",
"--all",
"--retry-times",
"5",
"--dest-precompute-digests",
src,
f"docker://{dest}",
]
try:
subprocess.run(
cmd,
check=True,
capture_output=True,
text=True,
)
except subprocess.CalledProcessError as e:
output = (e.stdout or "") + (e.stderr or "")
if output.strip():
print(output, flush=True)
raise