authoravatarclover caruso<git@paperclover.net>2026-10-23 18:18:08-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-24 19:01:23-07:00
loge94cb54d03596ba5bc9cd1b6bcad948ad1d934be
treee1f00a1ad160e3ebe57cae1d0f6397cc77e6ea07
parent225f9e709874838f85d8a55d28e270cd44a1b624
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Keep VM consoles visible through reconnects and use CPU encoding

Preserve the desktop and keyboard focus during transport recovery, use x264 without GPU access, and give VM previews a private screen broker. Admins can list every VM in the dashboard; MCP ownership and grants stay enforced. Verified in staging with native screen tests, authenticated browser input, forced broker recovery, and a fresh-eyes browser review. Assisted-by: gpt-6.1-sol

10 files changed, 179 insertions(+), 57 deletions(-)

dashboard/src/computer.rs+12
......@@ -389,6 +389,18 @@ pub fn router(app: Arc<App>) -> Router {
389389#[cfg(test)]
390390mod tests {
391391 use super::*;
392
393 #[test]
394 fn dashboard_admin_access_does_not_extend_mcp_grants() {
395 let admin = json!({"id":"admin","groups":["infra-admin"]});
396 let vm = json!({"owner":"another-user","ownerOnly":true});
397 assert!(vms::visible(&admin, &vm));
398 let grant = json!({"user":"admin","scopes":["computer:read","computer:control"],"resources":["test-vm"]});
399 for scope in ["computer:read", "computer:control"] {
400 assert!(permitted(&grant, "test-vm", scope, &vm).is_err());
401 }
402 }
403
392404 #[test]
393405 fn control_requires_the_current_owner_selected_vm_and_scope() {
394406 let grant = json!({"user":"clover","scopes":["computer:read"],"resources":["m3-devbox"]});
dashboard/src/vms.rs+4-3
......@@ -9,7 +9,7 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
99pub fn visible(me: &Value, domain: &Value) -> bool {
1010 let owner = &domain["owner"];
1111 (owner.as_str().is_some_and(|id| !id.is_empty()) && owner == &me["id"])
12 || (domain["ownerOnly"] != true && array(&me["groups"]).iter().any(|group| group == "infra-admin"))
12 || array(&me["groups"]).iter().any(|group| group == "infra-admin")
1313}
1414
1515pub async fn authorize(me: &Value, name: &str) -> Result<()> {
......@@ -210,8 +210,9 @@ mod tests {
210210 ));
211211 let private = json!({"owner":"account-1","ownerOnly":true});
212212 assert!(visible(&user, &private));
213 assert!(!visible(&json!({"id":"admin","groups":["infra-admin"]}), &private));
214 assert!(!visible(&json!({"id":"admin","groups":["infra-admin"]}), &json!({"ownerOnly":true})));
213 assert!(!visible(&json!({"id":"other","groups":["vm"]}), &private));
214 assert!(visible(&json!({"id":"admin","groups":["infra-admin"]}), &private));
215 assert!(visible(&json!({"id":"admin","groups":["infra-admin"]}), &json!({"ownerOnly":true})));
215216 }
216217
217218 #[test]
dashboard/web/components/VMConsole.tsx+54-21
......@@ -20,7 +20,8 @@ type WindowFrame = { windows: WindowSurface[]; rtpTimestamp: number };
2020export function VMConsole(props: { name: string; platform: Domain["platform"]; enabled: boolean; active: boolean; setup?: Domain["setup"]; booting: boolean; startedAt: number | null; arch: string; unavailable?: string; clipboard?: (text: string) => Promise<void>; toolbar: JSX.Element; content: (toolbar: HTMLDivElement) => JSX.Element }) {
2121 const enabled = createMemo(() => props.enabled);
2222 const [bootScreen, setBootScreen] = createSignal(false);
23 const preparing = () => !bootScreen() && (!!props.setup && props.setup !== "ready" || props.booting || state() === "Connecting…");
23 const [hasFrame, setHasFrame] = createSignal(false);
24 const preparing = () => !hasFrame() && !bootScreen() && (!!props.setup && props.setup !== "ready" || props.booting || state() === "Connecting…");
2425 const [now, setNow] = createSignal(Date.now());
2526 createEffect(() => {
2627 if (!preparing() || !props.active) return;
......@@ -89,6 +90,11 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
8990 const [clipboard, setClipboard] = createSignal("");
9091 const [sendingClipboard, setSendingClipboard] = createSignal(false);
9192 const [showClipboard, setShowClipboard] = createSignal(false);
93 let clipboardButton!: HTMLButtonElement;
94 const closeClipboard = () => {
95 setShowClipboard(false);
96 clipboardButton.closest("details")?.querySelector("summary")?.focus();
97 };
9298 const [windowsAvailable, setWindowsAvailable] = createSignal(false);
9399 const [separateWindows, setSeparateWindows] = createSignal(false);
94100 const [frameSync, setFrameSync] = createSignal(false);
......@@ -98,9 +104,10 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
98104 const [capture, setCapture] = createSignal<{ fps: number; capture: number; scan: number; write: number }>();
99105 const [tools, setTools] = createSignal<HTMLDivElement>();
100106 const send = (message: object) => { if (input?.readyState === "open") input.send(JSON.stringify(message)); };
101 const compose = () => {
107 const compose = (freeze = false) => {
102108 const frame = windowFrame;
103 canvas.hidden = !video.requestVideoFrameCallback || !video.srcObject || video.readyState < 2;
109 if (!video.srcObject || video.readyState < 2) return;
110 canvas.hidden = !video.requestVideoFrameCallback && !freeze;
104111 if (canvas.hidden || !props.active || !width || !height) return;
105112 if (canvas.width !== width || canvas.height !== height) { canvas.width = width; canvas.height = height; }
106113 const context = canvas.getContext("2d", { alpha: false });
......@@ -155,6 +162,7 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
155162 let peer: RTCPeerConnection | undefined;
156163 let frameCallback: number | undefined;
157164 let statsTimer: number | undefined;
165 let recoveryTimer: number | undefined;
158166 let previousStats: { time: number; bytes: number; frames: number } | undefined;
159167 const frameReady: VideoFrameRequestCallback = (_, metadata) => {
160168 if (current !== attempt || disposed) return;
......@@ -212,12 +220,16 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
212220 if (current !== attempt || disposed || video.readyState < 2 || input?.readyState !== "open") return;
213221 clearTimeout(timeout);
214222 retryDelay = 1000;
223 setHasFrame(true);
215224 setState("Connected");
225 if (document.activeElement === video) focus();
216226 };
217227 disconnect = () => {
218228 clearTimeout(reconnectTimer);
219229 clearTimeout(timeout);
230 clearTimeout(recoveryTimer);
220231 blur();
232 compose(true);
221233 video.removeEventListener("keydown", releaseKeyboard, true);
222234 video.removeEventListener("focus", focus);
223235 video.removeEventListener("blur", blur);
......@@ -228,10 +240,9 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
228240 if (frameCallback !== undefined) video.cancelVideoFrameCallback(frameCallback);
229241 windowFrame = undefined; pendingFrames = []; frameTimestamp = undefined; drag = undefined;
230242 movement = undefined; pointerSequence = 0;
231 setFrameSync(false); setWindowsAvailable(false); setWindowCapture(false); setStats(undefined); setCapture(undefined);
243 setFrameSync(false); setWindowsAvailable(false); setWindowCapture(false);
232244 video.onplaying = null;
233245 video.srcObject = null;
234 compose();
235246 clearTimeout(cursorTimer);
236247 cursorFrames = [];
237248 cursorFrame = 0;
......@@ -261,13 +272,21 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
261272 keyboard.onkeyevent = (key, _code, down) => { if (acceptsInput()) send({ type: "key", key, down }); };
262273 input.onopen = () => {
263274 ready();
264 if (document.activeElement === video) focus();
265275 if (windowsAvailable()) send({ type: "experiment", enabled: separateWindows() });
266276 };
267277 peer.onicecandidate = ({ candidate }) => { if (candidate) signal({ type: "candidate", ...candidate.toJSON() }); };
268278 peer.onconnectionstatechange = () => {
269 if (peer?.connectionState === "failed" || peer?.connectionState === "disconnected") {
279 if (peer?.connectionState === "connected") {
280 clearTimeout(recoveryTimer);
281 recoveryTimer = undefined;
282 setProblem("");
283 ready();
284 } else if (peer?.connectionState === "failed") {
270285 fail("The screen disconnected. Reconnect to try again.");
286 } else if (peer?.connectionState === "disconnected" && recoveryTimer === undefined) {
287 setState("Disconnected");
288 setProblem("The screen connection was interrupted. Reconnecting…");
289 recoveryTimer = window.setTimeout(() => fail("The screen disconnected. Reconnecting…"), 5000);
271290 }
272291 };
273292 peer.ontrack = ({ track }) => {
......@@ -360,25 +379,28 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
360379 setState("Disconnected");
361380 }
362381 };
363 createEffect(() => {
364 if (enabled()) void connect();
382 createEffect(on(enabled, value => {
383 if (value) void connect();
365384 else {
366385 attempt++;
367386 disconnect();
368387 input = undefined;
369388 setState("Disconnected");
370389 setProblem("");
390 setHasFrame(false);
391 setStats(undefined); setCapture(undefined);
392 canvas.hidden = true;
371393 }
372 });
394 }));
373395 createEffect(() => {
374396 if (acceptsInput()) return;
375397 release();
376 if (document.activeElement === video) {
398 if (document.activeElement === video && (!props.active || !enabled() || preparing())) {
377399 video.blur();
378400 if (props.active) tools()?.querySelector<HTMLElement>("summary")?.focus();
379401 }
380402 });
381 createEffect(on(() => props.setup, setup => {
403 createEffect(on(createMemo(() => props.setup), setup => {
382404 if (setup === "ready" && enabled() && state() === "Disconnected") void connect();
383405 }, { defer: true }));
384406 let playbackFrame: number | undefined;
......@@ -396,7 +418,12 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
396418 });
397419 };
398420 createEffect(on(() => [props.active, enabled(), preparing()], resumeVideo));
399 createEffect(on(() => props.startedAt, () => setBootScreen(false)));
421 createEffect(on(createMemo(() => props.startedAt), () => {
422 setBootScreen(false);
423 setHasFrame(false);
424 setStats(undefined); setCapture(undefined);
425 canvas.hidden = true;
426 }));
400427 document.addEventListener("visibilitychange", resumeVideo);
401428 onCleanup(() => {
402429 disposed = true;
......@@ -439,7 +466,7 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
439466 <Show when={props.active}>
440467 <VMMenu label="Input">
441468 <Show when={props.platform !== "macos"}><button disabled={!connected()} onClick={ctrlAltDel}><Keyboard size={14} aria-hidden="true" />Ctrl+Alt+Del</button></Show>
442 <button disabled={!connected()} onClick={() => setShowClipboard(!showClipboard())}><Clipboard size={14} aria-hidden="true" />Clipboard</button>
469 <button ref={clipboardButton} disabled={!connected() && !showClipboard()} aria-expanded={showClipboard()} aria-controls={`clipboard-${props.name}`} onClick={() => showClipboard() ? closeClipboard() : setShowClipboard(true)}><Clipboard size={14} aria-hidden="true" />Clipboard</button>
443470 </VMMenu>
444471 <VMMenu label="View">
445472 <button disabled={!connected() || !windowsAvailable() || !frameSync()} aria-pressed={separateWindows()}
......@@ -453,20 +480,24 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
453480 </VMMenu>
454481 <span class="spacer" />
455482 <Show when={connected() && !preparing()}><span class="vm-console-status" id={`keyboard-release-${props.name}`}>Ctrl+Alt+Esc releases keyboard</span></Show>
456 <span class="vm-console-status" role="status">{preparing() ? "" : props.enabled ? state() : props.unavailable ? "Unavailable" : "Off"}</span>
483 <span class="vm-console-status" role="status">{preparing() ? "" : props.enabled ? hasFrame() && state() === "Connecting…" ? "Reconnecting…" : state() : props.unavailable ? "Unavailable" : "Off"}</span>
457484 </Show>
458485 </div>
459486 <Show when={props.active && showStats()}>
460 <div class="vm-stream-stats">
487 <div class="vm-stream-stats" style={{ visibility: connected() ? "visible" : "hidden" }}>
461488 <Show when={stats()}>{(value) => <><span>{value().fps.toFixed(1)} FPS</span><span>{value().bitrate.toFixed(2)} Mbps</span><span>{value().lost} packets lost</span><span>{value().dropped} frames dropped</span><span>{value().rtt.toFixed(0)} ms round trip</span><span>{value().delay.toFixed(0)} ms buffer</span></>}</Show>
462489 <Show when={capture()}>{(value) => <><span>Guest {value().fps.toFixed(1)} FPS</span><span>Desktop capture {value().capture.toFixed(1)} ms</span><span>Compare {value().scan.toFixed(1)} ms</span><span>Stream work {value().write.toFixed(1)} ms</span></>}</Show>
463490 <Show when={separateWindows()}><span>{windowCapture() ? "Separate windows" : "Desktop fallback"}</span></Show>
464491 </div>
465492 </Show>
466 <Show when={props.active && !preparing() && problem()}><p class="error vm-console-error" role="alert">{problem()}</p></Show>
467493 <Show when={props.active && showClipboard()}>
468 <div class="vm-clipboard">
469 <label class="field">clipboard<textarea class="search" rows="3" value={clipboard()} onInput={(event) => setClipboard(event.currentTarget.value)} /></label>
494 <div class="vm-clipboard" id={`clipboard-${props.name}`} onKeyDown={(event) => {
495 if (event.key !== "Escape") return;
496 event.preventDefault();
497 event.stopPropagation();
498 closeClipboard();
499 }}>
500 <label class="field">clipboard<textarea ref={(field) => queueMicrotask(() => field.focus())} class="search" rows="3" value={clipboard()} onInput={(event) => setClipboard(event.currentTarget.value)} /></label>
470501 <span class="hint">{props.clipboard ? "Send text, then paste in the VM with Command+V." : "Clipboard sharing requires support in the guest"}</span>
471502 <div class="row">
472503 <button class="button small" disabled={!connected() || sendingClipboard()} aria-busy={sendingClipboard()} onClick={async () => {
......@@ -477,21 +508,23 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
477508 finally { setSendingClipboard(false); }
478509 }}>send text</button>
479510 <button class="button small" onClick={() => navigator.clipboard.writeText(clipboard()).catch((failure) => setProblem(reason(failure)))}>copy text</button>
511 <button class="button small" onClick={closeClipboard}>close</button>
480512 </div>
481513 </div>
482514 </Show>
483515 <div class="vm-console-screen" hidden={!props.active || !props.enabled}>
516 <Show when={!preparing() && problem()}><p class="error vm-console-error" role="alert">{problem()}</p></Show>
484517 <Show when={preparing()}>
485518 <div class="empty vm-setup">
486519 <div role={props.setup === "failed" ? "alert" : "status"}>
487520 <StatusLabel health={props.setup === "failed" ? "down" : "starting"}>{props.setup && props.setup !== "ready" ? VM_SETUP[props.setup] : props.booting ? "Starting your VM…" : "Connecting to screen…"}</StatusLabel>
488521 <p>{props.setup === "failed" ? "Restart this VM from the Machine menu to retry." : !props.booting ? "Waiting for the screen connection." : props.arch === "aarch64" ? "This preset can take several minutes to boot." : "This preset takes about 1 minute to boot."}</p>
489522 </div>
490 <Show when={props.startedAt !== null}><span class="vm-setup-elapsed" role="timer" aria-live="off">{elapsed()} elapsed</span></Show>
523 <Show when={props.startedAt !== null}><span class="vm-setup-elapsed" role="timer" aria-live="off">VM uptime {elapsed()}</span></Show>
491524 <button class="button small" onClick={() => setBootScreen(true)}>View boot screen</button>
492525 </div>
493526 </Show>
494 <video ref={video} autoplay muted playsinline tabindex={preparing() ? -1 : 0} aria-hidden={preparing()} aria-label="VM screen" aria-describedby={`keyboard-release-${props.name}`}
527 <video ref={video} autoplay muted playsinline tabindex={preparing() ? -1 : 0} aria-hidden={preparing()} aria-disabled={!acceptsInput()} aria-label="VM screen" aria-describedby={`keyboard-release-${props.name}`}
495528 onContextMenu={(event) => event.preventDefault()}
496529 onPointerMove={pointer}
497530 onPointerLeave={() => { cursorInside = false; drawCursor(); }}
dashboard/web/pages/VMs.css+4-2
......@@ -87,7 +87,7 @@
8787.vm-stream-stats { display: flex; flex-wrap: wrap; gap: 4px 16px; padding: 6px 12px; font-size: 12px; font-variant-numeric: tabular-nums; }
8888.vm-cursor { display: none; position: fixed; left: 0; top: 0; pointer-events: none; }
8989.vm-cursor-invert { mix-blend-mode: difference; }
90.vm-console-error { padding: 8px 12px; margin: 0; font-size: 12px; }
90.vm-console-error { position: absolute; top: 0; left: 0; right: 0; z-index: 1; padding: 8px 12px; margin: 0; font-size: 12px; background: var(--surface); }
9191.vm-clipboard { display: grid; gap: 8px; padding: 12px; }
9292@media (max-width: 640px) {
9393 .vm-detail .facts, .vm-charts { grid-template-columns: 1fr; }
......@@ -114,7 +114,8 @@
114114.vm-count { font-size: 12px; }
115115
116116.vms.vm-active { padding: 0; max-width: none; width: 100%; }
117.vm-active .list-body.flush { margin: 0; padding: 0; overflow: hidden; }
117.vm-active .list-body.flush { position: relative; margin: 0; padding: 0; overflow: hidden; }
118.vm-active .list-body.flush > .stale-note { position: absolute; bottom: 0; left: 0; right: 0; z-index: 2; margin: 0; padding: 8px 12px; background: var(--surface); }
118119.vm-active .list-body.flush > .vm-workspace { margin: 0; flex: 1; min-height: 0; }
119120.vm-workspace { display: flex; flex-direction: column; }
120121.vms .vm-home { display: none; }
......@@ -134,6 +135,7 @@
134135.vm-menu summary::-webkit-details-marker { display: none; }
135136.vm-menu summary:hover, .vm-menu[open] summary { background: var(--hover); }
136137.vm-menu > div { position: absolute; top: calc(100% + 6px); left: 0; min-width: 180px; max-width: 300px; z-index: 5; display: flex; flex-direction: column; padding: 4px; border: 1px solid var(--grid); border-radius: 7px; background: var(--panel); box-shadow: 0 6px 24px #0005; }
138.vm-menu:last-of-type > div { left: auto; right: 0; }
137139.vm-menu > div > button { display: flex; align-items: center; justify-content: flex-start; gap: 8px; width: 100%; border: 0; border-radius: 4px; background: transparent; color: var(--text); padding: 7px 10px; font: inherit; text-align: left; cursor: pointer; }
138140.vm-menu > div > button svg { flex: none; }
139141.vm-menu > div > button:hover:not(:disabled) { background: var(--hover); }
flake.nix+1
......@@ -6,6 +6,7 @@
66 outputs = { self, nixpkgs, ... }: {
77 packages.x86_64-linux.dashboard = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/dashboard.nix { };
88 packages.x86_64-linux.host-python = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/host-python.nix { };
9 packages.x86_64-linux.vm-screen = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/vm-screen.nix { };
910 packages.x86_64-linux.computer-ahk = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/computer-ahk.nix { };
1011 packages.x86_64-linux.dashboard-image = self.packages.x86_64-linux.dashboard.image;
1112 packages.x86_64-linux.installer = self.nixosConfigurations.installer.config.system.build.isoImage;
nixos/configuration.nix+4-11
......@@ -6,13 +6,12 @@ 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/vm-computer.py ../tools/vm-ai.py ../tools/vm-rfb.py ../tools/vm-screen.py ../tools/mac-computer.py ../guest/computer-windows.cs ../guest/computer-legacy-mac.py ../guest/computer-linux.py ../guest/computer-linux-launch ../guest/computer-linux.nix ../tools/mac-vms.py ../tools/vm-ssh.py ../tools/dashboard-run.py ../tools/release.py ../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/vm-computer.py ../tools/vm-ai.py ../tools/vm-rfb.py ../tools/mac-computer.py ../guest/computer-windows.cs ../guest/computer-legacy-mac.py ../guest/computer-linux.py ../guest/computer-linux-launch ../guest/computer-linux.nix ../tools/mac-vms.py ../tools/vm-ssh.py ../tools/dashboard-run.py ../tools/release.py ../guest/protocol.json ];
1010 };
1111 computerAhk = pkgs.callPackage ./computer-ahk.nix { };
1212 nativePkl = pkgs.callPackage ./pkl.nix { };
1313 hostPython = pkgs.callPackage ./host-python.nix { };
14 screenPython = pkgs.python3.withPackages (packages: [ packages.pygobject3 packages.gst-python ]);
15 screenPlugins = (with pkgs.gst_all_1; [ (lib.getLib gstreamer) gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly gst-plugins-rs ]) ++ [ (lib.getLib pkgs.libnice) ];
14 screen = pkgs.callPackage ./vm-screen.nix { };
1615 secureOvmf = pkgs.OVMF.override {
1716 secureBoot = true;
1817 msVarsTemplate = true;
......@@ -157,14 +156,9 @@ in
157156 wantedBy = [ "multi-user.target" ];
158157 wants = [ "snowglobe-host.service" ];
159158 after = [ "snowglobe-host.service" ];
160 environment = {
161 GST_PLUGIN_SYSTEM_PATH_1_0 = lib.makeSearchPath "lib/gstreamer-1.0" screenPlugins;
162 GI_TYPELIB_PATH = lib.makeSearchPath "lib/girepository-1.0" (map lib.getLib ([ pkgs.gst_all_1.gstreamer ] ++ screenPlugins));
163 LD_LIBRARY_PATH = "/run/opengl-driver/lib";
164 SNOWGLOBE_VM_GUEST_PROTOCOL = toString ../guest/protocol.json;
165 };
159 environment.XDG_RUNTIME_DIR = "/run/snowglobe-vm-screen";
166160 serviceConfig = {
167 ExecStart = "${screenPython}/bin/python3 ${hostTools}/tools/vm-screen.py";
161 ExecStart = "${screen}/bin/snowglobe-vm-screen";
168162 User = "snowglobe-dashboard";
169163 Group = "snowglobe-dashboard";
170164 RuntimeDirectory = "snowglobe-vm-screen";
......@@ -179,7 +173,6 @@ in
179173 MemoryMax = "2G";
180174 TasksMax = 128;
181175 DevicePolicy = "closed";
182 DeviceAllow = lib.optional (builtins.elem "nvidia" config.services.xserver.videoDrivers) "char-nvidia* rw";
183176 RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" "AF_NETLINK" ];
184177 Restart = "always";
185178 RestartSec = 2;
nixos/vm-screen.nix created+18
......@@ -0,0 +1,18 @@
1{ lib, python3, gst_all_1, libnice, writeShellApplication }:
2let
3 python = python3.withPackages (packages: [ packages.pygobject3 packages.gst-python ]);
4 plugins = (with gst_all_1; [ (lib.getLib gstreamer) gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly gst-plugins-rs ]) ++ [ (lib.getLib libnice) ];
5 source = lib.fileset.toSource {
6 root = ../.;
7 fileset = lib.fileset.unions [ ../tools/vm-screen.py ../tools/vm-rfb.py ../guest/protocol.json ];
8 };
9in
10writeShellApplication {
11 name = "snowglobe-vm-screen";
12 runtimeEnv = {
13 GST_PLUGIN_SYSTEM_PATH_1_0 = lib.makeSearchPath "lib/gstreamer-1.0" plugins;
14 GI_TYPELIB_PATH = lib.makeSearchPath "lib/girepository-1.0" (map lib.getLib ([ gst_all_1.gstreamer ] ++ plugins));
15 SNOWGLOBE_VM_GUEST_PROTOCOL = "${source}/guest/protocol.json";
16 };
17 text = ''exec ${python}/bin/python3 ${source}/tools/vm-screen.py'';
18}
tools/dashboard-stage-test.py+30-3
......@@ -57,7 +57,7 @@ class PreviewBoundary(unittest.TestCase):
5757 def test_attached_computer_cannot_target_another_vm(self):
5858 host = stage.PreviewHost({"clone": "globe/staging/fixture", "computer": "mac-fixture"})
5959 with patch.object(boundary.Host, "handle", return_value={"ok": True}) as call:
60 for operation in ["vm.computer", "vm.owner"]:
60 for operation in ["vm.computer", "vm.owner", "vm.console", "vm.guest"]:
6161 with self.assertRaises(boundary.dashboard_runs.Error):
6262 host.handle({"operation": operation, "payload": {"name": "personal"}})
6363 call.assert_not_called()
......@@ -125,8 +125,15 @@ class PreviewProof(unittest.TestCase):
125125 with tempfile.TemporaryDirectory() as temporary:
126126 root = Path(temporary)
127127 state = root / "state"
128 (root / "srv/staging").mkdir(parents=True)
128129 site = {"root": str(root / "srv"), "domain": "fixture.test", "pool": "fixture", "nodeName": "fixture", "tlsInternal": True}
129130 jobs = []
131 units = []
132 builds = []
133 image_stream = Mock()
134 image_stream.__enter__ = Mock(return_value=image_stream)
135 image_stream.__exit__ = Mock(return_value=False)
136 image_stream.wait.return_value = 0
130137
131138 def command(*args, **kwargs):
132139 output = ""
......@@ -140,17 +147,31 @@ class PreviewProof(unittest.TestCase):
140147 output = json.dumps({"username": "preview", "password": "disposable-fixture-password"})
141148 elif args[:3] == ("nomad", "job", "run"):
142149 jobs.append(json.loads(kwargs["input"]))
150 elif args[0] == "systemd-run":
151 units.append(args)
143152 return subprocess.CompletedProcess(args, 0, stdout=output)
144153
145154 def run(args, **kwargs):
155 if args[:3] == ["podman", "image", "exists"]:
156 return subprocess.CompletedProcess(args, 1)
157 if args[:2] == ["podman", "load"]:
158 self.assertIs(kwargs["stdin"], image_stream.stdout)
159 self.assertTrue(Path(kwargs["env"]["TMPDIR"]).is_relative_to(root / "srv/staging"))
160 if args[0] == "nix":
161 if "build" in args:
162 self.assertTrue(Path(args[args.index("build-dir") + 1]).is_relative_to(root / "srv/staging"))
163 builds.append(args)
146164 return subprocess.CompletedProcess(args, 0, stdout="fixture-image" if args[0] == "nix" else "")
147165
148166 original_exists = Path.exists
149167 def exists(path):
150 return str(path).startswith("/run/snowglobe-preview/") and path.name == "host.sock" or original_exists(path)
168 return (str(path).startswith("/run/snowglobe-preview/") and path.name == "host.sock"
169 or str(path).startswith("/run/snowglobe-preview-screen-") and path.name == "screen.sock"
170 or original_exists(path))
151171
152 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()):
172 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.subprocess, "Popen", return_value=image_stream), patch.object(stage.os, "chown") as chown, patch.object(Path, "exists", exists), redirect_stdout(io.StringIO()):
153173 stage.stage()
174 self.assertTrue(any(target.endswith("#dashboard-image.stream") for build in builds for target in build))
154175 check_http.assert_called_once_with(jobs[-1]["ID"] + ".fixture.test", "/sign-in", True)
155176 task = jobs[-1]["TaskGroups"][0]["Tasks"][0]
156177 check = jobs[-1]["TaskGroups"][0]["Services"][0]["Checks"][0]
......@@ -169,6 +190,12 @@ class PreviewProof(unittest.TestCase):
169190 stage.stage()
170191 self.assertEqual(proof.read_text(), value)
171192 self.assertEqual(jobs[-1]["ID"], jobs[-2]["ID"])
193 stage.stage("vm-fixture")
194 task = jobs[-1]["TaskGroups"][0]["Tasks"][0]
195 screen_runtime = "/run/snowglobe-preview-screen-" + jobs[-1]["ID"]
196 self.assertIn(screen_runtime + ":/run/snowglobe-vm-screen:ro", task["Config"]["volumes"])
197 screen = next(unit for unit in units if "--unit=snowglobe-preview-screen-" + jobs[-1]["ID"] in unit)
198 self.assertIn("--setenv=SNOWGLOBE_HOST_SOCKET=/run/snowglobe-preview/" + jobs[-1]["ID"] + "/host.sock", screen)
172199
173200
174201class NativePreview(unittest.TestCase):
tools/dashboard-stage.py+48-11
......@@ -12,6 +12,7 @@ import socket
1212import sqlite3
1313import subprocess
1414import sys
15import tempfile
1516import threading
1617import time
1718
......@@ -37,7 +38,7 @@ class PreviewHost(boundary.Host):
3738 def handle(self, request):
3839 operation = request.get("operation") if isinstance(request, dict) else None
3940 owner = None
40 if self.stage.get("computer") and self.stage.get("mount") and operation in {"vm.computer", "vm.owner", "vm.domains", "vm.computers", "vm.ai", "vm.act"}:
41 if self.stage.get("computer") and self.stage.get("mount") and operation in {"vm.computer", "vm.owner", "vm.console", "vm.guest", "vm.domains", "vm.computers", "vm.ai", "vm.act"}:
4142 with sqlite3.connect(f"file:{self.stage['mount']}/data/accounts.sqlite?mode=ro", uri=True) as accounts:
4243 owner = accounts.execute("SELECT id FROM users WHERE username='preview'").fetchone()
4344 if not owner:
......@@ -46,7 +47,7 @@ class PreviewHost(boundary.Host):
4647 payload = request.get("payload")
4748 if not owner or not isinstance(payload, dict) or payload.get("owner") != owner[0]:
4849 raise boundary.dashboard_runs.Error(403, "Test VMs must belong to this preview account.")
49 if operation in {"vm.computer", "vm.owner", "vm.act"}:
50 if operation in {"vm.computer", "vm.owner", "vm.act", "vm.console", "vm.guest"}:
5051 payload = request.get("payload")
5152 if not self.stage.get("computer") or not isinstance(payload, dict):
5253 raise boundary.dashboard_runs.Error(403, "Choose the VM explicitly attached to this preview.")
......@@ -104,14 +105,14 @@ def serve(stage):
104105 os.chown(path, 0, saved["uid"])
105106 path.chmod(0o660)
106107 listener.listen(8)
107 slots = threading.BoundedSemaphore(8)
108 with concurrent.futures.ThreadPoolExecutor(max_workers=4) as workers:
108 slots = threading.BoundedSemaphore(16)
109 with concurrent.futures.ThreadPoolExecutor(max_workers=12) as workers:
109110 while True:
110111 connection, _ = listener.accept()
111112 if not slots.acquire(blocking=False):
112113 connection.close()
113114 continue
114 operations = OPERATIONS | {"vm.computer", "vm.owner", "vm.ai", "vm.act"} if saved.get("computer") else OPERATIONS
115 operations = OPERATIONS | {"vm.computer", "vm.owner", "vm.ai", "vm.act", "vm.console", "vm.guest"} if saved.get("computer") else OPERATIONS
115116 work = workers.submit(boundary.serve_connection, connection, host, saved["uid"], operations)
116117 work.add_done_callback(lambda _: slots.release())
117118
......@@ -153,9 +154,18 @@ def stage(computer=None):
153154 image = subprocess.run([*nix, "eval", "--raw", package, "--apply", 'image: "${image.imageName}:${image.imageTag}"'],
154155 stdout=subprocess.PIPE, text=True, check=True).stdout.strip()
155156 if subprocess.run(["podman", "image", "exists", image], capture_output=True).returncode:
156 built = subprocess.run([*nix, "build", package, "--no-link", "--print-out-paths"],
157 stdout=subprocess.PIPE, text=True, check=True).stdout.strip()
158 snowglobe.command("podman", "load", "--quiet", "--input", built)
157 with tempfile.TemporaryDirectory(prefix="dashboard-image-", dir=site["stagingRoot"]) as temporary:
158 built = subprocess.run([*nix, "build", package + ".stream", "--option", "build-dir", temporary,
159 "--out-link", str(Path(temporary) / "stream"), "--print-out-paths"],
160 stdout=subprocess.PIPE, text=True, check=True).stdout.strip()
161 environment = {**os.environ, "TMPDIR": temporary}
162 with subprocess.Popen([built], stdout=subprocess.PIPE, env=environment) as stream:
163 try:
164 subprocess.run(["podman", "load", "--quiet"], stdin=stream.stdout, env=environment, check=True)
165 finally:
166 stream.stdout.close()
167 if stream.wait():
168 raise RuntimeError("dashboard image stream failed")
159169 env = {
160170 "SNOWGLOBE_DOMAIN": site["domain"], "SNOWGLOBE_PUBLIC_ORIGIN": "https://" + hostname,
161171 "SNOWGLOBE_FILE_ORIGIN": "https://files-" + hostname, "SNOWGLOBE_AUTH_RP_ID": hostname,
......@@ -209,7 +219,7 @@ def stage(computer=None):
209219 runtime = "/run/snowglobe-preview/" + identity
210220 computer_environment = []
211221 if saved.get("computer"):
212 ahk = snowglobe.command(*nix, "build", "--no-link", "--print-out-paths", "path:" + str(snowglobe.REPO) + "#computer-ahk", capture=True).stdout.strip()
222 ahk = snowglobe.command(*nix, "build", "--out-link", str(root / "computer-ahk"), "--print-out-paths", "path:" + str(snowglobe.REPO) + "#computer-ahk", capture=True).stdout.strip()
213223 computer_environment.append("--setenv=SNOWGLOBE_COMPUTER_AHK=" + ahk)
214224 if mac:
215225 with sqlite3.connect(f"file:{root / 'data/accounts.sqlite'}?mode=ro", uri=True) as accounts:
......@@ -218,7 +228,8 @@ def stage(computer=None):
218228 raise ValueError("The preview account is unavailable.")
219229 mac["owner"] = owner[0]
220230 computer_environment.append("--setenv=SNOWGLOBE_MAC_VM=" + json.dumps(mac))
221 host_python = snowglobe.command(*nix, "build", "--no-link", "--print-out-paths", "path:" + str(snowglobe.REPO) + "#host-python", capture=True).stdout.strip()
231 host_python = snowglobe.command(*nix, "build", "--out-link", str(root / "host-python"), "--print-out-paths", "path:" + str(snowglobe.REPO) + "#host-python", capture=True).stdout.strip()
232 subprocess.run(["systemctl", "stop", "snowglobe-preview-screen-" + identity], capture_output=True)
222233 subprocess.run(["systemctl", "stop", "snowglobe-preview-" + identity], capture_output=True)
223234 snowglobe.command("systemd-run", "--collect", "--quiet", "--unit=snowglobe-preview-" + identity,
224235 "--property=Restart=on-failure", "--property=MemoryMax=" + str(boundary.PROTOCOL["memory"]), "--property=TasksMax=32",
......@@ -231,6 +242,29 @@ def stage(computer=None):
231242 time.sleep(.1)
232243 else:
233244 raise RuntimeError("dashboard preview host socket did not start")
245 screen_volumes = []
246 if saved.get("computer"):
247 screen = snowglobe.command(*nix, "build", "--out-link", str(root / "vm-screen"), "--print-out-paths", "path:" + str(snowglobe.REPO) + "#vm-screen", capture=True).stdout.strip()
248 screen_runtime = "/run/snowglobe-preview-screen-" + identity
249 launcher = f"import os; os.chown({screen_runtime!r}, {saved['uid']}, {saved['uid']}); os.setgroups([]); os.setgid({saved['uid']}); os.setuid({saved['uid']}); os.execv({screen + '/bin/snowglobe-vm-screen'!r}, [{screen + '/bin/snowglobe-vm-screen'!r}])"
250 snowglobe.command("systemd-run", "--collect", "--quiet", "--unit=snowglobe-preview-screen-" + identity,
251 "--property=RuntimeDirectory=snowglobe-preview-screen-" + identity, "--property=RuntimeDirectoryMode=0700",
252 "--property=RuntimeDirectoryPreserve=yes",
253 "--property=Restart=on-failure", "--property=MemoryMax=2G", "--property=TasksMax=128",
254 "--property=NoNewPrivileges=yes", "--property=ProtectSystem=strict", "--property=ProtectHome=yes", "--property=PrivateTmp=yes",
255 "--property=DevicePolicy=closed",
256 "--setenv=SNOWGLOBE_HOST_SOCKET=" + runtime + "/host.sock",
257 "--setenv=SNOWGLOBE_VM_SCREEN_SOCKET=" + screen_runtime + "/screen.sock",
258 "--setenv=XDG_RUNTIME_DIR=" + screen_runtime,
259 "--setenv=XDG_CACHE_HOME=" + screen_runtime + "/cache",
260 host_python + "/bin/python3", "-c", launcher)
261 for _ in range(100):
262 if Path(screen_runtime, "screen.sock").exists():
263 break
264 time.sleep(.1)
265 else:
266 raise RuntimeError("dashboard preview screen socket did not start")
267 screen_volumes.append(screen_runtime + ":/run/snowglobe-vm-screen:ro")
234268 job = {"ID": identity, "Name": identity, "Type": "service", "Datacenters": ["clover"],
235269 "Meta": {"snowglobe_service": "dashboard", "snowglobe_name": "Snow Globe preview", "snowglobe_hostname": hostname},
236270 "TaskGroups": [{"Name": "app", "Count": 1, "Networks": [{"Mode": "host", "DynamicPorts": [{"Label": "http", "HostNetwork": "loopback"}]}],
......@@ -240,13 +274,14 @@ def stage(computer=None):
240274 "Config": {"image": image, "network_mode": "host", "cap_drop": ["ALL"], "security_opt": ["no-new-privileges"],
241275 "volumes": [str(root / "data") + ":/data:rw", str(root / "files") + ":/srv:rw,rslave", runtime + ":/run/snowglobe-host:ro",
242276 str(root / "nomad.token") + ":/run/secrets/nomad.token:ro",
243 str(proof) + ":/run/secrets/proxy.token:ro"]},
277 str(proof) + ":/run/secrets/proxy.token:ro", *screen_volumes]},
244278 "Resources": {"CPU": 1000, "MemoryMB": 2048}}]}]}
245279 snowglobe.command("nomad", "job", "run", "-json", "-", input=json.dumps(job))
246280 snowglobe.check_http(hostname, "/sign-in", site["tlsInternal"])
247281 saved["ready"] = True
248282 snowglobe.write_private(directory / f"{identity}.json", json.dumps(saved))
249283 except BaseException:
284 subprocess.run(["systemctl", "stop", "snowglobe-preview-screen-" + identity], capture_output=True)
250285 if created and (directory / f"{identity}.json").exists():
251286 destroy(identity)
252287 raise
......@@ -256,6 +291,7 @@ def stage(computer=None):
256291def destroy(identity):
257292 snowglobe.token()
258293 saved = metadata(identity)
294 subprocess.run(["systemctl", "stop", "snowglobe-preview-screen-" + identity], capture_output=True)
259295 subprocess.run(["systemctl", "stop", "snowglobe-preview-" + identity], capture_output=True)
260296 if saved.get("tokenAccessor"):
261297 revoked = subprocess.run(["nomad", "acl", "token", "delete", saved["tokenAccessor"]], capture_output=True, text=True)
......@@ -273,6 +309,7 @@ def destroy(identity):
273309 del saved["clone"]
274310 snowglobe.destroy_stage(identity, saved, {})
275311 shutil.rmtree(Path("/run/snowglobe-preview") / identity, ignore_errors=True)
312 shutil.rmtree(Path("/run") / ("snowglobe-preview-screen-" + identity), ignore_errors=True)
276313
277314
278315if __name__ == "__main__":
tools/vm-screen.py+4-6
......@@ -353,9 +353,6 @@ class Screen:
353353 self.last_rtp_pts = None
354354 self.sent_layout = None
355355 self.bwe = None
356 hardware = Gst.ElementFactory.make("nvh264enc")
357 encoder = "nvh264enc name=encoder preset=p4 tune=ultra-low-latency zerolatency=true bframes=0 rc-mode=cbr bitrate=4000 gop-size=60" if hardware else (
358 "x264enc name=encoder tune=zerolatency speed-preset=ultrafast bitrate=4000 key-int-max=60 bframes=0")
359356 width, height = self.desktop.width, self.desktop.height
360357 ssrc = struct.unpack("!I", os.urandom(4))[0] | 1
361358 # TWCC and RTX headers are added after payload packetization.
......@@ -363,7 +360,8 @@ class Screen:
363360 f"appsrc name=frames is-live=true format=time max-buffers=1 leaky-type=downstream "
364361 f"caps=video/x-raw,format=BGRx,width={width},height={height},framerate=60/1 "
365362 f"! videoconvert ! videoscale ! capsfilter name=size caps=video/x-raw,format=NV12,width={width + width % 2},height={height + height % 2} "
366 f"! {encoder} ! video/x-h264,profile=constrained-baseline ! h264parse "
363 "! x264enc name=encoder tune=zerolatency speed-preset=ultrafast bitrate=4000 key-int-max=60 bframes=0 "
364 "! video/x-h264,profile=constrained-baseline ! h264parse "
367365 f"! rtph264pay name=pay pt=96 ssrc={ssrc} mtu=1190 config-interval=-1 aggregate-mode=zero-latency "
368366 f"! capsfilter name=codec caps=application/x-rtp,media=video,encoding-name=H264,clock-rate=90000,ssrc=(uint){ssrc} "
369367 "! webrtcbin name=rtc bundle-policy=max-bundle")
......@@ -390,7 +388,7 @@ class Screen:
390388 self.bus.add_signal_watch()
391389 self.handlers.append((self.bus, self.bus.connect("message::error", self.pipeline_error)))
392390 self.pipeline.set_state(Gst.State.PLAYING)
393 self.send({"type": "display", "width": width, "height": height, "encoder": "nvenc" if hardware else "x264"})
391 self.send({"type": "display", "width": width, "height": height, "encoder": "x264"})
394392
395393 def close(self):
396394 self.writer.close()
......@@ -439,7 +437,7 @@ class Screen:
439437 self.send({"type": "display", "width": self.guest.width if self.guest and self.guest.pixels else width,
440438 "height": self.guest.height if self.guest and self.guest.pixels else height,
441439 "source": "guest" if self.guest and self.guest.pixels else "qemu",
442 "encoder": "nvenc" if self.pipeline.get_by_name("encoder").get_factory().get_name() == "nvh264enc" else "x264"})
440 "encoder": "x264"})
443441
444442 def refresh(self, _, info):
445443 event = info.get_event().get_structure()