CI / check (pull_request) Successful in 19s
On .50 a deploy refused by the database step had already copied the refused version's compose files and written its NETORK_VERSION to .env: a hand-typed `docker compose up -d` would have carried postgres back to the old volume. The database step now runs before .env is written, the previous compose files are kept as .prev while the new ones are fetched, and a refusal (exit 3) puts them back. Checked on .50: checksums of both compose files and .env unchanged after a refused deploy.
112 lines
4.1 KiB
Python
112 lines
4.1 KiB
Python
"""deploy.sh runs remote/database.sh on every host (NetOrk/netork#1308).
|
|
|
|
A normal deploy reconciles the bundled database (`reconcile`); only --infra-db
|
|
lets it move to a new volume (`move`). It runs after .env is written and before
|
|
the broker and the engine, and a refusal (exit 3: compose wants a new volume)
|
|
stops the deploy there, with the old engine still running.
|
|
|
|
The remote side is a fake `ssh` that records every command and what it was fed.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
DEPLOY = Path(__file__).resolve().parent.parent / "deploy.sh"
|
|
DATABASE = Path(__file__).resolve().parent.parent / "remote" / "database.sh"
|
|
|
|
_FAKE_SSH = """#!/bin/sh
|
|
for arg in "$@"; do cmd="$arg"; done
|
|
case "$cmd" in *.deploy.lock*) exec sh -c "$cmd" ;; esac
|
|
printf '%s\\n----\\n' "$cmd" >> "{state}/commands"
|
|
case "$cmd" in
|
|
*"bash -s -- "*) cat > "{state}/database-stdin"; exit {db_exit} ;;
|
|
esac
|
|
cat > /dev/null
|
|
exit 0
|
|
"""
|
|
|
|
|
|
def _deploy(tmp_path: Path, *args: str, db_exit: int = 0):
|
|
bin_dir = tmp_path / "bin"
|
|
bin_dir.mkdir()
|
|
fake = bin_dir / "ssh"
|
|
fake.write_text(_FAKE_SSH.replace("{state}", str(tmp_path)).replace("{db_exit}", str(db_exit)))
|
|
fake.chmod(0o755)
|
|
result = subprocess.run(
|
|
["bash", str(DEPLOY), *args, "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, c in enumerate(commands) if needle in c)
|
|
|
|
|
|
def test_a_normal_deploy_reconciles_the_database_first(tmp_path: Path) -> None:
|
|
result, commands = _deploy(tmp_path)
|
|
|
|
assert result.returncode == 0, result.stderr
|
|
db = _index(commands, "bash -s -- reconcile netork-api")
|
|
assert "NETORK_VERSION='latest-dev'" in commands[db]
|
|
assert db < _index(commands, "--wait-timeout 120 redis") < _index(commands, "--force-recreate")
|
|
assert (tmp_path / "database-stdin").read_text() == DATABASE.read_text()
|
|
|
|
|
|
def test_infra_db_lets_it_move(tmp_path: Path) -> None:
|
|
result, commands = _deploy(tmp_path, "--infra-db")
|
|
|
|
assert result.returncode == 0, result.stderr
|
|
assert any("bash -s -- move netork-api" in c for c in commands)
|
|
assert not any("bash -s -- reconcile" in c for c in commands)
|
|
|
|
|
|
def test_a_refusal_stops_the_deploy_before_the_engine(tmp_path: Path) -> None:
|
|
result, commands = _deploy(tmp_path, db_exit=3)
|
|
|
|
assert result.returncode != 0
|
|
assert not any("--force-recreate" in c for c in commands)
|
|
assert not any("redis" in c and "up -d" in c for c in commands)
|
|
|
|
|
|
def test_the_database_is_checked_before_env_names_the_new_version(tmp_path: Path) -> None:
|
|
"""A refused deploy must leave .env as it was: the check runs first."""
|
|
_, commands = _deploy(tmp_path)
|
|
|
|
env_write = next(i for i, c in enumerate(commands) if c.strip() == "bash -s")
|
|
assert _index(commands, "bash -s -- reconcile") < env_write
|
|
|
|
|
|
def test_the_previous_compose_files_are_kept_while_the_new_ones_are_fetched(tmp_path: Path) -> None:
|
|
_, commands = _deploy(tmp_path)
|
|
|
|
fetch = commands[_index(commands, "/app/deploy/")]
|
|
assert 'cp "$f" "$f.prev"' in fetch
|
|
|
|
|
|
def test_a_refusal_puts_the_previous_compose_files_back(tmp_path: Path) -> None:
|
|
"""Refused (exit 3) means nothing changed; the host's compose files must not
|
|
name the refused version either, or a hand-typed `docker compose up -d`
|
|
would carry the database to the other volume (seen on .50, 2026-10-21)."""
|
|
result, commands = _deploy(tmp_path, db_exit=3)
|
|
|
|
assert result.returncode != 0
|
|
restore = [c for c in commands if 'mv "$f.prev" "$f"' in c]
|
|
assert restore, "the previous compose files are not put back"
|
|
assert _index(commands, 'mv "$f.prev" "$f"') > _index(commands, "bash -s -- reconcile")
|