diff --git a/app/src/lib/api.ts b/app/src/lib/api.ts index 8ee73a6..822e4c7 100644 --- a/app/src/lib/api.ts +++ b/app/src/lib/api.ts @@ -322,15 +322,33 @@ export const api = { request(`/api/helm/releases/${ns}/${name}`, { method: "DELETE" }), runtimes: () => request<{ name: string; available: boolean }[]>("/api/runtimes"), - listVMs: () => request("/api/runtimes/firecracker/vms"), - createVM: (body: any) => - request("/api/runtimes/firecracker/vms", { method: "POST", body: JSON.stringify(body) }), + listVMs: () => request("/api/runtimes/firecracker/vms"), + createVM: (body: MicroVMCreate) => + request("/api/runtimes/firecracker/vms", { method: "POST", body: JSON.stringify(body) }), startVM: (id: string) => request(`/api/runtimes/firecracker/vms/${id}/start`, { method: "POST" }), stopVM: (id: string) => request(`/api/runtimes/firecracker/vms/${id}/stop`, { method: "POST" }), destroyVM: (id: string) => request(`/api/runtimes/firecracker/vms/${id}`, { method: "DELETE" }), vmLogs: (id: string) => request<{ output: string }>(`/api/runtimes/firecracker/vms/${id}/logs`), }; +export type MicroVM = { + id: string; + name: string; + state: string; + vcpu: number; + memoryMb: number; + kernel?: string; + rootfs?: string; +}; + +export type MicroVMCreate = { + name: string; + kernel: string; + rootfs: string; + vcpu: number; + memoryMb: number; +}; + export type StatusResponse = { docker: { connected: boolean; diff --git a/app/src/routes/microvms/MicroVMsOverviewPage.tsx b/app/src/routes/microvms/MicroVMsOverviewPage.tsx index 8a3982e..82fa4c5 100644 --- a/app/src/routes/microvms/MicroVMsOverviewPage.tsx +++ b/app/src/routes/microvms/MicroVMsOverviewPage.tsx @@ -1,19 +1,98 @@ import { useQuery } from "@tanstack/react-query"; +import { useNavigate } from "@tanstack/react-router"; +import { Button, Text } from "@react-spectrum/s2"; +import { style } from "@react-spectrum/s2/style" with { type: "macro" }; import { api } from "@/lib/api"; -import { EmptyState, PageShell } from "@/components/PageShell"; +import { EmptyState, MetricTile, PageShell } from "@/components/PageShell"; +import { StatusBadge } from "@/components/spectrum/StatusBadge"; +import { HelpHint } from "@/components/HelpHint"; export function MicroVMsOverviewPage() { + const navigate = useNavigate(); + const status = useQuery({ queryKey: ["status"], queryFn: api.status }); const runtimes = useQuery({ queryKey: ["runtimes"], queryFn: api.runtimes }); - const available = runtimes.data?.some((r) => r.name === "firecracker" && r.available); + const vms = useQuery({ queryKey: ["vms"], queryFn: api.listVMs, refetchInterval: 5000 }); + + const available = + status.data?.firecracker.available || + runtimes.data?.some((r) => r.name === "firecracker" && r.available); + const list = vms.data || []; + const running = list.filter((vm) => (vm.state || "").toLowerCase() === "running").length; + const created = list.filter((vm) => (vm.state || "").toLowerCase() === "created").length; + return ( - - {available ? ( - - ) : ( + navigate({ to: "/microvms/vms" })}> + Open VMs + + ) : null + } + > + {!available ? ( + ) : ( +
+
+ Firecracker ready + + Create and manage microVMs from the VMs tab + + +
+ +
+ navigate({ to: "/microvms/vms" })} + /> + navigate({ to: "/microvms/vms" })} + /> + navigate({ to: "/microvms/vms" })} + /> +
+ + {list.length === 0 ? ( + navigate({ to: "/microvms/vms" })}> + Create microVM + + } + /> + ) : null} +
)}
); diff --git a/app/src/routes/microvms/MicroVMsPage.tsx b/app/src/routes/microvms/MicroVMsPage.tsx index abb1cd2..466716e 100644 --- a/app/src/routes/microvms/MicroVMsPage.tsx +++ b/app/src/routes/microvms/MicroVMsPage.tsx @@ -1,91 +1,423 @@ import { useQuery, useQueryClient } from "@tanstack/react-query"; -import { useState } from "react"; -import { Button } from "@react-spectrum/s2"; +import { useEffect, useMemo, useState } from "react"; +import { Button, NumberField, Tabs, TabList, Tab, TabPanel } from "@react-spectrum/s2"; import { style } from "@react-spectrum/s2/style" with { type: "macro" }; -import { api } from "@/lib/api"; +import { api, type MicroVM } from "@/lib/api"; import { CodeBlock } from "@/components/CodeBlock"; -import { PageShell } from "@/components/PageShell"; +import { ConfirmDialog } from "@/components/ConfirmDialog"; +import { CopyButton } from "@/components/CopyButton"; +import { DetailEmpty, DetailHeading, DetailPane } from "@/components/DetailPane"; +import { GlassSheet } from "@/components/GlassSheet"; +import { InspectFields } from "@/components/InspectFields"; +import { ListEmpty, ListPane } from "@/components/ListPane"; +import { toast } from "@/components/Toaster"; import { Field } from "@/components/spectrum/Field"; +import { RowMenu } from "@/components/spectrum/RowMenu"; +import { StatusBadge } from "@/components/spectrum/StatusBadge"; +import { Tip } from "@/components/spectrum/Tip"; +import { shortId } from "@/lib/utils"; +import { copyText } from "@/routes/shared"; + +function stateTone(state?: string): "success" | "muted" | "warn" | "destructive" | "accent" { + switch ((state || "").toLowerCase()) { + case "running": + return "success"; + case "created": + return "accent"; + case "stopped": + case "exited": + return "muted"; + case "error": + case "failed": + return "destructive"; + default: + return "muted"; + } +} + +function isRunning(state?: string) { + return (state || "").toLowerCase() === "running"; +} export function MicroVMsPage() { const qc = useQueryClient(); const list = useQuery({ queryKey: ["vms"], queryFn: api.listVMs, refetchInterval: 5000 }); - const [form, setForm] = useState({ name: "", kernel: "", rootfs: "", vcpu: 1, memoryMb: 512 }); - const [logs, setLogs] = useState(""); + const [selected, setSelected] = useState(null); + const [q, setQ] = useState(""); + const [tab, setTab] = useState<"inspect" | "logs">("inspect"); + const [createOpen, setCreateOpen] = useState(false); + const [creating, setCreating] = useState(false); + const [confirmDestroy, setConfirmDestroy] = useState(false); + const [busy, setBusy] = useState(false); + const [form, setForm] = useState({ + name: "", + kernel: "", + rootfs: "", + vcpu: 1, + memoryMb: 512, + }); + + const filtered = useMemo(() => { + const items = list.data || []; + if (!q) return items; + const needle = q.toLowerCase(); + return items.filter( + (vm) => + vm.name?.toLowerCase().includes(needle) || + vm.id?.toLowerCase().includes(needle) || + vm.state?.toLowerCase().includes(needle) || + vm.kernel?.toLowerCase().includes(needle) || + vm.rootfs?.toLowerCase().includes(needle), + ); + }, [list.data, q]); + + const row = filtered.find((vm) => vm.id === selected) || (list.data || []).find((vm) => vm.id === selected); + + useEffect(() => { + if (selected && list.data && !list.data.some((vm) => vm.id === selected)) { + setSelected(null); + } + }, [list.data, selected]); + + const logs = useQuery({ + queryKey: ["vm-logs", selected], + queryFn: () => api.vmLogs(selected!), + enabled: !!selected && tab === "logs", + refetchInterval: tab === "logs" && selected && isRunning(row?.state) ? 4000 : false, + }); + + const act = async (fn: () => Promise, ok: string) => { + setBusy(true); + try { + await fn(); + await qc.invalidateQueries({ queryKey: ["vms"] }); + toast.success(ok); + } catch (e: any) { + toast.error("Action failed", { description: e?.message }); + } finally { + setBusy(false); + } + }; + + const createVM = async () => { + const name = form.name.trim(); + const kernel = form.kernel.trim(); + const rootfs = form.rootfs.trim(); + if (!kernel || !rootfs) return; + setCreating(true); + try { + const vm = await api.createVM({ + name, + kernel, + rootfs, + vcpu: form.vcpu || 1, + memoryMb: form.memoryMb || 512, + }); + setSelected(vm.id); + setForm({ name: "", kernel: "", rootfs: "", vcpu: 1, memoryMb: 512 }); + setCreateOpen(false); + await qc.invalidateQueries({ queryKey: ["vms"] }); + toast.success("MicroVM created", { description: vm.name }); + } catch (e: any) { + toast.error("Create failed", { description: e?.message }); + } finally { + setCreating(false); + } + }; + + const rowItems = (vm: MicroVM) => [ + { id: "open", label: "Open", onAction: () => setSelected(vm.id) }, + { id: "copy-id", label: "Copy ID", onAction: () => void copyText(vm.id) }, + { id: "copy-name", label: "Copy name", onAction: () => void copyText(vm.name) }, + { id: "sep-1", label: "", onAction: () => {} }, + { + id: "start", + label: "Start", + onAction: () => void act(() => api.startVM(vm.id), "Started"), + }, + { + id: "stop", + label: "Stop", + onAction: () => void act(() => api.stopVM(vm.id), "Stopped"), + }, + { + id: "logs", + label: "View logs", + onAction: () => { + setSelected(vm.id); + setTab("logs"); + }, + }, + { id: "sep-2", label: "", onAction: () => {} }, + { + id: "destroy", + label: "Destroy…", + onAction: () => { + setSelected(vm.id); + setConfirmDestroy(true); + }, + destructive: true, + }, + ]; + + const running = isRunning(row?.state); return ( - -
- setForm({ ...form, name: v })} /> - setForm({ ...form, kernel: v })} /> - setForm({ ...form, rootfs: v })} /> - -
-
- {(list.data || []).map((vm) => ( -
+
+ setCreateOpen(true)}> + Create + + ) + } + /> + } + search={{ value: q, onChange: setQ, placeholder: "Search VMs" }} + actions={ + + + + } + > + {filtered.map((vm) => ( + setSelected(vm.id)} + items={rowItems(vm)} + suffix={{vm.state || "—"}} + > +
+ {vm.name} +
+
+ {vm.vcpu} vCPU · {vm.memoryMb} MB + {vm.id ? ` · ${shortId(vm.id)}` : ""} +
+
+ ))} +
+ + setCreateOpen(true)}> + Create microVM + + } + /> + } + > + {row ? ( +
-
-
{vm.name}
-
- {vm.state} · {vm.vcpu} vCPU · {vm.memoryMb} MB +
+
+ {row.name} + {row.state || "—"} +
+
+ {shortId(row.id)} + {` · ${row.vcpu} vCPU · ${row.memoryMb} MB`}
-
- +
+
+ + setTab(k as "inspect" | "logs")} + > + + Inspect + Logs + + +
+ +
+
+ +
+
+ +
+ {logs.isError ? ( +
+ {(logs.error as Error)?.message || "Failed to load logs"} +
+ ) : ( + + )} +
+
+
+
+ ) : null} + + + + + + + } + > +
+ setForm((f) => ({ ...f, name }))} + placeholder="name (optional)" + aria-label="VM name" + /> + setForm((f) => ({ ...f, kernel }))} + placeholder="/path/to/vmlinux" + aria-label="Kernel path" + /> + setForm((f) => ({ ...f, rootfs }))} + placeholder="/path/to/rootfs.ext4" + aria-label="Rootfs path" + /> +
+ setForm((f) => ({ ...f, vcpu: vcpu || 1 }))} + minValue={1} + /> + setForm((f) => ({ ...f, memoryMb: memoryMb || 512 }))} + minValue={128} + />
- ))} -
- {logs ? ( -
-
- ) : null} - +
+ + { + if (!selected) return; + try { + await api.destroyVM(selected); + toast.success("MicroVM destroyed"); + setSelected(null); + await qc.invalidateQueries({ queryKey: ["vms"] }); + } catch (e: any) { + toast.error("Destroy failed", { description: e?.message }); + throw e; + } + }} + /> +
); }