Files
computer-playbook/utils/docker/image_ref.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

54 lines
1.3 KiB
Python

from __future__ import annotations
import re
from typing import Any
DOCKER_HUB_REGISTRIES = frozenset(
{"docker.io", "registry-1.docker.io", "index.docker.io"}
)
GHCR_REGISTRY = "ghcr.io"
# Docker image reference (name only, WITHOUT tag/digest).
IMAGE_NAME_RE = re.compile(
r"^"
r"(" # optional registry
r"(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)"
r"(?:\.(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?))*"
r"(?:\:[0-9]{1,5})?"
r"/"
r")?"
r"[a-z0-9]+(?:[._-][a-z0-9]+)*"
r"(?:/[a-z0-9]+(?:[._-][a-z0-9]+)*)*"
r"$"
)
def is_valid_image_name(image: Any) -> bool:
if not isinstance(image, str):
return False
image = image.strip()
if not image or " " in image or "@" in image:
return False
# Reject repo:tag while still allowing registry hosts with a port.
if image.rfind(":") > image.rfind("/"):
return False
return IMAGE_NAME_RE.fullmatch(image) is not None
def split_registry_and_name(image: str) -> tuple[str | None, str] | None:
"""Return (registry, repository_name) for a validated Docker image name."""
image = image.strip()
if not is_valid_image_name(image):
return None
first, sep, rest = image.partition("/")
if not sep:
return None, image
if "." in first or ":" in first or first == "localhost":
return first, rest
return None, image