diff --git a/README.md b/README.md index 8edd547..d8d0664 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,8 @@ started earlier pulls whatever image the registry held before, which is stale. 4. Reconciles `apt-cacher-ng` and `signal-api`: each is recreated only if its definition changed. It never touches `postgres` or `redis`. It then removes services netOrk no longer ships, container and volume, where a host - still has them: today the bundled `registry` (NetOrk/netork#763). + still has them: today the bundled `registry` (NetOrk/netork#763). It reports only + what it actually removed; on a host that was cleaned earlier it says nothing. 5. Verifies that the containers run exactly the image that was pulled. If they don't, the deploy fails. 6. Records `REGISTRY_HOST` and `NETORK_VERSION` in `~/netork/.env`, so that a diff --git a/deploy.sh b/deploy.sh index 229c40a..c28b2a0 100644 --- a/deploy.sh +++ b/deploy.sh @@ -316,6 +316,9 @@ deploy_steps() { # file, so each would keep running on every host until someone removed it by # hand. Idempotent: a host that never had one, or was cleaned already, passes # untouched. Containers first; a volume still in use cannot be removed. + # Only what exists is removed and reported: `docker rm -f` exits 0 for a + # container that is not there, so without the check every deploy said it had + # removed the registry, long after it was gone (NetOrk/deploy#7). # registry (netork-registry-1, netork_registry_data): the bundled registry:2 # for satellite images. Satellites pull from registry.netork.io; it held one # old image, nothing had pulled from it in 30 days, and it accepted @@ -325,7 +328,8 @@ deploy_steps() { echo "[${SERVER}] Removing retired services..." run_remote "$SERVER" "removing ${RETIRED_CONTAINERS} ${RETIRED_VOLUMES}" "" \ "for c in ${RETIRED_CONTAINERS}; do \ - docker rm -f \$c >/dev/null 2>&1 && echo \"removed container \$c\"; done; \ + docker inspect \$c >/dev/null 2>&1 && docker rm -f \$c >/dev/null 2>&1 \ + && echo \"removed container \$c\"; done; \ for v in ${RETIRED_VOLUMES}; do \ docker volume rm \$v >/dev/null 2>&1 && echo \"removed volume \$v\"; done; true" || true diff --git a/tests/test_deploy_retired_services.py b/tests/test_deploy_retired_services.py index 75eab06..7248e0d 100644 --- a/tests/test_deploy_retired_services.py +++ b/tests/test_deploy_retired_services.py @@ -71,3 +71,47 @@ def test_the_infrastructure_reconcile_no_longer_starts_the_registry(tmp_path: Pa reconcile = [c for c in commands if "up -d --no-deps" in c and "--force-recreate" not in c] assert reconcile, "the infrastructure reconcile step is missing" assert " registry" not in reconcile[0].split("up -d --no-deps", 1)[1] + + +# ── It says it removed only what was there (NetOrk/deploy#7) ────────────────── +# +# `docker rm -f` exits 0 for a container that does not exist, so the step said +# "removed container netork-registry-1" on every deploy of every host, long after +# the container was gone -- which read as something recreating it each time. + +_FAKE_DOCKER = """#!/bin/sh +case "$1" in + inspect) [ "{exists}" = yes ] ;; + rm) exit 0 ;; + volume) exit 1 ;; +esac +""" + + +def _removal_output(tmp_path: Path, *, exists: bool) -> str: + """Run the deploy's removal command against a docker that has the container or not.""" + _, commands = _deploy(tmp_path) + removal = next(c for c in commands if "netork-registry-1" in c) + docker_dir = tmp_path / "docker-bin" + docker_dir.mkdir() + docker = docker_dir / "docker" + docker.write_text(_FAKE_DOCKER.replace("{exists}", "yes" if exists else "no")) + docker.chmod(0o755) + run = subprocess.run( + ["sh", "-c", removal], + env={"PATH": f"{docker_dir}:{os.environ['PATH']}"}, + capture_output=True, + text=True, + timeout=30, + check=False, + ) + assert run.returncode == 0, run.stderr + return run.stdout + + +def test_a_container_that_is_not_there_is_not_reported_removed(tmp_path: Path) -> None: + assert "removed container" not in _removal_output(tmp_path, exists=False) + + +def test_one_that_was_there_is(tmp_path: Path) -> None: + assert "removed container netork-registry-1" in _removal_output(tmp_path, exists=True)