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):...@@ -162,6 +162,7 @@ class DeploymentBoundary(unittest.TestCase):
162162
163 def test_first_activation_exposes_current_before_starting_host_units(self):163 def test_first_activation_exposes_current_before_starting_host_units(self):
164 (self.root / "current").unlink()164 (self.root / "current").unlink()
165 read_text = Path.read_text
165166
166 def execute(argv, **kwargs):167 def execute(argv, **kwargs):
167 if argv[:2] == ["nixos-rebuild", "switch"]:168 if argv[:2] == ["nixos-rebuild", "switch"]:
...@@ -169,7 +170,10 @@ class DeploymentBoundary(unittest.TestCase):...@@ -169,7 +170,10 @@ class DeploymentBoundary(unittest.TestCase):
169 output = 'job "fixture" {\n' if argv[-1] == "render" else '"leader"'170 output = 'job "fixture" {\n' if argv[-1] == "render" else '"leader"'
170 return subprocess.CompletedProcess(argv, 0, stdout=output, stderr="")171 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 ):
173 self.assertIsNone(release.activate(self.version, initial=True))177 self.assertIsNone(release.activate(self.version, initial=True))
174 self.assertEqual(release.current_release(), self.version)178 self.assertEqual(release.current_release(), self.version)
175179
tools/namespace-transition-test.py+5-1
...@@ -127,6 +127,7 @@ class NamespaceTransition(unittest.TestCase):...@@ -127,6 +127,7 @@ class NamespaceTransition(unittest.TestCase):
127 (source / "nixos/dashboard.nix").touch()127 (source / "nixos/dashboard.nix").touch()
128 transition.layout(source)["manifest"].write_text('{"version":2}')128 transition.layout(source)["manifest"].write_text('{"version":2}')
129 observed = []129 observed = []
130 read_text = Path.read_text
130131
131 def run(argv, **kwargs):132 def run(argv, **kwargs):
132 observed.append((argv, kwargs))133 observed.append((argv, kwargs))
...@@ -140,7 +141,10 @@ class NamespaceTransition(unittest.TestCase):...@@ -140,7 +141,10 @@ class NamespaceTransition(unittest.TestCase):
140 release, "host_configuration", return_value=transition.NAMESPACE["previousHost"],141 release, "host_configuration", return_value=transition.NAMESPACE["previousHost"],
141 ), patch.object(release, "jobs", return_value=set()), patch.object(142 ), patch.object(release, "jobs", return_value=set()), patch.object(
142 release.os, "uname", return_value=SimpleNamespace(nodename="snowglobe"),143 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 ):
144 release.activate(revision, initial=True)148 release.activate(revision, initial=True)
145 activate.assert_called_once_with(source)149 activate.assert_called_once_with(source)
146 scripts = [(argv, kwargs) for argv, kwargs in observed if argv[0] == "python3"]150 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):...@@ -54,6 +54,7 @@ class ActivationEnvironment(unittest.TestCase):
54 canonical.symlink_to(previous, target_is_directory=True)54 canonical.symlink_to(previous, target_is_directory=True)
55 logical_source = canonical / "releases" / source.name55 logical_source = canonical / "releases" / source.name
56 observed = []56 observed = []
57 read_text = Path.read_text
5758
58 def activate(path):59 def activate(path):
59 self.assertEqual(path, logical_source)60 self.assertEqual(path, logical_source)
...@@ -75,6 +76,9 @@ class ActivationEnvironment(unittest.TestCase):...@@ -75,6 +76,9 @@ class ActivationEnvironment(unittest.TestCase):
75 release.os, "uname", return_value=SimpleNamespace(nodename="snowglobe"),76 release.os, "uname", return_value=SimpleNamespace(nodename="snowglobe"),
76 ), patch.object(release.subprocess, "run", side_effect=run), patch.object(77 ), patch.object(release.subprocess, "run", side_effect=run), patch.object(
77 release.transition, "activate", side_effect=activate,78 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),
78 ):82 ):
79 release.activate(source.name, initial=True)83 release.activate(source.name, initial=True)
80 self.assertIn(f"path:{source}#nixosConfigurations", observed[0])84 self.assertIn(f"path:{source}#nixosConfigurations", observed[0])
...@@ -94,17 +98,22 @@ class ActivationEnvironment(unittest.TestCase):...@@ -94,17 +98,22 @@ class ActivationEnvironment(unittest.TestCase):
94 source = root / "release"98 source = root / "release"
95 source.mkdir()99 source.mkdir()
96 (source / "tools").mkdir()100 (source / "tools").mkdir()
97 (source / "tools/snowglobe.py").touch()101 prefix = release.transition.PREVIOUS if after == PREVIOUS_HOST else "snowglobe"
98 (source / ".snowglobe-release.json").write_text('{"version":1}')102 (source / "tools" / (prefix + ".py")).touch()
103 (source / ("." + prefix + "-release.json")).write_text('{"version":1}')
99 hostname = before104 hostname = before
100 observed = []105 observed = []
106 calls = []
107 read_text = Path.read_text
101108
102 def run(argv, **kwargs):109 def run(argv, **kwargs):
103 nonlocal hostname110 nonlocal hostname
104 if argv[:2] == ["nixos-rebuild", "switch"]:111 calls.append(argv)
112 if argv[:3] == ["hostnamectl", "set-hostname", "--transient"]:
105 hostname = after113 hostname = after
106 if argv[-1] == "deploy":114 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))
108 return subprocess.CompletedProcess(argv, 0, '"leader"')117 return subprocess.CompletedProcess(argv, 0, '"leader"')
109118
110 with patch.dict(os.environ), patch.object(release, "ROOT", root), patch.object(119 with patch.dict(os.environ), patch.object(release, "ROOT", root), patch.object(
...@@ -120,9 +129,21 @@ class ActivationEnvironment(unittest.TestCase):...@@ -120,9 +129,21 @@ class ActivationEnvironment(unittest.TestCase):
120 os.environ.pop("SNOWGLOBE_NODE_NAME", None)129 os.environ.pop("SNOWGLOBE_NODE_NAME", None)
121 else:130 else:
122 os.environ["SNOWGLOBE_NODE_NAME"] = inherited131 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 ):
124 release.activate("0123456789abcdef", initial=True)136 release.activate("0123456789abcdef", initial=True)
125 self.assertEqual(observed, [expected])137 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
128if __name__ == "__main__":149if __name__ == "__main__":
tools/release.py+3
...@@ -180,6 +180,9 @@ def activate(release, legacy=False, initial=False):...@@ -180,6 +180,9 @@ def activate(release, legacy=False, initial=False):
180 hostname = os.uname().nodename.split(".", 1)[0]180 hostname = os.uname().nodename.split(".", 1)[0]
181 transition.activate(path)181 transition.activate(path)
182 subprocess.run(["nixos-rebuild", "switch", "--flake", f"path:{path.resolve()}#{configuration}"], check=True)182 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)
183 if os.environ.get("SNOWGLOBE_NODE_NAME", hostname) == hostname:186 if os.environ.get("SNOWGLOBE_NODE_NAME", hostname) == hostname:
184 os.environ["SNOWGLOBE_NODE_NAME"] = os.uname().nodename.split(".", 1)[0]187 os.environ["SNOWGLOBE_NODE_NAME"] = os.uname().nodename.split(".", 1)[0]
185 environment = transition.environment(path)188 environment = transition.environment(path)