You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
5.4 KiB
JavaScript
145 lines
5.4 KiB
JavaScript
const assetVersion = new URL(import.meta.url).searchParams.get('v');
|
|
|
|
function versionedModuleUrl(path) {
|
|
const url = new URL(path, import.meta.url);
|
|
url.search = '';
|
|
if (assetVersion) url.searchParams.set('v', assetVersion);
|
|
return url.href;
|
|
}
|
|
|
|
const [{
|
|
announceTaskAccepted, observeTask, recoveryTaskFailureNotice, taskUrl,
|
|
taskSucceededWithoutRecovery,
|
|
}, { createAdminInstanceTransport }, { createAdminInstanceDirectory },
|
|
{ createAdminInstanceDetail }, { createAdminInstanceConfiguration },
|
|
{ createAdminInstanceMigration }, { createAdminInstanceBackups }] = await Promise.all([
|
|
import(versionedModuleUrl('./task-observer.js')),
|
|
import(versionedModuleUrl('./admin-instance-transport.js')),
|
|
import(versionedModuleUrl('./admin-instance-directory.js')),
|
|
import(versionedModuleUrl('./admin-instance-detail.js')),
|
|
import(versionedModuleUrl('./admin-instance-configuration.js')),
|
|
import(versionedModuleUrl('./admin-instance-migration.js')),
|
|
import(versionedModuleUrl('./admin-instance-backups.js')),
|
|
]);
|
|
|
|
const root = document.querySelector('[data-admin-instances]');
|
|
const byId = id => document.getElementById(id);
|
|
const { read, mutate } = createAdminInstanceTransport({ taskUrl });
|
|
|
|
function mutationBlockMessage(task, action) {
|
|
if (task?.blocks_mutation !== true) return '';
|
|
const id = task?.id || '—';
|
|
if (task.completed === false && task.status === 'active') {
|
|
return `实例已有任务 #${id}(执行中),完成前不能${action}。`;
|
|
}
|
|
if (task.completed === true && task.recovery?.pending === true) {
|
|
const code = String(task.recovery?.code || task.finalization_error_code || '');
|
|
if (code === 'recovery_dispatch_ledger_conflict' || task.dispatch_state === 'invalid') {
|
|
return `任务 #${id} 的派发账本异常,资源保持安全锁定;修复账本并完成核验前不能${action}。`;
|
|
}
|
|
if (task.dispatch_state === 'not_dispatched') {
|
|
return `任务 #${id} 已确认未派发到宿主机,但 PHP 本地账本收尾尚未完成;收尾成功前不能${action}。`;
|
|
}
|
|
return `任务 #${id} 尚在持续恢复,资源保持安全锁定;恢复收敛前不能${action}。`;
|
|
}
|
|
if (task.completed === true && task.status === 'failed') {
|
|
return `任务 #${id} 已失败,资源安全锁定,需管理员人工处理后才能${action}。`;
|
|
}
|
|
return `任务 #${id} 状态协议无效,资源安全锁定,需管理员人工处理后才能${action}。`;
|
|
}
|
|
|
|
function taskFailureMessage(task, fallback) {
|
|
const message = task?.error || fallback;
|
|
const recoveryNotice = recoveryTaskFailureNotice(task);
|
|
if (recoveryNotice) return recoveryNotice;
|
|
if (task?.effect_may_have_applied) {
|
|
return `${message} 任务已失败,操作效果可能已经发生,相关资源已锁定。请勿再次提交,并将任务编号交由管理员核查。`;
|
|
}
|
|
return task?.safe_to_resubmit === false
|
|
? `${message} 任务已失败且相关资源已锁定,请勿再次提交,并联系管理员核查。` : message;
|
|
}
|
|
|
|
function listNotice(message, error = false) {
|
|
const node = byId('instances-notice');
|
|
if (!node) return;
|
|
node.textContent = message;
|
|
node.className = `admin-config-status ${error ? 'is-error' : 'is-success'}`;
|
|
node.setAttribute('role', error ? 'alert' : 'status');
|
|
}
|
|
|
|
let directory = null;
|
|
let detail = null;
|
|
let configuration = null;
|
|
let backups = null;
|
|
|
|
async function copy(value) {
|
|
try {
|
|
if (!navigator.clipboard?.writeText) throw new Error('Clipboard unavailable');
|
|
await navigator.clipboard.writeText(value);
|
|
listNotice('已复制。');
|
|
if (detail?.isOpen()) detail.notice('已复制。');
|
|
} catch {
|
|
const message = '复制失败,请长按或选中文本手动复制。';
|
|
listNotice(message, true);
|
|
if (detail?.isOpen()) detail.notice(message, true);
|
|
}
|
|
}
|
|
|
|
if (root) {
|
|
let bootstrap = null;
|
|
try { bootstrap = JSON.parse(byId('admin-instances-bootstrap').textContent); }
|
|
catch { /* The directory retries the canonical endpoint. */ }
|
|
|
|
directory = createAdminInstanceDirectory({
|
|
root,
|
|
byId,
|
|
read,
|
|
onOpenDetail: (id, opener) => { void detail?.open(id, opener); },
|
|
onOpenBackupLibrary: opener => backups?.openLibrary(opener),
|
|
onCopy: value => { void copy(value); },
|
|
});
|
|
|
|
configuration = createAdminInstanceConfiguration({
|
|
byId,
|
|
read,
|
|
mutate,
|
|
announceTaskAccepted,
|
|
observeTask,
|
|
taskSucceededWithoutRecovery,
|
|
taskFailureMessage,
|
|
mutationBlockMessage,
|
|
onNotice: (message, error = false) => detail?.notice(message, error),
|
|
onRefresh: id => detail?.refresh(id),
|
|
});
|
|
|
|
backups = createAdminInstanceBackups({
|
|
byId,
|
|
read,
|
|
mutate,
|
|
createMigration: createAdminInstanceMigration,
|
|
announceTaskAccepted,
|
|
observeTask,
|
|
taskSucceededWithoutRecovery,
|
|
taskFailureMessage,
|
|
mutationBlockMessage,
|
|
onNotice: (message, error = false) => detail?.notice(message, error),
|
|
});
|
|
|
|
detail = createAdminInstanceDetail({
|
|
byId,
|
|
read,
|
|
renderConfiguration: vm => configuration.render(vm),
|
|
cancelConfiguration: reason => configuration.cancel(reason),
|
|
loadBackups: (id, force = false) => { void backups.load(id, force); },
|
|
cancelBackups: reason => backups.cancel(reason),
|
|
onPublicIpChange: (id, publicIp, publicIpCidr) => {
|
|
directory.updatePublicIp(id, publicIp, publicIpCidr);
|
|
},
|
|
onCopy: value => { void copy(value); },
|
|
});
|
|
|
|
backups.start();
|
|
detail.start();
|
|
directory.start(bootstrap);
|
|
}
|