diff --git a/dashboard/src/apps.rs b/dashboard/src/apps.rs index 3e8bfd27e46791523e3f11617826ace89a5de309..61fc62a4f04ec6aba7ed86649059a99fc4080736 100644 --- a/dashboard/src/apps.rs +++ b/dashboard/src/apps.rs @@ -136,14 +136,7 @@ async fn paper(app: Arc, path: &str, body: Option) -> Result let key = key .as_str() .ok_or_else(|| Error::new(502, "Paper Clover is unavailable."))?; - let url = format!( - "https://{host}/{}", - if path == "scan" { - path.into() - } else { - format!("{}/{path}", namespace::PREVIOUS.as_str()) - } - ); + let url = format!("https://{host}/{path}"); let request = if let Some(body) = body { app.http.post(&url).json(&body) } else { diff --git a/dashboard/src/core.rs b/dashboard/src/core.rs index bbd84d30fe2f4567ad50c015dfd988965f4c030a..d5fe219239a7a8beca4f095f67a5e5b5b1cf130b 100644 --- a/dashboard/src/core.rs +++ b/dashboard/src/core.rs @@ -5,12 +5,10 @@ use std::path::Path; pub async fn nomad(app: &App, path: &str) -> Result { let value = nomad_raw(app, path).await?; - let mut value = value + Ok(value .map(|bytes| serde_json::from_slice(&bytes)) .transpose()? - .unwrap_or(Value::Null); - namespace::nomad(&mut value); - Ok(value) + .unwrap_or(Value::Null)) } pub async fn nomad_raw(app: &App, path: &str) -> Result> { let request = async { diff --git a/dashboard/src/main.rs b/dashboard/src/main.rs index 3569b73fd9b2458ede76b57700d44fea601c5bd5..a1caea1d51efc463f2d5053d25be525d439728aa 100644 --- a/dashboard/src/main.rs +++ b/dashboard/src/main.rs @@ -11,7 +11,6 @@ mod guest; mod host; mod index; mod mcp; -mod namespace; mod observability; mod oidc; mod relay; diff --git a/dashboard/src/namespace-transition.json b/dashboard/src/namespace-transition.json deleted file mode 100644 index 29f654d6dfc06fcc3a03dbb40098fc8bb727e62c..0000000000000000000000000000000000000000 --- a/dashboard/src/namespace-transition.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "previous": "studio", - "previousHost": "zenith" -} diff --git a/dashboard/src/namespace.rs b/dashboard/src/namespace.rs deleted file mode 100644 index 03457f2467b745bfd5d9995b7b9afe0acb4b16be..0000000000000000000000000000000000000000 --- a/dashboard/src/namespace.rs +++ /dev/null @@ -1,87 +0,0 @@ -use crate::*; -use serde_json::Map; - -pub static PREVIOUS: std::sync::LazyLock = std::sync::LazyLock::new(|| { - serde_json::from_str::(include_str!("namespace-transition.json")).unwrap()["previous"] - .as_str().unwrap().to_owned() -}); - -fn keys(values: &mut Map, separator: &str) { - let prefix = format!("{}{separator}", PREVIOUS.as_str()); - let renamed: Vec<_> = values.keys().filter_map(|key| { - key.strip_prefix(&prefix).map(|suffix| (key.clone(), format!("snowglobe{separator}{suffix}"))) - }).collect(); - for (old, new) in renamed { - let value = values.remove(&old).unwrap(); - values.entry(new).or_insert(value); - } -} - -pub fn nomad(value: &mut Value) { - match value { - Value::Object(values) => { - if let Some(meta) = values.get_mut("Meta").and_then(Value::as_object_mut) { - keys(meta, "_"); - } - if let Some(tags) = values.get_mut("Tags").and_then(Value::as_array_mut) { - let prefix = format!("{}-metrics-path=", PREVIOUS.as_str()); - for tag in tags { - if let Some(path) = tag.as_str().and_then(|tag| tag.strip_prefix(&prefix)) { - *tag = json!(format!("snowglobe-metrics-path={path}")); - } - } - } - for value in values.values_mut() { - nomad(value); - } - } - Value::Array(values) => values.iter_mut().for_each(nomad), - _ => {} - } -} - -pub fn attributes(values: &mut Map) { - keys(values, "."); -} - -pub fn metric_selector(metric: &str, labels: &str) -> String { - let previous = metric.replacen("snowglobe_", &format!("{}_", PREVIOUS.as_str()), 1); - format!("sum without (__name__) ({metric}{labels}) or sum without (__name__) ({previous}{labels})") -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn persisted_metadata_is_read_once_with_current_values_winning() { - let old = PREVIOUS.as_str(); - let mut value = json!({"Meta": { - format!("{old}_name"): "old name", "snowglobe_name": "Snow Globe", - format!("{old}_hostname"): "knife.example.test" - }, "TaskGroups": [{"Services": [{"Tags": [format!("{old}-metrics-path=/metrics"), "caddy-host=knife.example.test"]}]}], - "Config": {"Env": {format!("{}_DOMAIN", old.to_uppercase()): "untouched"}}}); - nomad(&mut value); - assert_eq!(value["Meta"]["snowglobe_name"], "Snow Globe"); - assert_eq!(value["Meta"]["snowglobe_hostname"], "knife.example.test"); - assert!(value["Meta"].get(format!("{old}_name")).is_none()); - assert_eq!(value["TaskGroups"][0]["Services"][0]["Tags"][0], "snowglobe-metrics-path=/metrics"); - assert_eq!(value["Config"]["Env"][format!("{}_DOMAIN", old.to_uppercase())], "untouched"); - let normalized = value.clone(); - nomad(&mut value); - assert_eq!(value, normalized); - } - - #[test] - fn historical_trace_ownership_remains_explicit() { - let mut values = Map::from_iter([ - (format!("{}.service", PREVIOUS.as_str()), json!("private")), - (format!("{}.kind", PREVIOUS.as_str()), json!("edge")), - ("snowglobe.service".into(), json!("current")), - ]); - attributes(&mut values); - assert_eq!(values["snowglobe.service"], "current"); - assert_eq!(values["snowglobe.kind"], "edge"); - assert_eq!(values.len(), 2); - } -} diff --git a/dashboard/src/telemetry.rs b/dashboard/src/telemetry.rs index 5019e7a0738c53178702981a7cf75f4f588aa3c5..88b1a8376edc3b020ea9b7843fe1f61714be0edb 100644 --- a/dashboard/src/telemetry.rs +++ b/dashboard/src/telemetry.rs @@ -189,7 +189,7 @@ pub async fn metrics( quote(id) )) .unwrap_or_default(); - namespace::metric_selector(metric, &labels) + format!("{metric}{labels}") }; let key = if window.is_some() { format!("series:{selector}:fixed:{from}:{to}") @@ -228,8 +228,8 @@ pub async fn metric_names(app: Arc, id: &str) -> Result { ( "match[]", format!( - "{{{label}={},__name__!~\"(snowglobe|{})_.*\"}}", - serde_json::to_string(id).unwrap(), namespace::PREVIOUS.as_str() + "{{{label}={},__name__!~\"snowglobe_.*\"}}", + serde_json::to_string(id).unwrap() ), ) })); @@ -467,7 +467,6 @@ fn span(row: &Value) -> Value { .map(|key| (key.into(), value.clone())) }) .collect(); - namespace::attributes(&mut attributes); for (from, to) in [ ("http.request.method", "http.request.method"), ("http.response.status_code", "http.response.status_code"), diff --git a/nixos/configuration.nix b/nixos/configuration.nix index d308f68053870ceaeb89177893204725ead167f4..a0dfb3a2009d157808500d7cb480cbce7c568f71 100644 --- a/nixos/configuration.nix +++ b/nixos/configuration.nix @@ -6,7 +6,7 @@ let proxyToken = "/var/lib/snowglobe/dashboard-proxy.token"; hostTools = lib.fileset.toSource { root = ../.; - 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 ]; + 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 ]; }; nativePkl = pkgs.callPackage ./pkl.nix { }; hostPython = pkgs.callPackage ./host-python.nix { }; diff --git a/service/clover-source-of-truth/build-source.json b/service/clover-source-of-truth/build-source.json index 44d3470b8aa47ecf73920b2a354d58d3944e9397..3927a0704e2f8e1842038f063b0ff53c16b7b8a5 100644 --- a/service/clover-source-of-truth/build-source.json +++ b/service/clover-source-of-truth/build-source.json @@ -1,6 +1,6 @@ { "source": "../sitegen", - "revision": "13f64d71038f5e72ab34dc1f55805e2fee05f368", + "revision": "4964d828660c6603c009e55b41e06c56b49c4bcf", "dockerfile": "src/source-of-truth.dockerfile", "include": [ "package.json", diff --git a/service/plausible/service.pkl b/service/plausible/service.pkl index 8e129881abbac147d45af6c6e8b5be01fcba250c..53e741bbd686e473269390790448e8a9dbfd1790 100644 --- a/service/plausible/service.pkl +++ b/service/plausible/service.pkl @@ -58,8 +58,8 @@ containers { } volumes { ["/var/lib/clickhouse"] {} - ["/etc/clickhouse-server/config.d/studio.xml"] { config = "clickhouse.xml" } - ["/etc/clickhouse-server/users.d/studio.xml"] { config = "clickhouse-users.xml" } + ["/etc/clickhouse-server/config.d/snowglobe.xml"] { config = "clickhouse.xml" } + ["/etc/clickhouse-server/users.d/snowglobe.xml"] { config = "clickhouse-users.xml" } } env { ["CLICKHOUSE_SKIP_USER_SETUP"] = "1" diff --git a/tools/dashboard-deploy-test.py b/tools/dashboard-deploy-test.py index b8a3ce989880357bd1f6072049bc6c342403eae5..1b1623da0368805568bcd60cfd5e02e2dc31d363 100644 --- a/tools/dashboard-deploy-test.py +++ b/tools/dashboard-deploy-test.py @@ -170,7 +170,7 @@ 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"), patch.object( + with patch.object(release.subprocess, "run", side_effect=execute), patch.object(release, "host_configuration", return_value="snowglobe"), patch.object( Path, "read_text", autospec=True, side_effect=lambda path: os.uname().nodename + "\n" if path == Path("/etc/hostname") else read_text(path), ): diff --git a/tools/dashboard-logs-test.py b/tools/dashboard-logs-test.py index 7fc27eb85fef8dc18c6fe3a85ff2583accade19b..048c93bb2ddfc387193e7cac295e44e5f6a6285a 100644 --- a/tools/dashboard-logs-test.py +++ b/tools/dashboard-logs-test.py @@ -111,7 +111,7 @@ def main(): port = reservation.getsockname()[1] origin = f'http://127.0.0.1:{port}' environment = {**os.environ, 'PORT': str(port), 'SNOWGLOBE_DATA_DIR': str(root / 'data'), - 'SNOWGLOBE_PUBLIC_ORIGIN': 'https://globe.studio.test', 'SNOWGLOBE_NOMAD_TOKEN_FILE': str(root / 'token'), + 'SNOWGLOBE_PUBLIC_ORIGIN': 'https://globe.snowglobe.test', 'SNOWGLOBE_NOMAD_TOKEN_FILE': str(root / 'token'), 'NOMAD_ADDR': f'http://127.0.0.1:{fixture.server_port}'} environment['SNOWGLOBE_PREVIEW'] = '1' if args.preview else '0' for name in ['SNOWGLOBE_PROXY_TOKEN_FILE', 'SNOWGLOBE_INTERNAL_URL', 'SNOWGLOBE_AUTH_REQUIRED', 'SNOWGLOBE_INDEX_POOL', 'SNOWGLOBE_YT_STATE']: diff --git a/tools/dashboard-run.py b/tools/dashboard-run.py index 36ab84c9ac42f1a3645fbc318b9681626e0b16a0..7225235dfd5a535d3b6d3f88833aa56398863207 100644 --- a/tools/dashboard-run.py +++ b/tools/dashboard-run.py @@ -160,11 +160,11 @@ def command(action, target, key=None): version = release.current_release() if version is None: raise Error(409, "No release is running. Deploy a release before starting this service.") - return [sys.executable, str(release.transition.layout(release.check_release(version))["script"]), "deploy", target] + return [sys.executable, str(release.check_release(version) / "tools/snowglobe.py"), "deploy", target] if action == "rollback": saved = entry(target) version = saved["release"] - release.check_release(version, legacy=saved.get("legacy") is True) + release.check_release(version) return [sys.executable, str(Path(__file__).with_name("release.py")), "rollback", version] if action == "deploy": candidate = release.main_release() @@ -177,7 +177,7 @@ def command(action, target, key=None): if not isinstance(version, str): raise Error(409, "This stage has no release. Stage it again.") root = release.check_release(version) - return [sys.executable, str(release.transition.layout(root)["script"]), "destroy", target] + return [sys.executable, str(root / "tools/snowglobe.py"), "destroy", target] def last(): @@ -372,7 +372,7 @@ def worker(identity, action, target, key=None): try: argv = command(action, target, key) output.write(("$ " + " ".join(argv) + "\n").encode()) - environment = release.transition.environment(Path(argv[1]).parent.parent) if len(argv) > 1 and argv[1].endswith(".py") else None + environment = None if action in {"stop", "restart"}: environment = {**os.environ, "NOMAD_TOKEN": read(release.STATE / "nomad.token", missing="").strip()} if not environment["NOMAD_TOKEN"]: diff --git a/tools/dashboard-stage-test.py b/tools/dashboard-stage-test.py index 7d8532dacfdd972258b5fbb0e0dbeadae85d59ff..5c1ebacd02dcaf347e31d35a6aa03b93c1a7d6d2 100644 --- a/tools/dashboard-stage-test.py +++ b/tools/dashboard-stage-test.py @@ -98,6 +98,8 @@ class PreviewProof(unittest.TestCase): output = "" if args[0] == "pkl": output = json.dumps(site) + elif args[:3] == ("systemctl", "show", "snowglobe-host"): + output = "PATH=/bin" elif args[:4] == ("nomad", "acl", "token", "create"): output = json.dumps({"AccessorID": "fixture-accessor", "SecretID": "fixture-nomad-token"}) elif args[:2] == ("podman", "run"): @@ -113,7 +115,7 @@ class PreviewProof(unittest.TestCase): def exists(path): return str(path).startswith("/run/snowglobe-preview/") and path.name == "host.sock" or original_exists(path) - 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()): + 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()): stage.stage() check_http.assert_called_once_with(jobs[-1]["ID"] + ".fixture.test", "/sign-in", True) task = jobs[-1]["TaskGroups"][0]["Tasks"][0] diff --git a/tools/dashboard-stage.py b/tools/dashboard-stage.py index 38137db63f30e420dfda977af2f3a909e9b5500a..dbf9206b346d5d3419e54e4619c43cf53ac36887 100644 --- a/tools/dashboard-stage.py +++ b/tools/dashboard-stage.py @@ -166,10 +166,10 @@ def stage(): *["--env=" + key + "=" + value for key, value in env.items()], image, "--preview-account", capture=True) snowglobe.write_private(root / "access.json", imported.stdout) runtime = "/run/snowglobe-preview/" + identity - host_environment = snowglobe.transition.host_environment() + host_environment = snowglobe.command("systemctl", "show", "snowglobe-host", "--property=Environment", "--value", capture=True).stdout host_path = next(value for value in shlex.split(host_environment) if value.startswith("PATH=")) host_python = snowglobe.command(*nix, "build", "--no-link", "--print-out-paths", "path:" + str(snowglobe.REPO) + "#host-python", capture=True).stdout.strip() - snowglobe.transition.stop_preview(identity) + subprocess.run(["systemctl", "stop", "snowglobe-preview-" + identity], capture_output=True) snowglobe.command("systemd-run", "--collect", "--quiet", "--unit=snowglobe-preview-" + identity, "--property=Restart=on-failure", "--property=MemoryMax=256M", "--property=TasksMax=32", "--setenv=" + host_path, "--setenv=SNOWGLOBE_DOMAIN=" + site["domain"], @@ -205,7 +205,7 @@ def stage(): def destroy(identity): snowglobe.token() saved = metadata(identity) - snowglobe.transition.stop_preview(identity) + subprocess.run(["systemctl", "stop", "snowglobe-preview-" + identity], capture_output=True) if saved.get("tokenAccessor"): revoked = subprocess.run(["nomad", "acl", "token", "delete", saved["tokenAccessor"]], capture_output=True, text=True) if revoked.returncode and "400 (Cannot delete nonexistent tokens:" not in revoked.stderr: diff --git a/tools/data.py b/tools/data.py index 4016bcc137debbef9ee6e320b998838da88730d3..0fb47bb73822af74c2a3ae1c9b51232488308528 100644 --- a/tools/data.py +++ b/tools/data.py @@ -4,7 +4,6 @@ from contextlib import closing, contextmanager from datetime import datetime, timezone import hashlib import json -from importlib import import_module import os from pathlib import Path import re @@ -15,8 +14,6 @@ import subprocess import time -transition = import_module("namespace-transition") - ROOT = Path("/opt/snowglobe") STATE = Path("/var/lib/snowglobe") BACKUPS = STATE / "backups" @@ -265,8 +262,7 @@ def restore(backup_id, service): path = source / name if not path.resolve().is_relative_to(source.resolve()) or not path.is_file() or checksum(path) != digest: raise ValueError("Dashboard backup is missing or changed") - unit = transition.layout(current_release())["prefix"] + "-dashboard" - run("systemctl", "stop", unit) + run("systemctl", "stop", "snowglobe-dashboard") safety = BACKUPS / ("before-restore-" + datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ") + "-" + secrets.token_hex(3)) safety.mkdir(mode=0o700) files = backup_dashboard(safety / "dashboard") @@ -277,8 +273,8 @@ def restore(backup_id, service): if (root / "pictures").exists(): shutil.rmtree(root / "pictures") shutil.copytree(source, root, dirs_exist_ok=True) - run("systemctl", "start", unit) - run("systemctl", "is-active", "--quiet", unit) + run("systemctl", "start", "snowglobe-dashboard") + run("systemctl", "is-active", "--quiet", "snowglobe-dashboard") print(f"restored=dashboard backup={backup_id} safety={safety.name}") return dataset = entry.get("dataset") @@ -323,9 +319,9 @@ def restore(backup_id, service): with (directory / database["dump"]).open("rb") as input_file: run(*PODMAN, "exec", "-i", container, "pg_restore", "-U", "postgres", "-d", name, "--no-owner", "--no-acl", "--role=" + owner, stdin=input_file) - script = transition.layout(current_release())["script"] - run("python3", str(script), "deploy", service, env=transition.environment(current_release())) - run("python3", str(script), "check", service, env=transition.environment(current_release())) + script = current_release() / "tools/snowglobe.py" + run("python3", str(script), "deploy", service) + run("python3", str(script), "check", service) print(f"restored={service} backup={backup_id} safety={safety}") diff --git a/tools/deploy.py b/tools/deploy.py index b36cd8cac55f94a4c74ad81a971e4f4d1c0656f5..86a938988162a4e630bcc1e9ff74eb2a974cbbc1 100644 --- a/tools/deploy.py +++ b/tools/deploy.py @@ -38,18 +38,9 @@ def run_logged(mode, target, command): print(f"run={run}") -def prepare_remote(): - subprocess.run( - ["ssh", "-p", PORT, "-o", "BatchMode=yes", HOST, "python3 - prepare"], - input="NAMESPACE = " + repr(json.loads((REPO / "dashboard/src/namespace-transition.json").read_text())) + "\n" - + (REPO / "tools/namespace-transition.py").read_text(), text=True, check=True, - ) - - def sync_manager(release): source = REMOTE / "releases" / release / "tools" - ssh(f"cp {source}/release.py {source}/data.py {source}/namespace-transition.py {REMOTE}/") - ssh(f"cp {source}/../dashboard/src/namespace-transition.json {REMOTE}/namespace-transition.json") + ssh(f"cp {source}/release.py {source}/data.py {REMOTE}/") def export_revision(source, commit, paths, destination): @@ -207,7 +198,6 @@ def main(): values = args.file.read_text() if not values.strip(): raise ValueError("secret input is empty") - prepare_remote() manager = f"python3 {REMOTE}/release.py" if args.mode == "stage": release = upload() diff --git a/tools/namespace-transition-test.py b/tools/namespace-transition-test.py deleted file mode 100644 index f2a1d04277c9c8ee86b2436bee46f4a9ab39e482..0000000000000000000000000000000000000000 --- a/tools/namespace-transition-test.py +++ /dev/null @@ -1,342 +0,0 @@ -#!/usr/bin/env python3 -from importlib import import_module -import io -import json -import os -from pathlib import Path -import shutil -import subprocess -import sys -import tempfile -from types import SimpleNamespace -import unittest -from unittest.mock import patch -import xml.etree.ElementTree as ET - -import release -import snowglobe - - -transition = import_module("namespace-transition") - - -class NamespaceTransition(unittest.TestCase): - def setUp(self): - temporary = tempfile.TemporaryDirectory() - self.addCleanup(temporary.cleanup) - self.root = Path(temporary.name).resolve() - self.directories = tuple((self.root / parent / "snowglobe", self.root / parent / transition.PREVIOUS) - for parent in ("opt", "state", "assets", "cache")) - self.addCleanup(patch.stopall) - patch.object(transition, "DIRECTORIES", self.directories).start() - registry = self.root / "nixos" - registry.mkdir() - patch.object(transition, "NIXOS_STATE", registry).start() - patch.object(transition.pwd, "getpwnam", side_effect=KeyError).start() - patch.object(transition.grp, "getgrnam", side_effect=KeyError).start() - - def test_staging_aliases_share_existing_state_and_never_seed_another_registry(self): - for _, old in self.directories: - old.mkdir(parents=True) - new, old = self.directories[1] - (old / "identities.json").write_text('{"knife":3132}') - transition.prepare() - transition.prepare() - self.assertTrue(new.is_symlink()) - self.assertEqual(new.resolve(), old) - (new / "identities.json").write_text('{"knife":3132,"dashboard":3133}') - self.assertEqual(json.loads((old / "identities.json").read_text()), {"knife": 3132, "dashboard": 3133}) - self.assertTrue(all(new.resolve() == old for new, old in self.directories)) - - def test_staging_bootstrap_starts_the_installed_router_generation_without_restart(self): - state = self.root / "bootstrap" - state.mkdir() - for name in ("nomad", "router", "dashboard"): - (state / (name + ".token")).write_text("disposable-" + name) - original = transition.layout - source = self.root / "installed" - (source / "tools").mkdir(parents=True) - (source / "tools" / (transition.PREVIOUS + ".py")).touch() - with patch.object(snowglobe, "STATE", state), patch.object(snowglobe, "token"), patch.object( - snowglobe.os, "geteuid", return_value=0, - ), patch.object(Path, "is_symlink", return_value=True), patch.object( - transition, "layout", return_value=original(source), - ), patch.object(snowglobe, "command", return_value=subprocess.CompletedProcess([], 0)) as command: - snowglobe.bootstrap([]) - units = [call.args for call in command.call_args_list if call.args[0] == "systemctl"] - self.assertEqual(units, [("systemctl", "start", transition.PREVIOUS + "-router.service")]) - - def test_split_state_is_rejected_before_any_alias_is_created(self): - self.directories[0][1].mkdir(parents=True) - new, old = self.directories[1] - new.mkdir(parents=True) - old.mkdir() - with self.assertRaisesRegex(ValueError, "independent control stores"): - transition.prepare() - self.assertFalse(self.directories[0][0].exists()) - - def test_promotion_preserves_whole_stores_and_release_links_then_is_idempotent(self): - revision = "1" * 16 - for _, old in self.directories: - old.mkdir(parents=True) - (old / "durable").write_text("existing bytes") - (old / "durable").chmod(0o600) - new_root, old_root = self.directories[0] - (old_root / "releases" / revision).mkdir(parents=True) - for name in ("current", "main"): - (old_root / name).symlink_to(old_root / "releases" / revision) - before = [(old / "durable").stat().st_ino for _, old in self.directories] - with patch.object(transition.subprocess, "run", return_value=subprocess.CompletedProcess([], 0)) as run, patch.object( - transition, "migrate_policies", - ) as policies: - transition.promote(self.root) - policies.assert_called_once_with(self.root, self.directories[1][0]) - self.assertEqual(run.call_count, 6) - transition.promote(self.root) - self.assertEqual(run.call_count, 6) - for index, (new, old) in enumerate(self.directories): - self.assertFalse(new.is_symlink()) - self.assertTrue(old.is_symlink()) - self.assertEqual((new / "durable").stat().st_ino, before[index]) - self.assertEqual((new / "durable").stat().st_mode & 0o777, 0o600) - for name in ("current", "main"): - self.assertEqual(os.readlink(new_root / name), str(new_root / "releases" / revision)) - - def test_old_release_manifest_and_canonical_alias_validate_without_rewriting_the_tree(self): - new_root, old_root = self.directories[0] - revision = "a" * 16 - source = old_root / "releases" / revision - (source / "tools").mkdir(parents=True) - (source / "tools" / (transition.PREVIOUS + ".py")).write_text("immutable") - manifest = source / ("." + transition.PREVIOUS + "-release.json") - manifest.write_text(json.dumps({"id": revision, "digest": release.tree_digest(source), "version": 2})) - original = manifest.read_bytes() - (old_root / "current").symlink_to(source) - transition.prepare() - with patch.object(release, "ROOT", new_root), patch.object(release, "RELEASES", new_root / "releases"): - self.assertEqual(release.current_release(), revision) - self.assertEqual(release.check_release(revision).resolve(), source) - self.assertEqual(manifest.read_bytes(), original) - - def test_old_release_activation_uses_its_units_script_and_environment(self): - revision = "b" * 16 - source = self.root / "release" - (source / "tools").mkdir(parents=True) - (source / "tools" / (transition.PREVIOUS + ".py")).touch() - (source / "nixos").mkdir() - (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)) - return subprocess.CompletedProcess(argv, 0, '"leader"') - - with patch.dict(os.environ, {"SNOWGLOBE_POOL": "globe"}), patch.object(release, "ROOT", self.root), patch.object( - release, "STATE", self.root, - ), patch.object(release, "check_release", return_value=source), patch.object( - release, "current_release", return_value=None, - ), patch.object(release, "host_digest", return_value="different"), patch.object( - 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( - 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"] - self.assertEqual([argv[-1] for argv, _ in scripts], ["preflight", "pool", "deploy", "check"]) - self.assertTrue(all(argv[1].endswith("/" + transition.PREVIOUS + ".py") for argv, _ in scripts)) - self.assertTrue(all(kwargs["env"][transition.PREVIOUS.upper() + "_POOL"] == "globe" for _, kwargs in scripts)) - self.assertIn(["systemctl", "restart", transition.PREVIOUS + "-router.service"], [argv for argv, _ in observed]) - - def test_policy_attachment_changes_after_new_policy_exists_without_changing_token_files(self): - state = self.root / "state" - state.mkdir() - for name in ("nomad", "router", "dashboard"): - (state / (name + ".token")).write_text("disposable-" + name) - responses = [io.BytesIO(json.dumps({"Name": transition.PREVIOUS + "-" + consumer, - "AccessorID": consumer, "Policies": [transition.PREVIOUS + "-" + consumer, "other"]}).encode()) - for consumer in ("router", "dashboard")] - with patch.object(transition.urllib.request, "urlopen", side_effect=responses), patch.object( - transition.subprocess, "run", return_value=subprocess.CompletedProcess([], 0), - ) as run: - transition.migrate_policies(self.root, state) - commands = [call.args[0] for call in run.call_args_list] - for index, consumer in enumerate(("router", "dashboard")): - self.assertEqual(commands[index * 2][:5], ["nomad", "acl", "policy", "apply", "snowglobe-" + consumer]) - self.assertIn("-policy=snowglobe-" + consumer, commands[index * 2 + 1]) - self.assertIn("-policy=other", commands[index * 2 + 1]) - self.assertFalse(any("disposable" in argument for argument in commands[index * 2 + 1])) - self.assertEqual((state / (consumer + ".token")).read_text(), "disposable-" + consumer) - - def test_existing_vm_owner_profile_and_seed_still_resolve(self): - old = "https://paperclover.net/" + transition.PREVIOUS - root = ET.fromstring(f'owner-idwindows-q35-secure{transition.PREVIOUS}-cloud-init') - transition.normalize_vm(root) - namespace = "{https://paperclover.net/snowglobe}" - self.assertEqual(root.findtext(f"metadata/{namespace}vm/{namespace}owner"), "owner-id") - self.assertEqual(root.findtext(f"metadata/{namespace}vm/{namespace}hardwareProfile"), "windows-q35-secure") - self.assertEqual(root.findtext("devices/disk/serial"), "snowglobe-cloud-init") - - def test_account_and_nixos_registries_round_trip_with_fixed_numeric_ownership(self): - old, new = transition.PREVIOUS + "-dashboard", "snowglobe-dashboard" - users = {old: SimpleNamespace(pw_uid=997, pw_gid=994)} - groups = {old: SimpleNamespace(gr_gid=994)} - registry = transition.NIXOS_STATE - (registry / "uid-map").write_text(json.dumps({old: 997, "other": 888})) - (registry / "gid-map").write_text(json.dumps({old: 994})) - for name in ("declarative-users", "declarative-groups"): - (registry / name).write_text("root " + old + "\n") - - def lookup(values, name): - if name not in values: - raise KeyError(name) - return values[name] - - calls = [] - - def command(argv, **kwargs): - calls.append(argv) - if argv[0] == "groupmod": - groups[argv[2]] = groups.pop(argv[3]) - if argv[0] == "usermod": - users[argv[2]] = users.pop(argv[3]) - return subprocess.CompletedProcess(argv, 0) - - for _, directory in self.directories: - directory.mkdir(parents=True) - release_path = self.directories[0][1] / "releases" / ("c" * 16) - (release_path / "tools").mkdir(parents=True) - (release_path / "tools/snowglobe.py").touch() - transition.prepare() - canonical_release = self.directories[0][0] / "releases" / release_path.name - with patch.object(transition, "NIXOS_STATE", registry), patch.object( - transition.pwd, "getpwnam", side_effect=lambda name: lookup(users, name), - ), patch.object(transition.grp, "getgrnam", side_effect=lambda name: lookup(groups, name)), patch.object( - transition.subprocess, "run", side_effect=command, - ), patch.object(transition, "migrate_policies"): - transition.activate(canonical_release) - self.assertEqual(set(users), {new}) - self.assertEqual((users[new].pw_uid, users[new].pw_gid, groups[new].gr_gid), (997, 994, 994)) - self.assertEqual(json.loads((registry / "uid-map").read_text()), {new: 997, "other": 888}) - self.assertEqual((registry / "declarative-users").read_text().split(), ["root", new]) - previous_release = self.directories[0][0] / "releases" / ("d" * 16) - (previous_release / "tools").mkdir(parents=True) - (previous_release / "tools" / (transition.PREVIOUS + ".py")).touch() - transition.activate(previous_release) - self.assertEqual(set(users), {old}) - self.assertEqual((users[old].pw_uid, users[old].pw_gid, groups[old].gr_gid), (997, 994, 994)) - self.assertEqual(json.loads((registry / "uid-map").read_text()), {old: 997, "other": 888}) - self.assertEqual((registry / "declarative-users").read_text().split(), ["root", old]) - self.assertEqual(calls[0], ["systemctl", "stop", old]) - self.assertEqual(calls[8], ["systemctl", "stop", new]) - - def test_policy_failure_after_directory_move_is_retried_without_moving_state_again(self): - for _, old in self.directories: - old.mkdir(parents=True) - with patch.object(transition.subprocess, "run", return_value=subprocess.CompletedProcess([], 0)) as run, patch.object( - transition, "migrate_policies", side_effect=[RuntimeError("interrupted ACL update"), None], - ) as policies: - with self.assertRaisesRegex(RuntimeError, "ACL update"): - transition.promote(self.root) - before = [new.stat().st_ino for new, _ in self.directories] - transition.promote(self.root) - self.assertEqual(policies.call_count, 2) - self.assertEqual(run.call_count, 6) - self.assertEqual(before, [new.stat().st_ino for new, _ in self.directories]) - - def test_only_legacy_transition_stops_the_retired_window_proxy(self): - with patch.object(transition.subprocess, "run", return_value=subprocess.CompletedProcess([], 0)) as run: - transition.stop_units(transition.PREVIOUS) - previous = [call.args[0] for call in run.call_args_list] - run.reset_mock() - transition.stop_units("snowglobe") - canonical = [call.args[0] for call in run.call_args_list] - self.assertIn(["systemctl", "stop", transition.PREVIOUS + "-vm-window-host-proxy"], previous) - self.assertFalse(any("vm-window-host-proxy" in command[-1] for command in canonical)) - self.assertTrue(all(command[:2] == ["systemctl", "stop"] for command in previous + canonical)) - - def test_missing_reverse_alias_after_directory_move_is_recovered_from_old_releases(self): - new, old = self.directories[0] - source = new / "releases" / ("e" * 16) / "tools" - source.mkdir(parents=True) - (source / (transition.PREVIOUS + ".py")).touch() - transition.prepare() - self.assertTrue(old.is_symlink()) - self.assertEqual(old.resolve(), new) - - @unittest.skipUnless(sys.platform == "linux" and os.geteuid() == 0 and shutil.which("usermod") and shutil.which("groupmod"), - "requires Linux root and shadow tools; all account files stay inside a temporary prefix") - def test_real_shadow_tools_and_interrupted_registry_update_resume_with_only_fixture_accounts(self): - old, new = transition.PREVIOUS + "-dashboard", "snowglobe-dashboard" - prefix = self.root / "accounts" - etc = prefix / "etc" - etc.mkdir(parents=True) - (etc / "passwd").write_text(f"{old}:x:997:994:fixture:/var/empty:/bin/false\n") - (etc / "group").write_text(f"{old}:x:994:\n") - (etc / "shadow").write_text(f"{old}:!:1:0:99999:7:::\n") - (etc / "gshadow").write_text(f"{old}:!::\n") - for name in ("shadow", "gshadow"): - (etc / name).chmod(0o600) - (etc / "login.defs").write_text("") - registry = transition.NIXOS_STATE - for name, value in (("uid-map", 997), ("gid-map", 994)): - (registry / name).write_text(json.dumps({old: value})) - for name in ("declarative-users", "declarative-groups"): - (registry / name).write_text(old + "\n") - command = subprocess.run - replace = Path.replace - fail_login = True - fail_registry = True - - def run(argv, **kwargs): - nonlocal fail_login - self.assertIn(argv[0], {"usermod", "groupmod"}) - if argv[0] == "usermod" and fail_login: - fail_login = False - raise subprocess.CalledProcessError(1, argv) - return command([argv[0], "--prefix", str(prefix), *argv[1:]], **kwargs) - - def atomic_replace(path, target): - nonlocal fail_registry - if Path(target) == registry / "gid-map" and fail_registry: - fail_registry = False - raise OSError("interrupted registry write") - return replace(path, target) - - def user(name): - fields = (etc / "passwd").read_text().splitlines()[0].split(":") - if fields[0] != name: - raise KeyError(name) - return SimpleNamespace(pw_uid=int(fields[2]), pw_gid=int(fields[3])) - - def group(name): - fields = (etc / "group").read_text().splitlines()[0].split(":") - if fields[0] != name: - raise KeyError(name) - return SimpleNamespace(gr_gid=int(fields[2])) - - with patch.object(transition.pwd, "getpwnam", side_effect=user), patch.object( - transition.grp, "getgrnam", side_effect=group, - ), patch.object(transition.subprocess, "run", side_effect=run), patch.object(Path, "replace", atomic_replace): - with self.assertRaises(subprocess.CalledProcessError): - transition.rename_identity(transition.identity_change("snowglobe")) - self.assertEqual(group(new).gr_gid, 994) - self.assertEqual(user(old).pw_uid, 997) - with self.assertRaisesRegex(OSError, "registry write"): - transition.rename_identity(transition.identity_change("snowglobe")) - self.assertEqual(user(new).pw_uid, 997) - transition.rename_identity(transition.identity_change("snowglobe")) - self.assertIsNone(transition.identity_change("snowglobe")) - self.assertEqual(json.loads((registry / "gid-map").read_text()), {new: 994}) - transition.rename_identity(transition.identity_change(transition.PREVIOUS)) - self.assertEqual((user(old).pw_uid, user(old).pw_gid, group(old).gr_gid), (997, 994, 994)) - self.assertIsNone(transition.identity_change(transition.PREVIOUS)) - - -if __name__ == "__main__": - unittest.main() diff --git a/tools/namespace-transition.py b/tools/namespace-transition.py deleted file mode 100644 index 8f5ca4de89f108e68d57cb48485c86618da7e5d5..0000000000000000000000000000000000000000 --- a/tools/namespace-transition.py +++ /dev/null @@ -1,245 +0,0 @@ -#!/usr/bin/env python3 -"""Bridge the installed namespace and immutable releases during the rename.""" -import argparse -import grp -import json -import os -from pathlib import Path -import pwd -import subprocess -import urllib.request - - -NAMESPACE = globals().get("NAMESPACE") or json.loads( - (Path(__file__).with_suffix(".json") if Path(__file__).with_suffix(".json").exists() - else Path(__file__).resolve().parent.parent / "dashboard/src/namespace-transition.json").read_text() -) -PREVIOUS = NAMESPACE["previous"] -NIXOS_STATE = Path("/var/lib/nixos") -DIRECTORIES = tuple((Path(parent) / "snowglobe", Path(parent) / PREVIOUS) - for parent in ("/opt", "/var/lib", "/var/lib/caddy", "/var/cache")) - - -def prepare(): - for new, old in DIRECTORIES: - if new.exists() and old.exists() and new.resolve() != old.resolve(): - raise ValueError(f"Two independent control stores exist: {new}, {old}") - new_root, _ = DIRECTORIES[0] - previous_releases = any((new_root / "releases").glob("*/tools/" + PREVIOUS + ".py")) - for new, old in DIRECTORIES: - if old.exists() and not new.exists() and not new.is_symlink(): - new.symlink_to(old, target_is_directory=True) - elif previous_releases and new.exists() and not old.exists() and not old.is_symlink(): - old.symlink_to(new, target_is_directory=True) - - -def inherit_environment(): - for name, value in list(os.environ.items()): - if name.startswith(PREVIOUS.upper() + "_"): - os.environ.setdefault("SNOWGLOBE_" + name.removeprefix(PREVIOUS.upper() + "_"), value) - - -def environment(path, values=None): - values = dict(os.environ if values is None else values) - if layout(path)["prefix"] == PREVIOUS: - for name, value in list(values.items()): - if name.startswith("SNOWGLOBE_"): - values[PREVIOUS.upper() + "_" + name.removeprefix("SNOWGLOBE_")] = value - return values - - -def layout(path): - prefix = "snowglobe" if (path / ".snowglobe-release.json").exists() or (path / "tools/snowglobe.py").exists() else PREVIOUS - return {"prefix": prefix, "script": path / f"tools/{prefix}.py", "manifest": path / f".{prefix}-release.json"} - - -def host_configuration(hostname, available): - hostname = hostname.split(".", 1)[0] - candidates = ("snowglobe", NAMESPACE["previousHost"]) if hostname in {"snowglobe", NAMESPACE["previousHost"]} else ( - "vm" if hostname in {"snowglobe-rehearsal", "clover-demo"} else hostname, - ) - for name in candidates: - if name in available: - return name - raise ValueError(f"release has no NixOS configuration for {hostname}") - - -def host_environment(): - for prefix in ("snowglobe", PREVIOUS): - result = subprocess.run(["systemctl", "show", prefix + "-host", "--property=Environment", "--value"], - capture_output=True, text=True) - if result.stdout.strip(): - return result.stdout - raise ValueError("the host service environment is unavailable") - - -def stop_preview(identity): - for prefix in ("snowglobe", PREVIOUS): - subprocess.run(["systemctl", "stop", prefix + "-preview-" + identity], capture_output=True) - - -def dashboard_container(): - for name in ("snowglobe-dashboard", PREVIOUS + "-dashboard"): - if subprocess.run(["podman", "container", "exists", name], capture_output=True).returncode == 0: - return name - raise ValueError("the dashboard container is unavailable") - - -def normalize_vm(root): - for element in root.iter(): - if element.tag.startswith("{https://paperclover.net/" + PREVIOUS + "}"): - element.tag = element.tag.replace("{https://paperclover.net/" + PREVIOUS + "}", "{https://paperclover.net/snowglobe}", 1) - if element.tag == "serial" and element.text == PREVIOUS + "-cloud-init": - element.text = "snowglobe-cloud-init" - return root - - -def migrate_policies(source, state, prefix="snowglobe"): - management = {**os.environ, "NOMAD_TOKEN": (state / "nomad.token").read_text().strip()} - for consumer in ("router", "dashboard"): - request = urllib.request.Request("http://127.0.0.1:4646/v1/acl/token/self", headers={ - "X-Nomad-Token": (state / (consumer + ".token")).read_text().strip(), - }) - with urllib.request.urlopen(request, timeout=10) as response: - token = json.load(response) - old, new = (PREVIOUS if prefix == "snowglobe" else "snowglobe") + "-" + consumer, prefix + "-" + consumer - subprocess.run(["nomad", "acl", "policy", "apply", new, str(source / "config/policies" / (consumer + ".hcl"))], - env=management, check=True, capture_output=True) - policies = [new if value == old else value for value in token.get("Policies", [])] - if old in token.get("Policies", []) or token.get("Name") == old: - subprocess.run(["nomad", "acl", "token", "update", "-name=" + new, - *("-policy=" + value for value in policies), token["AccessorID"]], - env=management, check=True, capture_output=True) - - -def stop_units(prefix): - units = ("dashboard", "router", "log-shipper", "vm-screen", "host") - if prefix == PREVIOUS: - units += ("vm-window-host-proxy",) - for unit in units: - result = subprocess.run(["systemctl", "stop", prefix + "-" + unit], capture_output=True, text=True) - if result.returncode and "not loaded" not in result.stderr: - result.check_returncode() - - -def identity_change(prefix): - old = (PREVIOUS if prefix == "snowglobe" else "snowglobe") + "-dashboard" - new = prefix + "-dashboard" - users, groups = {}, {} - for name in (old, new): - try: - users[name] = pwd.getpwnam(name) - except KeyError: - pass - try: - groups[name] = grp.getgrnam(name) - except KeyError: - pass - if len(users) > 1 or len(groups) > 1: - raise ValueError("Two dashboard identities exist; refusing to merge identities") - if any((user.pw_uid, user.pw_gid) != (997, 994) for user in users.values()) or any( - group.gr_gid != 994 for group in groups.values() - ): - raise ValueError("The dashboard account must retain UID 997 and GID 994") - registries = False - for filename, expected in (("uid-map", 997), ("gid-map", 994)): - path = NIXOS_STATE / filename - if path.exists(): - values = json.loads(path.read_text()) - if any(name in values and values[name] != expected for name in (old, new)): - raise ValueError("The NixOS identity registry disagrees with the dashboard account") - registries |= old in values - for filename in ("declarative-users", "declarative-groups"): - path = NIXOS_STATE / filename - registries |= path.exists() and old in path.read_text().split() - return (old, new) if old in users or old in groups or registries else None - - -def rename_identity(change): - if change is None: - return - old, new = change - try: - grp.getgrnam(old) - except KeyError: - pass - else: - subprocess.run(["groupmod", "--new-name", new, old], check=True, capture_output=True) - try: - pwd.getpwnam(old) - except KeyError: - pass - else: - subprocess.run(["usermod", "--login", new, old], check=True, capture_output=True) - for filename in ("uid-map", "gid-map", "declarative-users", "declarative-groups"): - path = NIXOS_STATE / filename - if not path.exists(): - continue - if filename.endswith("-map"): - values = json.loads(path.read_text()) - if old not in values: - continue - values[new] = values.pop(old) - content = json.dumps(values) + "\n" - else: - names = path.read_text().split() - if old not in names: - continue - content = " ".join(new if name == old else name for name in names) + "\n" - pending = path.with_suffix(".namespace-pending") - pending.write_text(content) - pending.chmod(path.stat().st_mode & 0o777) - pending.replace(path) - - -def promote(source): - prepare() - change = identity_change("snowglobe") - moving = any(old.exists() and not old.is_symlink() for _, old in DIRECTORIES) - if moving or change is not None: - stop_units(PREVIOUS) - rename_identity(change) - for new, old in DIRECTORIES: - if old.exists() and not old.is_symlink(): - if new.is_symlink(): - new.unlink() - old.rename(new) - old.symlink_to(new, target_is_directory=True) - new_root, old_root = DIRECTORIES[0] - for name in ("current", "main"): - link = new_root / name - if link.is_symlink(): - target = Path(os.readlink(link)) - if target.is_relative_to(old_root): - pending = link.with_suffix(".namespace-pending") - pending.unlink(missing_ok=True) - pending.symlink_to(new_root / target.relative_to(old_root)) - pending.replace(link) - if old_root.exists(): - migrate_policies(source, DIRECTORIES[1][0]) - - -def activate(path): - if layout(path)["prefix"] == "snowglobe": - promote(path) - else: - change = identity_change(PREVIOUS) - stop_units("snowglobe") - rename_identity(change) - migrate_policies(path, DIRECTORIES[1][0], PREVIOUS) - - -def main(): - parser = argparse.ArgumentParser() - parser.add_argument("mode", choices=("prepare", "promote")) - args = parser.parse_args() - if os.geteuid() != 0: - raise PermissionError("the namespace transition requires root") - if args.mode == "prepare": - prepare() - else: - promote(Path(__file__).resolve().parent.parent) - - -if __name__ == "__main__": - main() diff --git a/tools/oidc-provider.py b/tools/oidc-provider.py index 3d36374c4e925d841ba9a08db50ed519454573ab..59e0f4df43055627a2af2110e074b6c7592d39c2 100644 --- a/tools/oidc-provider.py +++ b/tools/oidc-provider.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 """Provision a first-party OIDC client through the dashboard's host-only CLI.""" import json -from importlib import import_module import subprocess import sys @@ -9,7 +8,7 @@ data = json.load(sys.stdin) if data["request"]["kind"] != "client": raise ValueError("unsupported Snowglobe input") result = subprocess.run( - ["podman", "exec", "-i", import_module("namespace-transition").dashboard_container(), "/bin/home-dashboard", "--oidc-client"], + ["podman", "exec", "-i", "snowglobe-dashboard", "/bin/home-dashboard", "--oidc-client"], input=json.dumps(data), text=True, check=True, capture_output=True, ) sys.stdout.write(result.stdout) diff --git a/tools/owl-neuron/README.md b/tools/owl-neuron/README.md index 2927b0311e33f8dd8d860e69d487bf5fb285e0c7..71e1268b15c1b702b5f3b5782427771f7ce76002 100644 --- a/tools/owl-neuron/README.md +++ b/tools/owl-neuron/README.md @@ -1,6 +1,6 @@ # Owl neuron experiment -Disposable experiment on Zenith (`root@10.0.0.1:22`), authorized on 2026-10-18 +Disposable experiment on Snow Globe (`root@10.0.0.1:22`), authorized on 2026-10-18 with inference downtime. Source and run state live at `/srv/staging/owl-neuron-20261018`; the existing model weights are mounted read-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 without changing the overlay. Deployment history and its pre-switch backup are recorded by the standard release workflow. -The checkout's `deploy.py stage` stopped before startup: Zenith uses the renamed -Snow Globe units and environment, while this checkout still targets Studio. -The installed `/opt/studio/current/tools/snowglobe.py` staging engine is therefore -loaded with `REPO` set to the candidate release's resolved path and `SERVICES` -set to its `service` directory. It runs the normal stage command with -`--pool globe --base-domain paperclover.net` and `STUDIO_LOCAL_AI=true`. -Use that same installed engine and candidate paths for `destroy `; -the checkout's destroy command targets the missing `studio-router.service`. +Use the current checkout's deployment tooling for staging and destruction. Only after preview destruction and GPU release should production count 1 return. The same launcher accepts `--program train.py -- ` or diff --git a/tools/plausible.md b/tools/plausible.md index 0dd8ac1e6b0b52ac3b9165ba8f8acff2a62293d0..48142cc58ee365a6ab128516077056e5d8205554 100644 --- a/tools/plausible.md +++ b/tools/plausible.md @@ -78,7 +78,7 @@ native events. Cloud exports also exclude earlier third-party imports. ## Rehearsal evidence -On 2026-10-19 UTC, release `12a2725e1a1372ce` passed on Zenith at +On 2026-10-19 UTC, release `12a2725e1a1372ce` passed on Snow Globe at `https://plausible-preview-3234cb91.paperclover.net`. Owner login, disabled registration, private dashboards, the CSV upload, keyboard filters, mobile, both themes, and installation instructions were checked in a real browser. diff --git a/tools/release-host-test.py b/tools/release-host-test.py index b3286702232eb99e69d549cc8d5994efa77c2223..67c6112c85f23a01f793975aee90ee17f48bf3c8 100644 --- a/tools/release-host-test.py +++ b/tools/release-host-test.py @@ -10,8 +10,6 @@ from unittest.mock import patch import release -PREVIOUS_HOST = release.transition.NAMESPACE["previousHost"] - class HostConfiguration(unittest.TestCase): def select(self, hostname, configurations): @@ -25,11 +23,10 @@ class HostConfiguration(unittest.TestCase): ) return result - def test_rename_and_rollback(self): - for hostname in (PREVIOUS_HOST, "snowglobe", "snowglobe.paperclover.net"): + def test_canonical_hostname(self): + for hostname in ("snowglobe", "snowglobe.paperclover.net"): with self.subTest(hostname=hostname): self.assertEqual(self.select(hostname, ["installer", "snowglobe", "vm"]), "snowglobe") - self.assertEqual(self.select(hostname, ["installer", PREVIOUS_HOST, "vm"]), PREVIOUS_HOST) def test_rehearsal_stays_on_vm(self): self.assertEqual(self.select("snowglobe-rehearsal", ["installer", "snowglobe", "vm"]), "vm") @@ -42,55 +39,11 @@ class HostConfiguration(unittest.TestCase): class ActivationEnvironment(unittest.TestCase): - def test_flake_paths_are_resolved_again_after_the_release_store_moves(self): - with tempfile.TemporaryDirectory() as temporary: - root = Path(temporary).resolve() - previous = root / release.transition.PREVIOUS - canonical = root / "snowglobe" - source = previous / "releases" / ("a" * 16) - (source / "tools").mkdir(parents=True) - (source / "tools/snowglobe.py").touch() - (source / ".snowglobe-release.json").write_text('{"version":1}') - 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) - canonical.unlink() - previous.rename(canonical) - previous.symlink_to(canonical, target_is_directory=True) - - def run(argv, **kwargs): - if argv[0] in {"nix", "nixos-rebuild"}: - observed.append(argv) - output = '["snowglobe"]' if argv[0] == "nix" else '"leader"' - return subprocess.CompletedProcess(argv, 0, output) - - with patch.dict(os.environ), patch.object(release, "ROOT", canonical), patch.object(release, "STATE", root), patch.object( - release, "check_release", return_value=logical_source, - ), patch.object(release, "current_release", return_value=None), patch.object( - release, "host_digest", return_value="new", - ), 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( - 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]) - self.assertEqual(observed[1], ["nixos-rebuild", "dry-build", "--flake", f"path:{source}#snowglobe"]) - self.assertEqual(observed[2], ["nixos-rebuild", "switch", "--flake", f"path:{logical_source}#snowglobe"]) - def test_host_switch_refreshes_inherited_name_and_preserves_overrides(self): for before, after, inherited, expected in ( - (PREVIOUS_HOST, "snowglobe", PREVIOUS_HOST, "snowglobe"), - ("snowglobe", PREVIOUS_HOST, "snowglobe", PREVIOUS_HOST), - (PREVIOUS_HOST, "snowglobe", None, "snowglobe"), - (PREVIOUS_HOST, "snowglobe", "test-override", "test-override"), + ("fixture", "snowglobe", "fixture", "snowglobe"), + ("fixture", "snowglobe", None, "snowglobe"), + ("fixture", "snowglobe", "test-override", "test-override"), ("snowglobe-rehearsal", "snowglobe-rehearsal", "snowglobe-rehearsal", "snowglobe-rehearsal"), ): with self.subTest(before=before, inherited=inherited), tempfile.TemporaryDirectory() as temporary: @@ -98,9 +51,8 @@ class ActivationEnvironment(unittest.TestCase): source = root / "release" source.mkdir() (source / "tools").mkdir() - prefix = release.transition.PREVIOUS if after == PREVIOUS_HOST else "snowglobe" - (source / "tools" / (prefix + ".py")).touch() - (source / ("." + prefix + "-release.json")).write_text('{"version":1}') + (source / "tools/snowglobe.py").touch() + (source / ".snowglobe-release.json").write_text('{"version":1}') hostname = before observed = [] calls = [] @@ -112,8 +64,7 @@ class ActivationEnvironment(unittest.TestCase): if argv[:3] == ["hostnamectl", "set-hostname", "--transient"]: hostname = after if argv[-1] == "deploy": - key = prefix.upper() + "_NODE_NAME" - observed.append(kwargs["env"].get(key)) + observed.append(kwargs["env"].get("SNOWGLOBE_NODE_NAME")) return subprocess.CompletedProcess(argv, 0, '"leader"') with patch.dict(os.environ), patch.object(release, "ROOT", root), patch.object( @@ -129,7 +80,7 @@ class ActivationEnvironment(unittest.TestCase): os.environ.pop("SNOWGLOBE_NODE_NAME", None) else: os.environ["SNOWGLOBE_NODE_NAME"] = inherited - with patch.object(release.transition, "activate"), patch.object( + with patch.object( Path, "read_text", autospec=True, side_effect=lambda path: after + "\n" if path == Path("/etc/hostname") else read_text(path), ): diff --git a/tools/release.py b/tools/release.py index fc6d8317fedda2044cbcf6e9deb4ecb2064180a5..5e85befaeacfe08046e1a0a024720473a6689269 100644 --- a/tools/release.py +++ b/tools/release.py @@ -4,7 +4,6 @@ from datetime import datetime, timezone import fcntl import hashlib import json -from importlib import import_module import os from pathlib import Path import re @@ -12,8 +11,6 @@ import subprocess import time -transition = import_module("namespace-transition") - ROOT = Path("/opt/snowglobe") RELEASES = ROOT / "releases" STATE = Path("/var/lib/snowglobe") @@ -63,27 +60,25 @@ def host_digest(root): digest = hashlib.sha256() paths = sorted(path for path, relative in files(root) if relative.parts[0] in {"nixos", "dashboard", "config", "service"} - 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"}) + 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"}) for path in paths: digest.update(str(path.relative_to(root)).encode()) _hash_contents(digest, path) return digest.hexdigest() -def check_release(release, legacy=False): +def check_release(release): if not RELEASE_ID.fullmatch(release): raise ValueError("invalid release ID") path = RELEASES / release if not path.is_dir(): raise ValueError(f"release is missing: {release}") - manifest = transition.layout(path)["manifest"] + manifest = path / ".snowglobe-release.json" if not manifest.exists(): - if not legacy: - raise ValueError(f"release has no manifest: {release}") - else: - details = json.loads(manifest.read_text()) - if details.get("id") != release or details.get("digest") != tree_digest(path) or details.get("version", 1) not in (1, 2): - raise ValueError(f"release contents changed: {release}") + raise ValueError(f"release has no manifest: {release}") + details = json.loads(manifest.read_text()) + if details.get("id") != release or details.get("digest") != tree_digest(path) or details.get("version", 1) not in (1, 2): + raise ValueError(f"release contents changed: {release}") return path @@ -103,7 +98,7 @@ def main_release(version=None): return None if not RELEASE_ID.fullmatch(version): raise ValueError("invalid release ID") - details = json.loads(transition.layout(RELEASES / version)["manifest"].read_text()) + details = json.loads((RELEASES / version / ".snowglobe-release.json").read_text()) revision = details.get("main") if (not isinstance(revision, dict) or not isinstance(revision.get("commit"), str) or not re.fullmatch(r"[0-9a-f]{40}", revision["commit"]) @@ -124,8 +119,8 @@ def save_history(entries): def jobs(path): rendered = subprocess.run( - ["python3", str(transition.layout(path)["script"]), "render"], - check=True, capture_output=True, text=True, env=transition.environment(path), + ["python3", str(path / "tools/snowglobe.py"), "render"], + check=True, capture_output=True, text=True, ).stdout result = set(re.findall(r'^job "([a-z][a-z0-9-]*)" \{$', rendered, re.MULTILINE)) if not result: @@ -139,15 +134,16 @@ def host_configuration(path): ["nix", "--extra-experimental-features", "nix-command flakes", "eval", "--json", f"path:{path.resolve()}#nixosConfigurations", "--apply", "builtins.attrNames"], check=True, capture_output=True, text=True, ).stdout) - return transition.host_configuration(hostname, available) + name = "vm" if hostname == "snowglobe-rehearsal" else hostname + if name not in available: + raise ValueError(f"release has no NixOS configuration for {hostname}") + return name -def activate(release, legacy=False, initial=False): - path = check_release(release, legacy) - layout = transition.layout(path) - script = layout["script"] - environment = transition.environment(path) - prefix = layout["prefix"] +def activate(release, initial=False): + path = check_release(release) + script = path / "tools/snowglobe.py" + environment = dict(os.environ) managed = STATE / "managed-jobs.json" current = current_release() previous = set(json.loads(managed.read_text())) if managed.exists() else ( @@ -175,14 +171,13 @@ def activate(release, legacy=False, initial=False): if current is None: (ROOT / "current").symlink_to(path) 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) + environment = dict(os.environ) next_link = ROOT / ("next-" + release) next_link.unlink(missing_ok=True) next_link.symlink_to(path) @@ -199,10 +194,10 @@ def activate(release, legacy=False, initial=False): time.sleep(2) else: raise RuntimeError("Nomad API did not become ready") - subprocess.run(["systemctl", "restart", prefix + "-router.service"], check=True) + subprocess.run(["systemctl", "restart", "snowglobe-router.service"], check=True) if (path / "nixos/dashboard.nix").exists(): - subprocess.run(["systemctl", "start", prefix + "-dashboard.service"], check=True) - subprocess.run(["systemctl", "is-active", "--quiet", prefix + "-dashboard.service"], check=True) + subprocess.run(["systemctl", "start", "snowglobe-dashboard.service"], check=True) + subprocess.run(["systemctl", "is-active", "--quiet", "snowglobe-dashboard.service"], check=True) subprocess.run(["python3", str(script), "pool"], check=True, env=environment) subprocess.run(["python3", str(script), "deploy"], check=True, env=environment) desired = jobs(path) @@ -215,9 +210,9 @@ def activate(release, legacy=False, initial=False): pending.write_text(json.dumps(sorted(desired)) + "\n") pending.replace(managed) if (path / "tools/log-shipper.py").exists(): - subprocess.run(["systemctl", "restart", prefix + "-log-shipper.service"], check=True) - manifest = transition.layout(path)["manifest"] - if not legacy and json.loads(manifest.read_text()).get("version", 1) >= 2: + subprocess.run(["systemctl", "restart", "snowglobe-log-shipper.service"], check=True) + manifest = path / ".snowglobe-release.json" + if json.loads(manifest.read_text()).get("version", 1) >= 2: subprocess.run(["python3", str(script), "check"], check=True, env=environment) return backup @@ -227,16 +222,13 @@ def main(): parser.add_argument("mode", choices=["publish", "deploy", "rollback", "history", "bootstrap", "verify", "stage-destroy"]) parser.add_argument("target", nargs="?") args = parser.parse_args() - transition.prepare() - transition.inherit_environment() STATE.mkdir(parents=True, exist_ok=True) if args.mode == "stage-destroy": if not args.target or not STAGE_ID.fullmatch(args.target): parser.error("stage-destroy requires a stage ID") saved = json.loads((STATE / "stages" / (args.target + ".json")).read_text()) path = check_release(saved["release"]) - subprocess.run(["python3", str(transition.layout(path)["script"]), "destroy", args.target], - check=True, env=transition.environment(path)) + subprocess.run(["python3", str(path / "tools/snowglobe.py"), "destroy", args.target], check=True) return if args.mode == "verify": if not args.target: @@ -271,7 +263,7 @@ def main(): entries = history() current = current_release() if current and not entries and args.mode != "bootstrap": - entries.append({"release": current, "source": "previous", "time": int(time.time()), "legacy": not transition.layout(RELEASES / current)["manifest"].exists()}) + entries.append({"release": current, "source": "previous", "time": int(time.time())}) save_history(entries) if args.mode == "deploy": candidate = main_release() @@ -279,11 +271,10 @@ def main(): raise ValueError("main changed or is unavailable. Publish main and retry deployment.") release = candidate["release"] source = "main" - legacy = False elif args.mode == "bootstrap": if not args.target or not RELEASE_ID.fullmatch(args.target) or entries: parser.error("bootstrap requires a release ID and no successful deployment") - release, source, legacy = args.target, "bootstrap", False + release, source = args.target, "bootstrap" main_release(release) else: if args.target: @@ -294,12 +285,12 @@ def main(): match = next((item for item in reversed(entries) if item["release"] != current), None) if not match: parser.error("no earlier production release") - release, source, legacy = match["release"], "rollback", match.get("legacy", False) + release, source = match["release"], "rollback" if release == current and entries and entries[-1]["release"] == current: print(f"already running {release}") return - backup = activate(release, legacy, args.mode == "bootstrap") - entries.append({"release": release, "source": source, "time": int(time.time()), "legacy": legacy, "backup": backup}) + backup = activate(release, args.mode == "bootstrap") + entries.append({"release": release, "source": source, "time": int(time.time()), "backup": backup}) save_history(entries) print(f"production={release} previous={current or ''}") diff --git a/tools/snowglobe-hardware.md b/tools/snowglobe-hardware.md index c1ada1f9f48668ffe99f2c56cbd8dcb2524e40bc..c254153365f3c8a392cfe48040d4156e5033b217 100644 --- a/tools/snowglobe-hardware.md +++ b/tools/snowglobe-hardware.md @@ -103,3 +103,29 @@ Before destructive storage or OS work, verify key recovery and application/ database restores from at least two restricted copies stored separately off this computer. Preserve the existing encrypted roots, pool host ID, numeric owners, SSH trust, and retained recovery sources through a rebuild. + +## Namespace migration, 2026-10-21 + +Production's kernel hostname, static hostname, and Nomad node name are +`snowglobe`. Clover manages the Tailscale machine name separately. + +The final telemetry cutover validated 6,949,996 custom metric samples across +458 series and all 308,283 stored spans, including 4,953,978 renamed samples +and 3,861 renamed spans. Metrics retained every series and timestamp within +the store's numeric precision; two existing conflicting timestamps retained +one of their original values. Traces retained their timestamps, stream labels, +and every field, verified with a normalized all-field digest. Other metrics +and logs remained intact. The original metrics and traces directories are +preserved in `/srv/prod/victoria/archives/namespace-20261021`. Root-only +migration reports and the executed script live under +`/var/lib/snowglobe/migrations/namespace-final-20261021`. + +The 265 pre-rename releases are preserved under +`/opt/snowglobe/archives/releases`; they are archives, excluded from executable +rollback history. `/opt/snowglobe/archives/namespace-20261021` preserves the +complete prior deployment history, retired manager compatibility files and +ACL policy definitions, and the former `/etc/nixos` source tree. The installed +configuration now follows `/opt/snowglobe/current`. Old path aliases, stale +runtime sockets, the unused preview host unit, and unreferenced old ACL +policies have been removed. Numeric identities and active service data paths +were preserved. diff --git a/tools/snowglobe.py b/tools/snowglobe.py index 0085ec2eb706b5e60ec598c9f11e5da7b3abb9b5..e23f9bb2329ce8b4294547196a85c14edd0fa32d 100644 --- a/tools/snowglobe.py +++ b/tools/snowglobe.py @@ -4,7 +4,6 @@ from contextlib import nullcontext import fcntl import hashlib import json -from importlib import import_module import os from pathlib import Path import re @@ -18,9 +17,6 @@ import time from data import cloned_postgres, dataset_for -transition = import_module("namespace-transition") -transition.inherit_environment() - REPO = Path(__file__).resolve().parent.parent SERVICES = REPO / "service" STATE = Path("/var/lib/snowglobe") @@ -810,7 +806,7 @@ def bootstrap(all_data): Path(path).unlink() current = Path("/opt/snowglobe/current") if current.is_symlink(): - command("systemctl", "start", transition.layout(current)["prefix"] + "-router.service") + command("systemctl", "start", "snowglobe-router.service") def destroy_stage(stage, metadata, definitions): diff --git a/tools/vms.py b/tools/vms.py index a152654cb8c972d32f2e32cb843bb29b8102b5fc..33b1979277c82d8ec21f276cdfe1d93406b0d13c 100644 --- a/tools/vms.py +++ b/tools/vms.py @@ -19,7 +19,6 @@ DISKS = Path("/srv/vm") IMAGES = Path(os.environ.get("SNOWGLOBE_VM_IMAGES_ROOT", "/srv/clover/Media/vm")) PRESETS = Path(os.environ.get("SNOWGLOBE_VM_PRESETS_ROOT", str(IMAGES / "Presets"))) UPLOADS = Path(os.environ.get("SNOWGLOBE_VM_UPLOADS_ROOT", str(IMAGES / "Install Disks"))) -transition = import_module("namespace-transition") mac_vms = import_module("mac-vms") NS = "{https://paperclover.net/snowglobe}" NAME = re.compile(r"[a-z0-9][a-z0-9-]{0,62}\Z") @@ -245,7 +244,7 @@ def domains(): for name in virsh("list", "--all", "--name").splitlines(): if not name: continue - root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name))) + root = ET.fromstring(virsh("dumpxml", name)) state_line = virsh("domstate", name, "--reason").splitlines()[0].lower() state = next((value for value in ("running", "blocked", "paused", "shutdown", "crashed", "pmsuspended") if state_line.startswith(value)), "shutoff") 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): def change_media(spec): name = spec["name"] - root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name, "--inactive"))) + root = ET.fromstring(virsh("dumpxml", name, "--inactive")) drives = root.findall("./devices/disk[@device='cdrom']") source = resolve_image(spec["image"]) if spec["image"] else None details = profile(inferred_profile(root)) @@ -523,7 +522,7 @@ def save_preset(spec): name = spec["name"] if virsh("domstate", name).strip() != "shut off": raise ValueError("Shut down this VM before saving a preset.") - root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name, "--inactive"))) + root = ET.fromstring(virsh("dumpxml", name, "--inactive")) if root.findtext(f"metadata/{NS}vm/{NS}platform") == "macos": raise ValueError("Use a prepared OS X preset from the OS library.") 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): def console_target(name): - root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name))) + root = ET.fromstring(virsh("dumpxml", name)) graphics = root.find("./devices/graphics[@type='vnc']") if virsh("domstate", name).strip() not in {"running", "paused", "blocked"} or graphics is None: raise ValueError("Start this VM before opening its screen.") @@ -958,7 +957,7 @@ def main(): fields = Path(f"/proc/{pid}/stat").read_text().rsplit(") ", 1)[1].split() resident = int(Path(f"/proc/{pid}/statm").read_text().split()[1]) * os.sysconf("SC_PAGE_SIZE") result[name] = {"cpu": (int(fields[11]) + int(fields[12])) / os.sysconf("SC_CLK_TCK"), "memory": resident, - "vcpus": int(transition.normalize_vm(ET.fromstring(virsh("dumpxml", name))).findtext("vcpu", "1"))} + "vcpus": int(ET.fromstring(virsh("dumpxml", name)).findtext("vcpu", "1"))} return result if action == "library": return library() @@ -967,7 +966,7 @@ def main(): if action == "console": return console_target(payload["name"]) if action == "guest": - root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", payload["name"]))) + root = ET.fromstring(virsh("dumpxml", payload["name"])) source = root.find(f"./devices/channel[@type='unix']/target[@name='{GUEST_CHANNEL}']/../source") expected = f"/run/libvirt/qemu/channel/{root.get('id')}-{payload['name'][:20]}/{GUEST_CHANNEL}" 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(): if action == "serial": if virsh("domstate", payload["name"]).strip() not in {"running", "blocked"}: raise ValueError("Start this VM before opening its console.") - root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", payload["name"]))) + root = ET.fromstring(virsh("dumpxml", payload["name"])) console = root.find("./devices/console[@type='pty']") source = console.find("source") if console is not None else None target = console.find("target") if console is not None else None @@ -985,7 +984,7 @@ def main(): raise ValueError("This VM has no serial console. Add one in its hardware settings.") return {"path": path} if action == "owner": - return {"owner": transition.normalize_vm(ET.fromstring(virsh("dumpxml", payload["name"]))).findtext(f"metadata/{NS}vm/{NS}owner")} + return {"owner": ET.fromstring(virsh("dumpxml", payload["name"])).findtext(f"metadata/{NS}vm/{NS}owner")} if action == "access": try: with os.fdopen(open_regular(DISKS / payload["name"] / "access.json")) as incoming: @@ -1002,7 +1001,7 @@ def main(): name = payload["name"] ensure_name(name) if payload["action"] == "start": - root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name, "--inactive"))) + root = ET.fromstring(virsh("dumpxml", name, "--inactive")) if root.find("./devices/interface/source[@network='default']") is not None: ensure_network() virsh({"start": "start", "shutdown": "shutdown", "reboot": "reboot", "destroy": "destroy", "resume": "resume", "suspend": "suspend"}[payload["action"]], name) @@ -1023,7 +1022,7 @@ def main(): raise ValueError("VM does not exist") if virsh("domstate", name).strip() != "shut off": virsh("destroy", name) - root = transition.normalize_vm(ET.fromstring(virsh("dumpxml", name))) + root = ET.fromstring(virsh("dumpxml", name)) 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 [])) directory = DISKS / name if payload["disks"] and directory.is_dir():