diff --git a/README.md b/README.md index d3207120..df50e574 100644 --- a/README.md +++ b/README.md @@ -87,9 +87,9 @@ SELECT id, username, email FROM users WHERE id = 1; ## 公网云镜像模板创建(无需手动安装) -面板的“创建虚拟机”现在有独立的“公网云镜像”方式。先在宿主机设置中选择已工作的 Linux 网桥,例如 `bridge:br0`;进入创建虚拟机页面,展开“导入新的云镜像模板”,填写 [AlmaLinux 10 官方 Generic Cloud qcow2 镜像](https://wiki.almalinux.org/cloud/Generic-cloud)在 `https://repo.almalinux.org/almalinux/10/cloud/x86_64/images/` 下的下载地址、发布方 SHA-256 和模板显示名。地址必须是官方 HTTPS 直链,面板不会跟随重定向。面板服务器 A 需要能访问该地址。导入在后台运行,一次只允许一个任务,面板下载、校验 SHA-256 后通过当前 libvirt SSH 连接把模板镜像上传到 B 的磁盘池;无需登录 B 手工复制。模板目录记录加密保存在 MariaDB,绑定当前宿主机身份和磁盘池。不要在导入期间关闭面板进程;若进程异常退出,运行中的导入任务记录不会恢复,可能需要检查项目 `data/` 中的 `.cloud-image-*` 临时文件和 B 磁盘池中 `panel-template-*` 卷,不要未经核对就删除。 +面板的“创建虚拟机”有独立的“公网云镜像”方式。先在宿主机设置中选择已工作的 Linux 网桥,例如 `bridge:br0`。只有管理员可进入侧栏的“云镜像模板”页面,填写 [AlmaLinux 官方 Generic Cloud qcow2 镜像](https://wiki.almalinux.org/cloud/Generic-cloud)在 `https://repo.almalinux.org/almalinux/9/cloud/x86_64/images/` 或 `https://repo.almalinux.org/almalinux/10/cloud/x86_64/images/` 下的下载地址、发布方 SHA-256 和模板显示名;已保存的显示名也可在该页修改。地址必须是官方 HTTPS 直链,面板不会跟随重定向。面板服务器 A 需要能访问该地址。导入在后台运行,一次只允许一个任务,面板下载、校验 SHA-256 后通过当前 libvirt SSH 连接把模板镜像上传到 B 的磁盘池;无需登录 B 手工复制。模板目录记录加密保存在 MariaDB,绑定当前宿主机身份和磁盘池。改名不会改动镜像文件、模板 ID 或已经创建的虚拟机的系统名称记录,只影响后续创建和重装时的显示。普通用户只能在创建或重装页面从已导入的镜像卡片中选择具体版本,不能导入或改名。不要在导入期间关闭面板进程;若进程异常退出,运行中的导入任务记录不会恢复,可能需要检查项目 `data/` 中的 `.cloud-image-*` 临时文件和 B 磁盘池中 `panel-template-*` 卷,不要未经核对就删除。 -选择模板后填写 VM 资源和 root 登录方式。宿主机公网地址池支持实际获配的 `/24` 至 `/30` 前缀。例如服务商分配 `123.123.123.112/28`、宿主机网关为 `123.123.123.113` 时,在宿主机设置填写该 `/28` 网段(填写 `123.123.123.113/28` 也会规范化为 `123.123.123.112/28`),网关填 `123.123.123.113`,DNS 按服务商要求填写。自动分配只考虑此 `/28` 的有效主机地址;扣除网关后是 `123.123.123.114` 至 `123.123.123.126`,还会跳过保留、已用及池内 DNS 地址。**不要填写 `123.123.123.114/24` 或 `123.123.123.0/24`,除非服务商确实分配了整个 `/24`。**设置页会预览输入前缀换算出的实际网段和主机地址范围。 +选择镜像卡片并展开版本后,填写 VM 资源和 root 登录方式。宿主机公网地址池支持实际获配的 `/24` 至 `/30` 前缀。例如服务商分配 `123.123.123.112/28`、宿主机网关为 `123.123.123.113` 时,在宿主机设置填写该 `/28` 网段(填写 `123.123.123.113/28` 也会规范化为 `123.123.123.112/28`),网关填 `123.123.123.113`,DNS 按服务商要求填写。自动分配只考虑此 `/28` 的有效主机地址;扣除网关后是 `123.123.123.114` 至 `123.123.123.126`,还会跳过保留、已用及池内 DNS 地址。**不要填写 `123.123.123.114/24` 或 `123.123.123.0/24`,除非服务商确实分配了整个 `/24`。**设置页会预览输入前缀换算出的实际网段和主机地址范围。 “虚拟机网卡前缀”与可分配地址池是独立设置:留空时客体使用地址池前缀;如果现场确认服务商要求 VM 内配置 `/24`,而实际只获配上述 `/28`,这里填 `24`,新建 VM 将获得例如 `123.123.123.114/24`,但面板仍只会从 `123.123.123.112/28` 挑选地址。可填写的客体前缀范围为 `/24` 到地址池前缀,以确保整个地址池中的 IP 与网关处于同一客体子网。修改此项不会自动改写已创建 VM;云镜像重装会保留该 VM 首次创建时的网络前缀。若使用 `/28` 不通,其具体原因仍需结合宿主机路由、网桥及服务商网络确认;仅改变面板掩码无法替代上游放行或路由配置。 @@ -118,4 +118,4 @@ systemctl enable --now virsh-panel.service 独立的 `virsh-panel-update.service` 会先拉取私有仓库并安装目标版本 Python 依赖,再以快进方式切换代码、重启主面板并检查本机 HTTP 与数据库健康状态。更新任务绑定点击“检查更新”时看到的两个提交编号;若本地或远端版本变化,任务会拒绝切换并要求重新检查。更新任务状态加密存于 MariaDB,页面中断后可继续查询;独立服务异常退出或超时会尝试将未完成任务标为失败。更新前请备份 **MariaDB 数据和 `data/master.key`、`data/database-config.json`**;更新失败时不要重复点按钮,应先看 `journalctl -u virsh-panel-update.service -n 100`。依赖在代码切换前安装到当前 `.venv`,因此若依赖安装成功而代码切换失败,可能需要人工检查依赖版本。更新服务会保留更新前的 Git 回滚引用,但不会自动执行覆盖性的回滚。网页更新不负责安装系统 RPM、替换 `/etc/systemd/system` 中已安装的 unit 或修改反向代理配置;这些变化如后续版本需要,仍需人工处理。由于在线更新会运行私有仓库中的代码与依赖,必须保护 Gitea 仓库写入权限和访问令牌。 -功能范围:邮箱验证注册、管理员审核及按虚拟机归属隔离用户,虚拟机列表和详情页四项性能走势图、从 ISO 创建、导入 AlmaLinux 10 云镜像模板并配置公网静态 IP 创建、云镜像重装、开机、关机、重启、强制断电/重置、删除和浏览器 VNC。当前仍是单宿主机版本;不提供 ISO 上传、目录自动创建、虚拟网络创建、内网 IP 模板或异网段网关。真实 AlmaLinux 10 宿主机、SMTP 服务及 MariaDB 仍需现场联调。 +功能范围:邮箱验证注册、管理员审核及按虚拟机归属隔离用户,虚拟机列表和详情页四项性能走势图、从 ISO 创建、由管理员导入并重命名 AlmaLinux 9/10 云镜像模板、使用已导入模板配置公网静态 IP 创建、云镜像重装、开机、关机、重启、强制断电/重置、删除和浏览器 VNC。当前仍是单宿主机版本;不提供 ISO 上传、目录自动创建、虚拟网络创建、内网 IP 模板或异网段网关。真实 AlmaLinux 10 宿主机、SMTP 服务及 MariaDB 仍需现场联调。 diff --git a/static/app.js b/static/app.js index a832be9a..74ad2891 100644 --- a/static/app.js +++ b/static/app.js @@ -29,6 +29,7 @@ const state = { sshKeysLoad: null, sshKeysReturnView: null, templateLoad: null, + templates: [], templateImportJobId: null, loadingVMs: false, pollTimer: null, @@ -228,6 +229,7 @@ function showLogin() { state.resources = null; state.hostConfig = null; state.templateLoad = null; + state.templates = []; state.templateImportJobId = null; state.selectedVmId = null; clearAdminContent(); @@ -235,7 +237,7 @@ function showLogin() { clearVmMetrics("登录后加载监控数据。"); fillSelect("vm-network", [], "network"); fillSelect("vm-iso", [], "iso"); - fillTemplateSelect([]); + fillTemplateViews([]); fillSshKeyCards([]); byId("ssh-keys-list").replaceChildren(); byId("ssh-key-form").reset(); @@ -454,6 +456,7 @@ async function showDashboard(session) { function applySession(session) { state.sessionEpoch += 1; + state.templates = []; state.sshKeys = []; state.sshKeysLoad = null; fillSshKeyCards([]); @@ -466,13 +469,13 @@ function applySession(session) { state.admin = session.admin === true; state.approved = session.approved === true; state.hostReady = session.host_ready === true; + fillTemplateViews([]); if (!state.admin) clearAdminContent(); - for (const id of ["host-settings-button", "host-settings-mobile-button", "update-settings-button", "update-settings-mobile-button", "smtp-settings-button", "smtp-settings-mobile-button", "host-update-button", "unlock-update-button", "unlock-settings-button"]) { + for (const id of ["template-settings-button", "template-settings-mobile-button", "host-settings-button", "host-settings-mobile-button", "update-settings-button", "update-settings-mobile-button", "smtp-settings-button", "smtp-settings-mobile-button", "host-update-button", "unlock-update-button", "unlock-settings-button"]) { byId(id).hidden = !state.admin; } byId("admin-menu").hidden = false; byId("admin-menu").open = false; - byId("template-import").hidden = !state.admin; text("current-username", state.username); } @@ -509,6 +512,8 @@ async function initialize() { function resetCreatePage() { byId("create-form").reset(); + byId("vm-template").value = ""; + renderTemplateCatalog("vm-template"); clearSshKeyPicker("vm-guest-ssh-key-cards"); byId("vm-root-password").value = ""; updateRootPasswordHint("vm-root-password", "vm-root-password-hint"); @@ -523,6 +528,8 @@ function resetReinstallPage() { state.reinstallExpectGeneratedPassword = false; state.reinstallPasswordPendingAck = false; byId("reinstall-form").reset(); + byId("reinstall-template").value = ""; + renderTemplateCatalog("reinstall-template"); clearSshKeyPicker("reinstall-ssh-key-cards"); byId("reinstall-root-password").value = ""; updateRootPasswordHint("reinstall-root-password", "reinstall-root-password-hint"); @@ -578,6 +585,7 @@ function showVmWorkspace({ preserveDraft = false } = {}) { byId("create-view").hidden = true; byId("reinstall-view").hidden = true; byId("ssh-keys-workspace").hidden = true; + byId("template-workspace").hidden = true; byId("update-workspace").hidden = true; byId("smtp-workspace").hidden = true; byId("smtp-password").value = ""; @@ -585,6 +593,8 @@ function showVmWorkspace({ preserveDraft = false } = {}) { byId("vm-navigation-button").setAttribute("aria-current", "page"); byId("ssh-keys-button").classList.remove("active"); byId("ssh-keys-button").removeAttribute("aria-current"); + byId("template-settings-button").classList.remove("active"); + byId("template-settings-button").removeAttribute("aria-current"); byId("update-settings-button").classList.remove("active"); byId("update-settings-button").removeAttribute("aria-current"); byId("smtp-settings-button").classList.remove("active"); @@ -615,11 +625,12 @@ async function showSshKeysWorkspace(returnView = null) { clearVmCredential(); if (returnView !== "reinstall") state.selectedVmId = null; byId("vm-workspace").hidden = true; + byId("template-workspace").hidden = true; byId("update-workspace").hidden = true; byId("smtp-workspace").hidden = true; byId("smtp-password").value = ""; byId("ssh-keys-workspace").hidden = false; - for (const id of ["vm-navigation-button", "update-settings-button", "smtp-settings-button"]) { + for (const id of ["vm-navigation-button", "template-settings-button", "update-settings-button", "smtp-settings-button"]) { byId(id).classList.remove("active"); byId(id).removeAttribute("aria-current"); } @@ -649,6 +660,40 @@ function returnFromSshKeysWorkspace() { else updateRootPasswordHint("reinstall-root-password", "reinstall-root-password-hint"); } +async function showTemplateWorkspace() { + if (!state.admin || !state.csrfToken) return; + if (!canLeaveReinstallPage()) return; + discardVmSubpageDrafts(); + byId("admin-menu").open = false; + stopVmMetrics(); + clearVmCredential(); + state.selectedVmId = null; + byId("vm-workspace").hidden = true; + byId("ssh-keys-workspace").hidden = true; + byId("update-workspace").hidden = true; + byId("smtp-workspace").hidden = true; + byId("smtp-password").value = ""; + byId("template-workspace").hidden = false; + for (const id of ["vm-navigation-button", "ssh-keys-button", "update-settings-button", "smtp-settings-button"]) { + byId(id).classList.remove("active"); + byId(id).removeAttribute("aria-current"); + } + byId("template-settings-button").classList.add("active"); + byId("template-settings-button").setAttribute("aria-current", "page"); + text("breadcrumb-current", "云镜像模板"); + showError("template-import-error", ""); + showError("template-list-error", ""); + showTemplateImportStatus(state.templateImportJobId ? "宿主机仍在后台导入模板;点击“导入模板”继续查询任务状态。" : ""); + byId("template-admin-list").replaceChildren(makeElement("p", "muted", "正在加载…")); + window.scrollTo({ top: 0, behavior: "auto" }); + try { + await loadTemplates(); + } catch (error) { + showError("template-list-error", `无法读取云镜像模板:${error.message}`); + byId("template-admin-list").replaceChildren(); + } +} + function selectedSshKeyId(containerId) { return byId(containerId).querySelector('.ssh-key-option[aria-pressed="true"]')?.dataset.keyId || ""; } @@ -801,6 +846,7 @@ async function showUpdateWorkspace() { clearVmCredential(); byId("vm-workspace").hidden = true; byId("ssh-keys-workspace").hidden = true; + byId("template-workspace").hidden = true; byId("smtp-workspace").hidden = true; byId("smtp-password").value = ""; byId("update-workspace").hidden = false; @@ -808,6 +854,8 @@ async function showUpdateWorkspace() { byId("vm-navigation-button").removeAttribute("aria-current"); byId("ssh-keys-button").classList.remove("active"); byId("ssh-keys-button").removeAttribute("aria-current"); + byId("template-settings-button").classList.remove("active"); + byId("template-settings-button").removeAttribute("aria-current"); byId("update-settings-button").classList.add("active"); byId("update-settings-button").setAttribute("aria-current", "page"); byId("smtp-settings-button").classList.remove("active"); @@ -894,12 +942,15 @@ async function showSmtpWorkspace() { clearVmCredential(); byId("vm-workspace").hidden = true; byId("ssh-keys-workspace").hidden = true; + byId("template-workspace").hidden = true; byId("update-workspace").hidden = true; byId("smtp-workspace").hidden = false; byId("vm-navigation-button").classList.remove("active"); byId("vm-navigation-button").removeAttribute("aria-current"); byId("ssh-keys-button").classList.remove("active"); byId("ssh-keys-button").removeAttribute("aria-current"); + byId("template-settings-button").classList.remove("active"); + byId("template-settings-button").removeAttribute("aria-current"); byId("update-settings-button").classList.remove("active"); byId("update-settings-button").removeAttribute("aria-current"); byId("smtp-settings-button").classList.add("active"); @@ -1871,45 +1922,139 @@ async function loadResources() { } } -function fillTemplateSelect(items, preferredId = "") { - const select = byId("vm-template"); - const previous = preferredId || select.value; - select.replaceChildren(); - const placeholder = document.createElement("option"); - placeholder.value = ""; - placeholder.textContent = items.length ? "选择云镜像模板" : "暂无模板,请先导入"; - select.append(placeholder); - for (const item of items) { - const id = String(item?.id || ""); - if (!id) continue; - const option = document.createElement("option"); - option.value = id; - option.textContent = String(item.name || id); - select.append(option); +function templateSystem(item) { + const id = String(item?.id || ""); + const family = String(item?.os_family || "").toLowerCase() === "almalinux" || /^alma(?:9|10)-/.test(id) + ? "AlmaLinux" : "其他系统"; + const oldVersion = /^alma(9|10)-/.exec(id)?.[1] || ""; + const version = /^[0-9]+$/.test(String(item?.os_version || "")) + ? String(item.os_version) : oldVersion; + return { family, version }; +} + +function renderTemplateCatalog(inputId) { + const input = byId(inputId); + const container = byId(inputId === "vm-template" ? "vm-template-catalog" : "reinstall-template-catalog"); + const opened = new Set(Array.from(container.querySelectorAll("details[open]"), (detail) => detail.dataset.family)); + const groups = new Map(); + for (const item of state.templates) { + if (!item?.id) continue; + const { family, version } = templateSystem(item); + if (!groups.has(family)) groups.set(family, []); + groups.get(family).push({ item, version }); } - if (previous && Array.from(select.options).some((option) => option.value === previous)) select.value = previous; - select.disabled = selectedCreateSource() !== "cloud" || select.options.length <= 1; - if (!items.length && state.admin) byId("template-import").open = true; - - const reinstallSelect = byId("reinstall-template"); - const reinstallPrevious = reinstallSelect.value; - reinstallSelect.replaceChildren(); - const reinstallPlaceholder = document.createElement("option"); - reinstallPlaceholder.value = ""; - reinstallPlaceholder.textContent = items.length ? "选择重装模板" : "暂无模板,请先在创建虚拟机页面导入"; - reinstallSelect.append(reinstallPlaceholder); - for (const item of items) { + if (!groups.size) { + input.value = ""; + container.replaceChildren(makeElement("p", "template-catalog-empty", state.admin + ? "暂无云镜像,请先到“云镜像模板”页面导入。" + : "暂无可用云镜像,请联系管理员导入模板。")); + return; + } + if (!state.templates.some((item) => String(item.id) === input.value)) input.value = ""; + const cards = []; + for (const [family, entries] of groups) { + entries.sort((a, b) => Number(b.version || 0) - Number(a.version || 0) + || String(a.item.name || "").localeCompare(String(b.item.name || ""), "zh-CN")); + const card = makeElement("details", "template-family-card"); + card.dataset.family = family; + card.open = opened.has(family) || entries.some(({ item }) => String(item.id) === input.value); + const summary = makeElement("summary", "template-family-summary"); + const icon = makeElement("span", family === "AlmaLinux" ? "template-family-icon alma" : "template-family-icon", family === "AlmaLinux" ? "A" : "◈"); + icon.setAttribute("aria-hidden", "true"); + summary.append(icon, makeElement("strong", "", family), makeElement("span", "template-family-arrow", "⌄")); + const versions = makeElement("div", "template-versions"); + versions.setAttribute("role", "group"); + versions.setAttribute("aria-label", `${family} 可用版本`); + for (const { item, version } of entries) { + const id = String(item.id); + const versionLabel = version ? `${family} ${version}` : "版本未标注"; + const option = makeElement("button", "template-version-option"); + option.type = "button"; + option.dataset.templateId = id; + option.setAttribute("aria-pressed", String(input.value === id)); + option.setAttribute("aria-label", `选择 ${versionLabel},${String(item.name || id)}`); + option.append(makeElement("strong", "", versionLabel), makeElement("small", "", String(item.name || id))); + option.addEventListener("click", () => { + input.value = id; + container.querySelectorAll(".template-version-option").forEach((button) => { + button.setAttribute("aria-pressed", String(button.dataset.templateId === id)); + }); + if (inputId === "vm-template") showError("create-error", ""); + else showError("reinstall-error", ""); + }); + versions.append(option); + } + card.append(summary, versions); + cards.push(card); + } + container.replaceChildren(...cards); +} + +function renderTemplateAdminList() { + const list = byId("template-admin-list"); + list.replaceChildren(); + if (!state.admin) return; + if (!state.templates.length) { + list.append(makeElement("p", "muted", "当前宿主机还没有导入云镜像模板。")); + return; + } + for (const item of state.templates) { const id = String(item?.id || ""); if (!id) continue; - const option = document.createElement("option"); - option.value = id; - option.textContent = String(item.name || id); - reinstallSelect.append(option); - } - if (reinstallPrevious && Array.from(reinstallSelect.options).some((option) => option.value === reinstallPrevious)) { - reinstallSelect.value = reinstallPrevious; + const { family, version } = templateSystem(item); + const row = makeElement("form", "template-admin-row"); + const heading = makeElement("div", "template-admin-row-heading"); + const icon = makeElement("span", "template-admin-icon", family === "AlmaLinux" ? "A" : "◈"); + icon.setAttribute("aria-hidden", "true"); + heading.append(icon, makeElement("strong", "", `${family}${version ? ` ${version}` : ""}`)); + const label = makeElement("label", "", "模板名称"); + const name = makeElement("input", ""); + name.type = "text"; + name.required = true; + name.maxLength = 64; + name.value = String(item.name || ""); + name.setAttribute("autocomplete", "off"); + name.id = `template-admin-name-${id}`; + label.htmlFor = name.id; + const button = makeElement("button", "button button-outline small", "保存名称"); + button.type = "submit"; + const error = makeElement("p", "form-error"); + error.hidden = true; + error.setAttribute("role", "alert"); + row.addEventListener("submit", async (event) => { + event.preventDefault(); + const requested = name.value.trim(); + error.hidden = true; + if (!requested || requested.length > 64) { + error.textContent = "模板名称应为 1 至 64 个字符。"; + error.hidden = false; + return; + } + if (requested === item.name) return; + setButtonBusy(button, true, "正在保存…"); + try { + await api(`/api/v1/vm-templates/${encodeURIComponent(id)}`, { method: "PATCH", body: { name: requested } }); + await loadTemplates(); + toast("模板名称已更新。", "success"); + } catch (caught) { + error.textContent = caught.message; + error.hidden = false; + } finally { + if (button.isConnected) setButtonBusy(button, false); + } + }); + row.append(heading, label, name, button, error); + list.append(row); } - reinstallSelect.disabled = reinstallSelect.options.length <= 1; +} + +function fillTemplateViews(items, preferredId = "") { + state.templates = items.filter((item) => item && typeof item.id === "string"); + if (preferredId && state.templates.some((item) => item.id === preferredId)) byId("vm-template").value = preferredId; + renderTemplateCatalog("vm-template"); + renderTemplateCatalog("reinstall-template"); + renderTemplateAdminList(); + updateCreateSource(); } async function loadTemplates(preferredId = "") { @@ -1920,7 +2065,7 @@ async function loadTemplates(preferredId = "") { try { const result = await request; if (epoch !== state.sessionEpoch || byId("dashboard-view").hidden) return; - fillTemplateSelect(Array.isArray(result?.templates) ? result.templates : [], preferredId); + fillTemplateViews(Array.isArray(result?.templates) ? result.templates : [], preferredId); } finally { if (state.templateLoad === request) state.templateLoad = null; } @@ -1947,7 +2092,7 @@ async function waitForTemplateImport(jobId) { showTemplateImportStatus(`宿主机正在后台导入镜像,已等待约 ${seconds < 60 ? `${seconds} 秒` : `${Math.floor(seconds / 60)} 分钟`}。离开页面不会取消任务。`); } } - throw new Error("模板仍在后台导入。稍后返回创建虚拟机页面查看导入状态。"); + throw new Error("模板仍在后台导入。稍后返回“云镜像模板”页面查看导入状态。"); } function selectedCreateSource() { @@ -2015,8 +2160,7 @@ function updateCreateSource() { iso.required = !cloud; iso.disabled = cloud || iso.options.length <= 1; const template = byId("vm-template"); - template.required = cloud; - template.disabled = !cloud || template.options.length <= 1; + template.disabled = !cloud; byId("vm-guest-ssh-key-cards").hidden = !cloud; ["vm-public-ip-cidr", "vm-gateway", "vm-dns"].forEach((id) => { const input = byId(id); @@ -2071,9 +2215,6 @@ function openCreate() { resetCreatePage(); fillSshKeyCards(state.sshKeys); clearSshKeyPicker("vm-guest-ssh-key-cards"); - byId("template-import").open = !!state.templateImportJobId; - showTemplateImportStatus(state.templateImportJobId ? "宿主机仍在后台导入模板。查询会自动更新;若中断,可点击“导入模板”继续查看。" : ""); - showError("template-import-error", ""); showError("create-error", ""); updateCreateSource(); showVmSubpage("create"); @@ -2582,12 +2723,15 @@ byId("vm-navigation-button").addEventListener("click", leaveUpdateWorkspace); byId("vm-navigation-mobile-button").addEventListener("click", leaveUpdateWorkspace); byId("ssh-keys-button").addEventListener("click", () => void showSshKeysWorkspace()); byId("ssh-keys-mobile-button").addEventListener("click", () => void showSshKeysWorkspace()); +byId("template-settings-button").addEventListener("click", () => void showTemplateWorkspace()); +byId("template-settings-mobile-button").addEventListener("click", () => void showTemplateWorkspace()); byId("update-settings-button").addEventListener("click", () => void showUpdateWorkspace()); byId("update-settings-mobile-button").addEventListener("click", () => void showUpdateWorkspace()); byId("smtp-settings-button").addEventListener("click", () => void showSmtpWorkspace()); byId("smtp-settings-mobile-button").addEventListener("click", () => void showSmtpWorkspace()); byId("smtp-back-button").addEventListener("click", showVmWorkspace); byId("ssh-keys-back-button").addEventListener("click", returnFromSshKeysWorkspace); +byId("template-back-button").addEventListener("click", showVmWorkspace); byId("update-back-button").addEventListener("click", leaveUpdateWorkspace); byId("unlock-settings-button").addEventListener("click", openHostSettings); byId("host-back-button").addEventListener("click", () => { @@ -2901,7 +3045,8 @@ document.querySelectorAll('input[name="source"]').forEach((radio) => radio.addEv } })); -byId("template-import-button").addEventListener("click", async () => { +byId("template-import-form").addEventListener("submit", async (event) => { + event.preventDefault(); if (!state.admin) return; showError("template-import-error", ""); const existingJobId = state.templateImportJobId; @@ -2914,8 +3059,9 @@ byId("template-import-button").addEventListener("click", async () => { showError("template-import-error", "请填写不超过 64 个字符的模板名称。"); return; } - if (!existingJobId && (!url || url.origin !== "https://repo.almalinux.org" || url.username || url.password || url.search || url.hash || !url.pathname.toLowerCase().endsWith(".qcow2"))) { - showError("template-import-error", "目前仅支持 repo.almalinux.org 上的 AlmaLinux 10 x86_64 GenericCloud qcow2 HTTPS 地址。"); + if (!existingJobId && (!url || url.origin !== "https://repo.almalinux.org" || url.username || url.password || url.search || url.hash + || !/^\/almalinux\/(9|10)\/cloud\/x86_64\/images\/AlmaLinux-\1-GenericCloud-[A-Za-z0-9._-]+\.x86_64\.qcow2$/.test(url.pathname))) { + showError("template-import-error", "目前仅支持 repo.almalinux.org 上的 AlmaLinux 9 / 10 x86_64 GenericCloud qcow2 HTTPS 地址。"); return; } if (!existingJobId && !/^[a-f0-9]{64}$/.test(sha256)) { @@ -2936,12 +3082,9 @@ byId("template-import-button").addEventListener("click", async () => { const completed = await waitForTemplateImport(jobId); state.templateImportJobId = null; showTemplateImportStatus(""); - await loadTemplates(String(completed?.template?.id || "")); - byId("template-import").open = false; - byId("template-name").value = ""; - byId("template-url").value = ""; - byId("template-sha256").value = ""; - toast("云镜像模板已导入,请在系统模板中确认选择。", "success"); + await loadTemplates(); + byId("template-import-form").reset(); + toast("云镜像模板已导入,用户现在可以在创建页面选择。", "success"); } catch (error) { showTemplateImportStatus(state.templateImportJobId ? "暂时无法确认导入进度,任务可能仍在后台运行;稍后点击按钮继续查看。" : ""); showError("template-import-error", error.message); @@ -3081,7 +3224,7 @@ byId("create-form").addEventListener("submit", async (event) => { return; } if (!body.template_id) { - showError("create-error", "请先选择或导入云镜像模板。"); + showError("create-error", state.admin ? "请先选择已导入的云镜像版本;如无可用镜像,请到“云镜像模板”页面导入。" : "请先选择云镜像版本;如无可用镜像,请联系管理员导入。"); return; } if (!automaticPublicIp && !validPublicIpCidr(body.public_ip_cidr)) { @@ -3145,7 +3288,7 @@ byId("reinstall-form").addEventListener("submit", async (event) => { const rootPassword = byId("reinstall-root-password").value; const confirmationName = byId("reinstall-confirm-name").value; if (!templateId) { - showError("reinstall-error", "请先选择已导入的云镜像模板。若列表为空,请在“创建虚拟机”页面先导入模板。"); + showError("reinstall-error", state.admin ? "请先选择已导入的云镜像版本;如无可用镜像,请到“云镜像模板”页面导入。" : "请先选择云镜像版本;如无可用镜像,请联系管理员导入。"); return; } if (selectedKeyId && !state.sshKeys.some((key) => String(key.id) === selectedKeyId)) { diff --git a/static/index.html b/static/index.html index c0ab620b..7d6207e2 100644 --- a/static/index.html +++ b/static/index.html @@ -5,8 +5,8 @@ VirtPilot · 虚拟机管理 - - + +
@@ -230,6 +230,7 @@ + @@ -242,7 +243,7 @@
-
菜单
用户
+
菜单
用户
@@ -306,14 +307,7 @@
GiB
镜像来自宿主机允许使用的 ISO 目录。
- + +