From 1ec773200ff768847f5e58ac0cc1b1e2f355668b Mon Sep 17 00:00:00 2001 From: clover caruso Date: Fri, 16 Oct 2026 23:13:17 -0700 Subject: [PATCH] Refresh the runtime hostname after a host activation Assisted-by: gpt-6.1-sol --- tools/dashboard-deploy-test.py | 6 +++++- tools/namespace-transition-test.py | 6 +++++- tools/release-host-test.py | 31 +++++++++++++++++++++++++----- tools/release.py | 3 +++ 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/tools/dashboard-deploy-test.py b/tools/dashboard-deploy-test.py index bd710ce71a75ab5d15cdc140abdf67846ad7bf3c..b8a3ce989880357bd1f6072049bc6c342403eae5 100644 --- a/tools/dashboard-deploy-test.py +++ b/tools/dashboard-deploy-test.py @@ -162,6 +162,7 @@ class DeploymentBoundary(unittest.TestCase): def test_first_activation_exposes_current_before_starting_host_units(self): (self.root / "current").unlink() + read_text = Path.read_text def execute(argv, **kwargs): if argv[:2] == ["nixos-rebuild", "switch"]: @@ -169,7 +170,10 @@ class DeploymentBoundary(unittest.TestCase): output = 'job "fixture" {\n' if argv[-1] == "render" else '"leader"' return subprocess.CompletedProcess(argv, 0, stdout=output, stderr="") - with patch.object(release.subprocess, "run", side_effect=execute), patch.object(release, "host_configuration", return_value="snowglobe"), patch.object(release.transition, "activate"): + with patch.object(release.subprocess, "run", side_effect=execute), patch.object(release, "host_configuration", return_value="snowglobe"), patch.object(release.transition, "activate"), patch.object( + Path, "read_text", autospec=True, + side_effect=lambda path: os.uname().nodename + "\n" if path == Path("/etc/hostname") else read_text(path), + ): self.assertIsNone(release.activate(self.version, initial=True)) self.assertEqual(release.current_release(), self.version) diff --git a/tools/namespace-transition-test.py b/tools/namespace-transition-test.py index 474a24be211d9d7415ed36c0cd9d4fb7d6001347..f2a1d04277c9c8ee86b2436bee46f4a9ab39e482 100644 --- a/tools/namespace-transition-test.py +++ b/tools/namespace-transition-test.py @@ -127,6 +127,7 @@ class NamespaceTransition(unittest.TestCase): (source / "nixos/dashboard.nix").touch() transition.layout(source)["manifest"].write_text('{"version":2}') observed = [] + read_text = Path.read_text def run(argv, **kwargs): observed.append((argv, kwargs)) @@ -140,7 +141,10 @@ class NamespaceTransition(unittest.TestCase): release, "host_configuration", return_value=transition.NAMESPACE["previousHost"], ), patch.object(release, "jobs", return_value=set()), patch.object( release.os, "uname", return_value=SimpleNamespace(nodename="snowglobe"), - ), patch.object(release.subprocess, "run", side_effect=run), patch.object(transition, "activate") as activate: + ), patch.object(release.subprocess, "run", side_effect=run), patch.object(transition, "activate") as activate, patch.object( + Path, "read_text", autospec=True, + side_effect=lambda path: "snowglobe\n" if path == Path("/etc/hostname") else read_text(path), + ): release.activate(revision, initial=True) activate.assert_called_once_with(source) scripts = [(argv, kwargs) for argv, kwargs in observed if argv[0] == "python3"] diff --git a/tools/release-host-test.py b/tools/release-host-test.py index 76b33462ff1de2d6abd38c94c32a98200c9fb72c..b3286702232eb99e69d549cc8d5994efa77c2223 100644 --- a/tools/release-host-test.py +++ b/tools/release-host-test.py @@ -54,6 +54,7 @@ class ActivationEnvironment(unittest.TestCase): canonical.symlink_to(previous, target_is_directory=True) logical_source = canonical / "releases" / source.name observed = [] + read_text = Path.read_text def activate(path): self.assertEqual(path, logical_source) @@ -75,6 +76,9 @@ class ActivationEnvironment(unittest.TestCase): release.os, "uname", return_value=SimpleNamespace(nodename="snowglobe"), ), patch.object(release.subprocess, "run", side_effect=run), patch.object( release.transition, "activate", side_effect=activate, + ), patch.object( + Path, "read_text", autospec=True, + side_effect=lambda path: "snowglobe\n" if path == Path("/etc/hostname") else read_text(path), ): release.activate(source.name, initial=True) self.assertIn(f"path:{source}#nixosConfigurations", observed[0]) @@ -94,17 +98,22 @@ class ActivationEnvironment(unittest.TestCase): source = root / "release" source.mkdir() (source / "tools").mkdir() - (source / "tools/snowglobe.py").touch() - (source / ".snowglobe-release.json").write_text('{"version":1}') + prefix = release.transition.PREVIOUS if after == PREVIOUS_HOST else "snowglobe" + (source / "tools" / (prefix + ".py")).touch() + (source / ("." + prefix + "-release.json")).write_text('{"version":1}') hostname = before observed = [] + calls = [] + read_text = Path.read_text def run(argv, **kwargs): nonlocal hostname - if argv[:2] == ["nixos-rebuild", "switch"]: + calls.append(argv) + if argv[:3] == ["hostnamectl", "set-hostname", "--transient"]: hostname = after if argv[-1] == "deploy": - observed.append(os.environ.get("SNOWGLOBE_NODE_NAME")) + key = prefix.upper() + "_NODE_NAME" + observed.append(kwargs["env"].get(key)) return subprocess.CompletedProcess(argv, 0, '"leader"') with patch.dict(os.environ), patch.object(release, "ROOT", root), patch.object( @@ -120,9 +129,21 @@ class ActivationEnvironment(unittest.TestCase): os.environ.pop("SNOWGLOBE_NODE_NAME", None) else: os.environ["SNOWGLOBE_NODE_NAME"] = inherited - with patch.object(release.transition, "activate"): + with patch.object(release.transition, "activate"), patch.object( + Path, "read_text", autospec=True, + side_effect=lambda path: after + "\n" if path == Path("/etc/hostname") else read_text(path), + ): release.activate("0123456789abcdef", initial=True) self.assertEqual(observed, [expected]) + self.assertEqual(hostname, after) + changes = [argv for argv in calls if argv[0] == "hostnamectl"] + self.assertEqual(changes, [["hostnamectl", "set-hostname", "--transient", after]] if before != after else []) + if changes: + switch = next(i for i, argv in enumerate(calls) if argv[:2] == ["nixos-rebuild", "switch"]) + rename = calls.index(changes[0]) + deploy = next(i for i, argv in enumerate(calls) if argv[-1] == "deploy") + self.assertLess(switch, rename) + self.assertLess(rename, deploy) if __name__ == "__main__": diff --git a/tools/release.py b/tools/release.py index 222d528253f0a7837a2b24183e356640f8cb2fbb..2a4745b9055f6623b592244cff7f03969b14db66 100644 --- a/tools/release.py +++ b/tools/release.py @@ -180,6 +180,9 @@ def activate(release, legacy=False, initial=False): hostname = os.uname().nodename.split(".", 1)[0] transition.activate(path) subprocess.run(["nixos-rebuild", "switch", "--flake", f"path:{path.resolve()}#{configuration}"], check=True) + configured_hostname = Path("/etc/hostname").read_text().strip() + if os.uname().nodename != configured_hostname: + subprocess.run(["hostnamectl", "set-hostname", "--transient", configured_hostname], check=True) if os.environ.get("SNOWGLOBE_NODE_NAME", hostname) == hostname: os.environ["SNOWGLOBE_NODE_NAME"] = os.uname().nodename.split(".", 1)[0] environment = transition.environment(path) -- 2.54.1