@ -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" ) ;
fillTemplate Select ( [ ] ) ;
fillTemplate Views ( [ ] ) ;
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 ;
fillTemplate Select ( Array . isArray ( result ? . templates ) ? result . templates : [ ] , preferredId ) ;
fillTemplate Views ( 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 ) ) {