Show VM network and creation details in header

main
admin 2 months ago
parent 0a94574c43
commit 8f1c258b54

@ -754,6 +754,15 @@ function vmIp(vm) {
return String(vm.public_ip_cidr || "").trim().split("/")[0] || "未获取";
}
function vmCreatedAt(vm) {
const value = typeof vm.created_at === "string" ? vm.created_at.trim() : "";
if (!value) return "未记录";
const date = new Date(value);
if (!Number.isFinite(date.getTime())) return "未记录";
const pad = (number) => String(number).padStart(2, "0");
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
}
function makeCell(row, className = "") {
const cell = document.createElement("td");
if (className) cell.className = className;
@ -855,7 +864,12 @@ function renderVMs(connectionError = false) {
nameButton.setAttribute("aria-label", `管理虚拟机 ${vm.name || uuid}`);
nameButton.addEventListener("click", () => openVmDetail(uuid));
identityText.append(nameButton);
identityText.append(makeElement("span", "vm-id", uuid || "UUID 未知"));
const uuidRow = makeElement("span", "vm-uuid-row");
const uuidText = makeElement("span", "vm-id", uuid || "UUID 未知");
if (uuid) uuidText.title = uuid;
uuidRow.append(uuidText);
if (uuid) uuidRow.append(makeCopyIconButton(uuid, "UUID"));
identityText.append(uuidRow);
identity.append(identityText);
const identityCell = makeCell(row);
identityCell.dataset.label = "虚拟机";
@ -968,13 +982,16 @@ function renderVmDetail(vm) {
const busy = state.vmActionInFlight === uuid;
const system = vmSystemInfo(vm);
text("vm-detail-name", vm.name || "未命名虚拟机");
text("vm-detail-uuid", uuid || "UUID 未知");
text("vm-detail-state", stateLabel(vm.state));
byId("vm-detail-state").className = `state-pill ${category}`;
text("vm-detail-region", vmRegion(vm));
const region = vmRegion(vm);
text("vm-detail-header-region", region);
text("vm-detail-header-created-at", vmCreatedAt(vm));
text("vm-detail-region", region);
text("vm-detail-os-label", system.label);
text("vm-detail-os", system.value);
const ip = vmIp(vm);
text("vm-detail-header-ip", ip);
text("vm-detail-ip", ip);
byId("vm-detail-ip-copy").hidden = ip === "未获取";
text("vm-detail-spec", `${vm.vcpu ?? "—"} vCPU · ${vm.memory_mib ?? "—"} MiB`);

@ -5,8 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#101d2c" />
<title>VirtPilot · 虚拟机管理</title>
<link rel="stylesheet" href="/static/styles.css?v=vm-details-3" />
<script type="module" src="/static/app.js?v=vm-details-3"></script>
<link rel="stylesheet" href="/static/styles.css?v=vm-details-4" />
<script type="module" src="/static/app.js?v=vm-details-4"></script>
</head>
<body>
<div id="boot-screen" class="boot-screen" role="status" aria-live="polite">
@ -224,7 +224,7 @@
</div>
<section id="vm-detail-view" class="vm-detail-view" aria-labelledby="vm-detail-name" hidden>
<button id="vm-detail-back-button" class="button button-ghost vm-detail-back" type="button">← 返回虚拟机列表</button>
<div class="page-heading vm-detail-heading"><div><span class="eyebrow">VIRTUAL MACHINE DETAILS</span><h1 id="vm-detail-name" tabindex="-1">虚拟机</h1><p id="vm-detail-uuid" class="vm-detail-uuid"></p></div><div class="heading-actions"><span id="vm-detail-state" class="state-pill other">未知</span><button id="vm-detail-refresh-button" class="button button-outline" type="button"><span aria-hidden="true">↻</span> 刷新</button></div></div>
<div class="page-heading vm-detail-heading"><div><span class="eyebrow">VIRTUAL MACHINE DETAILS</span><h1 id="vm-detail-name" tabindex="-1">虚拟机</h1><p class="vm-detail-summary"><span>服务器 IP <strong id="vm-detail-header-ip">未获取</strong></span><span>区域 <strong id="vm-detail-header-region">未设置</strong></span><span>创建时间 <strong id="vm-detail-header-created-at">未记录</strong></span></p></div><div class="heading-actions"><span id="vm-detail-state" class="state-pill other">未知</span><button id="vm-detail-refresh-button" class="button button-outline" type="button"><span aria-hidden="true">↻</span> 刷新</button></div></div>
<div class="vm-detail-grid">
<section class="inventory-panel vm-detail-panel" aria-labelledby="vm-detail-info-title"><div class="panel-heading"><div><h2 id="vm-detail-info-title">虚拟机信息</h2><p>状态和配置每 15 秒自动更新;IP 是面板记录的配置,不代表系统已联网。</p></div></div><dl class="vm-detail-facts">
<div><dt>地区</dt><dd id="vm-detail-region">未设置</dd></div><div><dt id="vm-detail-os-label">系统</dt><dd id="vm-detail-os">未识别</dd></div>

@ -116,7 +116,7 @@ thead{background:#fbfcfe}th{height:47px;text-align:left;font-size:10px;letter-sp
th.actions-heading{text-align:right;padding-right:28px}
td{height:83px;border-top:1px solid #edf1f5;padding:15px 24px;font-size:12px;color:#607087}
tbody tr:hover{background:#fcfefe}
.vm-identity{display:flex;align-items:center;gap:12px;min-width:160px}.vm-icon{width:38px;height:38px;border-radius:10px;background:#e9f8f8;color:#239eaa;display:flex;align-items:center;justify-content:center;font-size:19px;flex:none}.vm-name{display:block;color:#25384e;font-size:13px;font-weight:800;max-width:230px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.vm-id{display:block;color:#a6b1bf;font-size:10px;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;margin-top:6px;max-width:180px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.vm-identity{display:flex;align-items:center;gap:12px;min-width:160px}.vm-icon{width:38px;height:38px;border-radius:10px;background:#e9f8f8;color:#239eaa;display:flex;align-items:center;justify-content:center;font-size:19px;flex:none}.vm-name{display:block;color:#25384e;font-size:13px;font-weight:800;max-width:230px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.vm-uuid-row{display:flex;align-items:center;gap:5px;margin-top:6px;max-width:265px}.vm-id{display:block;min-width:0;color:#a6b1bf;font-size:10px;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.vm-uuid-row .vm-copy-icon{width:21px;min-height:21px;padding:2px}.vm-uuid-row .vm-copy-icon svg{width:13px;height:13px}
.vm-public-ip{display:block;color:#16868f;font-size:10px;font-weight:700;margin-top:5px;white-space:nowrap}
.vm-name-link{display:block;max-width:230px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;border:0;background:transparent;padding:2px 0;color:#25384e;font-size:13px;font-weight:800;text-align:left;text-decoration:underline;text-decoration-color:transparent;text-underline-offset:3px;cursor:pointer}.vm-name-link:not(:disabled):hover,.vm-name-link:not(:disabled):focus-visible{color:#118e98;text-decoration-color:currentColor}.vm-metadata{display:block;max-width:205px;color:#53647a;line-height:1.5;overflow-wrap:anywhere}.vm-ip{color:#16868f;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:11px;white-space:nowrap}
.state-pill{display:inline-flex;align-items:center;gap:7px;padding:6px 9px;border-radius:6px;font-size:11px;font-weight:800;background:#f1f3f6;color:#758499;white-space:nowrap}.state-pill:before{content:"";width:6px;height:6px;background:currentColor;border-radius:50%}.state-pill.running{background:#e7f8ef;color:#20a46f}.state-pill.stopped{background:#f2f4f7;color:#8290a3}.state-pill.other{background:#fff4e5;color:#b98129}
@ -124,7 +124,7 @@ tbody tr:hover{background:#fcfefe}
.row-actions{display:flex;align-items:center;justify-content:flex-end;gap:5px;white-space:nowrap}.row-button{border:1px solid transparent;background:transparent;color:#577083;border-radius:7px;padding:7px 8px;min-height:31px;font-size:11px;font-weight:700}.row-button:not(:disabled):hover{background:#e8f7f7;color:#117f88}.row-button.primary{background:#e7f7f7;color:#118e98}.row-button.danger{color:#bf6470}.row-button.danger:not(:disabled):hover{background:#fff0f1;color:#ba3a49}
.empty-state{text-align:center;padding:65px 20px 72px}.empty-icon{margin:0 auto 16px;width:54px;height:54px;display:flex;align-items:center;justify-content:center;border-radius:16px;background:#eaf7f7;color:#17a3aa;font-size:28px}.empty-state h3{font-size:16px;color:#34465a;margin:0 0 8px}.empty-state p{font-size:12px;color:#a0aebe;margin:0}
.footer-note{text-align:right;color:#a9b4c0;font-size:10px;margin-top:18px}
.vm-detail-back{padding-left:0;margin-bottom:22px;color:#267b86}.vm-detail-heading{align-items:center}.vm-detail-heading .heading-actions{align-items:center}.vm-detail-heading .state-pill{min-height:31px}.vm-detail-uuid{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;overflow-wrap:anywhere}.vm-detail-grid{display:grid;grid-template-columns:minmax(0,1.15fr) minmax(320px,.85fr);gap:18px;align-items:start}.vm-detail-panel{min-height:0}.vm-detail-facts{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:0;margin:0;padding:6px 24px 17px}.vm-detail-facts>div{min-width:0;padding:17px 9px 17px 0;border-bottom:1px solid #edf1f5}.vm-detail-facts dt{font-size:11px;font-weight:700;color:#91a0af;margin-bottom:9px}.vm-detail-facts dd{margin:0;color:#273c52;font-size:13px;font-weight:700;line-height:1.5;overflow-wrap:anywhere}.vm-detail-operations{display:grid;gap:18px}.vm-detail-actions{display:flex;flex-wrap:wrap;gap:10px;padding:20px 24px 23px}.vm-detail-actions .row-button{min-height:39px;padding:9px 15px;border-color:#dce9ea;font-size:12px}.vm-detail-actions .row-button.primary{border-color:#cbecee}.vm-danger-panel .panel-heading h2{color:#a4434f}.vm-danger-panel .row-button.danger{border-color:#f2dfe2;background:#fff9f9}.vm-danger-panel .row-button.danger:not(:disabled):hover{background:#ffedef}
.vm-detail-back{padding-left:0;margin-bottom:22px;color:#267b86}.vm-detail-heading{align-items:center}.vm-detail-heading .heading-actions{align-items:center}.vm-detail-heading .state-pill{min-height:31px}.vm-detail-summary{display:flex;align-items:center;flex-wrap:wrap;gap:5px 18px}.vm-detail-summary span{white-space:nowrap}.vm-detail-summary strong{color:#455a70;font-weight:700;margin-left:4px}.vm-detail-grid{display:grid;grid-template-columns:minmax(0,1.15fr) minmax(320px,.85fr);gap:18px;align-items:start}.vm-detail-panel{min-height:0}.vm-detail-facts{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:0;margin:0;padding:6px 24px 17px}.vm-detail-facts>div{min-width:0;padding:17px 9px 17px 0;border-bottom:1px solid #edf1f5}.vm-detail-facts dt{font-size:11px;font-weight:700;color:#91a0af;margin-bottom:9px}.vm-detail-facts dd{margin:0;color:#273c52;font-size:13px;font-weight:700;line-height:1.5;overflow-wrap:anywhere}.vm-detail-operations{display:grid;gap:18px}.vm-detail-actions{display:flex;flex-wrap:wrap;gap:10px;padding:20px 24px 23px}.vm-detail-actions .row-button{min-height:39px;padding:9px 15px;border-color:#dce9ea;font-size:12px}.vm-detail-actions .row-button.primary{border-color:#cbecee}.vm-danger-panel .panel-heading h2{color:#a4434f}.vm-danger-panel .row-button.danger{border-color:#f2dfe2;background:#fff9f9}.vm-danger-panel .row-button.danger:not(:disabled):hover{background:#ffedef}
.vm-detail-inline,.vm-ip-copy-row{display:flex;align-items:center;flex-wrap:wrap;gap:7px}.vm-detail-inline{min-height:25px}.vm-detail-secret{font:600 12px/1.5 ui-monospace,SFMono-Regular,Menlo,monospace;overflow-wrap:anywhere}.vm-detail-mini-button,.vm-copy-icon{display:inline-flex;align-items:center;justify-content:center;flex:none;border:1px solid #d9e9ea;border-radius:6px;background:#f5fbfb;color:#1b8790;font-size:11px;font-weight:700;min-height:25px;padding:3px 7px}.vm-detail-mini-button:not(:disabled):hover,.vm-copy-icon:not(:disabled):hover{background:#e6f6f7;border-color:#b6dcdf}.vm-copy-icon{width:25px;padding:3px}.vm-copy-icon svg{display:block;width:15px;height:15px;fill:none;stroke:currentColor;stroke-width:1.8;stroke-linecap:round;stroke-linejoin:round}
.vm-credential-icon{width:30px;min-height:30px}.vm-credential-icon svg{width:17px;height:17px}.vm-credential-icon .vm-eye-slash{visibility:hidden}.vm-credential-icon[aria-pressed="true"] .vm-eye-slash{visibility:visible}
.vm-detail-facts .vm-detail-credential-row{grid-column:1/-1}

@ -600,6 +600,7 @@ class VMServiceTests(unittest.TestCase):
self.assertEqual(self.backend.domain_autostart_changed, [vm["uuid"]])
self.assertEqual(vm["vcpu"], 2)
self.assertEqual(vm["memory_mib"], 2048)
self.assertRegex(vm["created_at"], r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$")
self.assertEqual(len(vm["disks"]), 1)
self.assertEqual(vm["disks"][0]["source"],
"/var/lib/libvirt/images/alma-test.qcow2")
@ -621,6 +622,10 @@ class VMServiceTests(unittest.TestCase):
)
self.assertEqual(root.findtext("on_reboot"), "destroy")
final_root = self.backend.domains[0].inactive_root
self.assertEqual(root.find("./metadata/{urn:virsh-panel:1}managed").get("created-at"),
vm["created_at"])
self.assertEqual(final_root.find("./metadata/{urn:virsh-panel:1}managed").get("created-at"),
vm["created_at"])
self.assertEqual([boot.get("dev") for boot in final_root.findall("./os/boot")],
["hd"])
self.assertIsNone(final_root.find("./devices/disk[@device='cdrom']/source"))
@ -639,6 +644,14 @@ class VMServiceTests(unittest.TestCase):
self.assertEqual(listed["os_source"], "unknown")
self.assertIsNone(listed["install_iso"])
self.assertIsNone(listed["public_ip_cidr"])
self.assertIsNone(listed["created_at"])
def test_older_panel_vm_without_creation_marker_has_unknown_time(self):
vm = self.service.create_vm(self.spec())
domain = self.backend.domains[0]
for root in (domain.live_root, domain.inactive_root):
root.find("./metadata/{urn:virsh-panel:1}managed").attrib.pop("created-at")
self.assertIsNone(self.service.get_vm(vm["uuid"])["created_at"])
def test_installer_reboot_stops_guest_and_next_start_uses_unmounted_iso(self):
vm = self.service.create_vm(self.spec())
@ -749,7 +762,8 @@ class VMServiceTests(unittest.TestCase):
self.assertGreater(int(balloon.find("stats").get("period")), 0)
self.assertEqual(root.find("./metadata/{urn:virsh-panel:1}managed").attrib,
{"version": "2", "disk-volume": "web.qcow2",
"os-source": "iso", "install-iso": iso_name})
"os-source": "iso", "install-iso": iso_name,
"created-at": vm["created_at"]})
self.assertIsNone(vm["os_name"])
self.assertEqual(vm["os_source"], "iso")
self.assertEqual(vm["install_iso"], iso_name)
@ -1273,6 +1287,8 @@ class VMServiceTests(unittest.TestCase):
self.assertEqual(created["public_ip_cidr"], "8.8.8.18/24")
self.assertEqual(managed.get("os-source"), "cloud_template")
self.assertEqual(managed.get("os-name"), "AlmaLinux 10")
self.assertEqual(managed.get("created-at"), created["created_at"])
self.assertRegex(created["created_at"], r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$")
self.assertEqual(created["os_name"], "AlmaLinux 10")
self.assertEqual(created["os_source"], "cloud_template")
self.assertEqual(self.backend.opened, self.backend.closed)
@ -1582,6 +1598,7 @@ class VMServiceTests(unittest.TestCase):
second_seed = second_managed.get("seed-volume")
self.assertEqual(second["vm"]["state"], "running")
self.assertEqual(second["vm"]["created_at"], created["created_at"])
self.assertNotEqual(first_disk, second_disk)
self.assertNotEqual(first_seed, second_seed)
self.assertNotIn(first_disk, pool.volumes)

@ -17,6 +17,7 @@ import time
import uuid as uuidlib
import xml.etree.ElementTree as ET
from contextlib import contextmanager
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Callable, Iterator
@ -326,6 +327,7 @@ class VMService:
"autostart": autostart,
"managed": managed is not None,
"public_ip_cidr": managed.get("public-ip-cidr") if managed is not None else None,
"created_at": managed.get("created-at") if managed is not None else None,
"credential_generation": managed.get("seed-volume") if os_source == "cloud_template" else None,
"os_name": managed.get("os-name") if os_source == "cloud_template" else None,
"os_source": os_source,
@ -497,6 +499,8 @@ class VMService:
metadata = ET.SubElement(root, "metadata")
managed = ET.SubElement(metadata, f"{{{PANEL_NS}}}managed", version="2",
**{"disk-volume": disk_name})
if spec.get("created_at"):
managed.set("created-at", spec["created_at"])
if "iso" in spec:
# The installation media does not prove which OS was installed.
managed.set("os-source", "iso")
@ -737,6 +741,7 @@ class VMService:
def create_cloud_vm(self, spec: dict[str, Any], template_volume_name: str) -> dict[str, Any]:
"""Clone a trusted cloud image and supply static public networking."""
validated = self._validate_common_create(spec)
validated["created_at"] = datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z")
validated["os_name"] = self._validated_os_name(spec.get("os_name"))
requested_uuid = spec.get("vm_uuid")
if requested_uuid is not None:
@ -1092,6 +1097,7 @@ class VMService:
def create_vm(self, spec: dict[str, Any]) -> dict[str, Any]:
validated = self._validate_create(spec)
validated["created_at"] = datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z")
with self._connect() as conn:
if any(domain.name() == validated["name"] for domain in conn.listAllDomains(0)):
raise VMError("虚拟机名称已存在", 409)

Loading…
Cancel
Save