authoravatarclover caruso<git@paperclover.net>2026-10-21 02:19:43-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-21 02:52:57-07:00
log792a5281167703c9ee7525e7016cb3faf1fc06b5
tree1d65bbfe07ec5ee53fbe90cb63831e381dbde43e
parent48149be04c0e2d6f0d219eb70e07ca7f45d824c9
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Finish the Snow Globe namespace migration

Remove legacy path, release, VM, telemetry and metadata compatibility after migrating retained telemetry to canonical names. Serve source status at /stats and /activity, rename Plausible configuration mounts, and preserve pre-rename releases as archives. Verified Rust, dashboard, deployment and host checks, fresh service previews, and a production telemetry cutover with complete sample and trace validation plus original-store archives. Assisted-by: gpt-6.1-sol

26 files changed, 110 insertions(+), 856 deletions(-)

dashboard/src/apps.rs+1-8
......@@ -136,14 +136,7 @@ async fn paper(app: Arc<App>, path: &str, body: Option<Value>) -> Result<Value>
136136 let key = key
137137 .as_str()
138138 .ok_or_else(|| Error::new(502, "Paper Clover is unavailable."))?;
139 let url = format!(
140 "https://{host}/{}",
141 if path == "scan" {
142 path.into()
143 } else {
144 format!("{}/{path}", namespace::PREVIOUS.as_str())
145 }
146 );
139 let url = format!("https://{host}/{path}");
147140 let request = if let Some(body) = body {
148141 app.http.post(&url).json(&body)
149142 } else {
dashboard/src/core.rs+2-4
......@@ -5,12 +5,10 @@ use std::path::Path;
55
66pub async fn nomad(app: &App, path: &str) -> Result<Value> {
77 let value = nomad_raw(app, path).await?;
8 let mut value = value
8 Ok(value
99 .map(|bytes| serde_json::from_slice(&bytes))
1010 .transpose()?
11 .unwrap_or(Value::Null);
12 namespace::nomad(&mut value);
13 Ok(value)
11 .unwrap_or(Value::Null))
1412}
1513pub async fn nomad_raw(app: &App, path: &str) -> Result<Option<Bytes>> {
1614 let request = async {
dashboard/src/main.rs-1
......@@ -11,7 +11,6 @@ mod guest;
1111mod host;
1212mod index;
1313mod mcp;
14mod namespace;
1514mod observability;
1615mod oidc;
1716mod relay;
dashboard/src/namespace-transition.json deleted-4
......@@ -1,4 +0,0 @@
1{
2 "previous": "studio",
3 "previousHost": "zenith"
4}
dashboard/src/namespace.rs deleted-87
......@@ -1,87 +0,0 @@
1use crate::*;
2use serde_json::Map;
3
4pub static PREVIOUS: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
5 serde_json::from_str::<Value>(include_str!("namespace-transition.json")).unwrap()["previous"]
6 .as_str().unwrap().to_owned()
7});
8
9fn keys(values: &mut Map<String, Value>, separator: &str) {
10 let prefix = format!("{}{separator}", PREVIOUS.as_str());
11 let renamed: Vec<_> = values.keys().filter_map(|key| {
12 key.strip_prefix(&prefix).map(|suffix| (key.clone(), format!("snowglobe{separator}{suffix}")))
13 }).collect();
14 for (old, new) in renamed {
15 let value = values.remove(&old).unwrap();
16 values.entry(new).or_insert(value);
17 }
18}
19
20pub fn nomad(value: &mut Value) {
21 match value {
22 Value::Object(values) => {
23 if let Some(meta) = values.get_mut("Meta").and_then(Value::as_object_mut) {
24 keys(meta, "_");
25 }
26 if let Some(tags) = values.get_mut("Tags").and_then(Value::as_array_mut) {
27 let prefix = format!("{}-metrics-path=", PREVIOUS.as_str());
28 for tag in tags {
29 if let Some(path) = tag.as_str().and_then(|tag| tag.strip_prefix(&prefix)) {
30 *tag = json!(format!("snowglobe-metrics-path={path}"));
31 }
32 }
33 }
34 for value in values.values_mut() {
35 nomad(value);
36 }
37 }
38 Value::Array(values) => values.iter_mut().for_each(nomad),
39 _ => {}
40 }
41}
42
43pub fn attributes(values: &mut Map<String, Value>) {
44 keys(values, ".");
45}
46
47pub fn metric_selector(metric: &str, labels: &str) -> String {
48 let previous = metric.replacen("snowglobe_", &format!("{}_", PREVIOUS.as_str()), 1);
49 format!("sum without (__name__) ({metric}{labels}) or sum without (__name__) ({previous}{labels})")
50}
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn persisted_metadata_is_read_once_with_current_values_winning() {
58 let old = PREVIOUS.as_str();
59 let mut value = json!({"Meta": {
60 format!("{old}_name"): "old name", "snowglobe_name": "Snow Globe",
61 format!("{old}_hostname"): "knife.example.test"
62 }, "TaskGroups": [{"Services": [{"Tags": [format!("{old}-metrics-path=/metrics"), "caddy-host=knife.example.test"]}]}],
63 "Config": {"Env": {format!("{}_DOMAIN", old.to_uppercase()): "untouched"}}});
64 nomad(&mut value);
65 assert_eq!(value["Meta"]["snowglobe_name"], "Snow Globe");
66 assert_eq!(value["Meta"]["snowglobe_hostname"], "knife.example.test");
67 assert!(value["Meta"].get(format!("{old}_name")).is_none());
68 assert_eq!(value["TaskGroups"][0]["Services"][0]["Tags"][0], "snowglobe-metrics-path=/metrics");
69 assert_eq!(value["Config"]["Env"][format!("{}_DOMAIN", old.to_uppercase())], "untouched");
70 let normalized = value.clone();
71 nomad(&mut value);
72 assert_eq!(value, normalized);
73 }
74
75 #[test]
76 fn historical_trace_ownership_remains_explicit() {
77 let mut values = Map::from_iter([
78 (format!("{}.service", PREVIOUS.as_str()), json!("private")),
79 (format!("{}.kind", PREVIOUS.as_str()), json!("edge")),
80 ("snowglobe.service".into(), json!("current")),
81 ]);
82 attributes(&mut values);
83 assert_eq!(values["snowglobe.service"], "current");
84 assert_eq!(values["snowglobe.kind"], "edge");
85 assert_eq!(values.len(), 2);
86 }
87}
dashboard/src/telemetry.rs+3-4
......@@ -189,7 +189,7 @@ pub async fn metrics(
189189 quote(id)
190190 ))
191191 .unwrap_or_default();
192 namespace::metric_selector(metric, &labels)
192 format!("{metric}{labels}")
193193 };
194194 let key = if window.is_some() {
195195 format!("series:{selector}:fixed:{from}:{to}")
......@@ -228,8 +228,8 @@ pub async fn metric_names(app: Arc<App>, id: &str) -> Result<Value> {
228228 (
229229 "match[]",
230230 format!(
231 "{{{label}={},__name__!~\"(snowglobe|{})_.*\"}}",
232 serde_json::to_string(id).unwrap(), namespace::PREVIOUS.as_str()
231 "{{{label}={},__name__!~\"snowglobe_.*\"}}",
232 serde_json::to_string(id).unwrap()
233233 ),
234234 )
235235 }));
......@@ -467,7 +467,6 @@ fn span(row: &Value) -> Value {
467467 .map(|key| (key.into(), value.clone()))
468468 })
469469 .collect();
470 namespace::attributes(&mut attributes);
471470 for (from, to) in [
472471 ("http.request.method", "http.request.method"),
473472 ("http.response.status_code", "http.response.status_code"),
nixos/configuration.nix+1-1
......@@ -6,7 +6,7 @@ let
66 proxyToken = "/var/lib/snowglobe/dashboard-proxy.token";
77 hostTools = lib.fileset.toSource {
88 root = ../.;
9 fileset = lib.fileset.unions [ ../tools/dashboard-host.py ../service/copyparty/manage.py ../service/copyparty/copyparty.conf ../tools/shale-labels.py ../tools/shale-access.py ../config/shale.json ../config/site.pkl ../dashboard/src/host-protocol.json ../tools/vms.py ../tools/mac-vms.py ../tools/vm-ssh.py ../tools/dashboard-run.py ../tools/release.py ../tools/namespace-transition.py ../dashboard/src/namespace-transition.json ../guest/protocol.json ];
9 fileset = lib.fileset.unions [ ../tools/dashboard-host.py ../service/copyparty/manage.py ../service/copyparty/copyparty.conf ../tools/shale-labels.py ../tools/shale-access.py ../config/shale.json ../config/site.pkl ../dashboard/src/host-protocol.json ../tools/vms.py ../tools/mac-vms.py ../tools/vm-ssh.py ../tools/dashboard-run.py ../tools/release.py ../guest/protocol.json ];
1010 };
1111 nativePkl = pkgs.callPackage ./pkl.nix { };
1212 hostPython = pkgs.callPackage ./host-python.nix { };
service/clover-source-of-truth/build-source.json+1-1
......@@ -1,6 +1,6 @@
11{
22 "source": "../sitegen",
3 "revision": "13f64d71038f5e72ab34dc1f55805e2fee05f368",
3 "revision": "4964d828660c6603c009e55b41e06c56b49c4bcf",
44 "dockerfile": "src/source-of-truth.dockerfile",
55 "include": [
66 "package.json",
service/plausible/service.pkl+2-2
......@@ -58,8 +58,8 @@ containers {
5858 }
5959 volumes {
6060 ["/var/lib/clickhouse"] {}
61 ["/etc/clickhouse-server/config.d/studio.xml"] { config = "clickhouse.xml" }
62 ["/etc/clickhouse-server/users.d/studio.xml"] { config = "clickhouse-users.xml" }
61 ["/etc/clickhouse-server/config.d/snowglobe.xml"] { config = "clickhouse.xml" }
62 ["/etc/clickhouse-server/users.d/snowglobe.xml"] { config = "clickhouse-users.xml" }
6363 }
6464 env {
6565 ["CLICKHOUSE_SKIP_USER_SETUP"] = "1"
tools/dashboard-deploy-test.py+1-1
......@@ -170,7 +170,7 @@ class DeploymentBoundary(unittest.TestCase):
170170 output = 'job "fixture" {\n' if argv[-1] == "render" else '"leader"'
171171 return subprocess.CompletedProcess(argv, 0, stdout=output, stderr="")
172172
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(
173 with patch.object(release.subprocess, "run", side_effect=execute), patch.object(release, "host_configuration", return_value="snowglobe"), patch.object(
174174 Path, "read_text", autospec=True,
175175 side_effect=lambda path: os.uname().nodename + "\n" if path == Path("/etc/hostname") else read_text(path),
176176 ):
tools/dashboard-logs-test.py+1-1
......@@ -111,7 +111,7 @@ def main():
111111 port = reservation.getsockname()[1]
112112 origin = f'http://127.0.0.1:{port}'
113113 environment = {**os.environ, 'PORT': str(port), 'SNOWGLOBE_DATA_DIR': str(root / 'data'),
114 'SNOWGLOBE_PUBLIC_ORIGIN': 'https://globe.studio.test', 'SNOWGLOBE_NOMAD_TOKEN_FILE': str(root / 'token'),
114 'SNOWGLOBE_PUBLIC_ORIGIN': 'https://globe.snowglobe.test', 'SNOWGLOBE_NOMAD_TOKEN_FILE': str(root / 'token'),
115115 'NOMAD_ADDR': f'http://127.0.0.1:{fixture.server_port}'}
116116 environment['SNOWGLOBE_PREVIEW'] = '1' if args.preview else '0'
117117 for name in ['SNOWGLOBE_PROXY_TOKEN_FILE', 'SNOWGLOBE_INTERNAL_URL', 'SNOWGLOBE_AUTH_REQUIRED', 'SNOWGLOBE_INDEX_POOL', 'SNOWGLOBE_YT_STATE']:
tools/dashboard-run.py+4-4
......@@ -160,11 +160,11 @@ def command(action, target, key=None):
160160 version = release.current_release()
161161 if version is None:
162162 raise Error(409, "No release is running. Deploy a release before starting this service.")
163 return [sys.executable, str(release.transition.layout(release.check_release(version))["script"]), "deploy", target]
163 return [sys.executable, str(release.check_release(version) / "tools/snowglobe.py"), "deploy", target]
164164 if action == "rollback":
165165 saved = entry(target)
166166 version = saved["release"]
167 release.check_release(version, legacy=saved.get("legacy") is True)
167 release.check_release(version)
168168 return [sys.executable, str(Path(__file__).with_name("release.py")), "rollback", version]
169169 if action == "deploy":
170170 candidate = release.main_release()
......@@ -177,7 +177,7 @@ def command(action, target, key=None):
177177 if not isinstance(version, str):
178178 raise Error(409, "This stage has no release. Stage it again.")
179179 root = release.check_release(version)
180 return [sys.executable, str(release.transition.layout(root)["script"]), "destroy", target]
180 return [sys.executable, str(root / "tools/snowglobe.py"), "destroy", target]
181181
182182
183183def last():
......@@ -372,7 +372,7 @@ def worker(identity, action, target, key=None):
372372 try:
373373 argv = command(action, target, key)
374374 output.write(("$ " + " ".join(argv) + "\n").encode())
375 environment = release.transition.environment(Path(argv[1]).parent.parent) if len(argv) > 1 and argv[1].endswith(".py") else None
375 environment = None
376376 if action in {"stop", "restart"}:
377377 environment = {**os.environ, "NOMAD_TOKEN": read(release.STATE / "nomad.token", missing="").strip()}
378378 if not environment["NOMAD_TOKEN"]:
tools/dashboard-stage-test.py+3-1
......@@ -98,6 +98,8 @@ class PreviewProof(unittest.TestCase):
9898 output = ""
9999 if args[0] == "pkl":
100100 output = json.dumps(site)
101 elif args[:3] == ("systemctl", "show", "snowglobe-host"):
102 output = "PATH=/bin"
101103 elif args[:4] == ("nomad", "acl", "token", "create"):
102104 output = json.dumps({"AccessorID": "fixture-accessor", "SecretID": "fixture-nomad-token"})
103105 elif args[:2] == ("podman", "run"):
......@@ -113,7 +115,7 @@ class PreviewProof(unittest.TestCase):
113115 def exists(path):
114116 return str(path).startswith("/run/snowglobe-preview/") and path.name == "host.sock" or original_exists(path)
115117
116 with patch.object(stage.snowglobe, "STATE", state), patch.object(stage.snowglobe, "REPO", root / "releases/1234567890abcdef"), patch.object(stage.snowglobe, "token"), patch.object(stage.snowglobe, "identity", return_value=os.getuid()), patch.object(stage.snowglobe, "check_staging_pool"), patch.object(stage.snowglobe, "check_http") as check_http, patch.object(stage.snowglobe, "command", side_effect=command), patch.object(stage.subprocess, "run", side_effect=run), patch.object(stage.os, "chown") as chown, patch.object(stage.snowglobe.transition, "host_environment", return_value="PATH=/bin"), patch.object(stage.snowglobe.transition, "stop_preview"), patch.object(Path, "exists", exists), redirect_stdout(io.StringIO()):
118 with patch.object(stage.snowglobe, "STATE", state), patch.object(stage.snowglobe, "REPO", root / "releases/1234567890abcdef"), patch.object(stage.snowglobe, "token"), patch.object(stage.snowglobe, "identity", return_value=os.getuid()), patch.object(stage.snowglobe, "check_staging_pool"), patch.object(stage.snowglobe, "check_http") as check_http, patch.object(stage.snowglobe, "command", side_effect=command), patch.object(stage.subprocess, "run", side_effect=run), patch.object(stage.os, "chown") as chown, patch.object(Path, "exists", exists), redirect_stdout(io.StringIO()):
117119 stage.stage()
118120 check_http.assert_called_once_with(jobs[-1]["ID"] + ".fixture.test", "/sign-in", True)
119121 task = jobs[-1]["TaskGroups"][0]["Tasks"][0]
tools/dashboard-stage.py+3-3
......@@ -166,10 +166,10 @@ def stage():
166166 *["--env=" + key + "=" + value for key, value in env.items()], image, "--preview-account", capture=True)
167167 snowglobe.write_private(root / "access.json", imported.stdout)
168168 runtime = "/run/snowglobe-preview/" + identity
169 host_environment = snowglobe.transition.host_environment()
169 host_environment = snowglobe.command("systemctl", "show", "snowglobe-host", "--property=Environment", "--value", capture=True).stdout
170170 host_path = next(value for value in shlex.split(host_environment) if value.startswith("PATH="))
171171 host_python = snowglobe.command(*nix, "build", "--no-link", "--print-out-paths", "path:" + str(snowglobe.REPO) + "#host-python", capture=True).stdout.strip()
172 snowglobe.transition.stop_preview(identity)
172 subprocess.run(["systemctl", "stop", "snowglobe-preview-" + identity], capture_output=True)
173173 snowglobe.command("systemd-run", "--collect", "--quiet", "--unit=snowglobe-preview-" + identity,
174174 "--property=Restart=on-failure", "--property=MemoryMax=256M", "--property=TasksMax=32",
175175 "--setenv=" + host_path, "--setenv=SNOWGLOBE_DOMAIN=" + site["domain"],
......@@ -205,7 +205,7 @@ def stage():
205205def destroy(identity):
206206 snowglobe.token()
207207 saved = metadata(identity)
208 snowglobe.transition.stop_preview(identity)
208 subprocess.run(["systemctl", "stop", "snowglobe-preview-" + identity], capture_output=True)
209209 if saved.get("tokenAccessor"):
210210 revoked = subprocess.run(["nomad", "acl", "token", "delete", saved["tokenAccessor"]], capture_output=True, text=True)
211211 if revoked.returncode and "400 (Cannot delete nonexistent tokens:" not in revoked.stderr:
tools/data.py+6-10
......@@ -4,7 +4,6 @@ from contextlib import closing, contextmanager
44from datetime import datetime, timezone
55import hashlib
66import json
7from importlib import import_module
87import os
98from pathlib import Path
109import re
......@@ -15,8 +14,6 @@ import subprocess
1514import time
1615
1716
18transition = import_module("namespace-transition")
19
2017ROOT = Path("/opt/snowglobe")
2118STATE = Path("/var/lib/snowglobe")
2219BACKUPS = STATE / "backups"
......@@ -265,8 +262,7 @@ def restore(backup_id, service):
265262 path = source / name
266263 if not path.resolve().is_relative_to(source.resolve()) or not path.is_file() or checksum(path) != digest:
267264 raise ValueError("Dashboard backup is missing or changed")
268 unit = transition.layout(current_release())["prefix"] + "-dashboard"
269 run("systemctl", "stop", unit)
265 run("systemctl", "stop", "snowglobe-dashboard")
270266 safety = BACKUPS / ("before-restore-" + datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ") + "-" + secrets.token_hex(3))
271267 safety.mkdir(mode=0o700)
272268 files = backup_dashboard(safety / "dashboard")
......@@ -277,8 +273,8 @@ def restore(backup_id, service):
277273 if (root / "pictures").exists():
278274 shutil.rmtree(root / "pictures")
279275 shutil.copytree(source, root, dirs_exist_ok=True)
280 run("systemctl", "start", unit)
281 run("systemctl", "is-active", "--quiet", unit)
276 run("systemctl", "start", "snowglobe-dashboard")
277 run("systemctl", "is-active", "--quiet", "snowglobe-dashboard")
282278 print(f"restored=dashboard backup={backup_id} safety={safety.name}")
283279 return
284280 dataset = entry.get("dataset")
......@@ -323,9 +319,9 @@ def restore(backup_id, service):
323319 with (directory / database["dump"]).open("rb") as input_file:
324320 run(*PODMAN, "exec", "-i", container, "pg_restore", "-U", "postgres", "-d", name,
325321 "--no-owner", "--no-acl", "--role=" + owner, stdin=input_file)
326 script = transition.layout(current_release())["script"]
327 run("python3", str(script), "deploy", service, env=transition.environment(current_release()))
328 run("python3", str(script), "check", service, env=transition.environment(current_release()))
322 script = current_release() / "tools/snowglobe.py"
323 run("python3", str(script), "deploy", service)
324 run("python3", str(script), "check", service)
329325 print(f"restored={service} backup={backup_id} safety={safety}")
330326
331327
tools/deploy.py+1-11
......@@ -38,18 +38,9 @@ def run_logged(mode, target, command):
3838 print(f"run={run}")
3939
4040
41def prepare_remote():
42 subprocess.run(
43 ["ssh", "-p", PORT, "-o", "BatchMode=yes", HOST, "python3 - prepare"],
44 input="NAMESPACE = " + repr(json.loads((REPO / "dashboard/src/namespace-transition.json").read_text())) + "\n"
45 + (REPO / "tools/namespace-transition.py").read_text(), text=True, check=True,
46 )
47
48
4941def sync_manager(release):
5042 source = REMOTE / "releases" / release / "tools"
51 ssh(f"cp {source}/release.py {source}/data.py {source}/namespace-transition.py {REMOTE}/")
52 ssh(f"cp {source}/../dashboard/src/namespace-transition.json {REMOTE}/namespace-transition.json")
43 ssh(f"cp {source}/release.py {source}/data.py {REMOTE}/")
5344
5445
5546def export_revision(source, commit, paths, destination):
......@@ -207,7 +198,6 @@ def main():
207198 values = args.file.read_text()
208199 if not values.strip():
209200 raise ValueError("secret input is empty")
210 prepare_remote()
211201 manager = f"python3 {REMOTE}/release.py"
212202 if args.mode == "stage":
213203 release = upload()
tools/namespace-transition-test.py deleted-342
......@@ -1,342 +0,0 @@
1#!/usr/bin/env python3
2from importlib import import_module
3import io
4import json
5import os
6from pathlib import Path
7import shutil
8import subprocess
9import sys
10import tempfile
11from types import SimpleNamespace
12import unittest
13from unittest.mock import patch
14import xml.etree.ElementTree as ET
15
16import release
17import snowglobe
18
19
20transition = import_module("namespace-transition")
21
22
23class NamespaceTransition(unittest.TestCase):
24 def setUp(self):
25 temporary = tempfile.TemporaryDirectory()
26 self.addCleanup(temporary.cleanup)
27 self.root = Path(temporary.name).resolve()
28 self.directories = tuple((self.root / parent / "snowglobe", self.root / parent / transition.PREVIOUS)
29 for parent in ("opt", "state", "assets", "cache"))
30 self.addCleanup(patch.stopall)
31 patch.object(transition, "DIRECTORIES", self.directories).start()
32 registry = self.root / "nixos"
33 registry.mkdir()
34 patch.object(transition, "NIXOS_STATE", registry).start()
35 patch.object(transition.pwd, "getpwnam", side_effect=KeyError).start()
36 patch.object(transition.grp, "getgrnam", side_effect=KeyError).start()
37
38 def test_staging_aliases_share_existing_state_and_never_seed_another_registry(self):
39 for _, old in self.directories:
40 old.mkdir(parents=True)
41 new, old = self.directories[1]
42 (old / "identities.json").write_text('{"knife":3132}')
43 transition.prepare()
44 transition.prepare()
45 self.assertTrue(new.is_symlink())
46 self.assertEqual(new.resolve(), old)
47 (new / "identities.json").write_text('{"knife":3132,"dashboard":3133}')
48 self.assertEqual(json.loads((old / "identities.json").read_text()), {"knife": 3132, "dashboard": 3133})
49 self.assertTrue(all(new.resolve() == old for new, old in self.directories))
50
51 def test_staging_bootstrap_starts_the_installed_router_generation_without_restart(self):
52 state = self.root / "bootstrap"
53 state.mkdir()
54 for name in ("nomad", "router", "dashboard"):
55 (state / (name + ".token")).write_text("disposable-" + name)
56 original = transition.layout
57 source = self.root / "installed"
58 (source / "tools").mkdir(parents=True)
59 (source / "tools" / (transition.PREVIOUS + ".py")).touch()
60 with patch.object(snowglobe, "STATE", state), patch.object(snowglobe, "token"), patch.object(
61 snowglobe.os, "geteuid", return_value=0,
62 ), patch.object(Path, "is_symlink", return_value=True), patch.object(
63 transition, "layout", return_value=original(source),
64 ), patch.object(snowglobe, "command", return_value=subprocess.CompletedProcess([], 0)) as command:
65 snowglobe.bootstrap([])
66 units = [call.args for call in command.call_args_list if call.args[0] == "systemctl"]
67 self.assertEqual(units, [("systemctl", "start", transition.PREVIOUS + "-router.service")])
68
69 def test_split_state_is_rejected_before_any_alias_is_created(self):
70 self.directories[0][1].mkdir(parents=True)
71 new, old = self.directories[1]
72 new.mkdir(parents=True)
73 old.mkdir()
74 with self.assertRaisesRegex(ValueError, "independent control stores"):
75 transition.prepare()
76 self.assertFalse(self.directories[0][0].exists())
77
78 def test_promotion_preserves_whole_stores_and_release_links_then_is_idempotent(self):
79 revision = "1" * 16
80 for _, old in self.directories:
81 old.mkdir(parents=True)
82 (old / "durable").write_text("existing bytes")
83 (old / "durable").chmod(0o600)
84 new_root, old_root = self.directories[0]
85 (old_root / "releases" / revision).mkdir(parents=True)
86 for name in ("current", "main"):
87 (old_root / name).symlink_to(old_root / "releases" / revision)
88 before = [(old / "durable").stat().st_ino for _, old in self.directories]
89 with patch.object(transition.subprocess, "run", return_value=subprocess.CompletedProcess([], 0)) as run, patch.object(
90 transition, "migrate_policies",
91 ) as policies:
92 transition.promote(self.root)
93 policies.assert_called_once_with(self.root, self.directories[1][0])
94 self.assertEqual(run.call_count, 6)
95 transition.promote(self.root)
96 self.assertEqual(run.call_count, 6)
97 for index, (new, old) in enumerate(self.directories):
98 self.assertFalse(new.is_symlink())
99 self.assertTrue(old.is_symlink())
100 self.assertEqual((new / "durable").stat().st_ino, before[index])
101 self.assertEqual((new / "durable").stat().st_mode & 0o777, 0o600)
102 for name in ("current", "main"):
103 self.assertEqual(os.readlink(new_root / name), str(new_root / "releases" / revision))
104
105 def test_old_release_manifest_and_canonical_alias_validate_without_rewriting_the_tree(self):
106 new_root, old_root = self.directories[0]
107 revision = "a" * 16
108 source = old_root / "releases" / revision
109 (source / "tools").mkdir(parents=True)
110 (source / "tools" / (transition.PREVIOUS + ".py")).write_text("immutable")
111 manifest = source / ("." + transition.PREVIOUS + "-release.json")
112 manifest.write_text(json.dumps({"id": revision, "digest": release.tree_digest(source), "version": 2}))
113 original = manifest.read_bytes()
114 (old_root / "current").symlink_to(source)
115 transition.prepare()
116 with patch.object(release, "ROOT", new_root), patch.object(release, "RELEASES", new_root / "releases"):
117 self.assertEqual(release.current_release(), revision)
118 self.assertEqual(release.check_release(revision).resolve(), source)
119 self.assertEqual(manifest.read_bytes(), original)
120
121 def test_old_release_activation_uses_its_units_script_and_environment(self):
122 revision = "b" * 16
123 source = self.root / "release"
124 (source / "tools").mkdir(parents=True)
125 (source / "tools" / (transition.PREVIOUS + ".py")).touch()
126 (source / "nixos").mkdir()
127 (source / "nixos/dashboard.nix").touch()
128 transition.layout(source)["manifest"].write_text('{"version":2}')
129 observed = []
130 read_text = Path.read_text
131
132 def run(argv, **kwargs):
133 observed.append((argv, kwargs))
134 return subprocess.CompletedProcess(argv, 0, '"leader"')
135
136 with patch.dict(os.environ, {"SNOWGLOBE_POOL": "globe"}), patch.object(release, "ROOT", self.root), patch.object(
137 release, "STATE", self.root,
138 ), patch.object(release, "check_release", return_value=source), patch.object(
139 release, "current_release", return_value=None,
140 ), patch.object(release, "host_digest", return_value="different"), patch.object(
141 release, "host_configuration", return_value=transition.NAMESPACE["previousHost"],
142 ), patch.object(release, "jobs", return_value=set()), patch.object(
143 release.os, "uname", return_value=SimpleNamespace(nodename="snowglobe"),
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 ):
148 release.activate(revision, initial=True)
149 activate.assert_called_once_with(source)
150 scripts = [(argv, kwargs) for argv, kwargs in observed if argv[0] == "python3"]
151 self.assertEqual([argv[-1] for argv, _ in scripts], ["preflight", "pool", "deploy", "check"])
152 self.assertTrue(all(argv[1].endswith("/" + transition.PREVIOUS + ".py") for argv, _ in scripts))
153 self.assertTrue(all(kwargs["env"][transition.PREVIOUS.upper() + "_POOL"] == "globe" for _, kwargs in scripts))
154 self.assertIn(["systemctl", "restart", transition.PREVIOUS + "-router.service"], [argv for argv, _ in observed])
155
156 def test_policy_attachment_changes_after_new_policy_exists_without_changing_token_files(self):
157 state = self.root / "state"
158 state.mkdir()
159 for name in ("nomad", "router", "dashboard"):
160 (state / (name + ".token")).write_text("disposable-" + name)
161 responses = [io.BytesIO(json.dumps({"Name": transition.PREVIOUS + "-" + consumer,
162 "AccessorID": consumer, "Policies": [transition.PREVIOUS + "-" + consumer, "other"]}).encode())
163 for consumer in ("router", "dashboard")]
164 with patch.object(transition.urllib.request, "urlopen", side_effect=responses), patch.object(
165 transition.subprocess, "run", return_value=subprocess.CompletedProcess([], 0),
166 ) as run:
167 transition.migrate_policies(self.root, state)
168 commands = [call.args[0] for call in run.call_args_list]
169 for index, consumer in enumerate(("router", "dashboard")):
170 self.assertEqual(commands[index * 2][:5], ["nomad", "acl", "policy", "apply", "snowglobe-" + consumer])
171 self.assertIn("-policy=snowglobe-" + consumer, commands[index * 2 + 1])
172 self.assertIn("-policy=other", commands[index * 2 + 1])
173 self.assertFalse(any("disposable" in argument for argument in commands[index * 2 + 1]))
174 self.assertEqual((state / (consumer + ".token")).read_text(), "disposable-" + consumer)
175
176 def test_existing_vm_owner_profile_and_seed_still_resolve(self):
177 old = "https://paperclover.net/" + transition.PREVIOUS
178 root = ET.fromstring(f'<domain><metadata><vm xmlns="{old}"><owner>owner-id</owner><hardwareProfile>windows-q35-secure</hardwareProfile></vm></metadata><devices><disk><serial>{transition.PREVIOUS}-cloud-init</serial></disk></devices></domain>')
179 transition.normalize_vm(root)
180 namespace = "{https://paperclover.net/snowglobe}"
181 self.assertEqual(root.findtext(f"metadata/{namespace}vm/{namespace}owner"), "owner-id")
182 self.assertEqual(root.findtext(f"metadata/{namespace}vm/{namespace}hardwareProfile"), "windows-q35-secure")
183 self.assertEqual(root.findtext("devices/disk/serial"), "snowglobe-cloud-init")
184
185 def test_account_and_nixos_registries_round_trip_with_fixed_numeric_ownership(self):
186 old, new = transition.PREVIOUS + "-dashboard", "snowglobe-dashboard"
187 users = {old: SimpleNamespace(pw_uid=997, pw_gid=994)}
188 groups = {old: SimpleNamespace(gr_gid=994)}
189 registry = transition.NIXOS_STATE
190 (registry / "uid-map").write_text(json.dumps({old: 997, "other": 888}))
191 (registry / "gid-map").write_text(json.dumps({old: 994}))
192 for name in ("declarative-users", "declarative-groups"):
193 (registry / name).write_text("root " + old + "\n")
194
195 def lookup(values, name):
196 if name not in values:
197 raise KeyError(name)
198 return values[name]
199
200 calls = []
201
202 def command(argv, **kwargs):
203 calls.append(argv)
204 if argv[0] == "groupmod":
205 groups[argv[2]] = groups.pop(argv[3])
206 if argv[0] == "usermod":
207 users[argv[2]] = users.pop(argv[3])
208 return subprocess.CompletedProcess(argv, 0)
209
210 for _, directory in self.directories:
211 directory.mkdir(parents=True)
212 release_path = self.directories[0][1] / "releases" / ("c" * 16)
213 (release_path / "tools").mkdir(parents=True)
214 (release_path / "tools/snowglobe.py").touch()
215 transition.prepare()
216 canonical_release = self.directories[0][0] / "releases" / release_path.name
217 with patch.object(transition, "NIXOS_STATE", registry), patch.object(
218 transition.pwd, "getpwnam", side_effect=lambda name: lookup(users, name),
219 ), patch.object(transition.grp, "getgrnam", side_effect=lambda name: lookup(groups, name)), patch.object(
220 transition.subprocess, "run", side_effect=command,
221 ), patch.object(transition, "migrate_policies"):
222 transition.activate(canonical_release)
223 self.assertEqual(set(users), {new})
224 self.assertEqual((users[new].pw_uid, users[new].pw_gid, groups[new].gr_gid), (997, 994, 994))
225 self.assertEqual(json.loads((registry / "uid-map").read_text()), {new: 997, "other": 888})
226 self.assertEqual((registry / "declarative-users").read_text().split(), ["root", new])
227 previous_release = self.directories[0][0] / "releases" / ("d" * 16)
228 (previous_release / "tools").mkdir(parents=True)
229 (previous_release / "tools" / (transition.PREVIOUS + ".py")).touch()
230 transition.activate(previous_release)
231 self.assertEqual(set(users), {old})
232 self.assertEqual((users[old].pw_uid, users[old].pw_gid, groups[old].gr_gid), (997, 994, 994))
233 self.assertEqual(json.loads((registry / "uid-map").read_text()), {old: 997, "other": 888})
234 self.assertEqual((registry / "declarative-users").read_text().split(), ["root", old])
235 self.assertEqual(calls[0], ["systemctl", "stop", old])
236 self.assertEqual(calls[8], ["systemctl", "stop", new])
237
238 def test_policy_failure_after_directory_move_is_retried_without_moving_state_again(self):
239 for _, old in self.directories:
240 old.mkdir(parents=True)
241 with patch.object(transition.subprocess, "run", return_value=subprocess.CompletedProcess([], 0)) as run, patch.object(
242 transition, "migrate_policies", side_effect=[RuntimeError("interrupted ACL update"), None],
243 ) as policies:
244 with self.assertRaisesRegex(RuntimeError, "ACL update"):
245 transition.promote(self.root)
246 before = [new.stat().st_ino for new, _ in self.directories]
247 transition.promote(self.root)
248 self.assertEqual(policies.call_count, 2)
249 self.assertEqual(run.call_count, 6)
250 self.assertEqual(before, [new.stat().st_ino for new, _ in self.directories])
251
252 def test_only_legacy_transition_stops_the_retired_window_proxy(self):
253 with patch.object(transition.subprocess, "run", return_value=subprocess.CompletedProcess([], 0)) as run:
254 transition.stop_units(transition.PREVIOUS)
255 previous = [call.args[0] for call in run.call_args_list]
256 run.reset_mock()
257 transition.stop_units("snowglobe")
258 canonical = [call.args[0] for call in run.call_args_list]
259 self.assertIn(["systemctl", "stop", transition.PREVIOUS + "-vm-window-host-proxy"], previous)
260 self.assertFalse(any("vm-window-host-proxy" in command[-1] for command in canonical))
261 self.assertTrue(all(command[:2] == ["systemctl", "stop"] for command in previous + canonical))
262
263 def test_missing_reverse_alias_after_directory_move_is_recovered_from_old_releases(self):
264 new, old = self.directories[0]
265 source = new / "releases" / ("e" * 16) / "tools"
266 source.mkdir(parents=True)
267 (source / (transition.PREVIOUS + ".py")).touch()
268 transition.prepare()
269 self.assertTrue(old.is_symlink())
270 self.assertEqual(old.resolve(), new)
271
272 @unittest.skipUnless(sys.platform == "linux" and os.geteuid() == 0 and shutil.which("usermod") and shutil.which("groupmod"),
273 "requires Linux root and shadow tools; all account files stay inside a temporary prefix")
274 def test_real_shadow_tools_and_interrupted_registry_update_resume_with_only_fixture_accounts(self):
275 old, new = transition.PREVIOUS + "-dashboard", "snowglobe-dashboard"
276 prefix = self.root / "accounts"
277 etc = prefix / "etc"
278 etc.mkdir(parents=True)
279 (etc / "passwd").write_text(f"{old}:x:997:994:fixture:/var/empty:/bin/false\n")
280 (etc / "group").write_text(f"{old}:x:994:\n")
281 (etc / "shadow").write_text(f"{old}:!:1:0:99999:7:::\n")
282 (etc / "gshadow").write_text(f"{old}:!::\n")
283 for name in ("shadow", "gshadow"):
284 (etc / name).chmod(0o600)
285 (etc / "login.defs").write_text("")
286 registry = transition.NIXOS_STATE
287 for name, value in (("uid-map", 997), ("gid-map", 994)):
288 (registry / name).write_text(json.dumps({old: value}))
289 for name in ("declarative-users", "declarative-groups"):
290 (registry / name).write_text(old + "\n")
291 command = subprocess.run
292 replace = Path.replace
293 fail_login = True
294 fail_registry = True
295
296 def run(argv, **kwargs):
297 nonlocal fail_login
298 self.assertIn(argv[0], {"usermod", "groupmod"})
299 if argv[0] == "usermod" and fail_login:
300 fail_login = False
301 raise subprocess.CalledProcessError(1, argv)
302 return command([argv[0], "--prefix", str(prefix), *argv[1:]], **kwargs)
303
304 def atomic_replace(path, target):
305 nonlocal fail_registry
306 if Path(target) == registry / "gid-map" and fail_registry:
307 fail_registry = False
308 raise OSError("interrupted registry write")
309 return replace(path, target)
310
311 def user(name):
312 fields = (etc / "passwd").read_text().splitlines()[0].split(":")
313 if fields[0] != name:
314 raise KeyError(name)
315 return SimpleNamespace(pw_uid=int(fields[2]), pw_gid=int(fields[3]))
316
317 def group(name):
318 fields = (etc / "group").read_text().splitlines()[0].split(":")
319 if fields[0] != name:
320 raise KeyError(name)
321 return SimpleNamespace(gr_gid=int(fields[2]))
322
323 with patch.object(transition.pwd, "getpwnam", side_effect=user), patch.object(
324 transition.grp, "getgrnam", side_effect=group,
325 ), patch.object(transition.subprocess, "run", side_effect=run), patch.object(Path, "replace", atomic_replace):
326 with self.assertRaises(subprocess.CalledProcessError):
327 transition.rename_identity(transition.identity_change("snowglobe"))
328 self.assertEqual(group(new).gr_gid, 994)
329 self.assertEqual(user(old).pw_uid, 997)
330 with self.assertRaisesRegex(OSError, "registry write"):
331 transition.rename_identity(transition.identity_change("snowglobe"))
332 self.assertEqual(user(new).pw_uid, 997)
333 transition.rename_identity(transition.identity_change("snowglobe"))
334 self.assertIsNone(transition.identity_change("snowglobe"))
335 self.assertEqual(json.loads((registry / "gid-map").read_text()), {new: 994})
336 transition.rename_identity(transition.identity_change(transition.PREVIOUS))
337 self.assertEqual((user(old).pw_uid, user(old).pw_gid, group(old).gr_gid), (997, 994, 994))
338 self.assertIsNone(transition.identity_change(transition.PREVIOUS))
339
340
341if __name__ == "__main__":
342 unittest.main()
tools/namespace-transition.py deleted-245
......@@ -1,245 +0,0 @@
1#!/usr/bin/env python3
2"""Bridge the installed namespace and immutable releases during the rename."""
3import argparse
4import grp
5import json
6import os
7from pathlib import Path
8import pwd
9import subprocess
10import urllib.request
11
12
13NAMESPACE = globals().get("NAMESPACE") or json.loads(
14 (Path(__file__).with_suffix(".json") if Path(__file__).with_suffix(".json").exists()
15 else Path(__file__).resolve().parent.parent / "dashboard/src/namespace-transition.json").read_text()
16)
17PREVIOUS = NAMESPACE["previous"]
18NIXOS_STATE = Path("/var/lib/nixos")
19DIRECTORIES = tuple((Path(parent) / "snowglobe", Path(parent) / PREVIOUS)
20 for parent in ("/opt", "/var/lib", "/var/lib/caddy", "/var/cache"))
21
22
23def prepare():
24 for new, old in DIRECTORIES:
25 if new.exists() and old.exists() and new.resolve() != old.resolve():
26 raise ValueError(f"Two independent control stores exist: {new}, {old}")
27 new_root, _ = DIRECTORIES[0]
28 previous_releases = any((new_root / "releases").glob("*/tools/" + PREVIOUS + ".py"))
29 for new, old in DIRECTORIES:
30 if old.exists() and not new.exists() and not new.is_symlink():
31 new.symlink_to(old, target_is_directory=True)
32 elif previous_releases and new.exists() and not old.exists() and not old.is_symlink():
33 old.symlink_to(new, target_is_directory=True)
34
35
36def inherit_environment():
37 for name, value in list(os.environ.items()):
38 if name.startswith(PREVIOUS.upper() + "_"):
39 os.environ.setdefault("SNOWGLOBE_" + name.removeprefix(PREVIOUS.upper() + "_"), value)
40
41
42def environment(path, values=None):
43 values = dict(os.environ if values is None else values)
44 if layout(path)["prefix"] == PREVIOUS:
45 for name, value in list(values.items()):
46 if name.startswith("SNOWGLOBE_"):
47 values[PREVIOUS.upper() + "_" + name.removeprefix("SNOWGLOBE_")] = value
48 return values
49
50
51def layout(path):
52 prefix = "snowglobe" if (path / ".snowglobe-release.json").exists() or (path / "tools/snowglobe.py").exists() else PREVIOUS
53 return {"prefix": prefix, "script": path / f"tools/{prefix}.py", "manifest": path / f".{prefix}-release.json"}
54
55
56def host_configuration(hostname, available):
57 hostname = hostname.split(".", 1)[0]
58 candidates = ("snowglobe", NAMESPACE["previousHost"]) if hostname in {"snowglobe", NAMESPACE["previousHost"]} else (
59 "vm" if hostname in {"snowglobe-rehearsal", "clover-demo"} else hostname,
60 )
61 for name in candidates:
62 if name in available:
63 return name
64 raise ValueError(f"release has no NixOS configuration for {hostname}")
65
66
67def host_environment():
68 for prefix in ("snowglobe", PREVIOUS):
69 result = subprocess.run(["systemctl", "show", prefix + "-host", "--property=Environment", "--value"],
70 capture_output=True, text=True)
71 if result.stdout.strip():
72 return result.stdout
73 raise ValueError("the host service environment is unavailable")
74
75
76def stop_preview(identity):
77 for prefix in ("snowglobe", PREVIOUS):
78 subprocess.run(["systemctl", "stop", prefix + "-preview-" + identity], capture_output=True)
79
80
81def dashboard_container():
82 for name in ("snowglobe-dashboard", PREVIOUS + "-dashboard"):
83 if subprocess.run(["podman", "container", "exists", name], capture_output=True).returncode == 0:
84 return name
85 raise ValueError("the dashboard container is unavailable")
86
87
88def normalize_vm(root):
89 for element in root.iter():
90 if element.tag.startswith("{https://paperclover.net/" + PREVIOUS + "}"):
91 element.tag = element.tag.replace("{https://paperclover.net/" + PREVIOUS + "}", "{https://paperclover.net/snowglobe}", 1)
92 if element.tag == "serial" and element.text == PREVIOUS + "-cloud-init":
93 element.text = "snowglobe-cloud-init"
94 return root
95
96
97def migrate_policies(source, state, prefix="snowglobe"):
98 management = {**os.environ, "NOMAD_TOKEN": (state / "nomad.token").read_text().strip()}
99 for consumer in ("router", "dashboard"):
100 request = urllib.request.Request("http://127.0.0.1:4646/v1/acl/token/self", headers={
101 "X-Nomad-Token": (state / (consumer + ".token")).read_text().strip(),
102 })
103 with urllib.request.urlopen(request, timeout=10) as response:
104 token = json.load(response)
105 old, new = (PREVIOUS if prefix == "snowglobe" else "snowglobe") + "-" + consumer, prefix + "-" + consumer
106 subprocess.run(["nomad", "acl", "policy", "apply", new, str(source / "config/policies" / (consumer + ".hcl"))],
107 env=management, check=True, capture_output=True)
108 policies = [new if value == old else value for value in token.get("Policies", [])]
109 if old in token.get("Policies", []) or token.get("Name") == old:
110 subprocess.run(["nomad", "acl", "token", "update", "-name=" + new,
111 *("-policy=" + value for value in policies), token["AccessorID"]],
112 env=management, check=True, capture_output=True)
113
114
115def stop_units(prefix):
116 units = ("dashboard", "router", "log-shipper", "vm-screen", "host")
117 if prefix == PREVIOUS:
118 units += ("vm-window-host-proxy",)
119 for unit in units:
120 result = subprocess.run(["systemctl", "stop", prefix + "-" + unit], capture_output=True, text=True)
121 if result.returncode and "not loaded" not in result.stderr:
122 result.check_returncode()
123
124
125def identity_change(prefix):
126 old = (PREVIOUS if prefix == "snowglobe" else "snowglobe") + "-dashboard"
127 new = prefix + "-dashboard"
128 users, groups = {}, {}
129 for name in (old, new):
130 try:
131 users[name] = pwd.getpwnam(name)
132 except KeyError:
133 pass
134 try:
135 groups[name] = grp.getgrnam(name)
136 except KeyError:
137 pass
138 if len(users) > 1 or len(groups) > 1:
139 raise ValueError("Two dashboard identities exist; refusing to merge identities")
140 if any((user.pw_uid, user.pw_gid) != (997, 994) for user in users.values()) or any(
141 group.gr_gid != 994 for group in groups.values()
142 ):
143 raise ValueError("The dashboard account must retain UID 997 and GID 994")
144 registries = False
145 for filename, expected in (("uid-map", 997), ("gid-map", 994)):
146 path = NIXOS_STATE / filename
147 if path.exists():
148 values = json.loads(path.read_text())
149 if any(name in values and values[name] != expected for name in (old, new)):
150 raise ValueError("The NixOS identity registry disagrees with the dashboard account")
151 registries |= old in values
152 for filename in ("declarative-users", "declarative-groups"):
153 path = NIXOS_STATE / filename
154 registries |= path.exists() and old in path.read_text().split()
155 return (old, new) if old in users or old in groups or registries else None
156
157
158def rename_identity(change):
159 if change is None:
160 return
161 old, new = change
162 try:
163 grp.getgrnam(old)
164 except KeyError:
165 pass
166 else:
167 subprocess.run(["groupmod", "--new-name", new, old], check=True, capture_output=True)
168 try:
169 pwd.getpwnam(old)
170 except KeyError:
171 pass
172 else:
173 subprocess.run(["usermod", "--login", new, old], check=True, capture_output=True)
174 for filename in ("uid-map", "gid-map", "declarative-users", "declarative-groups"):
175 path = NIXOS_STATE / filename
176 if not path.exists():
177 continue
178 if filename.endswith("-map"):
179 values = json.loads(path.read_text())
180 if old not in values:
181 continue
182 values[new] = values.pop(old)
183 content = json.dumps(values) + "\n"
184 else:
185 names = path.read_text().split()
186 if old not in names:
187 continue
188 content = " ".join(new if name == old else name for name in names) + "\n"
189 pending = path.with_suffix(".namespace-pending")
190 pending.write_text(content)
191 pending.chmod(path.stat().st_mode & 0o777)
192 pending.replace(path)
193
194
195def promote(source):
196 prepare()
197 change = identity_change("snowglobe")
198 moving = any(old.exists() and not old.is_symlink() for _, old in DIRECTORIES)
199 if moving or change is not None:
200 stop_units(PREVIOUS)
201 rename_identity(change)
202 for new, old in DIRECTORIES:
203 if old.exists() and not old.is_symlink():
204 if new.is_symlink():
205 new.unlink()
206 old.rename(new)
207 old.symlink_to(new, target_is_directory=True)
208 new_root, old_root = DIRECTORIES[0]
209 for name in ("current", "main"):
210 link = new_root / name
211 if link.is_symlink():
212 target = Path(os.readlink(link))
213 if target.is_relative_to(old_root):
214 pending = link.with_suffix(".namespace-pending")
215 pending.unlink(missing_ok=True)
216 pending.symlink_to(new_root / target.relative_to(old_root))
217 pending.replace(link)
218 if old_root.exists():
219 migrate_policies(source, DIRECTORIES[1][0])
220
221
222def activate(path):
223 if layout(path)["prefix"] == "snowglobe":
224 promote(path)
225 else:
226 change = identity_change(PREVIOUS)
227 stop_units("snowglobe")
228 rename_identity(change)
229 migrate_policies(path, DIRECTORIES[1][0], PREVIOUS)
230
231
232def main():
233 parser = argparse.ArgumentParser()
234 parser.add_argument("mode", choices=("prepare", "promote"))
235 args = parser.parse_args()
236 if os.geteuid() != 0:
237 raise PermissionError("the namespace transition requires root")
238 if args.mode == "prepare":
239 prepare()
240 else:
241 promote(Path(__file__).resolve().parent.parent)
242
243
244if __name__ == "__main__":
245 main()
tools/oidc-provider.py+1-2
......@@ -1,7 +1,6 @@
11#!/usr/bin/env python3
22"""Provision a first-party OIDC client through the dashboard's host-only CLI."""
33import json
4from importlib import import_module
54import subprocess
65import sys
76
......@@ -9,7 +8,7 @@ data = json.load(sys.stdin)
98if data["request"]["kind"] != "client":
109 raise ValueError("unsupported Snowglobe input")
1110result = subprocess.run(
12 ["podman", "exec", "-i", import_module("namespace-transition").dashboard_container(), "/bin/home-dashboard", "--oidc-client"],
11 ["podman", "exec", "-i", "snowglobe-dashboard", "/bin/home-dashboard", "--oidc-client"],
1312 input=json.dumps(data), text=True, check=True, capture_output=True,
1413)
1514sys.stdout.write(result.stdout)
tools/owl-neuron/README.md+2-9
......@@ -1,6 +1,6 @@
11# Owl neuron experiment
22
3Disposable experiment on Zenith (`root@10.0.0.1:22`), authorized on 2026-10-18
3Disposable experiment on Snow Globe (`root@10.0.0.1:22`), authorized on 2026-10-18
44with inference downtime. Source and run state live at
55`/srv/staging/owl-neuron-20261018`; the existing model weights are mounted
66read-only. The container has no network, host credentials, personal data, or
......@@ -165,14 +165,7 @@ The production change ports the verified code to the current Snow Globe namespac
165165without changing the overlay. Deployment history and its pre-switch backup are
166166recorded by the standard release workflow.
167167
168The checkout's `deploy.py stage` stopped before startup: Zenith uses the renamed
169Snow Globe units and environment, while this checkout still targets Studio.
170The installed `/opt/studio/current/tools/snowglobe.py` staging engine is therefore
171loaded with `REPO` set to the candidate release's resolved path and `SERVICES`
172set to its `service` directory. It runs the normal stage command with
173`--pool globe --base-domain paperclover.net` and `STUDIO_LOCAL_AI=true`.
174Use that same installed engine and candidate paths for `destroy <stage-id>`;
175the checkout's destroy command targets the missing `studio-router.service`.
168Use the current checkout's deployment tooling for staging and destruction.
176169Only after preview destruction and GPU release should production count 1 return.
177170
178171The same launcher accepts `--program train.py -- <training arguments>` or
tools/plausible.md+1-1
......@@ -78,7 +78,7 @@ native events. Cloud exports also exclude earlier third-party imports.
7878
7979## Rehearsal evidence
8080
81On 2026-10-19 UTC, release `12a2725e1a1372ce` passed on Zenith at
81On 2026-10-19 UTC, release `12a2725e1a1372ce` passed on Snow Globe at
8282`https://plausible-preview-3234cb91.paperclover.net`. Owner login, disabled
8383registration, private dashboards, the CSV upload, keyboard filters, mobile,
8484both themes, and installation instructions were checked in a real browser.
tools/release-host-test.py+9-58
......@@ -10,8 +10,6 @@ from unittest.mock import patch
1010
1111import release
1212
13PREVIOUS_HOST = release.transition.NAMESPACE["previousHost"]
14
1513
1614class HostConfiguration(unittest.TestCase):
1715 def select(self, hostname, configurations):
......@@ -25,11 +23,10 @@ class HostConfiguration(unittest.TestCase):
2523 )
2624 return result
2725
28 def test_rename_and_rollback(self):
29 for hostname in (PREVIOUS_HOST, "snowglobe", "snowglobe.paperclover.net"):
26 def test_canonical_hostname(self):
27 for hostname in ("snowglobe", "snowglobe.paperclover.net"):
3028 with self.subTest(hostname=hostname):
3129 self.assertEqual(self.select(hostname, ["installer", "snowglobe", "vm"]), "snowglobe")
32 self.assertEqual(self.select(hostname, ["installer", PREVIOUS_HOST, "vm"]), PREVIOUS_HOST)
3330
3431 def test_rehearsal_stays_on_vm(self):
3532 self.assertEqual(self.select("snowglobe-rehearsal", ["installer", "snowglobe", "vm"]), "vm")
......@@ -42,55 +39,11 @@ class HostConfiguration(unittest.TestCase):
4239
4340
4441class ActivationEnvironment(unittest.TestCase):
45 def test_flake_paths_are_resolved_again_after_the_release_store_moves(self):
46 with tempfile.TemporaryDirectory() as temporary:
47 root = Path(temporary).resolve()
48 previous = root / release.transition.PREVIOUS
49 canonical = root / "snowglobe"
50 source = previous / "releases" / ("a" * 16)
51 (source / "tools").mkdir(parents=True)
52 (source / "tools/snowglobe.py").touch()
53 (source / ".snowglobe-release.json").write_text('{"version":1}')
54 canonical.symlink_to(previous, target_is_directory=True)
55 logical_source = canonical / "releases" / source.name
56 observed = []
57 read_text = Path.read_text
58
59 def activate(path):
60 self.assertEqual(path, logical_source)
61 canonical.unlink()
62 previous.rename(canonical)
63 previous.symlink_to(canonical, target_is_directory=True)
64
65 def run(argv, **kwargs):
66 if argv[0] in {"nix", "nixos-rebuild"}:
67 observed.append(argv)
68 output = '["snowglobe"]' if argv[0] == "nix" else '"leader"'
69 return subprocess.CompletedProcess(argv, 0, output)
70
71 with patch.dict(os.environ), patch.object(release, "ROOT", canonical), patch.object(release, "STATE", root), patch.object(
72 release, "check_release", return_value=logical_source,
73 ), patch.object(release, "current_release", return_value=None), patch.object(
74 release, "host_digest", return_value="new",
75 ), patch.object(release, "jobs", return_value=set()), patch.object(
76 release.os, "uname", return_value=SimpleNamespace(nodename="snowglobe"),
77 ), patch.object(release.subprocess, "run", side_effect=run), patch.object(
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),
82 ):
83 release.activate(source.name, initial=True)
84 self.assertIn(f"path:{source}#nixosConfigurations", observed[0])
85 self.assertEqual(observed[1], ["nixos-rebuild", "dry-build", "--flake", f"path:{source}#snowglobe"])
86 self.assertEqual(observed[2], ["nixos-rebuild", "switch", "--flake", f"path:{logical_source}#snowglobe"])
87
8842 def test_host_switch_refreshes_inherited_name_and_preserves_overrides(self):
8943 for before, after, inherited, expected in (
90 (PREVIOUS_HOST, "snowglobe", PREVIOUS_HOST, "snowglobe"),
91 ("snowglobe", PREVIOUS_HOST, "snowglobe", PREVIOUS_HOST),
92 (PREVIOUS_HOST, "snowglobe", None, "snowglobe"),
93 (PREVIOUS_HOST, "snowglobe", "test-override", "test-override"),
44 ("fixture", "snowglobe", "fixture", "snowglobe"),
45 ("fixture", "snowglobe", None, "snowglobe"),
46 ("fixture", "snowglobe", "test-override", "test-override"),
9447 ("snowglobe-rehearsal", "snowglobe-rehearsal", "snowglobe-rehearsal", "snowglobe-rehearsal"),
9548 ):
9649 with self.subTest(before=before, inherited=inherited), tempfile.TemporaryDirectory() as temporary:
......@@ -98,9 +51,8 @@ class ActivationEnvironment(unittest.TestCase):
9851 source = root / "release"
9952 source.mkdir()
10053 (source / "tools").mkdir()
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}')
54 (source / "tools/snowglobe.py").touch()
55 (source / ".snowglobe-release.json").write_text('{"version":1}')
10456 hostname = before
10557 observed = []
10658 calls = []
......@@ -112,8 +64,7 @@ class ActivationEnvironment(unittest.TestCase):
11264 if argv[:3] == ["hostnamectl", "set-hostname", "--transient"]:
11365 hostname = after
11466 if argv[-1] == "deploy":
115 key = prefix.upper() + "_NODE_NAME"
116 observed.append(kwargs["env"].get(key))
67 observed.append(kwargs["env"].get("SNOWGLOBE_NODE_NAME"))
11768 return subprocess.CompletedProcess(argv, 0, '"leader"')
11869
11970 with patch.dict(os.environ), patch.object(release, "ROOT", root), patch.object(
......@@ -129,7 +80,7 @@ class ActivationEnvironment(unittest.TestCase):
12980 os.environ.pop("SNOWGLOBE_NODE_NAME", None)
13081 else:
13182 os.environ["SNOWGLOBE_NODE_NAME"] = inherited
132 with patch.object(release.transition, "activate"), patch.object(
83 with patch.object(
13384 Path, "read_text", autospec=True,
13485 side_effect=lambda path: after + "\n" if path == Path("/etc/hostname") else read_text(path),
13586 ):
tools/release.py+31-40
......@@ -4,7 +4,6 @@ from datetime import datetime, timezone
44import fcntl
55import hashlib
66import json
7from importlib import import_module
87import os
98from pathlib import Path
109import re
......@@ -12,8 +11,6 @@ import subprocess
1211import time
1312
1413
15transition = import_module("namespace-transition")
16
1714ROOT = Path("/opt/snowglobe")
1815RELEASES = ROOT / "releases"
1916STATE = Path("/var/lib/snowglobe")
......@@ -63,27 +60,25 @@ def host_digest(root):
6360 digest = hashlib.sha256()
6461 paths = sorted(path for path, relative in files(root)
6562 if relative.parts[0] in {"nixos", "dashboard", "config", "service"}
66 or str(relative) in {"flake.nix", "flake.lock", "tools/router.py", "tools/dashboard-host.py", "tools/shale-access.py", "tools/shale-labels.py", "tools/dashboard-run.py", "tools/release.py", "tools/namespace-transition.py", "tools/vms.py", "tools/mac-vms.py", "tools/vm-screen.py", "guest/protocol.json"})
63 or str(relative) in {"flake.nix", "flake.lock", "tools/router.py", "tools/dashboard-host.py", "tools/shale-access.py", "tools/shale-labels.py", "tools/dashboard-run.py", "tools/release.py", "tools/vms.py", "tools/mac-vms.py", "tools/vm-screen.py", "guest/protocol.json"})
6764 for path in paths:
6865 digest.update(str(path.relative_to(root)).encode())
6966 _hash_contents(digest, path)
7067 return digest.hexdigest()
7168
7269
73def check_release(release, legacy=False):
70def check_release(release):
7471 if not RELEASE_ID.fullmatch(release):
7572 raise ValueError("invalid release ID")
7673 path = RELEASES / release
7774 if not path.is_dir():
7875 raise ValueError(f"release is missing: {release}")
79 manifest = transition.layout(path)["manifest"]
76 manifest = path / ".snowglobe-release.json"
8077 if not manifest.exists():
81 if not legacy:
82 raise ValueError(f"release has no manifest: {release}")
83 else:
84 details = json.loads(manifest.read_text())
85 if details.get("id") != release or details.get("digest") != tree_digest(path) or details.get("version", 1) not in (1, 2):
86 raise ValueError(f"release contents changed: {release}")
78 raise ValueError(f"release has no manifest: {release}")
79 details = json.loads(manifest.read_text())
80 if details.get("id") != release or details.get("digest") != tree_digest(path) or details.get("version", 1) not in (1, 2):
81 raise ValueError(f"release contents changed: {release}")
8782 return path
8883
8984
......@@ -103,7 +98,7 @@ def main_release(version=None):
10398 return None
10499 if not RELEASE_ID.fullmatch(version):
105100 raise ValueError("invalid release ID")
106 details = json.loads(transition.layout(RELEASES / version)["manifest"].read_text())
101 details = json.loads((RELEASES / version / ".snowglobe-release.json").read_text())
107102 revision = details.get("main")
108103 if (not isinstance(revision, dict) or not isinstance(revision.get("commit"), str)
109104 or not re.fullmatch(r"[0-9a-f]{40}", revision["commit"])
......@@ -124,8 +119,8 @@ def save_history(entries):
124119
125120def jobs(path):
126121 rendered = subprocess.run(
127 ["python3", str(transition.layout(path)["script"]), "render"],
128 check=True, capture_output=True, text=True, env=transition.environment(path),
122 ["python3", str(path / "tools/snowglobe.py"), "render"],
123 check=True, capture_output=True, text=True,
129124 ).stdout
130125 result = set(re.findall(r'^job "([a-z][a-z0-9-]*)" \{$', rendered, re.MULTILINE))
131126 if not result:
......@@ -139,15 +134,16 @@ def host_configuration(path):
139134 ["nix", "--extra-experimental-features", "nix-command flakes", "eval", "--json", f"path:{path.resolve()}#nixosConfigurations", "--apply", "builtins.attrNames"],
140135 check=True, capture_output=True, text=True,
141136 ).stdout)
142 return transition.host_configuration(hostname, available)
137 name = "vm" if hostname == "snowglobe-rehearsal" else hostname
138 if name not in available:
139 raise ValueError(f"release has no NixOS configuration for {hostname}")
140 return name
143141
144142
145def activate(release, legacy=False, initial=False):
146 path = check_release(release, legacy)
147 layout = transition.layout(path)
148 script = layout["script"]
149 environment = transition.environment(path)
150 prefix = layout["prefix"]
143def activate(release, initial=False):
144 path = check_release(release)
145 script = path / "tools/snowglobe.py"
146 environment = dict(os.environ)
151147 managed = STATE / "managed-jobs.json"
152148 current = current_release()
153149 previous = set(json.loads(managed.read_text())) if managed.exists() else (
......@@ -175,14 +171,13 @@ def activate(release, legacy=False, initial=False):
175171 if current is None:
176172 (ROOT / "current").symlink_to(path)
177173 hostname = os.uname().nodename.split(".", 1)[0]
178 transition.activate(path)
179174 subprocess.run(["nixos-rebuild", "switch", "--flake", f"path:{path.resolve()}#{configuration}"], check=True)
180175 configured_hostname = Path("/etc/hostname").read_text().strip()
181176 if os.uname().nodename != configured_hostname:
182177 subprocess.run(["hostnamectl", "set-hostname", "--transient", configured_hostname], check=True)
183178 if os.environ.get("SNOWGLOBE_NODE_NAME", hostname) == hostname:
184179 os.environ["SNOWGLOBE_NODE_NAME"] = os.uname().nodename.split(".", 1)[0]
185 environment = transition.environment(path)
180 environment = dict(os.environ)
186181 next_link = ROOT / ("next-" + release)
187182 next_link.unlink(missing_ok=True)
188183 next_link.symlink_to(path)
......@@ -199,10 +194,10 @@ def activate(release, legacy=False, initial=False):
199194 time.sleep(2)
200195 else:
201196 raise RuntimeError("Nomad API did not become ready")
202 subprocess.run(["systemctl", "restart", prefix + "-router.service"], check=True)
197 subprocess.run(["systemctl", "restart", "snowglobe-router.service"], check=True)
203198 if (path / "nixos/dashboard.nix").exists():
204 subprocess.run(["systemctl", "start", prefix + "-dashboard.service"], check=True)
205 subprocess.run(["systemctl", "is-active", "--quiet", prefix + "-dashboard.service"], check=True)
199 subprocess.run(["systemctl", "start", "snowglobe-dashboard.service"], check=True)
200 subprocess.run(["systemctl", "is-active", "--quiet", "snowglobe-dashboard.service"], check=True)
206201 subprocess.run(["python3", str(script), "pool"], check=True, env=environment)
207202 subprocess.run(["python3", str(script), "deploy"], check=True, env=environment)
208203 desired = jobs(path)
......@@ -215,9 +210,9 @@ def activate(release, legacy=False, initial=False):
215210 pending.write_text(json.dumps(sorted(desired)) + "\n")
216211 pending.replace(managed)
217212 if (path / "tools/log-shipper.py").exists():
218 subprocess.run(["systemctl", "restart", prefix + "-log-shipper.service"], check=True)
219 manifest = transition.layout(path)["manifest"]
220 if not legacy and json.loads(manifest.read_text()).get("version", 1) >= 2:
213 subprocess.run(["systemctl", "restart", "snowglobe-log-shipper.service"], check=True)
214 manifest = path / ".snowglobe-release.json"
215 if json.loads(manifest.read_text()).get("version", 1) >= 2:
221216 subprocess.run(["python3", str(script), "check"], check=True, env=environment)
222217 return backup
223218
......@@ -227,16 +222,13 @@ def main():
227222 parser.add_argument("mode", choices=["publish", "deploy", "rollback", "history", "bootstrap", "verify", "stage-destroy"])
228223 parser.add_argument("target", nargs="?")
229224 args = parser.parse_args()
230 transition.prepare()
231 transition.inherit_environment()
232225 STATE.mkdir(parents=True, exist_ok=True)
233226 if args.mode == "stage-destroy":
234227 if not args.target or not STAGE_ID.fullmatch(args.target):
235228 parser.error("stage-destroy requires a stage ID")
236229 saved = json.loads((STATE / "stages" / (args.target + ".json")).read_text())
237230 path = check_release(saved["release"])
238 subprocess.run(["python3", str(transition.layout(path)["script"]), "destroy", args.target],
239 check=True, env=transition.environment(path))
231 subprocess.run(["python3", str(path / "tools/snowglobe.py"), "destroy", args.target], check=True)
240232 return
241233 if args.mode == "verify":
242234 if not args.target:
......@@ -271,7 +263,7 @@ def main():
271263 entries = history()
272264 current = current_release()
273265 if current and not entries and args.mode != "bootstrap":
274 entries.append({"release": current, "source": "previous", "time": int(time.time()), "legacy": not transition.layout(RELEASES / current)["manifest"].exists()})
266 entries.append({"release": current, "source": "previous", "time": int(time.time())})
275267 save_history(entries)
276268 if args.mode == "deploy":
277269 candidate = main_release()
......@@ -279,11 +271,10 @@ def main():
279271 raise ValueError("main changed or is unavailable. Publish main and retry deployment.")
280272 release = candidate["release"]
281273 source = "main"
282 legacy = False
283274 elif args.mode == "bootstrap":
284275 if not args.target or not RELEASE_ID.fullmatch(args.target) or entries:
285276 parser.error("bootstrap requires a release ID and no successful deployment")
286 release, source, legacy = args.target, "bootstrap", False
277 release, source = args.target, "bootstrap"
287278 main_release(release)
288279 else:
289280 if args.target:
......@@ -294,12 +285,12 @@ def main():
294285 match = next((item for item in reversed(entries) if item["release"] != current), None)
295286 if not match:
296287 parser.error("no earlier production release")
297 release, source, legacy = match["release"], "rollback", match.get("legacy", False)
288 release, source = match["release"], "rollback"
298289 if release == current and entries and entries[-1]["release"] == current:
299290 print(f"already running {release}")
300291 return
301 backup = activate(release, legacy, args.mode == "bootstrap")
302 entries.append({"release": release, "source": source, "time": int(time.time()), "legacy": legacy, "backup": backup})
292 backup = activate(release, args.mode == "bootstrap")
293 entries.append({"release": release, "source": source, "time": int(time.time()), "backup": backup})
303294 save_history(entries)
304295 print(f"production={release} previous={current or ''}")
305296
tools/snowglobe-hardware.md+26
......@@ -103,3 +103,29 @@ Before destructive storage or OS work, verify key recovery and application/
103103database restores from at least two restricted copies stored separately off
104104this computer. Preserve the existing encrypted roots, pool host ID, numeric
105105owners, SSH trust, and retained recovery sources through a rebuild.
106
107## Namespace migration, 2026-10-21
108
109Production's kernel hostname, static hostname, and Nomad node name are
110`snowglobe`. Clover manages the Tailscale machine name separately.
111
112The final telemetry cutover validated 6,949,996 custom metric samples across
113458 series and all 308,283 stored spans, including 4,953,978 renamed samples
114and 3,861 renamed spans. Metrics retained every series and timestamp within
115the store's numeric precision; two existing conflicting timestamps retained
116one of their original values. Traces retained their timestamps, stream labels,
117and every field, verified with a normalized all-field digest. Other metrics
118and logs remained intact. The original metrics and traces directories are
119preserved in `/srv/prod/victoria/archives/namespace-20261021`. Root-only
120migration reports and the executed script live under
121`/var/lib/snowglobe/migrations/namespace-final-20261021`.
122
123The 265 pre-rename releases are preserved under
124`/opt/snowglobe/archives/releases`; they are archives, excluded from executable
125rollback history. `/opt/snowglobe/archives/namespace-20261021` preserves the
126complete prior deployment history, retired manager compatibility files and
127ACL policy definitions, and the former `/etc/nixos` source tree. The installed
128configuration now follows `/opt/snowglobe/current`. Old path aliases, stale
129runtime sockets, the unused preview host unit, and unreferenced old ACL
130policies have been removed. Numeric identities and active service data paths
131were preserved.
tools/snowglobe.py+1-5
......@@ -4,7 +4,6 @@ from contextlib import nullcontext
44import fcntl
55import hashlib
66import json
7from importlib import import_module
87import os
98from pathlib import Path
109import re
......@@ -18,9 +17,6 @@ import time
1817from data import cloned_postgres, dataset_for
1918
2019
21transition = import_module("namespace-transition")
22transition.inherit_environment()
23
2420REPO = Path(__file__).resolve().parent.parent
2521SERVICES = REPO / "service"
2622STATE = Path("/var/lib/snowglobe")
......@@ -810,7 +806,7 @@ def bootstrap(all_data):
810806 Path(path).unlink()
811807 current = Path("/opt/snowglobe/current")
812808 if current.is_symlink():
813 command("systemctl", "start", transition.layout(current)["prefix"] + "-router.service")
809 command("systemctl", "start", "snowglobe-router.service")
814810
815811
816812def destroy_stage(stage, metadata, definitions):
tools/vms.py+10-11
......@@ -19,7 +19,6 @@ DISKS = Path("/srv/vm")
1919IMAGES = Path(os.environ.get("SNOWGLOBE_VM_IMAGES_ROOT", "/srv/clover/Media/vm"))
2020PRESETS = Path(os.environ.get("SNOWGLOBE_VM_PRESETS_ROOT", str(IMAGES / "Presets")))
2121UPLOADS = Path(os.environ.get("SNOWGLOBE_VM_UPLOADS_ROOT", str(IMAGES / "Install Disks")))
22transition = import_module("namespace-transition")
2322mac_vms = import_module("mac-vms")
2423NS = "{https://paperclover.net/snowglobe}"
2524NAME = re.compile(r"[a-z0-9][a-z0-9-]{0,62}\Z")
......@@ -245,7 +244,7 @@ def domains():
245244 for name in virsh("list", "--all", "--name").splitlines():
246245 if not name:
247246 continue
248 root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name)))
247 root = ET.fromstring(virsh("dumpxml", name))
249248 state_line = virsh("domstate", name, "--reason").splitlines()[0].lower()
250249 state = next((value for value in ("running", "blocked", "paused", "shutdown", "crashed", "pmsuspended") if state_line.startswith(value)), "shutoff")
251250 reason = state_line.split("(", 1)[1].rstrip(")") if state in {"paused", "crashed"} and "(" in state_line else None
......@@ -468,7 +467,7 @@ def managed_directory(path):
468467
469468def change_media(spec):
470469 name = spec["name"]
471 root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name, "--inactive")))
470 root = ET.fromstring(virsh("dumpxml", name, "--inactive"))
472471 drives = root.findall("./devices/disk[@device='cdrom']")
473472 source = resolve_image(spec["image"]) if spec["image"] else None
474473 details = profile(inferred_profile(root))
......@@ -523,7 +522,7 @@ def save_preset(spec):
523522 name = spec["name"]
524523 if virsh("domstate", name).strip() != "shut off":
525524 raise ValueError("Shut down this VM before saving a preset.")
526 root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name, "--inactive")))
525 root = ET.fromstring(virsh("dumpxml", name, "--inactive"))
527526 if root.findtext(f"metadata/{NS}vm/{NS}platform") == "macos":
528527 raise ValueError("Use a prepared OS X preset from the OS library.")
529528 if root.findtext(f"metadata/{NS}vm/{NS}setup") in {"windows-v2", "windows-v3", "windows-v4"} and spec.get("setup") not in {"windows-v3", "windows-v4"}:
......@@ -587,7 +586,7 @@ def save_preset(spec):
587586
588587
589588def console_target(name):
590 root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name)))
589 root = ET.fromstring(virsh("dumpxml", name))
591590 graphics = root.find("./devices/graphics[@type='vnc']")
592591 if virsh("domstate", name).strip() not in {"running", "paused", "blocked"} or graphics is None:
593592 raise ValueError("Start this VM before opening its screen.")
......@@ -958,7 +957,7 @@ def main():
958957 fields = Path(f"/proc/{pid}/stat").read_text().rsplit(") ", 1)[1].split()
959958 resident = int(Path(f"/proc/{pid}/statm").read_text().split()[1]) * os.sysconf("SC_PAGE_SIZE")
960959 result[name] = {"cpu": (int(fields[11]) + int(fields[12])) / os.sysconf("SC_CLK_TCK"), "memory": resident,
961 "vcpus": int(transition.normalize_vm(ET.fromstring(virsh("dumpxml", name))).findtext("vcpu", "1"))}
960 "vcpus": int(ET.fromstring(virsh("dumpxml", name)).findtext("vcpu", "1"))}
962961 return result
963962 if action == "library":
964963 return library()
......@@ -967,7 +966,7 @@ def main():
967966 if action == "console":
968967 return console_target(payload["name"])
969968 if action == "guest":
970 root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", payload["name"])))
969 root = ET.fromstring(virsh("dumpxml", payload["name"]))
971970 source = root.find(f"./devices/channel[@type='unix']/target[@name='{GUEST_CHANNEL}']/../source")
972971 expected = f"/run/libvirt/qemu/channel/{root.get('id')}-{payload['name'][:20]}/{GUEST_CHANNEL}"
973972 if virsh("domstate", payload["name"]).strip() != "running" or source is None or source.get("mode") != "bind" or source.get("path") != expected:
......@@ -976,7 +975,7 @@ def main():
976975 if action == "serial":
977976 if virsh("domstate", payload["name"]).strip() not in {"running", "blocked"}:
978977 raise ValueError("Start this VM before opening its console.")
979 root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", payload["name"])))
978 root = ET.fromstring(virsh("dumpxml", payload["name"]))
980979 console = root.find("./devices/console[@type='pty']")
981980 source = console.find("source") if console is not None else None
982981 target = console.find("target") if console is not None else None
......@@ -985,7 +984,7 @@ def main():
985984 raise ValueError("This VM has no serial console. Add one in its hardware settings.")
986985 return {"path": path}
987986 if action == "owner":
988 return {"owner": transition.normalize_vm(ET.fromstring(virsh("dumpxml", payload["name"]))).findtext(f"metadata/{NS}vm/{NS}owner")}
987 return {"owner": ET.fromstring(virsh("dumpxml", payload["name"])).findtext(f"metadata/{NS}vm/{NS}owner")}
989988 if action == "access":
990989 try:
991990 with os.fdopen(open_regular(DISKS / payload["name"] / "access.json")) as incoming:
......@@ -1002,7 +1001,7 @@ def main():
10021001 name = payload["name"]
10031002 ensure_name(name)
10041003 if payload["action"] == "start":
1005 root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name, "--inactive")))
1004 root = ET.fromstring(virsh("dumpxml", name, "--inactive"))
10061005 if root.find("./devices/interface/source[@network='default']") is not None:
10071006 ensure_network()
10081007 virsh({"start": "start", "shutdown": "shutdown", "reboot": "reboot", "destroy": "destroy", "resume": "resume", "suspend": "suspend"}[payload["action"]], name)
......@@ -1023,7 +1022,7 @@ def main():
10231022 raise ValueError("VM does not exist")
10241023 if virsh("domstate", name).strip() != "shut off":
10251024 virsh("destroy", name)
1026 root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name)))
1025 root = ET.fromstring(virsh("dumpxml", name))
10271026 virsh("undefine", name, "--managed-save", *(["--nvram"] if root.find("./os/nvram") is not None else []), *(["--tpm"] if root.find("./devices/tpm") is not None else []))
10281027 directory = DISKS / name
10291028 if payload["disks"] and directory.is_dir():