// deck-it - the VMs tab: libvirt power, snapshots and an ssh session // Copyright (C) 2026 Danilo M. // // 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 = `
CPU
RAM
`; 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 = `
`; }, 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."; }, };