diff --git a/lib/default.nix b/lib/default.nix
index 3f4295b9f..9f33bb3d1 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -1316,6 +1316,15 @@ let
python3 ${../pkgs/deploy/test_deploy_slice2.py} ${../pkgs/deploy/deploy.py}
mkdir -p $out
'';
+ deploy-slice3-tests =
+ pkgs.runCommandLocal "check-deploy-slice3-tests"
+ {
+ nativeBuildInputs = [ pkgs.python3 ];
+ }
+ ''
+ python3 ${../pkgs/deploy/test_deploy_slice3.py} ${../pkgs/deploy/deploy.py}
+ mkdir -p $out
+ '';
deploy-argv =
pkgs.runCommandLocal "check-deploy-argv"
{
diff --git a/pkgs/deploy/default.nix b/pkgs/deploy/default.nix
index 0c3c468b7..915677345 100644
--- a/pkgs/deploy/default.nix
+++ b/pkgs/deploy/default.nix
@@ -1,5 +1,6 @@
-# The deploy registry + CLI — issue #5766, slices 1-2 (registry + list;
-# unit/plist rendering, --dry-run plans, generation log, status/rollback logic).
+# The deploy registry + CLI — issue #5766, slices 1-3 (registry + list;
+# unit/plist rendering, --dry-run plans, generation log, status/rollback logic;
+# real systemd activation with health-check flip-back).
#
# deployTargets: this repo's service definitions (services.nix) in the
# design §3 shape, with each definition's `package` still a derivation —
@@ -44,7 +45,7 @@ in
{
nativeBuildInputs = [ makeWrapper ];
meta = {
- description = "Deploy CLI (slices 1-2: registry list + validation, unit rendering, dry-run plans, generation log, status/rollback logic) for the services deployment layer";
+ description = "Deploy CLI (slices 1-3: registry list + validation, unit rendering, dry-run plans, generation log, status/rollback logic, real systemd activation) for the services deployment layer";
mainProgram = "deploy";
};
}
diff --git a/pkgs/deploy/deploy.py b/pkgs/deploy/deploy.py
index 3b9d2956a..3bb57a93d 100644
--- a/pkgs/deploy/deploy.py
+++ b/pkgs/deploy/deploy.py
@@ -1,12 +1,12 @@
#!/usr/bin/env python3
-"""deploy — the deployment-architecture CLI (issue #5766, slices 1-2).
+"""deploy — the deployment-architecture CLI (issue #5766, slices 1-3).
Slice 1 ships the registry foundation: enumerates the
registry (name, class, targets) after validating every
definition against the design §3 option shape
(doc/2026-11-04_deployment-architecture-services-layer.md).
-Slice 2 adds the command logic that does not itself activate anything:
+Slice 2 added the command logic that does not itself activate anything:
- systemd unit rendering (and a launchd plist stub) from a definition:
the unit's ExecStart points at the service's per-service nix profile
@@ -21,10 +21,17 @@
timestamp, actor). folds the log into
current/previous generations and reports health; selects
the previous distinct generation — repointing, never rebuilding.
-- Real activation (nix-env profile install, systemctl restart,
- health-check flip-back) is the next slice of issue #5766: the
- non-dry-run deploy/rollback paths say so and exit non-zero rather
- than pretending to have activated something.
+- Slice 3 adds real activation for the systemd class: installing a
+ generation into the per-service profile (a deploy-managed profile
+ link whose generation directories hold a `current` symlink at the
+ artifact; store-path artifacts are additionally registered with
+ `nix-env --set` on a parallel nix profile), writing the unit,
+ daemon-reload, restarting only that unit, running the health
+ check, and flipping the profile back to the previous generation
+ (and restarting on it) on any failure. Rollback is the same
+ machinery pointed at the previous distinct generation. The
+ launchd real path still refuses explicitly: it cannot be exercised
+ from the Linux fleet and stays a rendering/planning stub.
The registry is JSON baked into the package at build time (the Nix
expression renders the attrset definitions, replacing each
@@ -33,7 +40,11 @@
exercise the malformed-definition path. `--state-dir PATH` (or
$DEPLOY_STATE_DIR) relocates the generation logs, `--profile-base PATH`
(or $DEPLOY_PROFILE_BASE) the per-service profile root; both exist so
-tests and dry runs never touch the real profile tree.
+tests and dry runs never touch the real profile tree. Slice 3 adds
+`--unit-dir PATH` (or $DEPLOY_UNIT_DIR) for the rendered unit's home
+and `--scope system|user` (or $DEPLOY_SCOPE, default system) selecting
+the systemctl scope; user scope exists so the laptop E2E acceptance
+can deploy a harmless service without root.
The validation below is the oracle for "well-formed definition": a
definition is valid iff it carries exactly the §3 fields (plus ),
@@ -47,7 +58,9 @@
import os
import re
import shlex
+import subprocess
import sys
+import time
import urllib.error
import urllib.request
from datetime import datetime, timezone
@@ -560,12 +573,253 @@ def describe(entry):
return "\n".join(lines)
+
+# --- Real activation (slice 3) -------------------------------------------
+#
+# Oracle: design §4.1 plan order, as printed by plan_deploy /
+# plan_rollback: install the generation into the profile, write the
+# unit, daemon-reload, restart only that unit, run the health check,
+# flip back on any failure. The profile is deploy-managed: the
+# profile link / points at a generation directory
+# /.generations/// whose `current` symlink names the
+# artifact, so the rendered unit's /current/ path
+# resolves and a flip is one atomic symlink repoint — the same shape
+# nix-env gives a profile, kept deploy-owned because the unit path
+# goes through `current`. Store-path artifacts are additionally
+# registered with nix-env --set on a parallel profile
+# (/.nix-env-profile) so the generation also exists in
+# nix's own profile bookkeeping. Every external effect goes through
+# run_cmd / write_text / probe_health, which the slice-3 tests
+# replace with fakes: the order asserted there is the plan order.
+
+
+class ActivationError(Exception):
+ """A step of the activation plan failed; the caller flips back."""
+
+
+def run_cmd(argv):
+ """Run one external command; return (returncode, stdout, stderr).
+ A missing binary is a failed step (127), never a crash."""
+ try:
+ proc = subprocess.run(argv, capture_output=True, text=True)
+ except OSError as exc:
+ return 127, "", f"{argv[0]}: {exc}"
+ return proc.returncode, proc.stdout, proc.stderr
+
+
+def write_text(path, content):
+ os.makedirs(os.path.dirname(path), exist_ok=True)
+ with open(path, "w", encoding="utf-8") as fh:
+ fh.write(content)
+
+
+def systemctl_argv(scope, *args):
+ if scope == "user":
+ return ["systemctl", "--user", *args]
+ return ["systemctl", *args]
+
+
+def unit_dir(explicit=None, scope="system"):
+ if explicit:
+ return explicit
+ env = os.environ.get("DEPLOY_UNIT_DIR")
+ if env:
+ return env
+ if scope == "user":
+ return os.path.expanduser("~/.config/systemd/user")
+ return "/etc/systemd/system"
+
+
+def _run_step(argv, what):
+ rc, _out, err = run_cmd(argv)
+ if rc != 0:
+ raise ActivationError(f"{what} failed (exit {rc}): {err.strip()}")
+
+
+def install_generation(name, artifact, generation, base=None):
+ """Point the profile link for `name` at a new generation dir
+ whose `current` symlink names `artifact`. Returns the link path."""
+ base_dir = profile_base(base)
+ gen_dir = os.path.join(base_dir, ".generations", name, str(generation))
+ os.makedirs(gen_dir, exist_ok=True)
+ current = os.path.join(gen_dir, "current")
+ if os.path.islink(current) or os.path.exists(current):
+ os.unlink(current)
+ os.symlink(artifact, current)
+ if artifact.startswith("/nix/store/") and _which("nix-env"):
+ _run_step(
+ ["nix-env", "--profile",
+ os.path.join(base_dir, f"{name}.nix-env-profile"),
+ "--set", artifact],
+ "nix-env profile install",
+ )
+ link = profile_link(name, base)
+ tmp = f"{link}.tmp-{os.getpid()}"
+ if os.path.islink(tmp) or os.path.exists(tmp):
+ os.unlink(tmp)
+ os.symlink(gen_dir, tmp)
+ os.replace(tmp, link)
+ return link
+
+
+def _which(tool):
+ for entry in os.environ.get("PATH", "").split(os.pathsep):
+ if os.path.exists(os.path.join(entry, tool)):
+ return True
+ return False
+
+
+def repoint_profile(name, artifact, generation, base=None):
+ """Flip the profile link back/forward to an existing artifact
+ (rollback and flip-back share this: a repoint, never a rebuild)."""
+ return install_generation(name, artifact, generation, base)
+
+
+def wait_health(url, attempts=10, interval=0.5):
+ """Probe the health check until it passes or attempts run out."""
+ detail = "not probed"
+ for attempt in range(attempts):
+ ok, detail = probe_health(url)
+ if ok:
+ return True, detail
+ if attempt + 1 < attempts:
+ time.sleep(interval)
+ return False, detail
+
+
+def _activate(name, defn, *, artifact, generation, base, state, scope,
+ units, action, actor):
+ """Shared tail of deploy/rollback: unit write, daemon-reload,
+ restart, health check. Raises ActivationError on any failure."""
+ if defn["class"] != "systemd":
+ raise ActivationError(
+ f"real activation for class {defn['class']!r} is not implemented "
+ "in this slice (systemd only; launchd remains a rendering stub)"
+ )
+ write_text(os.path.join(units, unit_name(name)),
+ render_unit(name, defn, base))
+ _run_step(systemctl_argv(scope, "daemon-reload"), "daemon-reload")
+ _run_step(systemctl_argv(scope, "restart", unit_name(name)),
+ f"restart of {unit_name(name)}")
+ if defn["healthCheck"] is not None:
+ ok, detail = wait_health(defn["healthCheck"])
+ if not ok:
+ raise ActivationError(f"health check failed: {detail}")
+
+
+def _flip_back(name, defn, *, previous, base, scope, units):
+ """Restore the previous generation after a failed activation and
+ restart on it; with no previous generation, stop the unit. Never
+ raises: a flip-back failure is an outcome to record, not a crash
+ that loses the failure entry (found by the slice-3 laptop E2E:
+ a broken unit restart-loops into systemd's start limit, so the
+ flip-back restart needs a reset-failed first)."""
+ if previous is None:
+ run_cmd(systemctl_argv(scope, "stop", unit_name(name)))
+ link = profile_link(name, base)
+ if os.path.islink(link):
+ os.unlink(link)
+ return "stopped (no previous generation)"
+ try:
+ repoint_profile(name, previous["artifact"], previous["generation"],
+ base)
+ run_cmd(systemctl_argv(scope, "reset-failed", unit_name(name)))
+ _run_step(systemctl_argv(scope, "restart", unit_name(name)),
+ f"restart of {unit_name(name)} on flip-back")
+ except ActivationError as exc:
+ return (f"flip-back to generation {previous['generation']} "
+ f"INCOMPLETE: {exc}")
+ return f"flipped back to generation {previous['generation']}"
+
+
+def execute_deploy(name, defn, entries, *, base=None, state=None,
+ scope="system", units=None, actor=None):
+ """Real (non-dry-run) deploy, in plan_deploy's order. Returns
+ (exit_code, lines). On failure the profile is flipped back to the
+ previously current generation and a non-folding failure entry is
+ appended to the generation log (action deploy_failed, so
+ current_previous never mistakes it for an activation)."""
+ actor = actor or getpass.getuser()
+ units = units if units is not None else unit_dir(scope=scope)
+ current, _prev = current_previous(entries)
+ generation = next_generation_id(entries)
+ artifact = defn["package"]
+ lines = []
+ try:
+ install_generation(name, artifact, generation, base)
+ lines.append(f"installed generation {generation} into the profile "
+ f"{profile_link(name, base)}")
+ _activate(name, defn, artifact=artifact, generation=generation,
+ base=base, state=state, scope=scope, units=units,
+ action="deploy", actor=actor)
+ except ActivationError as exc:
+ outcome = _flip_back(name, defn, previous=current, base=base,
+ scope=scope, units=units)
+ record_generation(name, artifact=artifact, source_rev=_source_rev(),
+ actor=actor, action="deploy_failed",
+ generation=generation,
+ result=f"failed: {exc}; {outcome}",
+ explicit_state=state)
+ lines.append(f"deploy {name}: FAILED: {exc}; {outcome}")
+ return 1, lines
+ entry = record_generation(name, artifact=artifact,
+ source_rev=_source_rev(), actor=actor,
+ action="deploy", generation=generation,
+ result="activated", explicit_state=state)
+ lines.append(f"deploy {name}: generation {entry['generation']} activated "
+ f"(artifact {artifact})")
+ return 0, lines
+
+
+def execute_rollback(name, defn, entries, *, base=None, state=None,
+ scope="system", units=None, actor=None):
+ """Real rollback: repoint at the previous distinct generation and
+ restart on it (design §4.1/§4.6 — never a rebuild). On health
+ failure, flip back to the generation that was current."""
+ actor = actor or getpass.getuser()
+ units = units if units is not None else unit_dir(scope=scope)
+ current, previous = current_previous(entries)
+ if current is None or previous is None:
+ _steps, error = plan_rollback(name, defn, entries, base)
+ return 1, [f"deploy: {error}"]
+ lines = []
+ try:
+ repoint_profile(name, previous["artifact"], previous["generation"],
+ base)
+ _activate(name, defn, artifact=previous["artifact"],
+ generation=previous["generation"], base=base, state=state,
+ scope=scope, units=units, action="rollback", actor=actor)
+ except ActivationError as exc:
+ outcome = _flip_back(name, defn, previous=current, base=base,
+ scope=scope, units=units)
+ record_generation(name, artifact=previous["artifact"],
+ source_rev=_source_rev(), actor=actor,
+ action="rollback_failed",
+ generation=previous["generation"],
+ result=f"failed: {exc}; {outcome}",
+ explicit_state=state)
+ lines.append(f"deploy {name} rollback: FAILED: {exc}; {outcome}")
+ return 1, lines
+ record_generation(name, artifact=previous["artifact"],
+ source_rev=_source_rev(), actor=actor,
+ action="rollback", generation=previous["generation"],
+ result="rolled-back", explicit_state=state)
+ lines.append(f"deploy {name}: rolled back to generation "
+ f"{previous['generation']} (artifact {previous['artifact']})")
+ return 0, lines
+
+
+def _source_rev():
+ return os.environ.get("DEPLOY_SOURCE_REV") or "working-tree"
+
+
+
# --- Command dispatch ------------------------------------------------------
NOT_IMPLEMENTED = (
- "real activation (nix profile install + service restart + health-check "
- "flip-back) is the next slice of issue #5766; slice 2 ships rendering, "
- "--dry-run plans, the generation log, and status/rollback logic — "
+ "real activation for this class is not implemented in this slice; "
+ "slice 3 implements real systemd activation (profile install + unit "
+ "write + daemon-reload + restart + health-check flip-back) — "
"no changes were made"
)
@@ -585,7 +839,8 @@ def main(argv):
flag = next(
(
f
- for f in ("--registry", "--state-dir", "--profile-base")
+ for f in ("--registry", "--state-dir", "--profile-base",
+ "--unit-dir", "--scope")
if arg == f or arg.startswith(f + "=")
),
None,
@@ -605,11 +860,19 @@ def main(argv):
registry_path = flag_values["--registry"]
explicit_state = flag_values.get("--state-dir")
explicit_base = flag_values.get("--profile-base")
+ explicit_units = flag_values.get("--unit-dir")
+ scope = flag_values.get("--scope") or os.environ.get("DEPLOY_SCOPE") \
+ or "system"
+ if scope not in ("system", "user"):
+ print(f"deploy: --scope must be system or user, got {scope!r}",
+ file=sys.stderr)
+ return 2
dry_run = "--dry-run" in args
args = [a for a in args if a != "--dry-run"]
usage = (
- "usage: deploy [--registry PATH] [--state-dir PATH] list | "
- " [rollback|status] [--dry-run]"
+ "usage: deploy [--registry PATH] [--state-dir PATH] "
+ "[--profile-base PATH] [--unit-dir PATH] [--scope system|user] "
+ "list | [rollback|status] [--dry-run]"
)
if not args:
print(__doc__.splitlines()[0], file=sys.stderr)
@@ -670,8 +933,16 @@ def main(argv):
)
return 2
if not dry_run:
- print(f"deploy: {NOT_IMPLEMENTED}", file=sys.stderr)
- return 2
+ if defn["class"] != "systemd":
+ print(f"deploy: {NOT_IMPLEMENTED}", file=sys.stderr)
+ return 2
+ code, lines = execute_deploy(
+ name, defn, entries, base=explicit_base,
+ state=explicit_state, scope=scope,
+ units=unit_dir(explicit_units, scope))
+ for line in lines:
+ print(line, file=sys.stderr if code else sys.stdout)
+ return code
print(f"deploy {name} --dry-run: no changes made")
for i, step in enumerate(steps, 1):
print(f" {i}. {step}")
@@ -682,8 +953,16 @@ def main(argv):
print(f"deploy: {error}", file=sys.stderr)
return 1
if not dry_run:
- print(f"deploy: {NOT_IMPLEMENTED}", file=sys.stderr)
- return 2
+ if defn["class"] != "systemd":
+ print(f"deploy: {NOT_IMPLEMENTED}", file=sys.stderr)
+ return 2
+ code, lines = execute_rollback(
+ name, defn, entries, base=explicit_base,
+ state=explicit_state, scope=scope,
+ units=unit_dir(explicit_units, scope))
+ for line in lines:
+ print(line, file=sys.stderr if code else sys.stdout)
+ return code
print(f"deploy {name} rollback --dry-run: no changes made")
for i, step in enumerate(steps, 1):
print(f" {i}. {step}")
diff --git a/pkgs/deploy/test_deploy_slice2.py b/pkgs/deploy/test_deploy_slice2.py
index fcd247b1b..d93c5fe28 100644
--- a/pkgs/deploy/test_deploy_slice2.py
+++ b/pkgs/deploy/test_deploy_slice2.py
@@ -240,12 +240,28 @@ def test_dry_run_prints_plan_and_creates_no_state(self):
self.assertIn("install generation 1 into the profile", proc.stdout)
self.assertFalse(os.path.exists(os.path.join(state, "generations")))
- def test_real_deploy_is_refused_in_this_slice(self):
+ def test_real_deploy_attempts_activation_and_records_failure(self):
+ # Slice 3 replaced the refusal with real activation: with no
+ # reachable systemctl (PATH emptied) the deploy must fail
+ # non-zero, flip back, and append a deploy_failed entry —
+ # never pretend success and never crash.
with tempfile.TemporaryDirectory() as state:
- proc = self.run_cli(state, "svc")
- self.assertEqual(proc.returncode, 2)
- self.assertIn("next slice of issue #5766", proc.stderr)
- self.assertFalse(os.path.exists(os.path.join(state, "generations")))
+ registry = {"svc": DEFN}
+ reg_path = os.path.join(state, "registry.json")
+ with open(reg_path, "w", encoding="utf-8") as fh:
+ json.dump(registry, fh)
+ env = dict(os.environ, DEPLOY_STATE_DIR=state,
+ DEPLOY_PROFILE_BASE=os.path.join(state, "profiles"),
+ DEPLOY_UNIT_DIR=os.path.join(state, "units"),
+ PATH=state)
+ proc = subprocess.run(
+ [sys.executable, DEPLOY_PATH, "--registry", reg_path, "svc"],
+ capture_output=True, text=True, env=env, timeout=30)
+ self.assertEqual(proc.returncode, 1, proc.stderr)
+ self.assertIn("FAILED", proc.stderr)
+ entries = deploy.read_generations("svc", state)
+ self.assertEqual(len(entries), 1)
+ self.assertEqual(entries[0]["action"], "deploy_failed")
def test_cli_rollback_dry_run_refused_with_one_generation(self):
with tempfile.TemporaryDirectory() as state:
diff --git a/pkgs/deploy/test_deploy_slice3.py b/pkgs/deploy/test_deploy_slice3.py
new file mode 100644
index 000000000..9583da29c
--- /dev/null
+++ b/pkgs/deploy/test_deploy_slice3.py
@@ -0,0 +1,229 @@
+#!/usr/bin/env python3
+"""Coverage for the deploy slice-3 real activation path (issue #5766).
+
+Oracle: design doc/2026-11-04_deployment-architecture-services-layer.md
+§4.1 plan order (install generation into the profile, write the unit,
+daemon-reload, restart only that unit, health check, flip back on any
+failure; append to the generation log) and §4.6 (rollback repoints at
+the previous distinct generation, never rebuilds).
+
+All external effects are faked: run_cmd records its argv instead of
+touching systemctl/nix-env, probe_health is scripted per test. The
+filesystem effects (profile link, generation dirs, unit file,
+generation log) are real, in temp dirs.
+
+Usage: test_deploy_slice3.py [path-to-deploy.py]
+"""
+
+import importlib.util
+import os
+import sys
+import tempfile
+import unittest
+
+DEPLOY_PATH = sys.argv[1] if len(sys.argv) > 1 else os.path.join(
+ os.path.dirname(os.path.abspath(__file__)), "deploy.py")
+spec = importlib.util.spec_from_file_location("deploy", DEPLOY_PATH)
+deploy = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(deploy)
+
+DEFN = {
+ "package": "/placeholder/artifact",
+ "entryPoint": "bin/svc",
+ "args": [],
+ "environment": {},
+ "secrets": [],
+ "user": "user",
+ "ports": [8080],
+ "restart": "on-failure",
+ "healthCheck": "http://127.0.0.1:8080/health",
+ "targets": ["amd"],
+ "class": "systemd",
+}
+
+
+class FakeEffects:
+ def __init__(self, health_ok=True):
+ self.commands = []
+ self.health_ok = health_ok
+ self._real_run_cmd = deploy.run_cmd
+ self._real_probe = deploy.probe_health
+
+ def __enter__(self):
+ deploy.run_cmd = self._run
+ deploy.probe_health = self._probe
+ return self
+
+ def __exit__(self, *exc):
+ deploy.run_cmd = self._real_run_cmd
+ deploy.probe_health = self._real_probe
+
+ def _run(self, argv):
+ self.commands.append(list(argv))
+ return 0, "", ""
+
+ def _probe(self, url, timeout=2.0):
+ return (True, "HTTP 200") if self.health_ok else (False, "HTTP 500")
+
+ def systemctl_steps(self):
+ return [c[2] if c[1] == "--user" else c[1] for c in self.commands
+ if c[0] == "systemctl"]
+
+
+class ActivationTests(unittest.TestCase):
+ def setUp(self):
+ self.tmp = tempfile.TemporaryDirectory()
+ root = self.tmp.name
+ self.base = os.path.join(root, "profiles")
+ self.state = os.path.join(root, "state")
+ self.units = os.path.join(root, "units")
+ self.art1 = os.path.join(root, "artifact1")
+ self.art2 = os.path.join(root, "artifact2")
+ for art in (self.art1, self.art2):
+ os.makedirs(os.path.join(art, "bin"))
+
+ def tearDown(self):
+ self.tmp.cleanup()
+
+ def defn(self, artifact):
+ return dict(DEFN, package=artifact)
+
+ def entries(self):
+ return deploy.read_generations("svc", self.state)
+
+ def current_artifact(self):
+ link = deploy.profile_link("svc", self.base)
+ return os.path.realpath(os.path.join(link, "current"))
+
+ def do_deploy(self, artifact, fx):
+ return deploy.execute_deploy(
+ "svc", self.defn(artifact), self.entries(), base=self.base,
+ state=self.state, scope="user", units=self.units, actor="test")
+
+ def test_deploy_success_follows_plan_order(self):
+ with FakeEffects() as fx:
+ code, lines = self.do_deploy(self.art1, fx)
+ self.assertEqual(code, 0, lines)
+ # profile install happened: link/current resolves to artifact
+ self.assertEqual(self.current_artifact(), os.path.realpath(self.art1))
+ # unit written, pointing through the profile current link
+ with open(os.path.join(self.units, "svc.service")) as fh:
+ unit = fh.read()
+ self.assertIn("current/bin/svc", unit)
+ # daemon-reload strictly before the single-unit restart
+ steps = fx.systemctl_steps()
+ self.assertEqual(steps, ["daemon-reload", "restart"])
+ restart_argv = [c for c in fx.commands if "restart" in c][0]
+ self.assertIn("svc.service", restart_argv)
+ self.assertNotIn("other.service", restart_argv)
+ # generation log folded: gen 1 current, no previous
+ current, previous = deploy.current_previous(self.entries())
+ self.assertEqual(current["generation"], 1)
+ self.assertIsNone(previous)
+
+ def test_store_path_artifact_registers_with_nix_env_first(self):
+ store_art = "/nix/store/abc123-svc-1.0"
+ with FakeEffects() as fx:
+ code, _lines = self.do_deploy(store_art, fx)
+ self.assertEqual(code, 0)
+ nix = [c for c in fx.commands if c[0] == "nix-env"]
+ self.assertEqual(len(nix), 1)
+ self.assertIn("--set", nix[0])
+ self.assertIn(store_art, nix[0])
+ # nix registration precedes daemon-reload in the plan order
+ self.assertLess(fx.commands.index(nix[0]),
+ min(i for i, c in enumerate(fx.commands)
+ if c[0] == "systemctl"))
+
+ def test_broken_generation_flips_back(self):
+ with FakeEffects() as fx:
+ self.assertEqual(self.do_deploy(self.art1, fx)[0], 0)
+ self.assertEqual(self.do_deploy(self.art2, fx)[0], 0)
+ # third deploy: same defn (artifact2 path) but health now fails
+ with FakeEffects(health_ok=False) as fx:
+ code, lines = self.do_deploy(self.art2, fx)
+ self.assertEqual(code, 1)
+ self.assertIn("flipped back to generation 2", lines[-1])
+ # profile is back on generation 2's artifact, and the fold
+ # still reports gen 2 current (the failure entry must not fold)
+ self.assertEqual(self.current_artifact(), os.path.realpath(self.art2))
+ current, _prev = deploy.current_previous(self.entries())
+ self.assertEqual(current["generation"], 2)
+ self.assertEqual(self.entries()[-1]["action"], "deploy_failed")
+ # flip-back restarted the unit again after the failed restart
+ self.assertEqual(fx.systemctl_steps().count("restart"), 2)
+
+ def test_first_deploy_failure_stops_unit(self):
+ with FakeEffects(health_ok=False) as fx:
+ code, lines = self.do_deploy(self.art1, fx)
+ self.assertEqual(code, 1)
+ self.assertIn("stopped (no previous generation)", lines[-1])
+ self.assertIn("stop", fx.systemctl_steps())
+ current, _prev = deploy.current_previous(self.entries())
+ self.assertIsNone(current)
+
+ def test_flip_back_restart_failure_is_recorded_not_raised(self):
+ # Regression (slice-3 laptop E2E): a broken unit restart-loops
+ # into systemd's start limit and the flip-back restart itself
+ # fails. The deploy must still exit 1 and append deploy_failed
+ # — an unrecorded failure is the one outcome not allowed.
+ with FakeEffects() as fx:
+ self.assertEqual(self.do_deploy(self.art1, fx)[0], 0)
+ restarts = {"n": 0}
+
+ def flaky(argv):
+ fx.commands.append(list(argv))
+ if "restart" in argv:
+ restarts["n"] += 1
+ if restarts["n"] >= 2: # the flip-back restart
+ return 1, "", "start limit hit"
+ return 0, "", ""
+
+ real_run = deploy.run_cmd
+ real_probe = deploy.probe_health
+ deploy.run_cmd = flaky
+ deploy.probe_health = lambda url, timeout=2.0: (False, "HTTP 500")
+ try:
+ code, lines = deploy.execute_deploy(
+ "svc", self.defn(self.art2), self.entries(), base=self.base,
+ state=self.state, scope="user", units=self.units,
+ actor="test")
+ finally:
+ deploy.run_cmd = real_run
+ deploy.probe_health = real_probe
+ self.assertEqual(code, 1)
+ self.assertIn("INCOMPLETE", lines[-1])
+ self.assertEqual(self.entries()[-1]["action"], "deploy_failed")
+ current, _prev = deploy.current_previous(self.entries())
+ self.assertEqual(current["generation"], 1)
+ # reset-failed is issued before the flip-back restart
+ flat = [step for c in fx.commands for step in c]
+ self.assertIn("reset-failed", flat)
+
+ def test_rollback_repoints_and_restarts(self):
+ with FakeEffects() as fx:
+ self.assertEqual(self.do_deploy(self.art1, fx)[0], 0)
+ self.assertEqual(self.do_deploy(self.art2, fx)[0], 0)
+ code, lines = deploy.execute_rollback(
+ "svc", self.defn(self.art2), self.entries(), base=self.base,
+ state=self.state, scope="user", units=self.units,
+ actor="test")
+ self.assertEqual(code, 0, lines)
+ self.assertEqual(self.current_artifact(), os.path.realpath(self.art1))
+ current, _prev = deploy.current_previous(self.entries())
+ self.assertEqual(current["generation"], 1)
+ self.assertEqual(self.entries()[-1]["action"], "rollback")
+
+ def test_rollback_refused_with_one_generation(self):
+ with FakeEffects() as fx:
+ self.assertEqual(self.do_deploy(self.art1, fx)[0], 0)
+ code, lines = deploy.execute_rollback(
+ "svc", self.defn(self.art1), self.entries(), base=self.base,
+ state=self.state, scope="user", units=self.units,
+ actor="test")
+ self.assertEqual(code, 1)
+ self.assertIn("only one generation", lines[0])
+
+
+if __name__ == "__main__":
+ unittest.main(argv=[sys.argv[0]])