diff --git a/Makefile b/Makefile index 37c133c391..b539f09084 100644 --- a/Makefile +++ b/Makefile @@ -238,17 +238,17 @@ format: test: test-lint test-unit test-integration lint-ansible test-deploy @echo "✅ Full test (setup + tests) executed." -test-lint: +test-lint: build-missing @TEST_TYPE="lint" \ INFINITO_COMPILE=0 \ bash scripts/tests/code.sh -test-unit: +test-unit: build-missing @TEST_TYPE="unit" \ INFINITO_COMPILE=0 \ bash scripts/tests/code.sh -test-integration: +test-integration: build-missing @TEST_TYPE="integration" \ INFINITO_COMPILE=0 \ bash scripts/tests/code.sh diff --git a/cli/deploy/development/build.py b/cli/deploy/development/build.py new file mode 100644 index 0000000000..98b16fe772 --- /dev/null +++ b/cli/deploy/development/build.py @@ -0,0 +1,71 @@ +from __future__ import annotations + +import argparse +import os +import subprocess + +from .common import repo_root_from_here + + +def add_parser(sub: argparse._SubParsersAction) -> None: + p = sub.add_parser( + "build", help="Build infinito image using scripts/build/image.sh (SPOT)." + ) + p.add_argument( + "--distro", + default=os.environ.get("INFINITO_DISTRO", "arch"), + choices=["arch", "debian", "ubuntu", "fedora", "centos"], + help="Target distro (INFINITO_DISTRO).", + ) + p.add_argument("--missing", action="store_true", help="Build only if missing.") + p.add_argument("--no-cache", action="store_true", help="Build with --no-cache.") + p.add_argument("--target", help="Dockerfile target (e.g. virgin).", default="") + p.add_argument("--tag", help="Override output tag.", default="") + p.add_argument("--push", action="store_true", help="Push image (buildx).") + p.add_argument( + "--publish", action="store_true", help="Publish semantic tags (implies --push)." + ) + p.add_argument("--registry", default="", help="Registry (e.g. ghcr.io).") + p.add_argument("--owner", default="", help="Owner/namespace (e.g. org/user).") + p.add_argument("--repo-prefix", default="", help="Repo prefix (default: infinito).") + p.add_argument("--version", default="", help="Version (required for --publish).") + p.add_argument( + "--stable", choices=["true", "false"], default="", help="Publish stable tags." + ) + + p.set_defaults(_handler=handler) + + +def handler(args: argparse.Namespace) -> int: + repo_root = repo_root_from_here() + script = repo_root / "scripts" / "build" / "image.sh" + + env = dict(os.environ) + env["INFINITO_DISTRO"] = args.distro + + cmd: list[str] = [str(script)] + if args.missing: + cmd.append("--missing") + if args.no_cache: + cmd.append("--no-cache") + if args.target: + cmd += ["--target", args.target] + if args.tag: + cmd += ["--tag", args.tag] + if args.push: + cmd.append("--push") + if args.publish: + cmd.append("--publish") + if args.registry: + cmd += ["--registry", args.registry] + if args.owner: + cmd += ["--owner", args.owner] + if args.repo_prefix: + cmd += ["--repo-prefix", args.repo_prefix] + if args.version: + cmd += ["--version", args.version] + if args.stable: + cmd += ["--stable", args.stable] + + r = subprocess.run(cmd, cwd=repo_root, env=env, check=False) + return int(r.returncode) diff --git a/cli/deploy/development/cli.py b/cli/deploy/development/cli.py index 7e8a5721a4..cb0731a8cc 100644 --- a/cli/deploy/development/cli.py +++ b/cli/deploy/development/cli.py @@ -3,6 +3,7 @@ from __future__ import annotations import argparse from typing import Optional +from . import build as cmd_build from . import deploy as cmd_deploy from . import down as cmd_down from . import exec as cmd_exec @@ -30,6 +31,7 @@ def build_parser() -> argparse.ArgumentParser: cmd_run.add_parser(sub) cmd_exec.add_parser(sub) cmd_logs.add_parser(sub) + cmd_build.add_parser(sub) return parser diff --git a/cli/deploy/development/compose.py b/cli/deploy/development/compose.py index 91ffb1895a..c61943cedd 100644 --- a/cli/deploy/development/compose.py +++ b/cli/deploy/development/compose.py @@ -56,36 +56,6 @@ class Compose: env=env, ) - def build_infinito(self, *, no_cache: bool, missing_only: bool) -> None: - if os.environ.get("INFINITO_NO_BUILD", "0") == "1": - print(">>> INFINITO_NO_BUILD=1 -> skipping docker compose build") - return - - # image name in compose: "infinito-${INFINITO_DISTRO}" - image = os.environ.get("INFINITO_IMAGE") or f"infinito-{self.distro}" - - if missing_only: - r = subprocess.run( - ["docker", "image", "inspect", image], - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - check=False, - ) - if r.returncode == 0: - print( - f">>> Image already exists: {image} (skipping build due to --missing)" - ) - return - - if no_cache: - print( - f">>> docker compose build --no-cache infinito (INFINITO_DISTRO={self.distro})" - ) - self.run(["build", "--no-cache", "infinito"], check=True) - else: - print(f">>> docker compose build infinito (INFINITO_DISTRO={self.distro})") - self.run(["build", "infinito"], check=True) - def up(self, *, run_entry_init: bool = True) -> None: print(">>> Rendering CoreDNS Corefile from template") self._render_coredns_corefile() @@ -103,7 +73,9 @@ class Compose: print(">>> env:", {k: env.get(k) for k in keys}) print(">>> NIX_CONFIG:", "" if env.get("NIX_CONFIG") else "") - no_build = os.environ.get("INFINITO_NO_BUILD", "0") == "1" + # IMPORTANT: use the same env snapshot that docker compose will use + no_build = env.get("INFINITO_NO_BUILD", "0") == "1" + args = ["--env-file", "env.ci", "up", "-d"] if no_build: args.append("--no-build") @@ -200,7 +172,6 @@ class Compose: ">>> ERROR: infinito container not healthy, dumping last 200 log lines\n" ) - # Preferred: systemd logs logs = self.exec( ["sh", "-lc", "journalctl -n 200 --no-pager || true"], check=False, @@ -211,7 +182,6 @@ class Compose: print(logs.stdout or "") print("======================================\n") - # Fallback: docker logs docker_logs = subprocess.run( ["docker", "logs", "--tail", "200", cid], cwd=self.repo_root, diff --git a/cli/deploy/development/restart.py b/cli/deploy/development/restart.py index 71101c1143..516f0a760b 100644 --- a/cli/deploy/development/restart.py +++ b/cli/deploy/development/restart.py @@ -19,16 +19,6 @@ def add_parser(sub: argparse._SubParsersAction) -> None: help="Target distro (compose env INFINITO_DISTRO).", ) - # These mirror 'up' options, but are optional; default behavior is "no force" - p.add_argument( - "--no-cache", action="store_true", help="Rebuild image with --no-cache." - ) - p.add_argument("--missing", action="store_true", help="Build only if missing.") - p.add_argument( - "--no-build", - action="store_true", - help="Do not build (regardless of INFINITO_NO_BUILD).", - ) p.add_argument( "--skip-entry-init", action="store_true", @@ -51,9 +41,6 @@ def handler(args: argparse.Namespace) -> int: # 2) up (normal behavior, no force unless flags provided) class _UpArgs: distro = args.distro - no_cache = bool(args.no_cache) - missing = bool(args.missing) - no_build = bool(args.no_build) skip_entry_init = bool(args.skip_entry_init) return int(up_handler(_UpArgs())) diff --git a/cli/deploy/development/run.py b/cli/deploy/development/run.py index 1592a15b25..b6d0ec14ca 100644 --- a/cli/deploy/development/run.py +++ b/cli/deploy/development/run.py @@ -4,10 +4,11 @@ import argparse import os from pathlib import Path -from .common import make_compose, resolve_deploy_ids_for_app -from .init import handler as init_handler from .deploy import handler as deploy_handler +from .init import handler as init_handler from .log_utils import append_text, log_path, write_text +from .common import make_compose, resolve_deploy_ids_for_app +from .up import handler as up_handler def add_parser(sub: argparse._SubParsersAction) -> None: @@ -26,10 +27,6 @@ def add_parser(sub: argparse._SubParsersAction) -> None: choices=["arch", "debian", "ubuntu", "fedora", "centos"], help="Target distro (compose env INFINITO_DISTRO).", ) - p.add_argument( - "--no-cache", action="store_true", help="Rebuild compose image with --no-cache." - ) - p.add_argument("--missing", action="store_true", help="Build only if missing.") p.add_argument( "--skip-entry-init", action="store_true", @@ -61,8 +58,6 @@ def add_parser(sub: argparse._SubParsersAction) -> None: def handler(args: argparse.Namespace) -> int: - compose = make_compose(distro=args.distro) - logs_dir = Path(args.logs_dir).resolve() logs_dir.mkdir(parents=True, exist_ok=True) @@ -79,16 +74,26 @@ def handler(args: argparse.Namespace) -> int: if passthrough and passthrough[0] == "--": passthrough = passthrough[1:] - try: - compose.build_infinito( - no_cache=bool(args.no_cache), missing_only=bool(args.missing) - ) - compose.up(run_entry_init=not bool(args.skip_entry_init)) + compose = None + try: + # 1) up (re-use up handler so build-missing stays SPOT) + class _UpArgs: + distro = args.distro + skip_entry_init = bool(args.skip_entry_init) + + rc_up = int(up_handler(_UpArgs())) + if rc_up != 0: + return rc_up + + # Create compose AFTER up so we have a fresh instance for the remainder. + compose = make_compose(distro=args.distro) + + # 2) resolve deps deploy_ids = resolve_deploy_ids_for_app(compose, args.app) append_text(main_log, f"\nresolved_ids={','.join(deploy_ids)}\n") - # init subcommand (re-using its handler via a tiny args object) + # 3) init subcommand (re-using its handler via a tiny args object) class _InitArgs: distro = args.distro app = args.app @@ -98,13 +103,13 @@ def handler(args: argparse.Namespace) -> int: init_handler(_InitArgs()) - # deploy subcommand + # 4) deploy subcommand class _DeployArgs: distro = args.distro type = args.type app = args.app id = None - debug = args.debug + debug = bool(args.debug) ansible_args = ["--", *passthrough] if passthrough else [] rc = deploy_handler(_DeployArgs()) @@ -130,7 +135,7 @@ def handler(args: argparse.Namespace) -> int: return 1 finally: should_keep = bool(args.keep_stack_on_failure) and exit_code != 0 - if not should_keep: + if not should_keep and compose is not None: try: compose.down() except Exception: diff --git a/cli/deploy/development/up.py b/cli/deploy/development/up.py index 01144e8c5a..7435c42c91 100644 --- a/cli/deploy/development/up.py +++ b/cli/deploy/development/up.py @@ -3,6 +3,7 @@ from __future__ import annotations import argparse import os +from .build import handler as build_handler from .common import make_compose @@ -14,36 +15,60 @@ def add_parser(sub: argparse._SubParsersAction) -> None: choices=["arch", "debian", "ubuntu", "fedora", "centos"], help="Target distro (compose env INFINITO_DISTRO).", ) - p.add_argument( - "--no-cache", action="store_true", help="Rebuild image with --no-cache." - ) - p.add_argument( - "--missing", - action="store_true", - help="Build only if missing (skip build if image exists).", - ) - p.add_argument( - "--no-build", - action="store_true", - help="Do not build (regardless of INFINITO_NO_BUILD).", - ) p.add_argument( "--skip-entry-init", action="store_true", help="Do not run /opt/src/infinito/scripts/docker/entry.sh true after stack is up.", ) + p.add_argument( + "--when-down", + action="store_true", + help="Only start stack if it is not already running.", + ) p.set_defaults(_handler=handler) -def handler(args: argparse.Namespace) -> int: - compose = make_compose(distro=args.distro) +def _maybe_build_missing(*, distro: str) -> int: + # CI: never build (image is pulled). Local: build if missing. + if os.environ.get("INFINITO_NO_BUILD", "0") == "1": + return 0 - # Allow explicit no-build via CLI even if local defaults build. - if args.no_build: - os.environ["INFINITO_NO_BUILD"] = "1" - - compose.build_infinito( - no_cache=bool(args.no_cache), missing_only=bool(args.missing) + build_args = argparse.Namespace( + distro=distro, + missing=True, + no_cache=False, + target="", + tag="", + push=False, + publish=False, + registry="", + owner="", + repo_prefix="", + version="", + stable="", ) + + return int(build_handler(build_args)) + + +def _stack_is_running(*, distro: str) -> bool: + compose = make_compose(distro=distro) + r = compose.run(["ps", "-q", "infinito"], check=False, capture=True) + cid = (r.stdout or "").strip() + return bool(cid) + + +def handler(args: argparse.Namespace) -> int: + # when-down: skip if already running + if args.when_down: + if _stack_is_running(distro=args.distro): + print(">>> Stack already running — skipping up") + return 0 + + rc = _maybe_build_missing(distro=args.distro) + if rc != 0: + return rc + + compose = make_compose(distro=args.distro) compose.up(run_entry_init=not bool(args.skip_entry_init)) return 0 diff --git a/scripts/tests/code.sh b/scripts/tests/code.sh index e9576c4aad..6fe3bb992d 100755 --- a/scripts/tests/code.sh +++ b/scripts/tests/code.sh @@ -9,11 +9,7 @@ echo "============================================================" : "${TEST_PATTERN:?TEST_PATTERN must be set}" : "${TEST_TYPE:?TEST_TYPE must be set}" -# 1) Bring up the development compose stack (coredns + infinito). -# This uses the new Python orchestrator (healthcheck + entry init). -python3 -m cli.deploy.development up \ - --no-build \ - --distro "${INFINITO_DISTRO}" +python3 -m cli.deploy.development up --when-down # 2) Run tests inside the already running infinito container via the new exec wrapper. # We keep the old NIX_CONFIG behavior (flake config acceptance) but run it inside the container. diff --git a/scripts/tests/deploy/local/all.sh b/scripts/tests/deploy/local/all.sh index b91f77bdc8..d7ed34a507 100755 --- a/scripts/tests/deploy/local/all.sh +++ b/scripts/tests/deploy/local/all.sh @@ -69,7 +69,6 @@ echo # --------------------------------------------------------------------------- echo ">>> Starting development compose stack (no build)" python3 -m cli.deploy.development up \ - --no-build \ --distro "${INFINITO_DISTRO}" \ --skip-entry-init