authoravatarclover caruso<git@paperclover.net>2026-10-16 23:13:17-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-21 02:52:29-07:00
log1ec773200ff768847f5e58ac0cc1b1e2f355668b
tree54fd43c4e110762be3f1639003797d684a312337
parent16755e176f00faeed64d1cf82053129722dd69f7
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Refresh the runtime hostname after a host activation

Assisted-by: gpt-6.1-sol

4 files changed, 39 insertions(+), 7 deletions(-)

tools/dashboard-deploy-test.py+5-1
......@@ -162,6 +162,7 @@ class DeploymentBoundary(unittest.TestCase):
162162
163163 def test_first_activation_exposes_current_before_starting_host_units(self):
164164 (self.root / "current").unlink()
165 read_text = Path.read_text
165166
166167 def execute(argv, **kwargs):
167168 if argv[:2] == ["nixos-rebuild", "switch"]:
......@@ -169,7 +170,10 @@ class DeploymentBoundary(unittest.TestCase):
169170 output = 'job "fixture" {\n' if argv[-1] == "render" else '"leader"'
170171 return subprocess.CompletedProcess(argv, 0, stdout=output, stderr="")
171172
172 with patch.object(release.subprocess, "run", side_effect=execute), patch.object(release, "host_configuration", return_value="snowglobe"), patch.object(release.transition, "activate"):
173 with patch.object(release.subprocess, "run", side_effect=execute), patch.object(release, "host_configuration", return_value="snowglobe"), patch.object(release.transition, "activate"), patch.object(
174 Path, "read_text", autospec=True,
175 side_effect=lambda path: os.uname().nodename + "\n" if path == Path("/etc/hostname") else read_text(path),
176 ):
173177 self.assertIsNone(release.activate(self.version, initial=True))
174178 self.assertEqual(release.current_release(), self.version)
175179
tools/namespace-transition-test.py+5-1
......@@ -127,6 +127,7 @@ class NamespaceTransition(unittest.TestCase):
127127 (source / "nixos/dashboard.nix").touch()
128128 transition.layout(source)["manifest"].write_text('{"version":2}')
129129 observed = []
130 read_text = Path.read_text
130131
131132 def run(argv, **kwargs):
132133 observed.append((argv, kwargs))
......@@ -140,7 +141,10 @@ class NamespaceTransition(unittest.TestCase):
140141 release, "host_configuration", return_value=transition.NAMESPACE["previousHost"],
141142 ), patch.object(release, "jobs", return_value=set()), patch.object(
142143 release.os, "uname", return_value=SimpleNamespace(nodename="snowglobe"),
143 ), patch.object(release.subprocess, "run", side_effect=run), patch.object(transition, "activate") as activate:
144 ), patch.object(release.subprocess, "run", side_effect=run), patch.object(transition, "activate") as activate, patch.object(
145 Path, "read_text", autospec=True,
146 side_effect=lambda path: "snowglobe\n" if path == Path("/etc/hostname") else read_text(path),
147 ):
144148 release.activate(revision, initial=True)
145149 activate.assert_called_once_with(source)
146150 scripts = [(argv, kwargs) for argv, kwargs in observed if argv[0] == "python3"]
tools/release-host-test.py+26-5
......@@ -54,6 +54,7 @@ class ActivationEnvironment(unittest.TestCase):
5454 canonical.symlink_to(previous, target_is_directory=True)
5555 logical_source = canonical / "releases" / source.name
5656 observed = []
57 read_text = Path.read_text
5758
5859 def activate(path):
5960 self.assertEqual(path, logical_source)
......@@ -75,6 +76,9 @@ class ActivationEnvironment(unittest.TestCase):
7576 release.os, "uname", return_value=SimpleNamespace(nodename="snowglobe"),
7677 ), patch.object(release.subprocess, "run", side_effect=run), patch.object(
7778 release.transition, "activate", side_effect=activate,
79 ), patch.object(
80 Path, "read_text", autospec=True,
81 side_effect=lambda path: "snowglobe\n" if path == Path("/etc/hostname") else read_text(path),
7882 ):
7983 release.activate(source.name, initial=True)
8084 self.assertIn(f"path:{source}#nixosConfigurations", observed[0])
......@@ -94,17 +98,22 @@ class ActivationEnvironment(unittest.TestCase):
9498 source = root / "release"
9599 source.mkdir()
96100 (source / "tools").mkdir()
97 (source / "tools/snowglobe.py").touch()
98 (source / ".snowglobe-release.json").write_text('{"version":1}')
101 prefix = release.transition.PREVIOUS if after == PREVIOUS_HOST else "snowglobe"
102 (source / "tools" / (prefix + ".py")).touch()
103 (source / ("." + prefix + "-release.json")).write_text('{"version":1}')
99104 hostname = before
100105 observed = []
106 calls = []
107 read_text = Path.read_text
101108
102109 def run(argv, **kwargs):
103110 nonlocal hostname
104 if argv[:2] == ["nixos-rebuild", "switch"]:
111 calls.append(argv)
112 if argv[:3] == ["hostnamectl", "set-hostname", "--transient"]:
105113 hostname = after
106114 if argv[-1] == "deploy":
107 observed.append(os.environ.get("SNOWGLOBE_NODE_NAME"))
115 key = prefix.upper() + "_NODE_NAME"
116 observed.append(kwargs["env"].get(key))
108117 return subprocess.CompletedProcess(argv, 0, '"leader"')
109118
110119 with patch.dict(os.environ), patch.object(release, "ROOT", root), patch.object(
......@@ -120,9 +129,21 @@ class ActivationEnvironment(unittest.TestCase):
120129 os.environ.pop("SNOWGLOBE_NODE_NAME", None)
121130 else:
122131 os.environ["SNOWGLOBE_NODE_NAME"] = inherited
123 with patch.object(release.transition, "activate"):
132 with patch.object(release.transition, "activate"), patch.object(
133 Path, "read_text", autospec=True,
134 side_effect=lambda path: after + "\n" if path == Path("/etc/hostname") else read_text(path),
135 ):
124136 release.activate("0123456789abcdef", initial=True)
125137 self.assertEqual(observed, [expected])
138 self.assertEqual(hostname, after)
139 changes = [argv for argv in calls if argv[0] == "hostnamectl"]
140 self.assertEqual(changes, [["hostnamectl", "set-hostname", "--transient", after]] if before != after else [])
141 if changes:
142 switch = next(i for i, argv in enumerate(calls) if argv[:2] == ["nixos-rebuild", "switch"])
143 rename = calls.index(changes[0])
144 deploy = next(i for i, argv in enumerate(calls) if argv[-1] == "deploy")
145 self.assertLess(switch, rename)
146 self.assertLess(rename, deploy)
126147
127148
128149if __name__ == "__main__":
tools/release.py+3
......@@ -180,6 +180,9 @@ def activate(release, legacy=False, initial=False):
180180 hostname = os.uname().nodename.split(".", 1)[0]
181181 transition.activate(path)
182182 subprocess.run(["nixos-rebuild", "switch", "--flake", f"path:{path.resolve()}#{configuration}"], check=True)
183 configured_hostname = Path("/etc/hostname").read_text().strip()
184 if os.uname().nodename != configured_hostname:
185 subprocess.run(["hostnamectl", "set-hostname", "--transient", configured_hostname], check=True)
183186 if os.environ.get("SNOWGLOBE_NODE_NAME", hostname) == hostname:
184187 os.environ["SNOWGLOBE_NODE_NAME"] = os.uname().nodename.split(".", 1)[0]
185188 environment = transition.environment(path)