|
|
|
|
@ -34,6 +34,8 @@ const state = {
|
|
|
|
|
createKey: '', createFingerprint: '', userKey: 'unknown',
|
|
|
|
|
mutatingVpcs: new Set(), mutatingInstances: new Set(),
|
|
|
|
|
observers: new Map(), terminalTasks: new Set(),
|
|
|
|
|
expandedVpcs: new Set(), selections: new Map(),
|
|
|
|
|
batchPlan: null, batchAdvancing: false, batchAdvanceScheduled: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function element(tag, className = '', content) {
|
|
|
|
|
@ -87,6 +89,7 @@ function apply(payload) {
|
|
|
|
|
state.used = Math.max(0, Number(payload?.used) || state.items.length);
|
|
|
|
|
render();
|
|
|
|
|
observeActiveTasks();
|
|
|
|
|
scheduleBatchAdvance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderRegions() {
|
|
|
|
|
@ -98,8 +101,7 @@ function renderRegions() {
|
|
|
|
|
empty.value = '';
|
|
|
|
|
select.append(empty);
|
|
|
|
|
for (const region of state.regions) {
|
|
|
|
|
const option = element('option', '',
|
|
|
|
|
`${region.name} · 可分配 ${region.available} 个子网`);
|
|
|
|
|
const option = element('option', '', region.name || region.code || '未命名地区');
|
|
|
|
|
option.value = String(region.id);
|
|
|
|
|
option.selected = String(region.id) === selected;
|
|
|
|
|
select.append(option);
|
|
|
|
|
@ -161,6 +163,89 @@ function candidatesForVpc(vpc) {
|
|
|
|
|
&& Number(instance?.region_id || 0) === regionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function vpcKey(vpc) {
|
|
|
|
|
return String(vpc?.vpc_key || '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function publicIpv4(instance) {
|
|
|
|
|
return String(instance?.public_ipv4 || '').trim() || '未分配';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function selectionForVpc(vpc) {
|
|
|
|
|
const key = vpcKey(vpc);
|
|
|
|
|
if (!state.selections.has(key)) state.selections.set(key, new Set());
|
|
|
|
|
return state.selections.get(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function batchStorageKey() {
|
|
|
|
|
return `virt-cloud:vpc-attach-batch:${state.userKey}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function persistBatchPlan() {
|
|
|
|
|
try {
|
|
|
|
|
if (state.batchPlan) {
|
|
|
|
|
sessionStorage.setItem(batchStorageKey(), JSON.stringify(state.batchPlan));
|
|
|
|
|
} else {
|
|
|
|
|
sessionStorage.removeItem(batchStorageKey());
|
|
|
|
|
}
|
|
|
|
|
} catch {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function restoreBatchPlan() {
|
|
|
|
|
let saved = null;
|
|
|
|
|
try { saved = JSON.parse(sessionStorage.getItem(batchStorageKey()) || 'null'); }
|
|
|
|
|
catch {}
|
|
|
|
|
const key = String(saved?.vpcKey || '');
|
|
|
|
|
const ids = Array.isArray(saved?.ids)
|
|
|
|
|
? [...new Set(saved.ids.map(String).filter(id => /^[0-9]+$/.test(id)))] : [];
|
|
|
|
|
if (!/^[a-f0-9]{32}$/.test(key) || !ids.length
|
|
|
|
|
|| !['running', 'failed', 'uncertain'].includes(String(saved?.status || ''))) {
|
|
|
|
|
state.batchPlan = null;
|
|
|
|
|
persistBatchPlan();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const completed = Array.isArray(saved?.completed)
|
|
|
|
|
? [...new Set(saved.completed.map(String).filter(id => ids.includes(id)))] : [];
|
|
|
|
|
const current = ids.includes(String(saved?.current || ''))
|
|
|
|
|
? String(saved.current) : '';
|
|
|
|
|
state.batchPlan = {
|
|
|
|
|
vpcKey: key,
|
|
|
|
|
ids,
|
|
|
|
|
completed,
|
|
|
|
|
current,
|
|
|
|
|
currentState: current ? String(saved?.currentState || 'submitting') : '',
|
|
|
|
|
status: String(saved.status),
|
|
|
|
|
failedMessage: String(saved?.failedMessage || ''),
|
|
|
|
|
};
|
|
|
|
|
state.expandedVpcs.add(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clearBatchPlan() {
|
|
|
|
|
state.batchPlan = null;
|
|
|
|
|
persistBatchPlan();
|
|
|
|
|
setPageStatus();
|
|
|
|
|
render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function failBatchPlan(status, message) {
|
|
|
|
|
if (!state.batchPlan) return;
|
|
|
|
|
state.batchPlan.status = status;
|
|
|
|
|
state.batchPlan.failedMessage = message;
|
|
|
|
|
persistBatchPlan();
|
|
|
|
|
setPageStatus(message, status === 'uncertain' ? 'warning' : 'error');
|
|
|
|
|
render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function scheduleBatchAdvance() {
|
|
|
|
|
if (state.batchAdvanceScheduled || state.batchAdvancing || state.loading
|
|
|
|
|
|| state.batchPlan?.status !== 'running') return;
|
|
|
|
|
state.batchAdvanceScheduled = true;
|
|
|
|
|
queueMicrotask(() => {
|
|
|
|
|
state.batchAdvanceScheduled = false;
|
|
|
|
|
void advanceBatchPlan();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderInstanceRow(instance, vpc) {
|
|
|
|
|
const port = instance?.port || {};
|
|
|
|
|
const [label, className] = PORT_STATE_LABELS[String(port.state || '')]
|
|
|
|
|
@ -169,7 +254,9 @@ function renderInstanceRow(instance, vpc) {
|
|
|
|
|
const identity = element('div', 'cloud-vpc-instance-identity');
|
|
|
|
|
identity.append(element('strong', '', instance.name || `实例 #${instanceId(instance)}`));
|
|
|
|
|
identity.append(element('span', '',
|
|
|
|
|
`${port.private_ip || '私网 IP 待分配'} · ${port.mac_address || 'MAC 待生成'}`));
|
|
|
|
|
`公网 IPv4 ${publicIpv4(instance)}`));
|
|
|
|
|
identity.append(element('span', '',
|
|
|
|
|
`私网 ${port.private_ip || '待分配'} · ${port.mac_address || 'MAC 待生成'}`));
|
|
|
|
|
if (instance.action_unavailable_reason) {
|
|
|
|
|
identity.append(element('small', '', instance.action_unavailable_reason));
|
|
|
|
|
}
|
|
|
|
|
@ -194,22 +281,53 @@ function renderInstanceRow(instance, vpc) {
|
|
|
|
|
}
|
|
|
|
|
if (button) {
|
|
|
|
|
button.type = 'button';
|
|
|
|
|
button.disabled = mutating || Boolean(activeTask) || !action;
|
|
|
|
|
button.disabled = mutating || Boolean(activeTask) || !action
|
|
|
|
|
|| Boolean(state.batchPlan);
|
|
|
|
|
controls.append(button);
|
|
|
|
|
}
|
|
|
|
|
row.append(identity, status, controls);
|
|
|
|
|
return row;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderBatchStatus(vpc) {
|
|
|
|
|
const plan = state.batchPlan;
|
|
|
|
|
if (!plan || plan.vpcKey !== vpcKey(vpc)) return null;
|
|
|
|
|
const box = element('div', `cloud-vpc-batch-status ${plan.status}`);
|
|
|
|
|
const content = element('div');
|
|
|
|
|
const completed = plan.completed.length;
|
|
|
|
|
const current = state.instances.find(item => instanceId(item) === plan.current);
|
|
|
|
|
let title = `已完成 ${completed} / ${plan.ids.length} 台`;
|
|
|
|
|
let message = plan.failedMessage;
|
|
|
|
|
if (plan.status === 'running') {
|
|
|
|
|
title = `正在按顺序应用 ${completed} / ${plan.ids.length} 台`;
|
|
|
|
|
message = plan.current
|
|
|
|
|
? `当前处理:${current?.name || `实例 #${plan.current}`}。完成后会自动提交下一台。`
|
|
|
|
|
: '正在核对下一台实例的连接资格。';
|
|
|
|
|
}
|
|
|
|
|
content.append(element('strong', '', title), element('small', '', message));
|
|
|
|
|
box.append(content);
|
|
|
|
|
if (plan.status !== 'running') {
|
|
|
|
|
const clear = element('button', 'button button-outline small', '清除队列');
|
|
|
|
|
clear.type = 'button';
|
|
|
|
|
clear.addEventListener('click', clearBatchPlan);
|
|
|
|
|
box.append(clear);
|
|
|
|
|
}
|
|
|
|
|
return box;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderInstanceManager(vpc) {
|
|
|
|
|
const section = element('section', 'cloud-vpc-instance-manager');
|
|
|
|
|
const heading = element('div', 'cloud-vpc-instance-heading');
|
|
|
|
|
const title = element('div');
|
|
|
|
|
title.append(element('strong', '', '实例连接'),
|
|
|
|
|
element('small', '', '加入或移出都会自动正常关机并冷启动。'));
|
|
|
|
|
element('small', '',
|
|
|
|
|
'可一次选择多台;系统会逐台正常关机、应用网卡并冷启动,请在队列完成前保持页面打开。'));
|
|
|
|
|
heading.append(title);
|
|
|
|
|
section.append(heading);
|
|
|
|
|
|
|
|
|
|
const batchStatus = renderBatchStatus(vpc);
|
|
|
|
|
if (batchStatus) section.append(batchStatus);
|
|
|
|
|
|
|
|
|
|
const connected = instancesForVpc(vpc);
|
|
|
|
|
const rows = element('div', 'cloud-vpc-instance-list');
|
|
|
|
|
if (!connected.length) {
|
|
|
|
|
@ -220,41 +338,90 @@ function renderInstanceManager(vpc) {
|
|
|
|
|
section.append(rows);
|
|
|
|
|
|
|
|
|
|
const candidates = candidatesForVpc(vpc);
|
|
|
|
|
const eligibleCandidateIds = new Set(candidates
|
|
|
|
|
.filter(instance => instance.can_attach === true).map(instanceId));
|
|
|
|
|
const selection = selectionForVpc(vpc);
|
|
|
|
|
for (const id of [...selection]) {
|
|
|
|
|
if (!eligibleCandidateIds.has(id)) selection.delete(id);
|
|
|
|
|
}
|
|
|
|
|
const form = element('form', 'cloud-vpc-attach-form');
|
|
|
|
|
const field = element('div', 'field');
|
|
|
|
|
const label = element('label', '', '加入同地区实例');
|
|
|
|
|
const select = document.createElement('select');
|
|
|
|
|
const placeholder = element('option', '', candidates.length
|
|
|
|
|
? '请选择实例' : '当前地区没有可加入的实例');
|
|
|
|
|
placeholder.value = '';
|
|
|
|
|
select.append(placeholder);
|
|
|
|
|
const picker = element('fieldset', 'cloud-vpc-candidate-picker');
|
|
|
|
|
const legend = element('legend', '', '选择要加入的同地区实例');
|
|
|
|
|
picker.append(legend);
|
|
|
|
|
const list = element('div', 'cloud-vpc-candidate-list');
|
|
|
|
|
const checkboxes = [];
|
|
|
|
|
const batchBlocked = Boolean(state.batchPlan);
|
|
|
|
|
for (const instance of candidates) {
|
|
|
|
|
const reason = String(instance.action_unavailable_reason || '');
|
|
|
|
|
const option = element('option', '',
|
|
|
|
|
`${instance.name || `实例 #${instanceId(instance)}`}${reason ? ` · ${reason}` : ''}`);
|
|
|
|
|
option.value = instanceId(instance);
|
|
|
|
|
option.disabled = instance.can_attach !== true;
|
|
|
|
|
select.append(option);
|
|
|
|
|
const id = instanceId(instance);
|
|
|
|
|
const row = element('label', 'cloud-vpc-candidate-row');
|
|
|
|
|
const checkbox = document.createElement('input');
|
|
|
|
|
checkbox.type = 'checkbox';
|
|
|
|
|
checkbox.value = id;
|
|
|
|
|
checkbox.checked = selection.has(id);
|
|
|
|
|
checkbox.disabled = batchBlocked || instance.can_attach !== true
|
|
|
|
|
|| state.mutatingInstances.has(id);
|
|
|
|
|
const identity = element('span', 'cloud-vpc-candidate-identity');
|
|
|
|
|
identity.append(element('strong', '', instance.name || `实例 #${id}`),
|
|
|
|
|
element('small', '', `公网 IPv4 ${publicIpv4(instance)}`));
|
|
|
|
|
if (reason) identity.append(element('small', 'warning', reason));
|
|
|
|
|
row.classList.toggle('selected', checkbox.checked);
|
|
|
|
|
row.classList.toggle('disabled', checkbox.disabled);
|
|
|
|
|
row.append(checkbox, identity);
|
|
|
|
|
list.append(row);
|
|
|
|
|
checkboxes.push({ checkbox, id, row });
|
|
|
|
|
}
|
|
|
|
|
label.htmlFor = `cloud-vpc-instance-${vpc.vpc_key}`;
|
|
|
|
|
select.id = label.htmlFor;
|
|
|
|
|
select.disabled = !candidates.some(instance => instance.can_attach === true);
|
|
|
|
|
field.append(label, select);
|
|
|
|
|
if (!candidates.length) {
|
|
|
|
|
list.append(element('p', 'cloud-vpc-instance-empty',
|
|
|
|
|
'当前地区没有可加入的实例。'));
|
|
|
|
|
}
|
|
|
|
|
picker.append(list);
|
|
|
|
|
|
|
|
|
|
const submit = element('button', 'button button-primary small', '加入并重启');
|
|
|
|
|
const toolbar = element('div', 'cloud-vpc-batch-toolbar');
|
|
|
|
|
const selectionCount = element('span', '', '已选择 0 台');
|
|
|
|
|
const selectAll = element('button', 'button button-outline small', '全选可用');
|
|
|
|
|
selectAll.type = 'button';
|
|
|
|
|
const submit = element('button', 'button button-primary small', '应用所选实例');
|
|
|
|
|
submit.type = 'submit';
|
|
|
|
|
submit.disabled = true;
|
|
|
|
|
select.addEventListener('change', () => {
|
|
|
|
|
const selected = candidates.find(instance => instanceId(instance) === select.value);
|
|
|
|
|
submit.disabled = !selected || selected.can_attach !== true
|
|
|
|
|
|| state.mutatingInstances.has(instanceId(selected));
|
|
|
|
|
const updateControls = () => {
|
|
|
|
|
const count = [...selection].filter(id => eligibleCandidateIds.has(id)).length;
|
|
|
|
|
selectionCount.textContent = `已选择 ${count} 台`;
|
|
|
|
|
submit.textContent = count ? `应用 ${count} 台实例` : '应用所选实例';
|
|
|
|
|
submit.disabled = batchBlocked || count < 1;
|
|
|
|
|
const selectable = checkboxes.filter(item => !item.checkbox.disabled);
|
|
|
|
|
const allSelected = selectable.length > 0
|
|
|
|
|
&& selectable.every(item => selection.has(item.id));
|
|
|
|
|
selectAll.textContent = allSelected ? '取消全选' : '全选可用';
|
|
|
|
|
selectAll.disabled = batchBlocked || selectable.length < 1;
|
|
|
|
|
};
|
|
|
|
|
for (const item of checkboxes) {
|
|
|
|
|
item.checkbox.addEventListener('change', () => {
|
|
|
|
|
if (item.checkbox.checked) selection.add(item.id);
|
|
|
|
|
else selection.delete(item.id);
|
|
|
|
|
item.row.classList.toggle('selected', item.checkbox.checked);
|
|
|
|
|
updateControls();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
selectAll.addEventListener('click', () => {
|
|
|
|
|
const selectable = checkboxes.filter(item => !item.checkbox.disabled);
|
|
|
|
|
const allSelected = selectable.length > 0
|
|
|
|
|
&& selectable.every(item => selection.has(item.id));
|
|
|
|
|
for (const item of selectable) {
|
|
|
|
|
item.checkbox.checked = !allSelected;
|
|
|
|
|
item.row.classList.toggle('selected', !allSelected);
|
|
|
|
|
if (allSelected) selection.delete(item.id);
|
|
|
|
|
else selection.add(item.id);
|
|
|
|
|
}
|
|
|
|
|
updateControls();
|
|
|
|
|
});
|
|
|
|
|
form.addEventListener('submit', event => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
const selected = candidates.find(instance => instanceId(instance) === select.value);
|
|
|
|
|
if (selected?.can_attach === true) void mutatePort(selected, vpc, 'attach');
|
|
|
|
|
void startBatchAttach(vpc, candidates.filter(instance =>
|
|
|
|
|
selection.has(instanceId(instance)) && instance.can_attach === true));
|
|
|
|
|
});
|
|
|
|
|
form.append(field, submit);
|
|
|
|
|
toolbar.append(selectionCount, selectAll, submit);
|
|
|
|
|
form.append(picker, toolbar);
|
|
|
|
|
updateControls();
|
|
|
|
|
section.append(form);
|
|
|
|
|
return section;
|
|
|
|
|
}
|
|
|
|
|
@ -270,8 +437,11 @@ function render() {
|
|
|
|
|
list.append(empty);
|
|
|
|
|
}
|
|
|
|
|
for (const vpc of state.items) {
|
|
|
|
|
const key = vpcKey(vpc);
|
|
|
|
|
const [label, className] = vpcState(vpc.state);
|
|
|
|
|
const card = element('article', 'cloud-vpc-card');
|
|
|
|
|
const card = element('details', 'cloud-vpc-card cloud-vpc-disclosure');
|
|
|
|
|
card.open = state.expandedVpcs.has(key);
|
|
|
|
|
const summary = element('summary', 'cloud-vpc-card-summary');
|
|
|
|
|
const heading = element('div', 'cloud-vpc-card-heading');
|
|
|
|
|
const title = element('div', 'cloud-vpc-card-title');
|
|
|
|
|
title.append(element('span', 'cloud-vpc-card-icon', '◇'),
|
|
|
|
|
@ -279,7 +449,13 @@ function render() {
|
|
|
|
|
title.lastElementChild.append(element('small', '',
|
|
|
|
|
`${vpc.region_name} · ${vpc.port_count} 台实例`));
|
|
|
|
|
heading.append(title, element('span', `state-pill ${className}`, label));
|
|
|
|
|
summary.append(heading);
|
|
|
|
|
card.addEventListener('toggle', () => {
|
|
|
|
|
if (card.open) state.expandedVpcs.add(key);
|
|
|
|
|
else state.expandedVpcs.delete(key);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const body = element('div', 'cloud-vpc-card-body');
|
|
|
|
|
const facts = element('dl', 'cloud-vpc-card-facts');
|
|
|
|
|
for (const [factLabel, value] of [
|
|
|
|
|
['私网 CIDR', vpc.cidr],
|
|
|
|
|
@ -295,17 +471,19 @@ function render() {
|
|
|
|
|
const remove = element('button', 'button button-outline small danger', '删除 VPC');
|
|
|
|
|
remove.type = 'button';
|
|
|
|
|
remove.disabled = state.mutatingVpcs.has(String(vpc.vpc_key))
|
|
|
|
|
|| vpc.can_delete !== true;
|
|
|
|
|
|| vpc.can_delete !== true || Boolean(state.batchPlan);
|
|
|
|
|
remove.title = vpc.port_count > 0 ? '请先移出该 VPC 中的全部实例。' : '';
|
|
|
|
|
remove.addEventListener('click', () => void removeVpc(vpc));
|
|
|
|
|
actions.append(remove);
|
|
|
|
|
card.append(heading, facts, renderInstanceManager(vpc), actions);
|
|
|
|
|
body.append(facts, renderInstanceManager(vpc), actions);
|
|
|
|
|
card.append(summary, body);
|
|
|
|
|
list.append(card);
|
|
|
|
|
}
|
|
|
|
|
byId('cloud-vpcs-count').textContent = `${state.items.length} 个`;
|
|
|
|
|
byId('cloud-vpcs-quota').textContent = `${state.used} / ${state.maximum}`;
|
|
|
|
|
byId('cloud-vpcs-refresh').disabled = state.loading || state.creating
|
|
|
|
|
|| state.mutatingInstances.size > 0 || state.mutatingVpcs.size > 0;
|
|
|
|
|
|| state.mutatingInstances.size > 0 || state.mutatingVpcs.size > 0
|
|
|
|
|
|| state.batchPlan?.status === 'running';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function taskFailureMessage(task) {
|
|
|
|
|
@ -331,12 +509,24 @@ function observeReceipt(instance, receipt, accepted = false) {
|
|
|
|
|
state.observers.delete(id);
|
|
|
|
|
state.terminalTasks.add(id);
|
|
|
|
|
state.mutatingInstances.delete(instanceId(instance));
|
|
|
|
|
const batchPlanAtTerminal = state.batchPlan;
|
|
|
|
|
const belongsToBatch = batchPlanAtTerminal?.status === 'running'
|
|
|
|
|
&& batchPlanAtTerminal.current === instanceId(instance);
|
|
|
|
|
void load(true).then(() => {
|
|
|
|
|
const refreshed = state.instances.find(item =>
|
|
|
|
|
instanceId(item) === instanceId(instance));
|
|
|
|
|
const nextTask = taskId(refreshed?.active_task);
|
|
|
|
|
const portState = String(refreshed?.port?.state || '');
|
|
|
|
|
const settled = !refreshed?.port || portState === 'attached';
|
|
|
|
|
if (belongsToBatch) {
|
|
|
|
|
if (state.batchPlan !== batchPlanAtTerminal
|
|
|
|
|
|| state.batchPlan?.status !== 'running') return;
|
|
|
|
|
setPageStatus(taskSucceededWithoutRecovery(task)
|
|
|
|
|
? '批量 VPC 队列正在核对当前实例,收敛后会自动处理下一台。'
|
|
|
|
|
: taskFailureMessage(task),
|
|
|
|
|
taskSucceededWithoutRecovery(task) ? '' : 'error');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (nextTask && nextTask !== id) {
|
|
|
|
|
setPageStatus(taskSucceededWithoutRecovery(task)
|
|
|
|
|
? 'VPC 编排正在继续执行下一阶段,请勿重复提交。'
|
|
|
|
|
@ -399,27 +589,164 @@ async function load(force = false) {
|
|
|
|
|
state.controller = null;
|
|
|
|
|
state.loading = false;
|
|
|
|
|
render();
|
|
|
|
|
scheduleBatchAdvance();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function startBatchAttach(vpc, instances) {
|
|
|
|
|
if (state.batchPlan || !instances.length) return;
|
|
|
|
|
const confirmed = await requestUserConfirmation({
|
|
|
|
|
title: `确认将 ${instances.length} 台实例加入 VPC`,
|
|
|
|
|
message: `系统会按顺序将所选实例加入“${vpc.name}”。每台实例都会正常关机、应用私网网卡并冷启动;前一台完全收敛后才会提交下一台。`,
|
|
|
|
|
confirmText: `应用 ${instances.length} 台实例`,
|
|
|
|
|
tone: 'default',
|
|
|
|
|
});
|
|
|
|
|
if (!confirmed || state.batchPlan) return;
|
|
|
|
|
const key = vpcKey(vpc);
|
|
|
|
|
state.batchPlan = {
|
|
|
|
|
vpcKey: key,
|
|
|
|
|
ids: instances.map(instanceId),
|
|
|
|
|
completed: [],
|
|
|
|
|
current: '',
|
|
|
|
|
currentState: '',
|
|
|
|
|
status: 'running',
|
|
|
|
|
failedMessage: '',
|
|
|
|
|
};
|
|
|
|
|
state.selections.get(key)?.clear();
|
|
|
|
|
state.expandedVpcs.add(key);
|
|
|
|
|
persistBatchPlan();
|
|
|
|
|
setPageStatus(`已接收 ${instances.length} 台实例,将按顺序应用 VPC 私网。`);
|
|
|
|
|
render();
|
|
|
|
|
scheduleBatchAdvance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function advanceBatchPlan() {
|
|
|
|
|
const plan = state.batchPlan;
|
|
|
|
|
if (!plan || plan.status !== 'running' || state.batchAdvancing
|
|
|
|
|
|| state.loading) return;
|
|
|
|
|
state.batchAdvancing = true;
|
|
|
|
|
try {
|
|
|
|
|
const vpc = state.items.find(item => vpcKey(item) === plan.vpcKey);
|
|
|
|
|
if (!vpc) {
|
|
|
|
|
failBatchPlan('failed', '批量加入已停止:目标 VPC 不存在或已不可见。');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (state.batchPlan === plan && plan.status === 'running') {
|
|
|
|
|
if (plan.current) {
|
|
|
|
|
const current = state.instances.find(item => instanceId(item) === plan.current);
|
|
|
|
|
if (!current) {
|
|
|
|
|
failBatchPlan('failed', `批量加入已停止:实例 #${plan.current} 已不可见。`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const attached = vpcKey(current?.port) === plan.vpcKey
|
|
|
|
|
&& String(current?.port?.state || '') === 'attached';
|
|
|
|
|
if (attached) {
|
|
|
|
|
if (!plan.completed.includes(plan.current)) plan.completed.push(plan.current);
|
|
|
|
|
plan.current = '';
|
|
|
|
|
plan.currentState = '';
|
|
|
|
|
persistBatchPlan();
|
|
|
|
|
render();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (taskId(current?.active_task)
|
|
|
|
|
|| state.mutatingInstances.has(instanceId(current))) return;
|
|
|
|
|
if (current?.port) {
|
|
|
|
|
const message = current.can_retry_attach === true
|
|
|
|
|
? `实例“${current.name}”的连接阶段需要人工重试,后续实例尚未提交。`
|
|
|
|
|
: `实例“${current.name}”未收敛到已连接状态,后续实例尚未提交。`;
|
|
|
|
|
failBatchPlan('failed', message);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (plan.currentState === 'submitting' || plan.currentState === 'accepted') {
|
|
|
|
|
failBatchPlan('uncertain',
|
|
|
|
|
`实例“${current.name}”的提交结果无法从服务端确认,已停止后续队列,请先在任务中心核对。`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
failBatchPlan('failed', `实例“${current.name}”未能加入 VPC,后续实例尚未提交。`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nextId = plan.ids.find(id => !plan.completed.includes(id));
|
|
|
|
|
if (!nextId) {
|
|
|
|
|
const total = plan.ids.length;
|
|
|
|
|
state.batchPlan = null;
|
|
|
|
|
persistBatchPlan();
|
|
|
|
|
render();
|
|
|
|
|
setPageStatus(`${total} 台实例已全部加入 VPC。`, 'success');
|
|
|
|
|
toast(`${total} 台实例已全部加入 VPC。`, 'success');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const next = state.instances.find(item => instanceId(item) === nextId);
|
|
|
|
|
if (!next) {
|
|
|
|
|
failBatchPlan('failed', `批量加入已停止:实例 #${nextId} 已不可见。`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (vpcKey(next?.port) === plan.vpcKey
|
|
|
|
|
&& String(next?.port?.state || '') === 'attached') {
|
|
|
|
|
plan.completed.push(nextId);
|
|
|
|
|
persistBatchPlan();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (next.can_attach !== true) {
|
|
|
|
|
failBatchPlan('failed',
|
|
|
|
|
`实例“${next.name}”当前不可加入:${next.action_unavailable_reason || '实例状态已变化'}。`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
plan.current = nextId;
|
|
|
|
|
plan.currentState = 'submitting';
|
|
|
|
|
persistBatchPlan();
|
|
|
|
|
render();
|
|
|
|
|
const outcome = await mutatePort(next, vpc, 'attach', {
|
|
|
|
|
confirmed: true,
|
|
|
|
|
quiet: true,
|
|
|
|
|
});
|
|
|
|
|
if (state.batchPlan !== plan) return;
|
|
|
|
|
if (outcome.status === 'accepted') {
|
|
|
|
|
plan.currentState = 'accepted';
|
|
|
|
|
persistBatchPlan();
|
|
|
|
|
render();
|
|
|
|
|
const refreshed = state.instances.find(item => instanceId(item) === nextId);
|
|
|
|
|
if (vpcKey(refreshed?.port) === plan.vpcKey
|
|
|
|
|
&& String(refreshed?.port?.state || '') === 'attached') continue;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (outcome.status === 'uncertain') {
|
|
|
|
|
failBatchPlan('uncertain', outcome.message
|
|
|
|
|
|| `实例“${next.name}”的提交结果暂时无法确认,已停止后续队列。`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
failBatchPlan('failed', outcome.message
|
|
|
|
|
|| `实例“${next.name}”提交失败,后续实例尚未提交。`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
state.batchAdvancing = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function mutatePort(instance, vpc, requestedAction) {
|
|
|
|
|
async function mutatePort(instance, vpc, requestedAction, options = {}) {
|
|
|
|
|
const id = instanceId(instance);
|
|
|
|
|
if (!id || state.mutatingInstances.has(id)) return;
|
|
|
|
|
if (!id || state.mutatingInstances.has(id)) return { status: 'ignored' };
|
|
|
|
|
const action = portAction(instance);
|
|
|
|
|
const attach = requestedAction === 'attach';
|
|
|
|
|
if (attach !== ['attach', 'retry_attach'].includes(action)
|
|
|
|
|
|| (!attach && !['detach', 'retry_detach'].includes(action))) return;
|
|
|
|
|
|| (!attach && !['detach', 'retry_detach'].includes(action))) {
|
|
|
|
|
return { status: 'ignored' };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const confirmed = await requestUserConfirmation({
|
|
|
|
|
title: attach ? '确认加入 VPC 私网' : '确认移出 VPC 私网',
|
|
|
|
|
message: attach
|
|
|
|
|
? `实例“${instance.name}”将自动正常关机,加入“${vpc.name}”后冷启动。期间服务会短暂中断。`
|
|
|
|
|
: `实例“${instance.name}”将自动正常关机,移除私网网卡后冷启动,并释放私网 IP。`,
|
|
|
|
|
confirmText: attach ? '加入并重启' : '移出并重启',
|
|
|
|
|
tone: attach ? 'default' : 'danger',
|
|
|
|
|
});
|
|
|
|
|
if (!confirmed) return;
|
|
|
|
|
const confirmed = options.confirmed === true
|
|
|
|
|
|| await requestUserConfirmation({
|
|
|
|
|
title: attach ? '确认加入 VPC 私网' : '确认移出 VPC 私网',
|
|
|
|
|
message: attach
|
|
|
|
|
? `实例“${instance.name}”将自动正常关机,加入“${vpc.name}”后冷启动。期间服务会短暂中断。`
|
|
|
|
|
: `实例“${instance.name}”将自动正常关机,移除私网网卡后冷启动,并释放私网 IP。`,
|
|
|
|
|
confirmText: attach ? '加入并重启' : '移出并重启',
|
|
|
|
|
tone: attach ? 'default' : 'danger',
|
|
|
|
|
});
|
|
|
|
|
if (!confirmed) return { status: 'cancelled' };
|
|
|
|
|
|
|
|
|
|
const fingerprint = attach ? `attach:${vpc.vpc_key}`
|
|
|
|
|
: `detach:${String(instance?.port?.port_key || '')}`;
|
|
|
|
|
@ -444,16 +771,21 @@ async function mutatePort(instance, vpc, requestedAction) {
|
|
|
|
|
});
|
|
|
|
|
observeReceipt(instance, receipt, true);
|
|
|
|
|
setPageStatus('VPC 变更任务已接收,正在执行关机、网卡和路由阶段。');
|
|
|
|
|
toast('VPC 私网任务已接收。', 'success');
|
|
|
|
|
if (options.quiet !== true) toast('VPC 私网任务已接收。', 'success');
|
|
|
|
|
await load(true);
|
|
|
|
|
return { status: 'accepted' };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
await load(true);
|
|
|
|
|
if (transportUncertain(error)) {
|
|
|
|
|
setPageStatus(error?.message
|
|
|
|
|
|| '请求结果暂时无法确认,已保留同一请求标识,请勿重复提交。', 'warning');
|
|
|
|
|
const message = error?.message
|
|
|
|
|
|| '请求结果暂时无法确认,已保留同一请求标识,请勿重复提交。';
|
|
|
|
|
setPageStatus(message, 'warning');
|
|
|
|
|
return { status: 'uncertain', message };
|
|
|
|
|
} else {
|
|
|
|
|
clearPendingMutationKey(instance);
|
|
|
|
|
setPageStatus(error?.message || 'VPC 私网操作提交失败。', 'error');
|
|
|
|
|
const message = error?.message || 'VPC 私网操作提交失败。';
|
|
|
|
|
setPageStatus(message, 'error');
|
|
|
|
|
return { status: 'failed', message };
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
state.mutatingInstances.delete(id);
|
|
|
|
|
@ -531,6 +863,7 @@ export async function initialize() {
|
|
|
|
|
const bootstrap = readBootstrap('cloud-vpcs-bootstrap');
|
|
|
|
|
if (!await initializeSession('vpcs', bootstrap?.user)) return;
|
|
|
|
|
state.userKey = String(bootstrap?.user?.id || bootstrap?.user?.user_id || 'unknown');
|
|
|
|
|
restoreBatchPlan();
|
|
|
|
|
if (Array.isArray(bootstrap?.items) && Array.isArray(bootstrap?.instances)
|
|
|
|
|
&& Array.isArray(bootstrap?.regions)) {
|
|
|
|
|
apply(bootstrap);
|
|
|
|
|
|