diff --git a/cli/administration/deploy/development/env.py b/cli/administration/deploy/development/env.py index 23116c6784..30fdf58506 100644 --- a/cli/administration/deploy/development/env.py +++ b/cli/administration/deploy/development/env.py @@ -8,17 +8,6 @@ from utils.distros import distro_names VALID_DISTROS: tuple[str, ...] = distro_names() -def gpu_runtime_available() -> bool: - """Return whether docker can hand an NVIDIA GPU to a container.""" - registered = subprocess.run( - ["docker", "info", "--format", "{{json .Runtimes}}"], - capture_output=True, - text=True, - check=False, - ) - return "nvidia" in registered.stdout - - def compose_file_args() -> list[str]: """Compose `-f` flags shared by up and down flows. @@ -39,8 +28,7 @@ def compose_file_args() -> list[str]: out += ["-f", "compose/cache.shared.override.yml"] if (os.environ.get("INFINITO_PUBLISH_PORTS") or "").strip().lower() == "false": out += ["-f", "compose/noports.override.yml"] - if gpu_runtime_available(): - out += ["-f", "compose/gpu.override.yml"] + out += ["-f", "i18n/compose.override.yml"] return out diff --git a/compose.yml b/compose.yml index 7dbf8d11c5..ae01115cad 100644 --- a/compose.yml +++ b/compose.yml @@ -61,6 +61,13 @@ services: mem_limit: ${INFINITO_MEM_LIMIT:?Run 'make dotenv' to generate the .env single source of truth} memswap_limit: ${INFINITO_MEMSWAP_LIMIT:?Run 'make dotenv' to generate the .env single source of truth} cpus: ${INFINITO_CPUS:?Run 'make dotenv' to generate the .env single source of truth} + deploy: + resources: + reservations: + devices: + - driver: nvidia + count: ${INFINITO_GPU_COUNT:?Run 'make dotenv' to generate the .env single source of truth} + capabilities: [gpu] <<: *ci_defaults volumes: - /sys/fs/cgroup:/sys/fs/cgroup:rw diff --git a/compose/cache-consumer.yml b/compose/cache-consumer.yml new file mode 100644 index 0000000000..727ae77980 --- /dev/null +++ b/compose/cache-consumer.yml @@ -0,0 +1,22 @@ +--- +# Cache wiring shared by every runner, consumed through `extends`. + +services: + cache-consumer: + environment: + INFINITO_CACHE_PACKAGE_FRONTEND_IP: ${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth} + extra_hosts: + - "infinito-registry-cache:${INFINITO_CACHE_REGISTRY_IP:?Run 'make dotenv' to generate the .env single source of truth}" + - "deb.debian.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP}" + - "archive.ubuntu.com:${INFINITO_CACHE_PACKAGE_FRONTEND_IP}" + - "security.ubuntu.com:${INFINITO_CACHE_PACKAGE_FRONTEND_IP}" + - "pypi.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP}" + - "files.pythonhosted.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP}" + - "registry.npmjs.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP}" + - "rubygems.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP}" + - "index.rubygems.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP}" + - "repo.packagist.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP}" + - "proxy.golang.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP}" + - "dl-cdn.alpinelinux.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP}" + - "raw.githubusercontent.com:${INFINITO_CACHE_PACKAGE_FRONTEND_IP}" + - "codeload.github.com:${INFINITO_CACHE_PACKAGE_FRONTEND_IP}" diff --git a/compose/cache.override.yml b/compose/cache.override.yml index 6b19f94232..d963c67eb3 100644 --- a/compose/cache.override.yml +++ b/compose/cache.override.yml @@ -112,24 +112,9 @@ services: package-cache-frontend: condition: service_healthy required: false - environment: - INFINITO_CACHE_PACKAGE_FRONTEND_IP: ${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth} - extra_hosts: - # Exception: /etc/hosts is used because coredns can't resolve the container names. - - "infinito-registry-cache:${INFINITO_CACHE_REGISTRY_IP:?Run 'make dotenv' to generate the .env single source of truth}" - - "deb.debian.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth}" - - "archive.ubuntu.com:${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth}" - - "security.ubuntu.com:${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth}" - - "pypi.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth}" - - "files.pythonhosted.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth}" - - "registry.npmjs.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth}" - - "rubygems.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth}" - - "index.rubygems.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth}" - - "repo.packagist.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth}" - - "proxy.golang.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth}" - - "dl-cdn.alpinelinux.org:${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth}" - - "raw.githubusercontent.com:${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth}" - - "codeload.github.com:${INFINITO_CACHE_PACKAGE_FRONTEND_IP:?Run 'make dotenv' to generate the .env single source of truth}" + extends: + file: compose/cache-consumer.yml + service: cache-consumer volumes: - type: bind source: ${INFINITO_CACHE_REGISTRY_CA_HOST_PATH:?Run 'make dotenv' to generate the .env single source of truth} diff --git a/compose/gpu.override.yml b/compose/gpu.override.yml deleted file mode 100644 index a413284494..0000000000 --- a/compose/gpu.override.yml +++ /dev/null @@ -1,5 +0,0 @@ -services: - infinito: - gpus: all - i18n: - gpus: all diff --git a/i18n/compose.override.yml b/i18n/compose.override.yml new file mode 100644 index 0000000000..431533a765 --- /dev/null +++ b/i18n/compose.override.yml @@ -0,0 +1,68 @@ +--- +# Second runner for the i18n toolchain, with its own inner docker daemon and +# docker state. Gated behind the `i18n` compose profile. + +services: + i18n: + image: "${INFINITO_IMAGE:?Resolved by cli.administration.deploy.development; the i18n runner reuses the image the infinito service builds}" + pull_policy: never + container_name: ${INFINITO_CONTAINER:?Run 'make dotenv' to generate the .env single source of truth}_i18n + profiles: + - i18n + command: + - /sbin/init + - systemd.log_target=console + - systemd.log_level=debug + stop_signal: SIGRTMIN+3 + privileged: true + cgroup: host + restart: "no" + tty: true + stdin_open: true + tmpfs: + - /run + - /run/lock + mem_limit: ${INFINITO_MEM_LIMIT:?Run 'make dotenv' to generate the .env single source of truth} + memswap_limit: ${INFINITO_MEMSWAP_LIMIT:?Run 'make dotenv' to generate the .env single source of truth} + cpus: ${INFINITO_CPUS:?Run 'make dotenv' to generate the .env single source of truth} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:rw + - .:${INFINITO_SRC_DIR} + - i18n-docker:${INFINITO_DOCKER_MOUNT:?Run 'make dotenv' to generate the .env single source of truth} + - i18n-var-lib:${INFINITO_DIR_VAR_LIB:?Run 'make dotenv' to generate the .env single source of truth}/ + - ${INFINITO_CACHE_REGISTRY_PROXY_CONF}:/etc/systemd/system/docker.service.d/registry-cache-proxy.conf:ro + - ${INFINITO_CACHE_PACKAGE_PIP_CONF}:/etc/pip.conf:ro + - ./compose/package-cache/apt/${INFINITO_DISTRO}.list:/etc/apt/sources.list.d/package-cache.list:ro + environment: + INFINITO_DIR_VAR_LIB: ${INFINITO_DIR_VAR_LIB} + INFINITO_DNS_IP: ${INFINITO_DNS_IP} + INFINITO_DOMAIN: ${INFINITO_DOMAIN} + INFINITO_SRC_DIR: ${INFINITO_SRC_DIR} + INFINITO_CACHE_PACKAGE_FRONTEND_IP: ${INFINITO_CACHE_PACKAGE_FRONTEND_IP} + DOCKER_DEFAULT_PLATFORM: ${INFINITO_DOCKER_PLATFORM} + extends: + file: compose/cache-consumer.yml + service: cache-consumer + ports: + - "${INFINITO_BIND_IP:?Run 'make dotenv' to generate the .env single source of truth}:${INFINITO_I18N_LIBRETRANSLATE_PORT:?Run 'make dotenv' to generate the + .env single source of truth}:${INFINITO_I18N_LIBRETRANSLATE_PORT:?Run 'make dotenv' to generate the .env single source of truth}" + + deploy: + resources: + reservations: + devices: + - driver: nvidia + count: ${INFINITO_GPU_COUNT:?Run 'make dotenv' to generate the .env single source of truth} + capabilities: [gpu] + networks: + default: + i18n-cache: + +volumes: + i18n-docker: + i18n-var-lib: + +networks: + i18n-cache: + external: true + name: ${INFINITO_CACHE_NETWORK:?Set by 'make worktree-up' from the running cache stack} diff --git a/i18n/inventory.yml b/i18n/inventory.yml new file mode 100644 index 0000000000..67f79d69cb --- /dev/null +++ b/i18n/inventory.yml @@ -0,0 +1,15 @@ +--- +# Host vars the i18n runner deploys web-svc-libretranslate with. + +TLS_MODE: self_signed + +applications: + web-svc-libretranslate: + server: + tls: + mode: self_signed + services: + libretranslate: + cpus: "{{ lookup('resource', 'avail_cpus') }}" + mem_limit: "{{ lookup('resource', 'mem_limit') }}" + pids_limit: 4096 diff --git a/tests/lint/repository/test_compose_override_services_defined.py b/tests/lint/repository/test_compose_override_services_defined.py new file mode 100644 index 0000000000..0a3f4e993a --- /dev/null +++ b/tests/lint/repository/test_compose_override_services_defined.py @@ -0,0 +1,75 @@ +"""Every service an override extends must be defined by a base compose file. + +An override that names a service nobody declares renders as an incomplete +service, and `docker compose config` rejects the whole project with "has +neither an image nor a build context". The failure hits every compose call of +the dev tooling, not just the override that introduced it. +""" + +from __future__ import annotations + +import unittest +from pathlib import Path +from typing import ClassVar + +import yaml + +from utils import PROJECT_ROOT + +BASES = ("compose.yml",) +OVERRIDES = ("compose/*.override.yml", "i18n/*.override.yml") + + +class _ComposeLoader(yaml.SafeLoader): + """SafeLoader that tolerates compose's own tags, such as ``!reset``.""" + + +_ComposeLoader.add_multi_constructor("!", lambda loader, suffix, node: None) + + +def _load(path: Path) -> dict: + return yaml.load(path.read_text(encoding="utf-8"), _ComposeLoader) or {} # noqa: S506 - _ComposeLoader derives from SafeLoader + + +def _services(path: Path) -> set[str]: + return set((_load(path).get("services") or {}).keys()) + + +def _defines(path: Path) -> set[str]: + loaded = _load(path) + return { + name + for name, body in (loaded.get("services") or {}).items() + if isinstance(body, dict) and ("image" in body or "build" in body) + } + + +class TestComposeOverrideServicesDefined(unittest.TestCase): + root: ClassVar[Path] = Path(PROJECT_ROOT) + + def test_every_extended_service_has_a_definition(self) -> None: + defined: set[str] = set() + for base in BASES: + defined |= _defines(self.root / base) + for pattern in OVERRIDES: + for path in sorted(self.root.glob(pattern)): + defined |= _defines(path) + + orphans: dict[str, set[str]] = {} + for pattern in OVERRIDES: + for path in sorted(self.root.glob(pattern)): + missing = _services(path) - defined + if missing: + orphans[str(path.relative_to(self.root))] = missing + + self.assertEqual( + orphans, + {}, + "Override files extend services that no compose file defines with an " + f"image or build: {orphans}. Define the service where it is extended, " + "or move the override next to its definition.", + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/unit/python/cli/administration/deploy/development/test_compose.py b/tests/unit/python/cli/administration/deploy/development/test_compose.py index 9e878575e5..37ddf77306 100644 --- a/tests/unit/python/cli/administration/deploy/development/test_compose.py +++ b/tests/unit/python/cli/administration/deploy/development/test_compose.py @@ -126,6 +126,8 @@ class TestComposeUpRetries(unittest.TestCase): "compose", "-f", "compose.yml", + "-f", + "i18n/compose.override.yml", "ps", "-q", "infinito", @@ -169,6 +171,8 @@ class TestComposeUpRetries(unittest.TestCase): "compose.yml", "-f", "compose/cache.override.yml", + "-f", + "i18n/compose.override.yml", "ps", "-q", "infinito", diff --git a/tests/unit/python/cli/administration/deploy/development/test_env_compose_file_args.py b/tests/unit/python/cli/administration/deploy/development/test_env_compose_file_args.py index d2a7f6cd2b..ee079c74ba 100644 --- a/tests/unit/python/cli/administration/deploy/development/test_env_compose_file_args.py +++ b/tests/unit/python/cli/administration/deploy/development/test_env_compose_file_args.py @@ -37,7 +37,14 @@ class TestComposeFileArgs(unittest.TestCase): def test_primary_instance_loads_the_cache_stack(self) -> None: self.assertEqual( compose_file_args(), - ["-f", "compose.yml", "-f", "compose/cache.override.yml"], + [ + "-f", + "compose.yml", + "-f", + "compose/cache.override.yml", + "-f", + "i18n/compose.override.yml", + ], ) @patch.dict(os.environ, _WORKTREE, clear=False) @@ -53,6 +60,8 @@ class TestComposeFileArgs(unittest.TestCase): "compose/cache.override.yml", "-f", "compose/cache.shared.override.yml", + "-f", + "i18n/compose.override.yml", ], ) @@ -60,7 +69,14 @@ class TestComposeFileArgs(unittest.TestCase): def test_a_stray_slot_alone_changes_nothing(self) -> None: self.assertEqual( compose_file_args(), - ["-f", "compose.yml", "-f", "compose/cache.override.yml"], + [ + "-f", + "compose.yml", + "-f", + "compose/cache.override.yml", + "-f", + "i18n/compose.override.yml", + ], ) @patch.dict(os.environ, {**_LOCAL_PRIMARY, "INFINITO_GIT_COMMON_DIR": "/repo/.git"}) @@ -74,6 +90,8 @@ class TestComposeFileArgs(unittest.TestCase): "compose/worktree.override.yml", "-f", "compose/cache.override.yml", + "-f", + "i18n/compose.override.yml", ], ) @@ -81,22 +99,30 @@ class TestComposeFileArgs(unittest.TestCase): def test_ci_keeps_the_git_mount_but_drops_every_cache_override(self) -> None: self.assertEqual( compose_file_args(), - ["-f", "compose.yml", "-f", "compose/worktree.override.yml"], + [ + "-f", + "compose.yml", + "-f", + "compose/worktree.override.yml", + "-f", + "i18n/compose.override.yml", + ], ) @patch.dict(os.environ, {**_LOCAL_PRIMARY, "CI": "true"}, clear=False) def test_ci_loads_no_override_at_all(self) -> None: - self.assertEqual(compose_file_args(), ["-f", "compose.yml"]) + self.assertEqual( + compose_file_args(), + ["-f", "compose.yml", "-f", "i18n/compose.override.yml"], + ) @patch.dict( os.environ, {**_LOCAL_PRIMARY, "INFINITO_PUBLISH_PORTS": "false"}, clear=False, ) - def test_unpublished_ports_append_the_noports_override(self) -> None: - self.assertEqual( - compose_file_args()[-2:], ["-f", "compose/noports.override.yml"] - ) + def test_unpublished_ports_add_the_noports_override(self) -> None: + self.assertIn("compose/noports.override.yml", compose_file_args()) if __name__ == "__main__": # pragma: no cover diff --git a/utils/env/handlers/__init__.py b/utils/env/handlers/__init__.py index adc237af9c..2e4bf0f897 100644 --- a/utils/env/handlers/__init__.py +++ b/utils/env/handlers/__init__.py @@ -21,6 +21,8 @@ from .infinito import ( distros, docker_volume, domain, + gpu_count, + i18n_libretranslate, image, image_repository, inventory, @@ -93,6 +95,8 @@ ORDERED_HANDLERS = [ fork_repository_url, nix_config, registry_cache_max_size, + gpu_count, + i18n_libretranslate, package_cache_heap, package_cache_direct_mem, package_cache_blobstore_max, diff --git a/utils/env/handlers/infinito/gpu_count.py b/utils/env/handlers/infinito/gpu_count.py new file mode 100644 index 0000000000..60df93fe31 --- /dev/null +++ b/utils/env/handlers/infinito/gpu_count.py @@ -0,0 +1,23 @@ +"""INFINITO_GPU_COUNT: GPUs the compose services reserve, 'all' when the docker +daemon registers an NVIDIA runtime, else 0.""" + +from __future__ import annotations + +import subprocess +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from utils.env.builder import BuildContext, EnvBuilder + +KEY = "INFINITO_GPU_COUNT" +COMMENT = "GPUs the compose services reserve: 'all' on an NVIDIA host, else 0." + + +def apply(eb: EnvBuilder, ctx: BuildContext) -> None: + runtimes = subprocess.run( # noqa: S607 - docker resolves from PATH as in every other env handler + ["docker", "info", "--format", "{{json .Runtimes}}"], + capture_output=True, + text=True, + check=False, + ) + eb.setdefault(KEY, "all" if "nvidia" in runtimes.stdout else "0", comment=COMMENT) diff --git a/utils/env/handlers/infinito/i18n_libretranslate.py b/utils/env/handlers/infinito/i18n_libretranslate.py new file mode 100644 index 0000000000..bdf0206f09 --- /dev/null +++ b/utils/env/handlers/infinito/i18n_libretranslate.py @@ -0,0 +1,27 @@ +"""INFINITO_I18N_LIBRETRANSLATE_PORT: loopback port the i18n runner forwards to +its LibreTranslate, read from roles/web-svc-libretranslate/meta/services.yml.""" + +from __future__ import annotations + +import re +from typing import TYPE_CHECKING + +from utils.cache.files import read_text +from utils.roles.mapping import ROLE_FILE_META_SERVICES + +if TYPE_CHECKING: + from utils.env.builder import BuildContext, EnvBuilder + +KEY = "INFINITO_I18N_LIBRETRANSLATE_PORT" +COMMENT = "Loopback port the i18n runner forwards to its LibreTranslate." +SERVICES = f"roles/web-svc-libretranslate/{ROLE_FILE_META_SERVICES}" +HTTP_PORT = re.compile(r"^\s+local:\n\s+http:\s*(\d+)\s*$", re.MULTILINE) + + +def apply(eb: EnvBuilder, ctx: BuildContext) -> None: + services = ctx.repo_root / SERVICES + if not services.is_file(): + return + found = HTTP_PORT.search(read_text(services)) + if found: + eb.setdefault(KEY, found.group(1), comment=COMMENT)