Files
kevinveenbirkenbachandClaude Opus 5.5 310663ae8c feat(compose): recreate the stack network without losing volumes or worktree stacks
When the IPAM config of a compose project's network changes (#999 added
`ip_range`), compose recreates the network on the next `up`. With a
foreign endpoint attached, as every worktree stack is on the primary's
network, compose first stops the project's own containers, then fails
with `error while removing network: ... has active endpoints`, and a
repeated `up` or `up --force-recreate` does not recover. On 2026-10-19
this took the shared registry/package cache and coredns down for every
local session.

`make compose-network-recreate` (alias `i8conere`) runs the sequence that
recovered it: detach every running container of another project from the
network, `docker compose down --remove-orphans` without `-v` so the
caches keep their volumes, the normal `up`, then attach the detached
containers again, which draw addresses from the dynamic pool. If `up`
fails, the still-detached containers are listed instead of reattached.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-20 13:45:37 +02:00

81 lines
1.8 KiB
Python

from __future__ import annotations
import argparse
import importlib
import importlib.util
import sys
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Iterable
def _module_exists(fullname: str) -> bool:
return importlib.util.find_spec(fullname) is not None
def _try_import(fullname: str):
"""
Import module if it exists, else return None.
"""
if not _module_exists(fullname):
return None
return importlib.import_module(fullname)
def _iter_command_modules(base_pkg: str, names: Iterable[str]):
"""
Yield imported command modules that implement add_parser(sub).
"""
for name in names:
m = _try_import(f"{base_pkg}.{name}")
if m is None:
continue
if hasattr(m, "add_parser"):
yield name, m
def _build_parser(base_pkg: str) -> argparse.ArgumentParser:
p = argparse.ArgumentParser(
prog=f"python -m {base_pkg}",
description="Infinito.Nexus development compose stack helper.",
)
sub = p.add_subparsers(dest="command", required=True)
command_names = [
"up",
"down",
"stop",
"restart",
"build",
"init",
"deploy",
"run",
"exec",
"logs",
"recreate",
]
for _, mod in _iter_command_modules(base_pkg, command_names):
mod.add_parser(sub)
return p
def main(argv: list[str] | None = None) -> int:
if argv is None:
argv = sys.argv[1:]
base_pkg = __package__ or "cli.administration.deploy.development"
parser = _build_parser(base_pkg)
args = parser.parse_args(argv)
handler = getattr(args, "_handler", None)
if handler is None:
raise SystemExit(
f"Command '{getattr(args, 'command', '<unknown>')}' has no handler"
)
return int(handler(args))