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 {...@@ -389,6 +389,18 @@ pub fn router(app: Arc<App>) -> Router {
389#[cfg(test)]389#[cfg(test)]
390mod tests {390mod tests {
391 use super::*;391 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
392 #[test]404 #[test]
393 fn control_requires_the_current_owner_selected_vm_and_scope() {405 fn control_requires_the_current_owner_selected_vm_and_scope() {
394 let grant = json!({"user":"clover","scopes":["computer:read"],"resources":["m3-devbox"]});406 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};...@@ -9,7 +9,7 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
9pub fn visible(me: &Value, domain: &Value) -> bool {9pub fn visible(me: &Value, domain: &Value) -> bool {
10 let owner = &domain["owner"];10 let owner = &domain["owner"];
11 (owner.as_str().is_some_and(|id| !id.is_empty()) && owner == &me["id"])11 (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")
13}13}
1414
15pub async fn authorize(me: &Value, name: &str) -> Result<()> {15pub async fn authorize(me: &Value, name: &str) -> Result<()> {
...@@ -210,8 +210,9 @@ mod tests {...@@ -210,8 +210,9 @@ mod tests {
210 ));210 ));
211 let private = json!({"owner":"account-1","ownerOnly":true});211 let private = json!({"owner":"account-1","ownerOnly":true});
212 assert!(visible(&user, &private));212 assert!(visible(&user, &private));
213 assert!(!visible(&json!({"id":"admin","groups":["infra-admin"]}), &private));213 assert!(!visible(&json!({"id":"other","groups":["vm"]}), &private));
214 assert!(!visible(&json!({"id":"admin","groups":["infra-admin"]}), &json!({"ownerOnly":true})));214 assert!(visible(&json!({"id":"admin","groups":["infra-admin"]}), &private));
215 assert!(visible(&json!({"id":"admin","groups":["infra-admin"]}), &json!({"ownerOnly":true})));
215 }216 }
216217
217 #[test]218 #[test]
dashboard/web/components/VMConsole.tsx+54-21
...@@ -20,7 +20,8 @@ type WindowFrame = { windows: WindowSurface[]; rtpTimestamp: number };...@@ -20,7 +20,8 @@ type WindowFrame = { windows: WindowSurface[]; rtpTimestamp: number };
20export 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 }) {20export 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 }) {
21 const enabled = createMemo(() => props.enabled);21 const enabled = createMemo(() => props.enabled);
22 const [bootScreen, setBootScreen] = createSignal(false);22 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…");
24 const [now, setNow] = createSignal(Date.now());25 const [now, setNow] = createSignal(Date.now());
25 createEffect(() => {26 createEffect(() => {
26 if (!preparing() || !props.active) return;27 if (!preparing() || !props.active) return;
...@@ -89,6 +90,11 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e...@@ -89,6 +90,11 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
89 const [clipboard, setClipboard] = createSignal("");90 const [clipboard, setClipboard] = createSignal("");
90 const [sendingClipboard, setSendingClipboard] = createSignal(false);91 const [sendingClipboard, setSendingClipboard] = createSignal(false);
91 const [showClipboard, setShowClipboard] = createSignal(false);92 const [showClipboard, setShowClipboard] = createSignal(false);
93 let clipboardButton!: HTMLButtonElement;
94 const closeClipboard = () => {
95 setShowClipboard(false);
96 clipboardButton.closest("details")?.querySelector("summary")?.focus();
97 };
92 const [windowsAvailable, setWindowsAvailable] = createSignal(false);98 const [windowsAvailable, setWindowsAvailable] = createSignal(false);
93 const [separateWindows, setSeparateWindows] = createSignal(false);99 const [separateWindows, setSeparateWindows] = createSignal(false);
94 const [frameSync, setFrameSync] = createSignal(false);100 const [frameSync, setFrameSync] = createSignal(false);
...@@ -98,9 +104,10 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e...@@ -98,9 +104,10 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
98 const [capture, setCapture] = createSignal<{ fps: number; capture: number; scan: number; write: number }>();104 const [capture, setCapture] = createSignal<{ fps: number; capture: number; scan: number; write: number }>();
99 const [tools, setTools] = createSignal<HTMLDivElement>();105 const [tools, setTools] = createSignal<HTMLDivElement>();
100 const send = (message: object) => { if (input?.readyState === "open") input.send(JSON.stringify(message)); };106 const send = (message: object) => { if (input?.readyState === "open") input.send(JSON.stringify(message)); };
101 const compose = () => {107 const compose = (freeze = false) => {
102 const frame = windowFrame;108 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;
104 if (canvas.hidden || !props.active || !width || !height) return;111 if (canvas.hidden || !props.active || !width || !height) return;
105 if (canvas.width !== width || canvas.height !== height) { canvas.width = width; canvas.height = height; }112 if (canvas.width !== width || canvas.height !== height) { canvas.width = width; canvas.height = height; }
106 const context = canvas.getContext("2d", { alpha: false });113 const context = canvas.getContext("2d", { alpha: false });
...@@ -155,6 +162,7 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e...@@ -155,6 +162,7 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
155 let peer: RTCPeerConnection | undefined;162 let peer: RTCPeerConnection | undefined;
156 let frameCallback: number | undefined;163 let frameCallback: number | undefined;
157 let statsTimer: number | undefined;164 let statsTimer: number | undefined;
165 let recoveryTimer: number | undefined;
158 let previousStats: { time: number; bytes: number; frames: number } | undefined;166 let previousStats: { time: number; bytes: number; frames: number } | undefined;
159 const frameReady: VideoFrameRequestCallback = (_, metadata) => {167 const frameReady: VideoFrameRequestCallback = (_, metadata) => {
160 if (current !== attempt || disposed) return;168 if (current !== attempt || disposed) return;
...@@ -212,12 +220,16 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e...@@ -212,12 +220,16 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
212 if (current !== attempt || disposed || video.readyState < 2 || input?.readyState !== "open") return;220 if (current !== attempt || disposed || video.readyState < 2 || input?.readyState !== "open") return;
213 clearTimeout(timeout);221 clearTimeout(timeout);
214 retryDelay = 1000;222 retryDelay = 1000;
223 setHasFrame(true);
215 setState("Connected");224 setState("Connected");
225 if (document.activeElement === video) focus();
216 };226 };
217 disconnect = () => {227 disconnect = () => {
218 clearTimeout(reconnectTimer);228 clearTimeout(reconnectTimer);
219 clearTimeout(timeout);229 clearTimeout(timeout);
230 clearTimeout(recoveryTimer);
220 blur();231 blur();
232 compose(true);
221 video.removeEventListener("keydown", releaseKeyboard, true);233 video.removeEventListener("keydown", releaseKeyboard, true);
222 video.removeEventListener("focus", focus);234 video.removeEventListener("focus", focus);
223 video.removeEventListener("blur", blur);235 video.removeEventListener("blur", blur);
...@@ -228,10 +240,9 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e...@@ -228,10 +240,9 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
228 if (frameCallback !== undefined) video.cancelVideoFrameCallback(frameCallback);240 if (frameCallback !== undefined) video.cancelVideoFrameCallback(frameCallback);
229 windowFrame = undefined; pendingFrames = []; frameTimestamp = undefined; drag = undefined;241 windowFrame = undefined; pendingFrames = []; frameTimestamp = undefined; drag = undefined;
230 movement = undefined; pointerSequence = 0;242 movement = undefined; pointerSequence = 0;
231 setFrameSync(false); setWindowsAvailable(false); setWindowCapture(false); setStats(undefined); setCapture(undefined);243 setFrameSync(false); setWindowsAvailable(false); setWindowCapture(false);
232 video.onplaying = null;244 video.onplaying = null;
233 video.srcObject = null;245 video.srcObject = null;
234 compose();
235 clearTimeout(cursorTimer);246 clearTimeout(cursorTimer);
236 cursorFrames = [];247 cursorFrames = [];
237 cursorFrame = 0;248 cursorFrame = 0;
...@@ -261,13 +272,21 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e...@@ -261,13 +272,21 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
261 keyboard.onkeyevent = (key, _code, down) => { if (acceptsInput()) send({ type: "key", key, down }); };272 keyboard.onkeyevent = (key, _code, down) => { if (acceptsInput()) send({ type: "key", key, down }); };
262 input.onopen = () => {273 input.onopen = () => {
263 ready();274 ready();
264 if (document.activeElement === video) focus();
265 if (windowsAvailable()) send({ type: "experiment", enabled: separateWindows() });275 if (windowsAvailable()) send({ type: "experiment", enabled: separateWindows() });
266 };276 };
267 peer.onicecandidate = ({ candidate }) => { if (candidate) signal({ type: "candidate", ...candidate.toJSON() }); };277 peer.onicecandidate = ({ candidate }) => { if (candidate) signal({ type: "candidate", ...candidate.toJSON() }); };
268 peer.onconnectionstatechange = () => {278 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") {
270 fail("The screen disconnected. Reconnect to try again.");285 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);
271 }290 }
272 };291 };
273 peer.ontrack = ({ track }) => {292 peer.ontrack = ({ track }) => {
...@@ -360,25 +379,28 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e...@@ -360,25 +379,28 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
360 setState("Disconnected");379 setState("Disconnected");
361 }380 }
362 };381 };
363 createEffect(() => {382 createEffect(on(enabled, value => {
364 if (enabled()) void connect();383 if (value) void connect();
365 else {384 else {
366 attempt++;385 attempt++;
367 disconnect();386 disconnect();
368 input = undefined;387 input = undefined;
369 setState("Disconnected");388 setState("Disconnected");
370 setProblem("");389 setProblem("");
390 setHasFrame(false);
391 setStats(undefined); setCapture(undefined);
392 canvas.hidden = true;
371 }393 }
372 });394 }));
373 createEffect(() => {395 createEffect(() => {
374 if (acceptsInput()) return;396 if (acceptsInput()) return;
375 release();397 release();
376 if (document.activeElement === video) {398 if (document.activeElement === video && (!props.active || !enabled() || preparing())) {
377 video.blur();399 video.blur();
378 if (props.active) tools()?.querySelector<HTMLElement>("summary")?.focus();400 if (props.active) tools()?.querySelector<HTMLElement>("summary")?.focus();
379 }401 }
380 });402 });
381 createEffect(on(() => props.setup, setup => {403 createEffect(on(createMemo(() => props.setup), setup => {
382 if (setup === "ready" && enabled() && state() === "Disconnected") void connect();404 if (setup === "ready" && enabled() && state() === "Disconnected") void connect();
383 }, { defer: true }));405 }, { defer: true }));
384 let playbackFrame: number | undefined;406 let playbackFrame: number | undefined;
...@@ -396,7 +418,12 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e...@@ -396,7 +418,12 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
396 });418 });
397 };419 };
398 createEffect(on(() => [props.active, enabled(), preparing()], resumeVideo));420 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 }));
400 document.addEventListener("visibilitychange", resumeVideo);427 document.addEventListener("visibilitychange", resumeVideo);
401 onCleanup(() => {428 onCleanup(() => {
402 disposed = true;429 disposed = true;
...@@ -439,7 +466,7 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e...@@ -439,7 +466,7 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
439 <Show when={props.active}>466 <Show when={props.active}>
440 <VMMenu label="Input">467 <VMMenu label="Input">
441 <Show when={props.platform !== "macos"}><button disabled={!connected()} onClick={ctrlAltDel}><Keyboard size={14} aria-hidden="true" />Ctrl+Alt+Del</button></Show>468 <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>
443 </VMMenu>470 </VMMenu>
444 <VMMenu label="View">471 <VMMenu label="View">
445 <button disabled={!connected() || !windowsAvailable() || !frameSync()} aria-pressed={separateWindows()}472 <button disabled={!connected() || !windowsAvailable() || !frameSync()} aria-pressed={separateWindows()}
...@@ -453,20 +480,24 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e...@@ -453,20 +480,24 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
453 </VMMenu>480 </VMMenu>
454 <span class="spacer" />481 <span class="spacer" />
455 <Show when={connected() && !preparing()}><span class="vm-console-status" id={`keyboard-release-${props.name}`}>Ctrl+Alt+Esc releases keyboard</span></Show>482 <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>
457 </Show>484 </Show>
458 </div>485 </div>
459 <Show when={props.active && showStats()}>486 <Show when={props.active && showStats()}>
460 <div class="vm-stream-stats">487 <div class="vm-stream-stats" style={{ visibility: connected() ? "visible" : "hidden" }}>
461 <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>488 <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>
462 <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>489 <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>
463 <Show when={separateWindows()}><span>{windowCapture() ? "Separate windows" : "Desktop fallback"}</span></Show>490 <Show when={separateWindows()}><span>{windowCapture() ? "Separate windows" : "Desktop fallback"}</span></Show>
464 </div>491 </div>
465 </Show>492 </Show>
466 <Show when={props.active && !preparing() && problem()}><p class="error vm-console-error" role="alert">{problem()}</p></Show>
467 <Show when={props.active && showClipboard()}>493 <Show when={props.active && showClipboard()}>
468 <div class="vm-clipboard">494 <div class="vm-clipboard" id={`clipboard-${props.name}`} onKeyDown={(event) => {
469 <label class="field">clipboard<textarea class="search" rows="3" value={clipboard()} onInput={(event) => setClipboard(event.currentTarget.value)} /></label>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>
470 <span class="hint">{props.clipboard ? "Send text, then paste in the VM with Command+V." : "Clipboard sharing requires support in the guest"}</span>501 <span class="hint">{props.clipboard ? "Send text, then paste in the VM with Command+V." : "Clipboard sharing requires support in the guest"}</span>
471 <div class="row">502 <div class="row">
472 <button class="button small" disabled={!connected() || sendingClipboard()} aria-busy={sendingClipboard()} onClick={async () => {503 <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...@@ -477,21 +508,23 @@ export function VMConsole(props: { name: string; platform: Domain["platform"]; e
477 finally { setSendingClipboard(false); }508 finally { setSendingClipboard(false); }
478 }}>send text</button>509 }}>send text</button>
479 <button class="button small" onClick={() => navigator.clipboard.writeText(clipboard()).catch((failure) => setProblem(reason(failure)))}>copy text</button>510 <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>
480 </div>512 </div>
481 </div>513 </div>
482 </Show>514 </Show>
483 <div class="vm-console-screen" hidden={!props.active || !props.enabled}>515 <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>
484 <Show when={preparing()}>517 <Show when={preparing()}>
485 <div class="empty vm-setup">518 <div class="empty vm-setup">
486 <div role={props.setup === "failed" ? "alert" : "status"}>519 <div role={props.setup === "failed" ? "alert" : "status"}>
487 <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>520 <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>
488 <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>521 <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>
489 </div>522 </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>
491 <button class="button small" onClick={() => setBootScreen(true)}>View boot screen</button>524 <button class="button small" onClick={() => setBootScreen(true)}>View boot screen</button>
492 </div>525 </div>
493 </Show>526 </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}`}
495 onContextMenu={(event) => event.preventDefault()}528 onContextMenu={(event) => event.preventDefault()}
496 onPointerMove={pointer}529 onPointerMove={pointer}
497 onPointerLeave={() => { cursorInside = false; drawCursor(); }}530 onPointerLeave={() => { cursorInside = false; drawCursor(); }}
dashboard/web/pages/VMs.css+4-2
...@@ -87,7 +87,7 @@...@@ -87,7 +87,7 @@
87.vm-stream-stats { display: flex; flex-wrap: wrap; gap: 4px 16px; padding: 6px 12px; font-size: 12px; font-variant-numeric: tabular-nums; }87.vm-stream-stats { display: flex; flex-wrap: wrap; gap: 4px 16px; padding: 6px 12px; font-size: 12px; font-variant-numeric: tabular-nums; }
88.vm-cursor { display: none; position: fixed; left: 0; top: 0; pointer-events: none; }88.vm-cursor { display: none; position: fixed; left: 0; top: 0; pointer-events: none; }
89.vm-cursor-invert { mix-blend-mode: difference; }89.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); }
91.vm-clipboard { display: grid; gap: 8px; padding: 12px; }91.vm-clipboard { display: grid; gap: 8px; padding: 12px; }
92@media (max-width: 640px) {92@media (max-width: 640px) {
93 .vm-detail .facts, .vm-charts { grid-template-columns: 1fr; }93 .vm-detail .facts, .vm-charts { grid-template-columns: 1fr; }
...@@ -114,7 +114,8 @@...@@ -114,7 +114,8 @@
114.vm-count { font-size: 12px; }114.vm-count { font-size: 12px; }
115115
116.vms.vm-active { padding: 0; max-width: none; width: 100%; }116.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); }
118.vm-active .list-body.flush > .vm-workspace { margin: 0; flex: 1; min-height: 0; }119.vm-active .list-body.flush > .vm-workspace { margin: 0; flex: 1; min-height: 0; }
119.vm-workspace { display: flex; flex-direction: column; }120.vm-workspace { display: flex; flex-direction: column; }
120.vms .vm-home { display: none; }121.vms .vm-home { display: none; }
...@@ -134,6 +135,7 @@...@@ -134,6 +135,7 @@
134.vm-menu summary::-webkit-details-marker { display: none; }135.vm-menu summary::-webkit-details-marker { display: none; }
135.vm-menu summary:hover, .vm-menu[open] summary { background: var(--hover); }136.vm-menu summary:hover, .vm-menu[open] summary { background: var(--hover); }
136.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; }137.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; }
137.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; }139.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; }
138.vm-menu > div > button svg { flex: none; }140.vm-menu > div > button svg { flex: none; }
139.vm-menu > div > button:hover:not(:disabled) { background: var(--hover); }141.vm-menu > div > button:hover:not(:disabled) { background: var(--hover); }
flake.nix+1
...@@ -6,6 +6,7 @@...@@ -6,6 +6,7 @@
6 outputs = { self, nixpkgs, ... }: {6 outputs = { self, nixpkgs, ... }: {
7 packages.x86_64-linux.dashboard = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/dashboard.nix { };7 packages.x86_64-linux.dashboard = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/dashboard.nix { };
8 packages.x86_64-linux.host-python = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/host-python.nix { };8 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 { };
9 packages.x86_64-linux.computer-ahk = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/computer-ahk.nix { };10 packages.x86_64-linux.computer-ahk = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/computer-ahk.nix { };
10 packages.x86_64-linux.dashboard-image = self.packages.x86_64-linux.dashboard.image;11 packages.x86_64-linux.dashboard-image = self.packages.x86_64-linux.dashboard.image;
11 packages.x86_64-linux.installer = self.nixosConfigurations.installer.config.system.build.isoImage;12 packages.x86_64-linux.installer = self.nixosConfigurations.installer.config.system.build.isoImage;
nixos/configuration.nix+4-11
...@@ -6,13 +6,12 @@ let...@@ -6,13 +6,12 @@ let
6 proxyToken = "/var/lib/snowglobe/dashboard-proxy.token";6 proxyToken = "/var/lib/snowglobe/dashboard-proxy.token";
7 hostTools = lib.fileset.toSource {7 hostTools = lib.fileset.toSource {
8 root = ../.;8 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 ];
10 };10 };
11 computerAhk = pkgs.callPackage ./computer-ahk.nix { };11 computerAhk = pkgs.callPackage ./computer-ahk.nix { };
12 nativePkl = pkgs.callPackage ./pkl.nix { };12 nativePkl = pkgs.callPackage ./pkl.nix { };
13 hostPython = pkgs.callPackage ./host-python.nix { };13 hostPython = pkgs.callPackage ./host-python.nix { };
14 screenPython = pkgs.python3.withPackages (packages: [ packages.pygobject3 packages.gst-python ]);14 screen = pkgs.callPackage ./vm-screen.nix { };
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) ];
16 secureOvmf = pkgs.OVMF.override {15 secureOvmf = pkgs.OVMF.override {
17 secureBoot = true;16 secureBoot = true;
18 msVarsTemplate = true;17 msVarsTemplate = true;
...@@ -157,14 +156,9 @@ in...@@ -157,14 +156,9 @@ in
157 wantedBy = [ "multi-user.target" ];156 wantedBy = [ "multi-user.target" ];
158 wants = [ "snowglobe-host.service" ];157 wants = [ "snowglobe-host.service" ];
159 after = [ "snowglobe-host.service" ];158 after = [ "snowglobe-host.service" ];
160 environment = {159 environment.XDG_RUNTIME_DIR = "/run/snowglobe-vm-screen";
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 };
166 serviceConfig = {160 serviceConfig = {
167 ExecStart = "${screenPython}/bin/python3 ${hostTools}/tools/vm-screen.py";161 ExecStart = "${screen}/bin/snowglobe-vm-screen";
168 User = "snowglobe-dashboard";162 User = "snowglobe-dashboard";
169 Group = "snowglobe-dashboard";163 Group = "snowglobe-dashboard";
170 RuntimeDirectory = "snowglobe-vm-screen";164 RuntimeDirectory = "snowglobe-vm-screen";
...@@ -179,7 +173,6 @@ in...@@ -179,7 +173,6 @@ in
179 MemoryMax = "2G";173 MemoryMax = "2G";
180 TasksMax = 128;174 TasksMax = 128;
181 DevicePolicy = "closed";175 DevicePolicy = "closed";
182 DeviceAllow = lib.optional (builtins.elem "nvidia" config.services.xserver.videoDrivers) "char-nvidia* rw";
183 RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" "AF_NETLINK" ];176 RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" "AF_NETLINK" ];
184 Restart = "always";177 Restart = "always";
185 RestartSec = 2;178 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):...@@ -57,7 +57,7 @@ class PreviewBoundary(unittest.TestCase):
57 def test_attached_computer_cannot_target_another_vm(self):57 def test_attached_computer_cannot_target_another_vm(self):
58 host = stage.PreviewHost({"clone": "globe/staging/fixture", "computer": "mac-fixture"})58 host = stage.PreviewHost({"clone": "globe/staging/fixture", "computer": "mac-fixture"})
59 with patch.object(boundary.Host, "handle", return_value={"ok": True}) as call:59 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"]:
61 with self.assertRaises(boundary.dashboard_runs.Error):61 with self.assertRaises(boundary.dashboard_runs.Error):
62 host.handle({"operation": operation, "payload": {"name": "personal"}})62 host.handle({"operation": operation, "payload": {"name": "personal"}})
63 call.assert_not_called()63 call.assert_not_called()
...@@ -125,8 +125,15 @@ class PreviewProof(unittest.TestCase):...@@ -125,8 +125,15 @@ class PreviewProof(unittest.TestCase):
125 with tempfile.TemporaryDirectory() as temporary:125 with tempfile.TemporaryDirectory() as temporary:
126 root = Path(temporary)126 root = Path(temporary)
127 state = root / "state"127 state = root / "state"
128 (root / "srv/staging").mkdir(parents=True)
128 site = {"root": str(root / "srv"), "domain": "fixture.test", "pool": "fixture", "nodeName": "fixture", "tlsInternal": True}129 site = {"root": str(root / "srv"), "domain": "fixture.test", "pool": "fixture", "nodeName": "fixture", "tlsInternal": True}
129 jobs = []130 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
131 def command(*args, **kwargs):138 def command(*args, **kwargs):
132 output = ""139 output = ""
...@@ -140,17 +147,31 @@ class PreviewProof(unittest.TestCase):...@@ -140,17 +147,31 @@ class PreviewProof(unittest.TestCase):
140 output = json.dumps({"username": "preview", "password": "disposable-fixture-password"})147 output = json.dumps({"username": "preview", "password": "disposable-fixture-password"})
141 elif args[:3] == ("nomad", "job", "run"):148 elif args[:3] == ("nomad", "job", "run"):
142 jobs.append(json.loads(kwargs["input"]))149 jobs.append(json.loads(kwargs["input"]))
150 elif args[0] == "systemd-run":
151 units.append(args)
143 return subprocess.CompletedProcess(args, 0, stdout=output)152 return subprocess.CompletedProcess(args, 0, stdout=output)
144153
145 def run(args, **kwargs):154 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)
146 return subprocess.CompletedProcess(args, 0, stdout="fixture-image" if args[0] == "nix" else "")164 return subprocess.CompletedProcess(args, 0, stdout="fixture-image" if args[0] == "nix" else "")
147165
148 original_exists = Path.exists166 original_exists = Path.exists
149 def exists(path):167 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()):
153 stage.stage()173 stage.stage()
174 self.assertTrue(any(target.endswith("#dashboard-image.stream") for build in builds for target in build))
154 check_http.assert_called_once_with(jobs[-1]["ID"] + ".fixture.test", "/sign-in", True)175 check_http.assert_called_once_with(jobs[-1]["ID"] + ".fixture.test", "/sign-in", True)
155 task = jobs[-1]["TaskGroups"][0]["Tasks"][0]176 task = jobs[-1]["TaskGroups"][0]["Tasks"][0]
156 check = jobs[-1]["TaskGroups"][0]["Services"][0]["Checks"][0]177 check = jobs[-1]["TaskGroups"][0]["Services"][0]["Checks"][0]
...@@ -169,6 +190,12 @@ class PreviewProof(unittest.TestCase):...@@ -169,6 +190,12 @@ class PreviewProof(unittest.TestCase):
169 stage.stage()190 stage.stage()
170 self.assertEqual(proof.read_text(), value)191 self.assertEqual(proof.read_text(), value)
171 self.assertEqual(jobs[-1]["ID"], jobs[-2]["ID"])192 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
174class NativePreview(unittest.TestCase):201class NativePreview(unittest.TestCase):
tools/dashboard-stage.py+48-11
...@@ -12,6 +12,7 @@ import socket...@@ -12,6 +12,7 @@ import socket
12import sqlite312import sqlite3
13import subprocess13import subprocess
14import sys14import sys
15import tempfile
15import threading16import threading
16import time17import time
1718
...@@ -37,7 +38,7 @@ class PreviewHost(boundary.Host):...@@ -37,7 +38,7 @@ class PreviewHost(boundary.Host):
37 def handle(self, request):38 def handle(self, request):
38 operation = request.get("operation") if isinstance(request, dict) else None39 operation = request.get("operation") if isinstance(request, dict) else None
39 owner = None40 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"}:
41 with sqlite3.connect(f"file:{self.stage['mount']}/data/accounts.sqlite?mode=ro", uri=True) as accounts:42 with sqlite3.connect(f"file:{self.stage['mount']}/data/accounts.sqlite?mode=ro", uri=True) as accounts:
42 owner = accounts.execute("SELECT id FROM users WHERE username='preview'").fetchone()43 owner = accounts.execute("SELECT id FROM users WHERE username='preview'").fetchone()
43 if not owner:44 if not owner:
...@@ -46,7 +47,7 @@ class PreviewHost(boundary.Host):...@@ -46,7 +47,7 @@ class PreviewHost(boundary.Host):
46 payload = request.get("payload")47 payload = request.get("payload")
47 if not owner or not isinstance(payload, dict) or payload.get("owner") != owner[0]:48 if not owner or not isinstance(payload, dict) or payload.get("owner") != owner[0]:
48 raise boundary.dashboard_runs.Error(403, "Test VMs must belong to this preview account.")49 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"}:
50 payload = request.get("payload")51 payload = request.get("payload")
51 if not self.stage.get("computer") or not isinstance(payload, dict):52 if not self.stage.get("computer") or not isinstance(payload, dict):
52 raise boundary.dashboard_runs.Error(403, "Choose the VM explicitly attached to this preview.")53 raise boundary.dashboard_runs.Error(403, "Choose the VM explicitly attached to this preview.")
...@@ -104,14 +105,14 @@ def serve(stage):...@@ -104,14 +105,14 @@ def serve(stage):
104 os.chown(path, 0, saved["uid"])105 os.chown(path, 0, saved["uid"])
105 path.chmod(0o660)106 path.chmod(0o660)
106 listener.listen(8)107 listener.listen(8)
107 slots = threading.BoundedSemaphore(8)108 slots = threading.BoundedSemaphore(16)
108 with concurrent.futures.ThreadPoolExecutor(max_workers=4) as workers:109 with concurrent.futures.ThreadPoolExecutor(max_workers=12) as workers:
109 while True:110 while True:
110 connection, _ = listener.accept()111 connection, _ = listener.accept()
111 if not slots.acquire(blocking=False):112 if not slots.acquire(blocking=False):
112 connection.close()113 connection.close()
113 continue114 continue
114 operations = OPERATIONS | {"vm.computer", "vm.owner", "vm.ai", "vm.act"} if saved.get("computer") else OPERATIONS115 operations = OPERATIONS | {"vm.computer", "vm.owner", "vm.ai", "vm.act", "vm.console", "vm.guest"} if saved.get("computer") else OPERATIONS
115 work = workers.submit(boundary.serve_connection, connection, host, saved["uid"], operations)116 work = workers.submit(boundary.serve_connection, connection, host, saved["uid"], operations)
116 work.add_done_callback(lambda _: slots.release())117 work.add_done_callback(lambda _: slots.release())
117118
...@@ -153,9 +154,18 @@ def stage(computer=None):...@@ -153,9 +154,18 @@ def stage(computer=None):
153 image = subprocess.run([*nix, "eval", "--raw", package, "--apply", 'image: "${image.imageName}:${image.imageTag}"'],154 image = subprocess.run([*nix, "eval", "--raw", package, "--apply", 'image: "${image.imageName}:${image.imageTag}"'],
154 stdout=subprocess.PIPE, text=True, check=True).stdout.strip()155 stdout=subprocess.PIPE, text=True, check=True).stdout.strip()
155 if subprocess.run(["podman", "image", "exists", image], capture_output=True).returncode:156 if subprocess.run(["podman", "image", "exists", image], capture_output=True).returncode:
156 built = subprocess.run([*nix, "build", package, "--no-link", "--print-out-paths"],157 with tempfile.TemporaryDirectory(prefix="dashboard-image-", dir=site["stagingRoot"]) as temporary:
157 stdout=subprocess.PIPE, text=True, check=True).stdout.strip()158 built = subprocess.run([*nix, "build", package + ".stream", "--option", "build-dir", temporary,
158 snowglobe.command("podman", "load", "--quiet", "--input", built)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")
159 env = {169 env = {
160 "SNOWGLOBE_DOMAIN": site["domain"], "SNOWGLOBE_PUBLIC_ORIGIN": "https://" + hostname,170 "SNOWGLOBE_DOMAIN": site["domain"], "SNOWGLOBE_PUBLIC_ORIGIN": "https://" + hostname,
161 "SNOWGLOBE_FILE_ORIGIN": "https://files-" + hostname, "SNOWGLOBE_AUTH_RP_ID": hostname,171 "SNOWGLOBE_FILE_ORIGIN": "https://files-" + hostname, "SNOWGLOBE_AUTH_RP_ID": hostname,
...@@ -209,7 +219,7 @@ def stage(computer=None):...@@ -209,7 +219,7 @@ def stage(computer=None):
209 runtime = "/run/snowglobe-preview/" + identity219 runtime = "/run/snowglobe-preview/" + identity
210 computer_environment = []220 computer_environment = []
211 if saved.get("computer"):221 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()
213 computer_environment.append("--setenv=SNOWGLOBE_COMPUTER_AHK=" + ahk)223 computer_environment.append("--setenv=SNOWGLOBE_COMPUTER_AHK=" + ahk)
214 if mac:224 if mac:
215 with sqlite3.connect(f"file:{root / 'data/accounts.sqlite'}?mode=ro", uri=True) as accounts:225 with sqlite3.connect(f"file:{root / 'data/accounts.sqlite'}?mode=ro", uri=True) as accounts:
...@@ -218,7 +228,8 @@ def stage(computer=None):...@@ -218,7 +228,8 @@ def stage(computer=None):
218 raise ValueError("The preview account is unavailable.")228 raise ValueError("The preview account is unavailable.")
219 mac["owner"] = owner[0]229 mac["owner"] = owner[0]
220 computer_environment.append("--setenv=SNOWGLOBE_MAC_VM=" + json.dumps(mac))230 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)
222 subprocess.run(["systemctl", "stop", "snowglobe-preview-" + identity], capture_output=True)233 subprocess.run(["systemctl", "stop", "snowglobe-preview-" + identity], capture_output=True)
223 snowglobe.command("systemd-run", "--collect", "--quiet", "--unit=snowglobe-preview-" + identity,234 snowglobe.command("systemd-run", "--collect", "--quiet", "--unit=snowglobe-preview-" + identity,
224 "--property=Restart=on-failure", "--property=MemoryMax=" + str(boundary.PROTOCOL["memory"]), "--property=TasksMax=32",235 "--property=Restart=on-failure", "--property=MemoryMax=" + str(boundary.PROTOCOL["memory"]), "--property=TasksMax=32",
...@@ -231,6 +242,29 @@ def stage(computer=None):...@@ -231,6 +242,29 @@ def stage(computer=None):
231 time.sleep(.1)242 time.sleep(.1)
232 else:243 else:
233 raise RuntimeError("dashboard preview host socket did not start")244 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")
234 job = {"ID": identity, "Name": identity, "Type": "service", "Datacenters": ["clover"],268 job = {"ID": identity, "Name": identity, "Type": "service", "Datacenters": ["clover"],
235 "Meta": {"snowglobe_service": "dashboard", "snowglobe_name": "Snow Globe preview", "snowglobe_hostname": hostname},269 "Meta": {"snowglobe_service": "dashboard", "snowglobe_name": "Snow Globe preview", "snowglobe_hostname": hostname},
236 "TaskGroups": [{"Name": "app", "Count": 1, "Networks": [{"Mode": "host", "DynamicPorts": [{"Label": "http", "HostNetwork": "loopback"}]}],270 "TaskGroups": [{"Name": "app", "Count": 1, "Networks": [{"Mode": "host", "DynamicPorts": [{"Label": "http", "HostNetwork": "loopback"}]}],
...@@ -240,13 +274,14 @@ def stage(computer=None):...@@ -240,13 +274,14 @@ def stage(computer=None):
240 "Config": {"image": image, "network_mode": "host", "cap_drop": ["ALL"], "security_opt": ["no-new-privileges"],274 "Config": {"image": image, "network_mode": "host", "cap_drop": ["ALL"], "security_opt": ["no-new-privileges"],
241 "volumes": [str(root / "data") + ":/data:rw", str(root / "files") + ":/srv:rw,rslave", runtime + ":/run/snowglobe-host:ro",275 "volumes": [str(root / "data") + ":/data:rw", str(root / "files") + ":/srv:rw,rslave", runtime + ":/run/snowglobe-host:ro",
242 str(root / "nomad.token") + ":/run/secrets/nomad.token:ro",276 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]},
244 "Resources": {"CPU": 1000, "MemoryMB": 2048}}]}]}278 "Resources": {"CPU": 1000, "MemoryMB": 2048}}]}]}
245 snowglobe.command("nomad", "job", "run", "-json", "-", input=json.dumps(job))279 snowglobe.command("nomad", "job", "run", "-json", "-", input=json.dumps(job))
246 snowglobe.check_http(hostname, "/sign-in", site["tlsInternal"])280 snowglobe.check_http(hostname, "/sign-in", site["tlsInternal"])
247 saved["ready"] = True281 saved["ready"] = True
248 snowglobe.write_private(directory / f"{identity}.json", json.dumps(saved))282 snowglobe.write_private(directory / f"{identity}.json", json.dumps(saved))
249 except BaseException:283 except BaseException:
284 subprocess.run(["systemctl", "stop", "snowglobe-preview-screen-" + identity], capture_output=True)
250 if created and (directory / f"{identity}.json").exists():285 if created and (directory / f"{identity}.json").exists():
251 destroy(identity)286 destroy(identity)
252 raise287 raise
...@@ -256,6 +291,7 @@ def stage(computer=None):...@@ -256,6 +291,7 @@ def stage(computer=None):
256def destroy(identity):291def destroy(identity):
257 snowglobe.token()292 snowglobe.token()
258 saved = metadata(identity)293 saved = metadata(identity)
294 subprocess.run(["systemctl", "stop", "snowglobe-preview-screen-" + identity], capture_output=True)
259 subprocess.run(["systemctl", "stop", "snowglobe-preview-" + identity], capture_output=True)295 subprocess.run(["systemctl", "stop", "snowglobe-preview-" + identity], capture_output=True)
260 if saved.get("tokenAccessor"):296 if saved.get("tokenAccessor"):
261 revoked = subprocess.run(["nomad", "acl", "token", "delete", saved["tokenAccessor"]], capture_output=True, text=True)297 revoked = subprocess.run(["nomad", "acl", "token", "delete", saved["tokenAccessor"]], capture_output=True, text=True)
...@@ -273,6 +309,7 @@ def destroy(identity):...@@ -273,6 +309,7 @@ def destroy(identity):
273 del saved["clone"]309 del saved["clone"]
274 snowglobe.destroy_stage(identity, saved, {})310 snowglobe.destroy_stage(identity, saved, {})
275 shutil.rmtree(Path("/run/snowglobe-preview") / identity, ignore_errors=True)311 shutil.rmtree(Path("/run/snowglobe-preview") / identity, ignore_errors=True)
312 shutil.rmtree(Path("/run") / ("snowglobe-preview-screen-" + identity), ignore_errors=True)
276313
277314
278if __name__ == "__main__":315if __name__ == "__main__":
tools/vm-screen.py+4-6
...@@ -353,9 +353,6 @@ class Screen:...@@ -353,9 +353,6 @@ class Screen:
353 self.last_rtp_pts = None353 self.last_rtp_pts = None
354 self.sent_layout = None354 self.sent_layout = None
355 self.bwe = None355 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")
359 width, height = self.desktop.width, self.desktop.height356 width, height = self.desktop.width, self.desktop.height
360 ssrc = struct.unpack("!I", os.urandom(4))[0] | 1357 ssrc = struct.unpack("!I", os.urandom(4))[0] | 1
361 # TWCC and RTX headers are added after payload packetization.358 # TWCC and RTX headers are added after payload packetization.
...@@ -363,7 +360,8 @@ class Screen:...@@ -363,7 +360,8 @@ class Screen:
363 f"appsrc name=frames is-live=true format=time max-buffers=1 leaky-type=downstream "360 f"appsrc name=frames is-live=true format=time max-buffers=1 leaky-type=downstream "
364 f"caps=video/x-raw,format=BGRx,width={width},height={height},framerate=60/1 "361 f"caps=video/x-raw,format=BGRx,width={width},height={height},framerate=60/1 "
365 f"! videoconvert ! videoscale ! capsfilter name=size caps=video/x-raw,format=NV12,width={width + width % 2},height={height + height % 2} "362 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 "
367 f"! rtph264pay name=pay pt=96 ssrc={ssrc} mtu=1190 config-interval=-1 aggregate-mode=zero-latency "365 f"! rtph264pay name=pay pt=96 ssrc={ssrc} mtu=1190 config-interval=-1 aggregate-mode=zero-latency "
368 f"! capsfilter name=codec caps=application/x-rtp,media=video,encoding-name=H264,clock-rate=90000,ssrc=(uint){ssrc} "366 f"! capsfilter name=codec caps=application/x-rtp,media=video,encoding-name=H264,clock-rate=90000,ssrc=(uint){ssrc} "
369 "! webrtcbin name=rtc bundle-policy=max-bundle")367 "! webrtcbin name=rtc bundle-policy=max-bundle")
...@@ -390,7 +388,7 @@ class Screen:...@@ -390,7 +388,7 @@ class Screen:
390 self.bus.add_signal_watch()388 self.bus.add_signal_watch()
391 self.handlers.append((self.bus, self.bus.connect("message::error", self.pipeline_error)))389 self.handlers.append((self.bus, self.bus.connect("message::error", self.pipeline_error)))
392 self.pipeline.set_state(Gst.State.PLAYING)390 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
395 def close(self):393 def close(self):
396 self.writer.close()394 self.writer.close()
...@@ -439,7 +437,7 @@ class Screen:...@@ -439,7 +437,7 @@ class Screen:
439 self.send({"type": "display", "width": self.guest.width if self.guest and self.guest.pixels else width,437 self.send({"type": "display", "width": self.guest.width if self.guest and self.guest.pixels else width,
440 "height": self.guest.height if self.guest and self.guest.pixels else height,438 "height": self.guest.height if self.guest and self.guest.pixels else height,
441 "source": "guest" if self.guest and self.guest.pixels else "qemu",439 "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
444 def refresh(self, _, info):442 def refresh(self, _, info):
445 event = info.get_event().get_structure()443 event = info.get_event().get_structure()