@ -3,7 +3,7 @@ import { toast } from './toast.js?v=2026100102';
// PHP owns authentication, authorisation, ownership and purchasing. No engine
// address, token, host credential or unrestricted create API exists in this UI.
const byId = ( id ) => document . getElementById ( id ) ;
const state = { csrfToken : '' , userId : '' , username : '' , admin : false , vms : [ ] , plans : [ ] , regions : [ ] , sshKeys : [ ] , templates : [ ] , templateCatalogUnavailable : false , resources : null , selectedRegionHostId : '' , view : 'list' , selectedVmId : null , detailTab : 'operations' , metricsRange : '24h' , metricsPayload : null , metricsRequest : null , metricsPollTimer : null , metricsResizeTimer : null , trafficDailyPayload : null , trafficDailyVmId : '' , trafficDailyRequest : null , trafficDailyResizeTimer : null , trafficDailyLoadedAt : 0 , credentialPassword : '' , credentialVisible : false , credentialTimer : null , r fb: null , consoleToken : 0 , loadingVMs : false , selectedKeys : { create : '' , reinstall : '' } , resourceController : null , mutationBusy : false , provisionIdempotency : '' , provisionFingerprint : '' , rescueActive : false , rescueStaged : false } ;
const state = { csrfToken : '' , userId : '' , username : '' , admin : false , vms : [ ] , plans : [ ] , regions : [ ] , sshKeys : [ ] , templates : [ ] , templateCatalogUnavailable : false , resources : null , selectedRegionHostId : '' , view : 'list' , selectedVmId : null , detailTab : 'operations' , metricsRange : '24h' , metricsPayload : null , metricsRequest : null , metricsPollTimer : null , metricsResizeTimer : null , trafficDailyPayload : null , trafficDailyVmId : '' , trafficDailyRequest : null , trafficDailyResizeTimer : null , trafficDailyLoadedAt : 0 , credentialPassword : '' , credentialVisible : false , credentialTimer : null , r einstallPassword: '' , reinstallPasswordDismissed : false , r fb: null , consoleToken : 0 , loadingVMs : false , selectedKeys : { create : '' , reinstall : '' } , resourceController : null , mutationBusy : false , provisionIdempotency : '' , provisionFingerprint : '' , rescueActive : false , rescueStaged : false } ;
// Keep the historical storage key so an interrupted deployment made just
// before an online update can still reconcile to the same opening invoice.
const PROVISION _INVOICE _STORAGE _PREFIX = 'virt-cloud:pending-order:' ;
@ -680,8 +680,55 @@ async function buy(event) {
updateCreateAvailability ( ) ;
}
}
function clearReinstallPasswordCard ( dismissed = false ) {
state . reinstallPassword = '' ;
if ( dismissed ) state . reinstallPasswordDismissed = true ;
const card = byId ( 'reinstall-password-card' ) ;
if ( ! card ) return ;
card . hidden = true ;
const value = byId ( 'reinstall-password-card-value' ) ;
if ( value ) value . textContent = '' ;
}
function showReinstallPasswordCard ( password , generated = true ) {
if ( state . reinstallPasswordDismissed || typeof password !== 'string' || password === '' ) return false ;
const card = byId ( 'reinstall-password-card' ) ;
const value = byId ( 'reinstall-password-card-value' ) ;
if ( ! card || ! value ) return false ;
state . reinstallPassword = password ;
state . reinstallPasswordDismissed = false ;
value . textContent = password ;
text ( 'reinstall-password-card-title' , generated ? '重装任务已提交' : '重装密码已提交' ) ;
text ( 'reinstall-password-card-message' , generated
? '系统已生成本次重装的 root 密码,请先复制保存。'
: '已按你填写的密码提交重装任务,请确认已保存。' ) ;
text ( 'reinstall-password-card-note' , '密码会在重装成功后生效;关闭此卡片不会取消后台任务。' ) ;
card . hidden = false ;
card . focus ( { preventScroll : true } ) ;
return true ;
}
async function loadReinstallCredential ( vm ) {
if ( state . reinstallPassword || state . reinstallPasswordDismissed || ! vm ) return ;
try {
const result = await api ( ` ${ vmPath ( vm ) } /credential ` , { method : 'POST' } ) ;
showReinstallPasswordCard ( result ? . password , false ) ;
} catch ( error ) {
toast ( '重装已完成,但密码暂时无法读取,请到实例详情查看。' , 'warning' , '密码提示' ) ;
}
}
async function trackReinstallJob ( vm , jobId ) {
try {
await pollJob ( 'reinstall' , jobId , 'reinstall-phase' , 'reinstall-error' ) ;
byId ( 'reinstall-progress' ) . hidden = true ;
await loadReinstallCredential ( vm ) ;
if ( state . reinstallPassword ) text ( 'reinstall-password-card-note' , '重装任务已完成。请将密码保存到安全位置;关闭此卡片不会删除面板中的加密凭据。' ) ;
} catch ( error ) {
byId ( 'reinstall-progress' ) . hidden = true ;
clearReinstallPasswordCard ( ) ;
toast ( error . message , 'error' , '重装任务失败' ) ;
}
}
async function openReinstall ( vm , bootstrap = null ) {
state . view = 'reinstall' ; text ( 'reinstall-subtitle' , ` ${ vm . name } · 保留原有公网网络参数,重新创建系统盘。 ` ) ; text ( 'reinstall-public-ip' , vmIp ( vm ) ) ; byId ( 'reinstall-confirm-name' ) . value = '' ; byId ( 'reinstall-root-password' ) . value = '' ; byId ( 'reinstall-template' ) . value = '' ; showError ( 'reinstall-error' , '' ) ; byId ( 'reinstall-progress' ) . hidden = true ; byId ( 'reinstall-result' ) . hidden = true ; byId ( 'reinstall-fields' ) . hidden = false ;
state . view = 'reinstall' ; state. reinstallPasswordDismissed = false ; clearReinstallPasswordCard ( ) ; text( 'reinstall-subtitle' , ` ${ vm . name } · 保留原有公网网络参数,重新创建系统盘。 ` ) ; text ( 'reinstall-public-ip' , vmIp ( vm ) ) ; byId ( 'reinstall-confirm-name' ) . value = '' ; byId ( 'reinstall-root-password' ) . value = '' ; byId ( 'reinstall-template' ) . value = '' ; showError ( 'reinstall-error' , '' ) ; byId ( 'reinstall-progress' ) . hidden = true ; byId ( 'reinstall-result' ) . hidden = true ; byId ( 'reinstall-fields' ) . hidden = false ;
const currentName = String ( vm . name ? ? '' ) ;
const nameValue = byId ( 'reinstall-confirm-name-value' ) ;
const nameInput = byId ( 'reinstall-confirm-name' ) ;
@ -708,6 +755,18 @@ async function openReinstall(vm, bootstrap = null) {
catch ( error ) { toast ( error . message , 'error' ) ; }
} ;
}
const passwordCardClose = byId ( 'reinstall-password-card-close' ) ;
const passwordCardSaved = byId ( 'reinstall-password-card-saved' ) ;
const passwordCardCopy = byId ( 'reinstall-password-card-copy' ) ;
if ( passwordCardClose ) passwordCardClose . onclick = ( ) => clearReinstallPasswordCard ( true ) ;
if ( passwordCardSaved ) passwordCardSaved . onclick = ( ) => clearReinstallPasswordCard ( true ) ;
if ( passwordCardCopy ) {
passwordCardCopy . onclick = async ( ) => {
if ( ! state . reinstallPassword ) return ;
try { await copyText ( state . reinstallPassword ) ; toast ( '重装密码已复制,请妥善保存。' , 'success' ) ; }
catch ( error ) { toast ( error . message , 'error' ) ; }
} ;
}
syncNameConfirmation ( ) ;
if ( ! vm ? . can _operate ) {
byId ( 'reinstall-fields' ) . inert = true ;
@ -742,9 +801,33 @@ async function reinstall(event) {
const template = byId ( 'reinstall-template' ) . value ; if ( ! template ) return showError ( 'reinstall-error' , '请选择系统镜像版本。' ) ;
state . mutationBusy = true ; byId ( 'reinstall-submit' ) . disabled = true ;
const body = { template _id : template , confirmation _name : vm . name } ; if ( state . selectedKeys . reinstall ) body . ssh _key _id = state . selectedKeys . reinstall ; if ( password ) body . root _password = password ;
try { const result = await api ( ` ${ vmPath ( vm ) } /reinstall ` , { method : 'POST' , body } ) ; toast ( '重装命令发送成功' ) ; byId ( 'reinstall-root-password' ) . value = '' ; byId ( 'reinstall-progress' ) . hidden = false ; if ( result . job _id ) await pollJob ( 'reinstall' , result . job _id , 'reinstall-phase' , 'reinstall-error' ) ; navigate ( vmPagePath ( vm . api _id ) ) ; }
let submitted = false ;
try {
const result = await api ( ` ${ vmPath ( vm ) } /reinstall ` , { method : 'POST' , body } ) ;
submitted = true ;
const returnedPassword = typeof result ? . initial _password === 'string' ? result . initial _password : '' ;
showReinstallPasswordCard ( returnedPassword || password , result ? . password _generated !== false && ! password ) ;
toast ( '重装任务已提交,正在后台执行。' , 'success' ) ;
byId ( 'reinstall-root-password' ) . value = '' ;
byId ( 'reinstall-progress' ) . hidden = false ;
text ( 'reinstall-phase' , '任务已提交,正在后台执行;你可以关闭当前页面。' ) ;
byId ( 'reinstall-fields' ) . inert = true ;
byId ( 'reinstall-fields' ) . setAttribute ( 'aria-disabled' , 'true' ) ;
if ( result . job _id ) void trackReinstallJob ( vm , result . job _id ) ;
else {
byId ( 'reinstall-progress' ) . hidden = true ;
toast ( '重装已提交,但任务编号缺失,请到实例详情核查状态。' , 'warning' , '重装任务' ) ;
}
}
catch ( error ) { showError ( 'reinstall-error' , error . message ) ; }
finally { delete body . root _password ; state . mutationBusy = false ; byId ( 'reinstall-submit' ) . disabled = false ; byId ( 'reinstall-progress' ) . hidden = true ; }
finally {
delete body . root _password ;
state . mutationBusy = false ;
if ( ! submitted ) {
byId ( 'reinstall-submit' ) . disabled = false ;
byId ( 'reinstall-progress' ) . hidden = true ;
}
}
}
async function openRescue ( vm ) { if ( ! vm ? . can _operate ) return toast ( '实例服务已到期或正在执行策略,日常操作暂不可用。' , 'error' ) ; state . view = 'rescue' ; text ( 'rescue-subtitle' , ` ${ vm . name } · 挂载救援 ISO 并启动救援环境。 ` ) ; byId ( 'rescue-confirm-name' ) . value = '' ; byId ( 'rescue-template-id' ) . value = '' ; showError ( 'rescue-error' , '' ) ; try { const [ resources , status ] = await Promise . all ( [ api ( ` /api/v1/resources?vm_id= ${ encodeURIComponent ( vm . api _id ) } ` ) , api ( ` ${ vmPath ( vm ) } /rescue ` ) ] ) ; const container = byId ( 'rescue-template-cards' ) ; container . replaceChildren ( ) ; for ( const template of items ( resources . rescue _templates ) ) { const button = makeElement ( 'button' , 'ssh-key-option' , template . name ) ; button . type = 'button' ; button . dataset . templateId = String ( template . id ) ; button . setAttribute ( 'aria-pressed' , 'false' ) ; button . addEventListener ( 'click' , ( ) => { byId ( 'rescue-template-id' ) . value = template . id ; container . querySelectorAll ( 'button' ) . forEach ( item => item . setAttribute ( 'aria-pressed' , String ( item === button ) ) ) ; } ) ; container . append ( button ) ; } if ( ! container . children . length ) container . append ( makeElement ( 'p' , 'muted' , '暂无救援模板,请联系管理员。' ) ) ; renderRescueStatus ( status ) ; } catch ( error ) { showError ( 'rescue-error' , error . message ) ; } }
function renderRescueStatus ( status ) { state . rescueActive = status . active === true || status . rescue _mode === true ; state . rescueStaged = status . staged === true || status . rescue _staged === true ; text ( 'rescue-status' , status . message || ( state . rescueActive ? '当前已进入救援模式。' : state . rescueStaged ? '救援镜像已预挂载,正常关机失败时可以强制冷启动进入救援。' : '尚未挂载救援镜像。' ) ) ; byId ( 'rescue-force-start' ) . hidden = ! state . rescueStaged ; byId ( 'rescue-exit' ) . hidden = ! state . rescueActive && ! state . rescueStaged ; }
@ -1646,6 +1729,7 @@ async function selectVmByApiId(apiId, bootstrapVm = null) {
function bindPageCleanup ( ) {
window . addEventListener ( 'pagehide' , ( ) => {
clearCredential ( ) ;
clearReinstallPasswordCard ( ) ;
stopVmMetrics ( ) ;
stopVmTrafficDaily ( ) ;
closeConsole ( ) ;