feat(env): give every checkout its own stack domain

Every stack answered to *.infinito.test, and the host resolver pointed that
zone at 127.0.0.1, so a worktree's stack on its own bind IP could not be opened
in a browser while the primary stack existed.

INFINITO_DOMAIN is now derived per checkout: main.infinito.test for the
primary one, and for a linked worktree its branch, lowercased with everything
outside a-z0-9_- turned into "-", below INFINITO_DNS_DOMAIN
(feature/store-i18n -> feature-store-i18n.infinito.test). The branch is read
from the worktree's gitdir HEAD instead of running git, because git refuses the
mounted checkout inside the stack container. GitHub and act runs keep the
static default, custom.env still wins.

The handler overrides the caller's value on purpose: inside the stack
container that value is the previous .env, which make setup rewrote on every
deploy, so the old domain kept feeding itself back into the next compose up.

make network-dns-setup routes each stack's domain to the INFINITO_BIND_IP in
that checkout's .env, with a local= line so AAAA answers NODATA instead of the
primary stack's ::1. It skips domains outside the base zone, which a stale
.env still carries, and fails when two checkouts claim one domain.
worktree-up prints the domain and the follow-up command.

Existing checkouts switch after make dotenv-force and a redeploy.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5 committed 2026-09-21 17:44:38 +02:00
1 parent 6eb9a59d14
commit 96df7ea0eb
10 files changed
+265 -29

No files matched your search

@@ -18,6 +18,7 @@ from utils.cleanup.nginx_vhosts import (
main,
purge_vhost_files_for_entities,
)
from utils.domains.default_primary import default_domain_primary
from utils.roles.categories import categories_file
from utils.roles.mapping import ROLE_FILE_META_DOMAINS, ROLE_FILE_VARS_MAIN
@@ -365,7 +366,9 @@ class TestResolveDomainPrimary(unittest.TestCase):
if k not in ("DOMAIN", "INFINITO_DOMAIN")
}
with patch.dict(os.environ, env, clear=True):
self.assertEqual(mod._resolve_domain_primary(None), "infinito.test")
self.assertEqual(
mod._resolve_domain_primary(None), default_domain_primary()
)
if __name__ == "__main__": # pragma: no cover
@@ -0,0 +1,112 @@
"""Unit tests for :mod:`utils.env.handlers.infinito.domain`."""
from __future__ import annotations
import io
import os
import subprocess
import unittest
from contextlib import redirect_stderr
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch
from utils.env.builder import BuildContext, EnvBuilder
from utils.env.handlers.infinito import domain as handler
_GIT = [
"git",
"-c",
"user.name=t",
"-c",
"user.email=t@t",
"-c",
"commit.gpgSign=false",
]
def _git(repo: Path, *args: str) -> None:
subprocess.run([*_GIT, "-C", str(repo), *args], check=True, capture_output=True)
def _repository(root: Path) -> Path:
repo = root / "repo"
repo.mkdir()
_git(repo, "init", "-q", "-b", "main")
_git(repo, "commit", "-q", "--allow-empty", "-m", "init")
return repo
def _worktree(repo: Path, branch: str) -> Path:
path = repo.parent / "worktree"
_git(repo, "worktree", "add", "-q", "-b", branch, str(path))
return path
def _apply(
repo_root: Path, *, on_gha: bool = False, on_act: bool = False
) -> str | None:
ctx = BuildContext(
static={},
static_comments={},
repo_root=repo_root,
on_gha=on_gha,
on_act=on_act,
)
eb = EnvBuilder()
eb.set("INFINITO_DNS_DOMAIN", "infinito.test")
handler.apply(eb, ctx)
return eb.values.get(handler.KEY)
class TestDomain(unittest.TestCase):
def test_primary_checkout_is_main(self) -> None:
with TemporaryDirectory() as td:
self.assertEqual(_apply(_repository(Path(td))), "main.infinito.test")
def test_directory_without_git_is_main(self) -> None:
with TemporaryDirectory() as td:
self.assertEqual(_apply(Path(td)), "main.infinito.test")
def test_worktree_is_its_normalised_branch(self) -> None:
with TemporaryDirectory() as td:
worktree = _worktree(_repository(Path(td)), "Feature/Store_I18n.v2")
self.assertEqual(_apply(worktree), "feature-store_i18n-v2.infinito.test")
def test_worktree_with_unreachable_git_directory_keeps_the_default(
self,
) -> None:
with TemporaryDirectory() as td:
checkout = Path(td)
(checkout / ".git").write_text(
"gitdir: /host/only/.git/worktrees/x\n", encoding="utf-8"
)
stderr = io.StringIO()
with redirect_stderr(stderr):
self.assertIsNone(_apply(checkout))
self.assertIn("not reachable", stderr.getvalue())
def test_branch_too_long_for_a_dns_label_fails(self) -> None:
with TemporaryDirectory() as td:
worktree = _worktree(_repository(Path(td)), "a" * 64)
with self.assertRaises(SystemExit):
_apply(worktree)
def test_stale_caller_value_is_replaced_but_ci_runs_keep_the_default(
self,
) -> None:
with TemporaryDirectory() as td:
worktree = _worktree(_repository(Path(td)), "feature/x")
with patch.dict(os.environ, {handler.KEY: "infinito.test"}):
self.assertEqual(_apply(worktree), "feature-x.infinito.test")
self.assertIsNone(_apply(worktree, on_gha=True))
self.assertIsNone(_apply(worktree, on_act=True))
if __name__ == "__main__":
unittest.main()