From 476afe3982bba7326fa83832d35410f615e73ead Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Fri, 23 Oct 2026 12:06:08 +0200 Subject: [PATCH 1/2] fix: remove only the images the deploy replaced, never every unused one The deploy ended with docker image prune -af: every image no container used at that moment. On a host that runs other things too, that takes their rollback images; the KB's copy of this line did exactly that on compute-01 (NetOrk/kb-server#157). deploy.sh now records the image IDs netOrk's own containers use before the swap and, after the verification and the migration, hands them to remote/remove_replaced_images.sh. It removes each by its tags, or by its ID once a moving tag left it without one, and keeps any image a container still uses: a redeploy of the same tag replaces nothing. --- README.md | 7 +- deploy.sh | 21 ++++- remote/remove_replaced_images.sh | 29 +++++++ tests/test_deploy_replaced_images.py | 80 ++++++++++++++++++ tests/test_remote_replaced_images.py | 120 +++++++++++++++++++++++++++ 5 files changed, 254 insertions(+), 3 deletions(-) create mode 100644 remote/remove_replaced_images.sh create mode 100644 tests/test_deploy_replaced_images.py create mode 100644 tests/test_remote_replaced_images.py diff --git a/README.md b/README.md index f53773e..c795a2d 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,12 @@ started earlier pulls whatever image the registry held before, which is stale. 10. Verifies that the containers run exactly the image that was pulled. If they don't, the deploy fails. 11. Runs `alembic upgrade head` inside `netork-api`. -12. Prunes unused images. +12. Removes the images the swap replaced, and only those. Before the swap it records which + images netOrk's own containers use; afterwards `remote/remove_replaced_images.sh` + removes them unless a container still uses one, a redeploy of the same tag for + example. Other projects' images on the host stay. This used to be + `docker image prune -af`, which took every unused image, theirs included + (NetOrk/kb-server#157). A failing step stops that server's deploy with a non-zero exit, and the other servers carry on. The script exits non-zero if any server failed and names those servers. diff --git a/deploy.sh b/deploy.sh index a396af4..79100fb 100644 --- a/deploy.sh +++ b/deploy.sh @@ -429,6 +429,14 @@ REMOTE_ENV # netOrk was down until someone ran the deploy again, which then went through # (NetOrk/netork#586). The second attempt is that rerun. Should it fail as well, # the container states go to the log and the deploy fails as before. + # The images netOrk's own containers use now, before the swap: after it, these + # are what the deploy replaced, and the only images it removes (below). + local REPLACED + REPLACED=$(ssh -n "$SERVER" "cd ~/netork && \ + REGISTRY_HOST='${REGISTRY_HOST}' NETORK_VERSION='${SERVER_VERSION}' \ + docker compose -f docker-compose.yml -f docker-compose.registry.yml ps -aq ${SERVICES} \ + | xargs -r docker inspect --format '{{.Image}}' | sort -u" 2>/dev/null || true) + echo "[${SERVER}] Starting containers..." local RECREATE="cd ~/netork && \ REGISTRY_HOST='${REGISTRY_HOST}' NETORK_VERSION='${SERVER_VERSION}' \ @@ -527,8 +535,17 @@ REMOTE_ENV run_remote "$SERVER" "alembic upgrade head (containers are ALREADY swapped)" "" \ "cd ~/netork && docker compose exec -T netork-api alembic upgrade head" - echo "[${SERVER}] Pruning unused images..." - ssh -n "$SERVER" "docker image prune -af 2>&1 | tail -1" | sed "s/^/[${SERVER}] /" + # The images the swap replaced, and nothing else. This was `docker image prune + # -af`, which removes every image no container uses at that moment: on a host + # that runs other things too, their rollback images (NetOrk/kb-server#157). + # remote/remove_replaced_images.sh keeps any image a container still uses. + if [[ -n "$REPLACED" ]]; then + echo "[${SERVER}] Removing the images this deploy replaced..." + local ids="${REPLACED//$'\n'/ }" + # shellcheck disable=SC2029 # the image IDs are meant to expand here + ssh "$SERVER" "bash -s -- ${ids}" < "${HERE}/remote/remove_replaced_images.sh" 2>&1 \ + | sed "s/^/[${SERVER}] /" || true + fi echo "[${SERVER}] Done." } diff --git a/remote/remove_replaced_images.sh b/remote/remove_replaced_images.sh new file mode 100644 index 0000000..ae059a1 --- /dev/null +++ b/remote/remove_replaced_images.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# remove_replaced_images.sh ...: the images a deploy replaced, and +# nothing else. deploy.sh records the IDs netOrk's own containers use before the +# swap and runs this on the host after it. +# +# It used to be `docker image prune -af`, which removes every image no container +# uses at that moment. On a host that runs other things too, that took their +# rollback images (NetOrk/kb-server#157). An ID goes here only if no container -- +# netOrk's or anyone else's -- uses it: a redeploy of the same tag replaces +# nothing. It goes by every tag it carries, or by its ID once a moving tag such as +# latest-dev has left it without one. +set -u + +in_use=$(docker ps -aq | xargs -r docker inspect --format '{{.Image}}' 2>/dev/null | sort -u) + +for id in "$@"; do + if printf '%s\n' "$in_use" | grep -qx -- "$id"; then + echo "kept ${id:7:12} (in use)" + continue + fi + tags=$(docker image inspect --format '{{range .RepoTags}}{{.}} {{end}}' "$id" 2>/dev/null) || continue + # shellcheck disable=SC2086 # the tags are separate words on purpose + if docker image rm -- ${tags:-$id} >/dev/null 2>&1; then + echo "removed ${tags:-$id}" + else + echo "kept ${id:7:12}" + fi +done +exit 0 diff --git a/tests/test_deploy_replaced_images.py b/tests/test_deploy_replaced_images.py new file mode 100644 index 0000000..2f0c8f3 --- /dev/null +++ b/tests/test_deploy_replaced_images.py @@ -0,0 +1,80 @@ +"""A deploy removes the images it replaced, and nothing else (NetOrk/kb-server#157). + +It ended with `docker image prune -af`: every image no container used at that +moment, on a host that may run other things too. Now it records the image IDs of +netOrk's own containers before the swap and, once the swap is verified and the +migration ran, hands them to remote/remove_replaced_images.sh. + +The remote side is a fake `ssh` that records every command and plays a host whose +netOrk containers run one image. +""" + +from __future__ import annotations + +import os +import subprocess +from pathlib import Path + +DEPLOY = Path(__file__).resolve().parent.parent / "deploy.sh" + +_FAKE_SSH = """#!/bin/sh +for arg in "$@"; do cmd="$arg"; done +case "$cmd" in *.deploy.lock*) exec sh -c "$cmd" ;; esac +case "$cmd" in + *"bash -s"*) cat > "{state}/stdin-$(date +%s%N)" ;; + *) cat > /dev/null ;; +esac +printf '%s\\n----\\n' "$cmd" >> "{state}/commands" +case "$cmd" in *"xargs -r docker inspect"*) echo sha256:old ;; esac +exit 0 +""" + + +def _deploy(tmp_path: Path) -> tuple[subprocess.CompletedProcess[str], list[str]]: + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + fake = bin_dir / "ssh" + fake.write_text(_FAKE_SSH.replace("{state}", str(tmp_path))) + fake.chmod(0o755) + result = subprocess.run( + ["bash", str(DEPLOY), "testhost"], + env={ + "PATH": f"{bin_dir}:{os.environ['PATH']}", + "HOME": str(tmp_path), + "DEPLOY_ENV_FILE": "/dev/null", + "REGISTRY_HOST": "registry.example", + "NETORK_VERSION": "latest-dev", + }, + stdin=subprocess.DEVNULL, + capture_output=True, + text=True, + timeout=60, + check=False, + ) + commands = (tmp_path / "commands").read_text().split("\n----\n") + return result, commands + + +def _index(commands: list[str], needle: str) -> int: + return next(i for i, cmd in enumerate(commands) if needle in cmd) + + +def test_no_prune_of_everything(tmp_path: Path) -> None: + result, commands = _deploy(tmp_path) + assert result.returncode == 0, result.stderr + assert not [cmd for cmd in commands if "image prune -af" in cmd] + + +def test_the_replaced_images_are_recorded_before_the_swap_and_removed_after( + tmp_path: Path, +) -> None: + result, commands = _deploy(tmp_path) + assert result.returncode == 0, result.stderr + recorded = _index(commands, "xargs -r docker inspect") + swap = _index(commands, "--force-recreate") + migrate = _index(commands, "alembic upgrade head") + removal = _index(commands, "sha256:old") + assert recorded < swap < migrate < removal + assert "bash -s" in commands[removal] + sent = [p.read_text() for p in tmp_path.glob("stdin-*")] + assert any("remove_replaced_images.sh" in text for text in sent), result.stdout diff --git a/tests/test_remote_replaced_images.py b/tests/test_remote_replaced_images.py new file mode 100644 index 0000000..eb0136c --- /dev/null +++ b/tests/test_remote_replaced_images.py @@ -0,0 +1,120 @@ +"""After a deploy, the images it replaced go, and nothing else: remote/remove_replaced_images.sh. + +deploy.sh used to end with `docker image prune -af`, which removes every image no +container uses at that moment. On a host that runs other things too, that took +their rollback images (NetOrk/kb-server#157, the same line in the KB's script). + +deploy.sh now records the image IDs netOrk's own containers use before the swap +and hands them to this script afterwards. It removes those, by every tag they +carry, unless a container -- netOrk's or anyone else's -- still uses them: a +redeploy of the same tag replaces nothing. + +The remote side is a fake `docker` that keeps images and containers in files. +""" + +from __future__ import annotations + +import os +import subprocess +from pathlib import Path + +SCRIPT = Path(__file__).resolve().parent.parent / "remote" / "remove_replaced_images.sh" + +_FAKE_DOCKER = r"""#!/bin/bash +state=$FAKE_STATE +printf '%s\n' "$*" >> "$state/calls" +case "$1 $2" in + "ps -aq") cut -d' ' -f1 "$state/containers" ;; + "inspect --format") + shift 3 + for c in "$@"; do grep "^$c " "$state/containers" | cut -d' ' -f2; done ;; + "image inspect") + id="${*: -1}" + line=$(grep "^$id" "$state/images") || exit 1 + echo "${line#* }" ;; + "image rm") + shift 2 + [ "$1" = -- ] && shift + for ref in "$@"; do + if grep -q " $ref\b" "$state/containers" 2>/dev/null; then exit 1; fi + sed -i "s# $ref##" "$state/images" + done + # An image without tags is gone. + sed -i '/^[^ ]*$/d' "$state/images" ;; +esac +exit 0 +""" + + +def _run(tmp_path: Path, images: list[str], containers: list[str], ids: list[str]): + (tmp_path / "images").write_text("".join(f"{line}\n" for line in images)) + (tmp_path / "containers").write_text("".join(f"{line}\n" for line in containers)) + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + (bin_dir / "docker").write_text(_FAKE_DOCKER) + (bin_dir / "docker").chmod(0o755) + result = subprocess.run( + ["bash", str(SCRIPT), *ids], + env={"PATH": f"{bin_dir}:{os.environ['PATH']}", "FAKE_STATE": str(tmp_path)}, + capture_output=True, + text=True, + timeout=30, + check=False, + ) + left = (tmp_path / "images").read_text().split() + return result, left + + +def test_the_replaced_release_goes_by_every_tag(tmp_path: Path) -> None: + result, left = _run( + tmp_path, + images=[ + "sha256:old r/netork/engine:0.41.0 r/netork/engine:main-aaa", + "sha256:new r/netork/engine:0.42.0", + ], + containers=["c1 sha256:new"], + ids=["sha256:old"], + ) + assert result.returncode == 0, result.stderr + assert "sha256:old" not in left + assert "sha256:new" in left + assert "removed" in result.stdout + + +def test_an_image_still_in_use_stays(tmp_path: Path) -> None: + # A redeploy of the same tag: the "old" image is the one running now. + result, left = _run( + tmp_path, + images=["sha256:same r/netork/engine:latest-dev r/netork/engine:main-bbb"], + containers=["c1 sha256:same"], + ids=["sha256:same"], + ) + assert result.returncode == 0 + assert left == ["sha256:same", "r/netork/engine:latest-dev", "r/netork/engine:main-bbb"] + assert "kept" in result.stdout + + +def test_another_projects_images_are_never_named(tmp_path: Path) -> None: + result, left = _run( + tmp_path, + images=["sha256:old r/netork/engine:0.41.0", "sha256:theirs other/app:1.0"], + containers=[], + ids=["sha256:old"], + ) + assert result.returncode == 0 + assert "sha256:theirs" in left + assert "prune" not in (tmp_path / "calls").read_text() + + +def test_an_untagged_leftover_goes_by_its_id(tmp_path: Path) -> None: + # latest-dev moved on: the old image has no tag left. + result, left = _run( + tmp_path, images=["sha256:dangling"], containers=[], ids=["sha256:dangling"] + ) + assert result.returncode == 0 + assert "sha256:dangling" not in left + + +def test_nothing_to_remove_is_no_failure(tmp_path: Path) -> None: + result, _ = _run(tmp_path, images=[], containers=[], ids=["sha256:gone"]) + assert result.returncode == 0 -- 2.54.1 From abefafe070f8ca05819d70ebeb1482bb29d3d0f7 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Wed, 2 Dec 2026 17:27:03 +0100 Subject: [PATCH 2/2] fix: record the replaced images before the database step, for the whole project The record was taken right before the engine's swap. Since then the database step and the broker replace their containers earlier, so a new PostgreSQL or Valkey image left the old one on the host for good; docker image prune -af used to take it. It also listed only the engine services, so postgres (behind its compose profile), apt-cacher-ng, signal-api and the retired registry were never recorded. deploy.sh now records the images of every container of the compose project netork, by its label, before the database step, and keeps only image IDs: they are expanded into the command that removes them. They are still removed only after the verification and the migration; a failed deploy removes nothing. remove_replaced_images.sh says nothing about an image still in use, which most of them now are on every deploy, and names only what it removed or could not remove. --- README.md | 16 +++-- deploy.sh | 30 ++++---- remote/remove_replaced_images.sh | 15 ++-- tests/test_deploy_replaced_images.py | 89 ++++++++++++++++++++---- tests/test_deploy_script_fails_loudly.py | 6 +- tests/test_remote_replaced_images.py | 9 ++- 6 files changed, 121 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 8fffac0..9933791 100644 --- a/README.md +++ b/README.md @@ -136,12 +136,16 @@ started earlier pulls whatever image the registry held before, which is stale. 11. Verifies that the containers run exactly the image that was pulled. If they don't, the deploy fails. 12. Runs `alembic upgrade head` inside `netork-api`. -13. Removes the images the swap replaced, and only those. Before the swap it records which - images netOrk's own containers use; afterwards `remote/remove_replaced_images.sh` - removes them unless a container still uses one, a redeploy of the same tag for - example. Other projects' images on the host stay. This used to be - `docker image prune -af`, which took every unused image, theirs included - (NetOrk/kb-server#157). +13. Removes the images this deploy replaced, and only those. Before step 5, the first + that can replace a container, it records the images of every container of the + compose project `netork`: the engine, the database, the broker, the infrastructure + containers and any retired service. After the migration, + `remote/remove_replaced_images.sh` removes them, unless a container still uses one: + a redeploy of the same tag, or a database whose image did not change. It names only + what it removed. Other projects' images on + the host stay. A deploy that failed earlier removes nothing, so the previous images + remain for the way back. This used to be `docker image prune -af`, which took every + unused image, theirs included (NetOrk/kb-server#157). A failing step stops that server's deploy with a non-zero exit, and the other servers carry on. The script exits non-zero if any server failed and names those servers. diff --git a/deploy.sh b/deploy.sh index 8c3e8fb..bde6443 100644 --- a/deploy.sh +++ b/deploy.sh @@ -326,6 +326,19 @@ deploy_steps() { REGISTRY_HOST='${REGISTRY_HOST}' NETORK_VERSION='${SERVER_VERSION}' \ docker compose -f docker-compose.yml -f docker-compose.registry.yml pull ${SERVICES}" + # The images netOrk's containers use now, before the first of them can be + # replaced: the database step and the broker go before the engine's swap. Once + # the deploy went through, these are what it replaced, and the only images it + # removes (at the end). Every container of the compose project, by its label: + # `compose ps` would miss postgres, which sits behind a profile, and a retired + # service that is in no compose file any more. Only image IDs are kept, since + # they are expanded into the command that removes them. A record that fails + # leaves nothing to remove, and the deploy goes on. + local REPLACED + REPLACED=$(ssh -n "$SERVER" "docker ps -aq --filter label=com.docker.compose.project=netork \ + | xargs -r docker inspect --format '{{.Image}}'" 2>/dev/null \ + | grep -E '^sha256:[0-9a-f]{64}$' | sort -u || true) + # The bundled database, before .env, broker and engine (remote/database.sh). A # changed image on the same volume (a minor release) is recreated; a new # volume (a major release) moves only with --infra-db, and without it the @@ -445,14 +458,6 @@ REMOTE_ENV # netOrk was down until someone ran the deploy again, which then went through # (NetOrk/netork#586). The second attempt is that rerun. Should it fail as well, # the container states go to the log and the deploy fails as before. - # The images netOrk's own containers use now, before the swap: after it, these - # are what the deploy replaced, and the only images it removes (below). - local REPLACED - REPLACED=$(ssh -n "$SERVER" "cd ~/netork && \ - REGISTRY_HOST='${REGISTRY_HOST}' NETORK_VERSION='${SERVER_VERSION}' \ - docker compose -f docker-compose.yml -f docker-compose.registry.yml ps -aq ${SERVICES} \ - | xargs -r docker inspect --format '{{.Image}}' | sort -u" 2>/dev/null || true) - echo "[${SERVER}] Starting containers..." local RECREATE="cd ~/netork && \ REGISTRY_HOST='${REGISTRY_HOST}' NETORK_VERSION='${SERVER_VERSION}' \ @@ -551,10 +556,11 @@ REMOTE_ENV run_remote "$SERVER" "alembic upgrade head (containers are ALREADY swapped)" "" \ "cd ~/netork && docker compose exec -T netork-api alembic upgrade head" - # The images the swap replaced, and nothing else. This was `docker image prune - # -af`, which removes every image no container uses at that moment: on a host - # that runs other things too, their rollback images (NetOrk/kb-server#157). - # remote/remove_replaced_images.sh keeps any image a container still uses. + # The images this deploy replaced, and nothing else. This was `docker image + # prune -af`, which removes every image no container uses at that moment: on a + # host that runs other things too, their rollback images (NetOrk/kb-server#157). + # remote/remove_replaced_images.sh keeps any image a container still uses. Not + # earlier: until the deploy went through, the old images are the way back. if [[ -n "$REPLACED" ]]; then echo "[${SERVER}] Removing the images this deploy replaced..." local ids="${REPLACED//$'\n'/ }" diff --git a/remote/remove_replaced_images.sh b/remote/remove_replaced_images.sh index ae059a1..fe041eb 100644 --- a/remote/remove_replaced_images.sh +++ b/remote/remove_replaced_images.sh @@ -1,21 +1,22 @@ #!/bin/bash # remove_replaced_images.sh ...: the images a deploy replaced, and -# nothing else. deploy.sh records the IDs netOrk's own containers use before the -# swap and runs this on the host after it. +# nothing else. deploy.sh records the IDs of every container of the compose +# project netork before the first of them can be replaced, and runs this on the +# host once the swap is verified and the migration ran. # # It used to be `docker image prune -af`, which removes every image no container # uses at that moment. On a host that runs other things too, that took their # rollback images (NetOrk/kb-server#157). An ID goes here only if no container -- -# netOrk's or anyone else's -- uses it: a redeploy of the same tag replaces -# nothing. It goes by every tag it carries, or by its ID once a moving tag such as -# latest-dev has left it without one. +# netOrk's or anyone else's -- uses it: a redeploy of the same tag, or a database +# whose image did not change, replaces nothing and keeps it without a word. It +# goes by every tag it carries, or by its ID once a moving tag such as latest-dev +# has left it without one. Only what was removed, or could not be, is reported. set -u in_use=$(docker ps -aq | xargs -r docker inspect --format '{{.Image}}' 2>/dev/null | sort -u) for id in "$@"; do if printf '%s\n' "$in_use" | grep -qx -- "$id"; then - echo "kept ${id:7:12} (in use)" continue fi tags=$(docker image inspect --format '{{range .RepoTags}}{{.}} {{end}}' "$id" 2>/dev/null) || continue @@ -23,7 +24,7 @@ for id in "$@"; do if docker image rm -- ${tags:-$id} >/dev/null 2>&1; then echo "removed ${tags:-$id}" else - echo "kept ${id:7:12}" + echo "kept ${id:7:12}: docker would not remove it" fi done exit 0 diff --git a/tests/test_deploy_replaced_images.py b/tests/test_deploy_replaced_images.py index 2f0c8f3..a1ec326 100644 --- a/tests/test_deploy_replaced_images.py +++ b/tests/test_deploy_replaced_images.py @@ -1,9 +1,15 @@ """A deploy removes the images it replaced, and nothing else (NetOrk/kb-server#157). It ended with `docker image prune -af`: every image no container used at that -moment, on a host that may run other things too. Now it records the image IDs of -netOrk's own containers before the swap and, once the swap is verified and the -migration ran, hands them to remote/remove_replaced_images.sh. +moment, on a host that may run other things too. Now it records the images of +netOrk's containers before the first of them can be replaced, and, once the swap +is verified and the migration ran, hands them to remote/remove_replaced_images.sh. + +"netOrk's containers" are every container of the compose project `netork`: the +engine, and also the bundled database (behind its compose profile), the broker, +the infrastructure containers and a retired service such as the old registry. +The database step and the broker step replace their containers before the +engine's swap, so the record is taken before the database step. The remote side is a fake `ssh` that records every command and plays a host whose netOrk containers run one image. @@ -17,6 +23,8 @@ from pathlib import Path DEPLOY = Path(__file__).resolve().parent.parent / "deploy.sh" +OLD = "sha256:" + "0" * 64 + _FAKE_SSH = """#!/bin/sh for arg in "$@"; do cmd="$arg"; done case "$cmd" in *.deploy.lock*) exec sh -c "$cmd" ;; esac @@ -25,16 +33,25 @@ case "$cmd" in *) cat > /dev/null ;; esac printf '%s\\n----\\n' "$cmd" >> "{state}/commands" -case "$cmd" in *"xargs -r docker inspect"*) echo sha256:old ;; esac +case "$cmd" in + *"xargs -r docker inspect"*) printf '{recorded}' ;; + *"alembic upgrade head"*) exit {migrate_exit} ;; +esac exit 0 """ -def _deploy(tmp_path: Path) -> tuple[subprocess.CompletedProcess[str], list[str]]: +def _deploy( + tmp_path: Path, *, recorded: str = f"{OLD}\n", migrate_exit: int = 0 +) -> tuple[subprocess.CompletedProcess[str], list[str]]: bin_dir = tmp_path / "bin" bin_dir.mkdir() fake = bin_dir / "ssh" - fake.write_text(_FAKE_SSH.replace("{state}", str(tmp_path))) + fake.write_text( + _FAKE_SSH.replace("{state}", str(tmp_path)) + .replace("{recorded}", recorded.replace("\n", "\\n")) + .replace("{migrate_exit}", str(migrate_exit)) + ) fake.chmod(0o755) result = subprocess.run( ["bash", str(DEPLOY), "testhost"], @@ -59,22 +76,68 @@ def _index(commands: list[str], needle: str) -> int: return next(i for i, cmd in enumerate(commands) if needle in cmd) +def _removals(commands: list[str]) -> list[str]: + return [cmd for cmd in commands if "bash -s -- sha256:" in cmd] + + def test_no_prune_of_everything(tmp_path: Path) -> None: result, commands = _deploy(tmp_path) assert result.returncode == 0, result.stderr - assert not [cmd for cmd in commands if "image prune -af" in cmd] + assert not [cmd for cmd in commands if "image prune" in cmd] -def test_the_replaced_images_are_recorded_before_the_swap_and_removed_after( +def test_the_replaced_images_are_recorded_before_any_container_is_replaced( tmp_path: Path, ) -> None: result, commands = _deploy(tmp_path) assert result.returncode == 0, result.stderr recorded = _index(commands, "xargs -r docker inspect") - swap = _index(commands, "--force-recreate") - migrate = _index(commands, "alembic upgrade head") - removal = _index(commands, "sha256:old") - assert recorded < swap < migrate < removal - assert "bash -s" in commands[removal] + assert recorded > _index(commands, "pull netork-api") + assert recorded < _index(commands, "bash -s -- reconcile") # the database step + assert recorded < _index(commands, "--wait-timeout 120 redis") # the broker + assert recorded < _index(commands, "--force-recreate") # the engine + + +def test_the_record_covers_every_container_of_the_project(tmp_path: Path) -> None: + """Not only the engine services: postgres sits behind a compose profile, and the + retired registry is in no compose file any more, so `compose ps` misses both.""" + _, commands = _deploy(tmp_path) + record = commands[_index(commands, "xargs -r docker inspect")] + assert "docker ps -aq --filter label=com.docker.compose.project=netork" in record + assert "compose" not in record.replace("com.docker.compose.project", "") + + +def test_they_are_removed_after_the_migration_through_the_script(tmp_path: Path) -> None: + result, commands = _deploy(tmp_path) + assert result.returncode == 0, result.stderr + (removal,) = _removals(commands) + assert removal.strip() == f"bash -s -- {OLD}" + verified = _index(commands, "docker image inspect --format '{{.Id}}'") + assert verified < _index(commands, "alembic upgrade head") < commands.index(removal) sent = [p.read_text() for p in tmp_path.glob("stdin-*")] assert any("remove_replaced_images.sh" in text for text in sent), result.stdout + + +def test_a_failed_deploy_removes_nothing(tmp_path: Path) -> None: + """The old images are the way back until the deploy went through.""" + result, commands = _deploy(tmp_path, migrate_exit=1) + assert result.returncode != 0 + assert not _removals(commands) + + +def test_nothing_recorded_means_no_removal(tmp_path: Path) -> None: + """A first deploy: no containers yet, nothing replaced.""" + result, commands = _deploy(tmp_path, recorded="") + assert result.returncode == 0, result.stderr + assert not _removals(commands) + assert "Removing the images" not in result.stdout + + +def test_only_image_ids_reach_the_remote_command_line(tmp_path: Path) -> None: + """The IDs are expanded into the ssh command; anything else the host printed + (a login script's greeting, say) is not passed on as an image to remove.""" + new = "sha256:" + "a" * 64 + result, commands = _deploy(tmp_path, recorded=f"Welcome; rm -rf ~\npostgres\n{OLD}\n{new}\n") + assert result.returncode == 0, result.stderr + (removal,) = _removals(commands) + assert removal.strip() == f"bash -s -- {OLD} {new}" diff --git a/tests/test_deploy_script_fails_loudly.py b/tests/test_deploy_script_fails_loudly.py index d727108..1049ecd 100644 --- a/tests/test_deploy_script_fails_loudly.py +++ b/tests/test_deploy_script_fails_loudly.py @@ -17,9 +17,9 @@ answered 500 on every request touching the changed table for eleven minutes. So: tail and grep locally, on captured output, where `pipefail` and `set -e` can still act. The rule below is deliberately narrow — it covers only the steps whose -failure must stop a deploy. `docker image prune | tail -1` is cosmetic and stays -exempt, because a rule that forbids every pipe would be worked around rather than -followed. +failure must stop a deploy. Removing the images a deploy replaced is cosmetic and +stays exempt, because a rule that forbids every pipe would be worked around rather +than followed. """ from __future__ import annotations diff --git a/tests/test_remote_replaced_images.py b/tests/test_remote_replaced_images.py index eb0136c..36db839 100644 --- a/tests/test_remote_replaced_images.py +++ b/tests/test_remote_replaced_images.py @@ -81,8 +81,10 @@ def test_the_replaced_release_goes_by_every_tag(tmp_path: Path) -> None: assert "removed" in result.stdout -def test_an_image_still_in_use_stays(tmp_path: Path) -> None: - # A redeploy of the same tag: the "old" image is the one running now. +def test_an_image_still_in_use_stays_and_says_nothing(tmp_path: Path) -> None: + # A redeploy of the same tag: the "old" image is the one running now. The + # database and the broker are recorded on every deploy and are mostly + # unchanged; a line for each on every deploy would bury what was removed. result, left = _run( tmp_path, images=["sha256:same r/netork/engine:latest-dev r/netork/engine:main-bbb"], @@ -91,7 +93,8 @@ def test_an_image_still_in_use_stays(tmp_path: Path) -> None: ) assert result.returncode == 0 assert left == ["sha256:same", "r/netork/engine:latest-dev", "r/netork/engine:main-bbb"] - assert "kept" in result.stdout + assert result.stdout == "" + assert "image rm" not in (tmp_path / "calls").read_text() def test_another_projects_images_are_never_named(tmp_path: Path) -> None: -- 2.54.1