diff options
| author | Danilo M. <danix@danix.xyz> | 2026-11-13 20:34:47 +0100 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-11-13 20:34:47 +0100 |
| commit | 69e6ce2bf7272797cf08ee4fbe0ff9fe0fc3b1f4 (patch) | |
| tree | b69bc4f6f8a092605cb2fac82328e340a5071ae2 /static | |
| parent | f9330aff36868b723fd6918686360ea8992b285e (diff) | |
| download | deck-it-69e6ce2bf7272797cf08ee4fbe0ff9fe0fc3b1f4.tar.gz deck-it-69e6ce2bf7272797cf08ee4fbe0ff9fe0fc3b1f4.zip | |
feat(vm): a VMs tab for libvirt power, snapshots and ssh
virsh domstats each poll for state, CPU and memory; managed save,
snapshots and the guest agent's IP in a thread every 10s. Actions are
the drawer's power set plus snapshot take/revert/delete, names checked
against the last state. SSH opens a tab in a running kitty over its
remote-control socket and focuses it, else spawns kitty. Destructive
buttons are press and hold.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'static')
| -rw-r--r-- | static/app.js | 2 | ||||
| -rw-r--r-- | static/deck.css | 21 | ||||
| -rw-r--r-- | static/modules/vm.js | 149 |
3 files changed, 171 insertions, 1 deletions
diff --git a/static/app.js b/static/app.js index 60cbb9f..5d1688a 100644 --- a/static/app.js +++ b/static/app.js @@ -29,7 +29,7 @@ const tabs = {}; // Tab icons, glyphs in the Nerd Font the server serves, or a brand logo from // static/logos; a tab with neither shows its name. The name also shows next // to the active one. -const TAB_ICONS = { Dashboard: "\uf0e4", Windows: "\uf2d2", Assistant: "\uf086", K1C: "\u{f0e5b}" }; +const TAB_ICONS = { Dashboard: "\uf0e4", Windows: "\uf2d2", Assistant: "\uf086", K1C: "\u{f0e5b}", VMs: "\uf233" }; const TAB_LOGOS = { OBS: "obs" }; function ensureTab(name) { diff --git a/static/deck.css b/static/deck.css index a08f037..c316ff3 100644 --- a/static/deck.css +++ b/static/deck.css @@ -812,3 +812,24 @@ select { /* Hyprland: speed 30 * 100 ms, flatlinetwo = linear, style = loop */ animation: border-rotate 3s linear infinite; } + +/* VMs tab. Destructive buttons are press and hold, filled over --hold. */ +.vm-list { display: flex; flex-direction: column; gap: 12px; padding: 4px 2px; } +.vm-list[hidden], .vm-off[hidden], .vm-use[hidden], .vm-err[hidden] { display: none; } +.vm-card { display: flex; flex-direction: column; gap: 10px; padding: 12px 14px; border-radius: 12px; background: var(--udt-surface0); } +.vm-card.busy { opacity: .6; pointer-events: none; } +.vm-head { display: flex; align-items: center; gap: 12px; } +.vm-name { font-size: 1.286rem; font-weight: 600; } +.vm-state { padding: 2px 10px; border-radius: 999px; background: var(--udt-surface2); font-size: 0.857rem; } +.vm-state.good { background: var(--udt-green); color: var(--udt-crust); } +.vm-state.warn { background: var(--udt-yellow); color: var(--udt-crust); } +.vm-state.bad { background: var(--udt-red); color: var(--udt-crust); } +.vm-ip { margin-left: auto; color: var(--udt-subtext0); } +.vm-use { display: grid; grid-template-columns: auto 1fr auto auto auto; align-items: center; gap: 10px; } +.vm-err { color: var(--udt-red); font-size: 0.857rem; } +.vm-acts { margin-bottom: 0; } +.vm-snap { display: flex; align-items: center; gap: 8px; padding: 4px 0; border-top: 1px solid var(--udt-surface1); } +.vm-snap > span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } +.vm .hold { position: relative; overflow: hidden; isolation: isolate; border-color: var(--udt-red); touch-action: none; } +.vm .hold::before { content: ""; position: absolute; inset: 0; z-index: -1; background: var(--udt-red); transform: scaleX(0); transform-origin: left; } +.vm .hold.holding::before { transform: scaleX(1); transition: transform var(--hold, 800ms) linear; } diff --git a/static/modules/vm.js b/static/modules/vm.js new file mode 100644 index 0000000..fdffd09 --- /dev/null +++ b/static/modules/vm.js @@ -0,0 +1,149 @@ +// deck-it - the VMs tab: libvirt power, snapshots and an ssh session +// Copyright (C) 2026 Danilo M. <danix@danix.xyz> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import { offline } from "../offline.js"; + +// Destructive actions fire only after a hold this long, so a stray touch on +// an always-on tablet cannot force a VM off. +const HOLD_MS = 800; +const HOLD = new Set(["reset", "destroy", "discardsave", "revert", "snapdelete"]); +const PILL = { running: "good", paused: "warn", suspended: "warn", "shutting down": "warn", crashed: "bad" }; + +// The buttons per state, as keystone's drawer offers them. +function actionsFor(st, saved) { + if (st === "running") + return [["ssh", "SSH"], ["shutdown", "Shutdown"], ["reboot", "Reboot"], ["suspend", "Suspend"], + ["reset", "Reset"], ["destroy", "Force stop"]]; + if (st === "paused" || st === "suspended") + return [["resume", "Resume"], ["shutdown", "Shutdown"], ["destroy", "Force stop"]]; + return saved ? [["start", "Start"], ["discardsave", "Discard saved state"]] : [["start", "Start"]]; +} + +const gib = (b) => (b == null ? "—" : `${(b / 2 ** 30).toFixed(1)} GiB`); + +async function post(action, body, card) { + const err = card.querySelector(".vm-err"); + card.classList.add("busy"); + try { + const r = await fetch(`/api/vm/${action}`, { + method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), + }); + err.textContent = r.ok ? "" : (await r.text()) || r.statusText; + } catch (e) { + err.textContent = String(e); + } + err.hidden = !err.textContent; + card.classList.remove("busy"); +} + +function button(label, action, body, card) { + const b = document.createElement("button"); + b.textContent = label; + const fire = () => post(action, body, card); + if (!HOLD.has(action)) { + b.onclick = fire; + return b; + } + // Press and hold: the fill grows over HOLD_MS, releasing early cancels. + b.className = "hold"; + b.title = "Press and hold"; + b.style.setProperty("--hold", `${HOLD_MS}ms`); + let timer = null; + const cancel = () => { clearTimeout(timer); b.classList.remove("holding"); }; + b.addEventListener("pointerdown", (e) => { + e.preventDefault(); + b.setPointerCapture(e.pointerId); + b.classList.add("holding"); + timer = setTimeout(() => { cancel(); fire(); }, HOLD_MS); + }); + b.addEventListener("pointerup", cancel); + b.addEventListener("pointercancel", cancel); + return b; +} + +function card() { + const c = document.createElement("div"); + c.className = "vm-card"; + c.innerHTML = `<div class="vm-head"><span class="vm-name"></span><span class="vm-state"></span> + <span class="vm-ip num"></span></div> + <div class="vm-use"><span class="mlabel">CPU</span><div class="gbar"><i></i></div><span class="vm-cpu num"></span> + <span class="mlabel">RAM</span><span class="vm-mem num"></span></div> + <div class="row vm-acts"></div> + <div class="vm-err" hidden></div> + <div class="vm-snaps"></div>`; + return c; +} + +// Text every poll; buttons only when what they depend on changed, so a +// hold in progress is not torn down under the finger. +function fill(c, vm) { + c.querySelector(".vm-name").textContent = vm.name; + const pill = c.querySelector(".vm-state"); + pill.textContent = vm.saved && vm.state === "shut off" ? "saved" : vm.state; + pill.className = `vm-state ${PILL[vm.state] ?? ""}`; + c.querySelector(".vm-ip").textContent = vm.ip; + const on = vm.state === "running"; + c.querySelector(".vm-use").hidden = !on; + c.querySelector(".gbar > i").style.width = `${vm.cpu ?? 0}%`; + c.querySelector(".vm-cpu").textContent = vm.cpu == null ? "—" : `${Math.round(vm.cpu)}% of ${vm.vcpus}`; + c.querySelector(".vm-mem").textContent = gib(vm.mem); + + const key = JSON.stringify([vm.state, vm.saved, vm.snapshots]); + if (c.dataset.key === key) return; + c.dataset.key = key; + const acts = c.querySelector(".vm-acts"); + acts.replaceChildren(...actionsFor(vm.state, vm.saved).map(([a, l]) => button(l, a, { vm: vm.name }, c)), + button("Snapshot", "snapshot", { vm: vm.name }, c)); + c.querySelector(".vm-snaps").replaceChildren(...vm.snapshots.map((s) => { + const row = document.createElement("div"); + row.className = "vm-snap"; + const name = document.createElement("span"); + name.textContent = s; + row.append(name, button("Revert", "revert", { vm: vm.name, snap: s }, c), + button("Delete", "snapdelete", { vm: vm.name, snap: s }, c)); + return row; + })); +} + +export default { + title: "VMs", + tab: "VMs", + interval: 2, + + mount(el) { + el.innerHTML = `<div class="vm"><div class="vm-off"></div><div class="vm-list"></div></div>`; + }, + + update(st, el) { + const off = el.querySelector(".vm-off"); + const list = el.querySelector(".vm-list"); + off.hidden = !st.offline; + list.hidden = Boolean(st.offline); + if (st.offline) { + offline(off, { icon: "/icons/virt-manager.svg", title: "libvirt is not answering", + detail: "Start libvirtd and the VMs appear here.", reason: st.reason }); + return; + } + const cards = new Map([...list.children].map((c) => [c.dataset.vm, c])); + list.replaceChildren(...st.vms.map((vm) => { + const c = cards.get(vm.name) ?? card(); + c.dataset.vm = vm.name; + fill(c, vm); + return c; + })); + if (!st.vms.length) list.textContent = "No VMs defined."; + }, +}; |
