|
|
|
|
@ -14,7 +14,7 @@ const {
|
|
|
|
|
|
|
|
|
|
const root = document.querySelector('[data-cloud-admin]');
|
|
|
|
|
const byId = id => document.getElementById(id);
|
|
|
|
|
const state = { csrf: window.ADMIN_CSRF_TOKEN || '', hosts: [], selectedHost: '', busy: false, epoch: 0, storageBusy: false, storageOpener: null, storageScroll: null, storageObserver: null, schedulingBusy: false, schedulingOpener: null, schedulingScroll: null, networkPoolBusy: false, networkPoolOpener: null, networkPoolScroll: null, networkPoolPayload: null, networkPoolEpoch: 0, networkPoolHostId: '', vpcTransportBusy: false, vpcTransportOpener: null, vpcTransportScroll: null, vpcTransportHostId: '', vpcTransportCandidates: [], taskObservers: new Set() };
|
|
|
|
|
const state = { csrf: window.ADMIN_CSRF_TOKEN || '', hosts: [], hostRegions: [], hostSummary: {}, expandedHostRegions: new Set(), hostRegionsInitialized: false, selectedHost: '', busy: false, epoch: 0, storageBusy: false, storageOpener: null, storageScroll: null, storageObserver: null, schedulingBusy: false, schedulingOpener: null, schedulingScroll: null, networkPoolBusy: false, networkPoolOpener: null, networkPoolScroll: null, networkPoolPayload: null, networkPoolEpoch: 0, networkPoolHostId: '', vpcTransportBusy: false, vpcTransportOpener: null, vpcTransportScroll: null, vpcTransportHostId: '', vpcTransportCandidates: [], taskObservers: new Set() };
|
|
|
|
|
// Permission state is rendered by the server on the page. API endpoints keep
|
|
|
|
|
// the authoritative authorization check; this only controls the UI affordances.
|
|
|
|
|
const canManage = root?.dataset.canManage === '1';
|
|
|
|
|
@ -75,7 +75,21 @@ function uploadApi(path, file, onProgress) {
|
|
|
|
|
}
|
|
|
|
|
function formList(value) { return String(value || '').split(/[\s,]+/).filter(Boolean); }
|
|
|
|
|
function profile(host) { return { ...host, ...(host.profile || host.config || {}) }; }
|
|
|
|
|
async function loadHosts() { state.hosts = collection(await api('/api/v1/admin/hosts'), 'hosts').map(profile); return state.hosts; }
|
|
|
|
|
async function loadHosts() {
|
|
|
|
|
const payload = await api('/api/v1/admin/hosts');
|
|
|
|
|
state.hosts = collection(payload, 'hosts').map(profile);
|
|
|
|
|
state.hostRegions = collection(payload, 'regions').map(region => ({ ...region, hosts: collection(region, 'hosts').map(profile) }));
|
|
|
|
|
state.hostSummary = payload?.summary && typeof payload.summary === 'object' ? payload.summary : {};
|
|
|
|
|
if (!state.hostRegionsInitialized) {
|
|
|
|
|
const first = state.hostRegions[0];
|
|
|
|
|
if (first) state.expandedHostRegions.add(hostRegionKey(first));
|
|
|
|
|
for (const region of state.hostRegions) {
|
|
|
|
|
if (Number(region?.summary?.attention_hosts || 0) > 0) state.expandedHostRegions.add(hostRegionKey(region));
|
|
|
|
|
}
|
|
|
|
|
state.hostRegionsInitialized = true;
|
|
|
|
|
}
|
|
|
|
|
return state.hosts;
|
|
|
|
|
}
|
|
|
|
|
async function loadUserIsoSettings() {
|
|
|
|
|
const settings = await api('/api/v1/admin/user-iso/settings');
|
|
|
|
|
byId('user-iso-enabled').checked = settings?.enabled === true;
|
|
|
|
|
@ -132,28 +146,134 @@ function fillHost(host) {
|
|
|
|
|
warning.textContent = network ? '' : `原网络接入“${h.default_network || '未配置'}”需要重新配置。请先核实该宿主机实际使用的网桥或 libvirt 网络,再选择类型并填写名称;不会自动改为 br0。已有实例或任务占用的地址池需要受控迁移。`;
|
|
|
|
|
byId('host-enabled').checked = h.enabled !== false; byId('host-form-error').textContent = '';
|
|
|
|
|
}
|
|
|
|
|
function hostRegionKey(region) { return region?.id === null || region?.id === undefined ? 'unclassified' : `region:${region.id}`; }
|
|
|
|
|
function agentLabel(value) { return ({ ready: 'Agent 已就绪', pending: 'Agent 待验证', failed: 'Agent 测试失败', disabled: 'Agent 已停用', unconfigured: 'Agent 未配置' })[value] || 'Agent 状态未知'; }
|
|
|
|
|
function hostStateBadge(label, tone) { return element('span', `cloud-admin-host-badge is-${tone}`, label); }
|
|
|
|
|
function renderHostSummary() {
|
|
|
|
|
const summary = byId('host-summary');
|
|
|
|
|
if (!summary) return;
|
|
|
|
|
const data = state.hostSummary || {};
|
|
|
|
|
const items = [
|
|
|
|
|
['map-marker-radius-outline', '地区', data.regions_total || 0],
|
|
|
|
|
['server-network', '计算节点', data.hosts_total || 0],
|
|
|
|
|
['check-decagram-outline', '可调度', data.placement_ready_hosts || 0],
|
|
|
|
|
['timer-sand', '正在排空', data.scheduler_draining || 0],
|
|
|
|
|
['alert-circle-outline', '需要处理', data.attention_hosts || 0],
|
|
|
|
|
];
|
|
|
|
|
summary.replaceChildren(...items.map(([icon, label, value]) => {
|
|
|
|
|
const card = element('article', 'cloud-admin-host-summary-card');
|
|
|
|
|
const copy = element('div'); copy.append(element('span', '', label), element('strong', '', value));
|
|
|
|
|
const badge = element('span', 'cloud-admin-host-summary-icon'); badge.innerHTML = `<i class="mdi mdi-${icon}" aria-hidden="true"></i>`;
|
|
|
|
|
card.append(badge, copy); return card;
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
function syncHostRegionFilter() {
|
|
|
|
|
const select = byId('host-region-filter');
|
|
|
|
|
if (!select) return;
|
|
|
|
|
const selected = select.value;
|
|
|
|
|
select.replaceChildren(new Option('全部地区', ''));
|
|
|
|
|
for (const region of state.hostRegions) {
|
|
|
|
|
const value = region.id === null || region.id === undefined ? 'unclassified' : String(region.id);
|
|
|
|
|
const count = Number(region?.summary?.hosts_total || collection(region, 'hosts').length);
|
|
|
|
|
select.append(new Option(`${region.country_flag || '🌐'} ${region.name || '未分类节点'}(${count})`, value));
|
|
|
|
|
}
|
|
|
|
|
select.value = Array.from(select.options).some(option => option.value === selected) ? selected : '';
|
|
|
|
|
}
|
|
|
|
|
function hostMatchesFilter(host, search, regionValue, stateValue) {
|
|
|
|
|
const placement = host?.placement || {};
|
|
|
|
|
const regionId = placement.region_id === null || placement.region_id === undefined ? 'unclassified' : String(placement.region_id);
|
|
|
|
|
if (regionValue && regionId !== regionValue) return false;
|
|
|
|
|
if (stateValue === 'ready' && host?.readiness?.placement_ready !== true) return false;
|
|
|
|
|
if (stateValue === 'attention' && host?.needs_attention !== true) return false;
|
|
|
|
|
if (['active', 'draining', 'disabled'].includes(stateValue) && host.scheduler_state !== stateValue) return false;
|
|
|
|
|
if (!search) return true;
|
|
|
|
|
return [host.name, host.id, host.region, host.country_code, placement.region_name, placement.line_name, placement.pool_name]
|
|
|
|
|
.some(value => String(value || '').toLocaleLowerCase().includes(search));
|
|
|
|
|
}
|
|
|
|
|
function renderHostNode(host) {
|
|
|
|
|
const row = element('article', 'cloud-admin-host-node');
|
|
|
|
|
const button = element('button', 'cloud-admin-host');
|
|
|
|
|
button.type = 'button';
|
|
|
|
|
button.dataset.hostId = String(host.id);
|
|
|
|
|
button.setAttribute('aria-label', `配置计算节点 ${host.name || host.id}`);
|
|
|
|
|
if (String(state.selectedHost) === String(host.id)) button.setAttribute('aria-current', 'true');
|
|
|
|
|
const heading = element('span', 'cloud-admin-host-heading');
|
|
|
|
|
heading.append(element('strong', '', host.name || `计算节点 ${host.id}`), element('small', '', `#${host.id} · ${host?.location?.physical_label || host.region || '物理位置未设置'}`));
|
|
|
|
|
const status = element('span', 'cloud-admin-host-statuses');
|
|
|
|
|
status.append(
|
|
|
|
|
hostStateBadge(agentLabel(host?.agent?.state), host?.agent?.ready ? 'success' : 'warning'),
|
|
|
|
|
hostStateBadge(host.enabled ? '服务启用' : '服务停用', host.enabled ? 'success' : 'muted'),
|
|
|
|
|
hostStateBadge(schedulerLabel(host.scheduler_state), host.scheduler_state === 'active' ? 'success' : host.scheduler_state === 'draining' ? 'warning' : 'muted')
|
|
|
|
|
);
|
|
|
|
|
const path = element('span', 'cloud-admin-host-path', [host?.placement?.line_name, host?.placement?.pool_name].filter(Boolean).join(' · ') || '尚未选择资源池');
|
|
|
|
|
const capacity = host?.capacity || {};
|
|
|
|
|
const metrics = element('span', 'cloud-admin-host-metrics');
|
|
|
|
|
metrics.append(
|
|
|
|
|
element('span', '', `实例 ${Number(capacity?.instances?.allocated || 0)}`),
|
|
|
|
|
element('span', '', `CPU ${Number(capacity?.cpu?.allocated || 0)}/${Number(capacity?.cpu?.configured || 0)} 核`),
|
|
|
|
|
element('span', '', `公网 IP ${Number(capacity?.ip?.available || 0)}/${Number(capacity?.ip?.total || 0)}`)
|
|
|
|
|
);
|
|
|
|
|
button.append(heading, status, path, metrics);
|
|
|
|
|
button.addEventListener('click', async () => {
|
|
|
|
|
if (state.busy) return;
|
|
|
|
|
try {
|
|
|
|
|
state.selectedHost = String(host.id);
|
|
|
|
|
fillHost(profile(await api(`/api/v1/admin/hosts/${encodeURIComponent(host.id)}`)));
|
|
|
|
|
renderHosts();
|
|
|
|
|
notice('已读取计算节点配置。Agent 地址和密钥请在“Agent API”页面维护。');
|
|
|
|
|
} catch (error) { notice(error.message, true); }
|
|
|
|
|
});
|
|
|
|
|
const actions = element('div', 'cloud-admin-host-node-actions');
|
|
|
|
|
const scheduling = element('button', 'btn btn-outline-primary btn-sm', '调度与容量');
|
|
|
|
|
scheduling.type = 'button'; scheduling.addEventListener('click', () => openHostScheduling(host.id, scheduling));
|
|
|
|
|
const networkPools = element('button', 'btn btn-outline-primary btn-sm', '公网地址池');
|
|
|
|
|
networkPools.type = 'button'; networkPools.addEventListener('click', () => openHostNetworkPools(host.id, networkPools));
|
|
|
|
|
const vpcTransport = element('button', 'btn btn-outline-primary btn-sm', 'VPC 私网传输');
|
|
|
|
|
vpcTransport.type = 'button'; vpcTransport.addEventListener('click', () => openHostVpcTransport(host.id, vpcTransport));
|
|
|
|
|
actions.append(scheduling, networkPools, vpcTransport);
|
|
|
|
|
if (canManage) {
|
|
|
|
|
const storage = element('button', 'btn btn-outline-primary btn-sm', '存储目录');
|
|
|
|
|
storage.type = 'button'; storage.addEventListener('click', () => openHostStorage(host.id, storage)); actions.append(storage);
|
|
|
|
|
}
|
|
|
|
|
row.append(button, actions);
|
|
|
|
|
return row;
|
|
|
|
|
}
|
|
|
|
|
function renderHosts() {
|
|
|
|
|
const list = byId('host-list'); list.replaceChildren(); if (!state.hosts.length) list.append(element('p', 'text-muted', '暂无宿主机。'));
|
|
|
|
|
for (const host of state.hosts) {
|
|
|
|
|
const row = element('div', 'd-grid gap-2');
|
|
|
|
|
const button = element('button', 'btn btn-light cloud-admin-host');
|
|
|
|
|
button.type = 'button';
|
|
|
|
|
button.append(element('strong', '', host.name || `宿主机 ${host.id}`), element('div', 'text-muted', `${host.region || '地区未设置'} · #${host.id} · ${host.enabled ? '连接启用' : '连接停用'} · ${schedulerLabel(host.scheduler_state)}`));
|
|
|
|
|
button.addEventListener('click', async () => { if (state.busy) return; try { fillHost(profile(await api(`/api/v1/admin/hosts/${encodeURIComponent(host.id)}`))); notice('已读取宿主机面板配置。Agent 地址和密钥请在“Agent API”页面维护。'); } catch (error) { notice(error.message, true); } });
|
|
|
|
|
const actions = element('div', 'd-grid gap-2');
|
|
|
|
|
const scheduling = element('button', 'btn btn-outline-primary btn-sm', '资源池与调度策略');
|
|
|
|
|
scheduling.type = 'button'; scheduling.addEventListener('click', () => openHostScheduling(host.id, scheduling));
|
|
|
|
|
const networkPools = element('button', 'btn btn-outline-primary btn-sm', '公网地址池');
|
|
|
|
|
networkPools.type = 'button'; networkPools.addEventListener('click', () => openHostNetworkPools(host.id, networkPools));
|
|
|
|
|
const vpcTransport = element('button', 'btn btn-outline-primary btn-sm', 'VPC 私网传输');
|
|
|
|
|
vpcTransport.type = 'button'; vpcTransport.addEventListener('click', () => openHostVpcTransport(host.id, vpcTransport));
|
|
|
|
|
actions.append(scheduling, networkPools, vpcTransport);
|
|
|
|
|
if (canManage) {
|
|
|
|
|
const storage = element('button', 'btn btn-outline-primary btn-sm', '存储目录');
|
|
|
|
|
storage.type = 'button'; storage.addEventListener('click', () => openHostStorage(host.id, storage)); actions.append(storage);
|
|
|
|
|
}
|
|
|
|
|
row.append(button, actions); list.append(row);
|
|
|
|
|
renderHostSummary(); syncHostRegionFilter();
|
|
|
|
|
const list = byId('host-list'); list.replaceChildren();
|
|
|
|
|
if (!state.hosts.length) { list.append(element('p', 'cloud-admin-host-empty', '暂无计算节点。')); return; }
|
|
|
|
|
const search = String(byId('host-search')?.value || '').trim().toLocaleLowerCase();
|
|
|
|
|
const regionValue = String(byId('host-region-filter')?.value || '');
|
|
|
|
|
const stateValue = String(byId('host-state-filter')?.value || '');
|
|
|
|
|
let visible = 0;
|
|
|
|
|
for (const region of state.hostRegions) {
|
|
|
|
|
const hosts = collection(region, 'hosts').filter(host => hostMatchesFilter(host, search, regionValue, stateValue));
|
|
|
|
|
if (!hosts.length) continue;
|
|
|
|
|
visible += hosts.length;
|
|
|
|
|
const key = hostRegionKey(region);
|
|
|
|
|
const group = element('details', 'cloud-admin-host-region');
|
|
|
|
|
group.open = search !== '' || regionValue !== '' || stateValue !== '' || state.expandedHostRegions.has(key);
|
|
|
|
|
group.addEventListener('toggle', () => group.open ? state.expandedHostRegions.add(key) : state.expandedHostRegions.delete(key));
|
|
|
|
|
const header = element('summary', 'cloud-admin-host-region-head');
|
|
|
|
|
const identity = element('span', 'cloud-admin-host-region-identity');
|
|
|
|
|
identity.append(element('span', 'cloud-admin-host-region-flag', region.country_flag || '🌐'), element('span', '', region.name || '未分类节点'));
|
|
|
|
|
const meta = element('span', 'cloud-admin-host-region-meta');
|
|
|
|
|
const attention = Number(region?.summary?.attention_hosts || 0);
|
|
|
|
|
meta.append(element('span', '', `${hosts.length} 台节点`), element('span', '', `${Number(region?.summary?.agent_ready_hosts || 0)} 台 Agent 就绪`));
|
|
|
|
|
if (attention > 0) meta.append(hostStateBadge(`${attention} 项需处理`, 'warning'));
|
|
|
|
|
header.append(identity, meta);
|
|
|
|
|
const capacity = region?.capacity || {};
|
|
|
|
|
const overview = element('div', 'cloud-admin-host-region-capacity');
|
|
|
|
|
overview.append(
|
|
|
|
|
element('span', '', `CPU ${Number(capacity?.cpu?.allocated || 0)}/${Number(capacity?.cpu?.configured || 0)} 核`),
|
|
|
|
|
element('span', '', `内存 ${Number(capacity?.memory?.allocated || 0)}/${Number(capacity?.memory?.configured || 0)} MiB`),
|
|
|
|
|
element('span', '', `公网 IP ${Number(capacity?.ip?.available || 0)}/${Number(capacity?.ip?.total || 0)}`)
|
|
|
|
|
);
|
|
|
|
|
const nodes = element('div', 'cloud-admin-host-region-nodes');
|
|
|
|
|
nodes.append(...hosts.map(renderHostNode));
|
|
|
|
|
group.append(header, overview, nodes); list.append(group);
|
|
|
|
|
}
|
|
|
|
|
if (visible === 0) list.append(element('p', 'cloud-admin-host-empty', '没有符合当前筛选条件的计算节点。'));
|
|
|
|
|
}
|
|
|
|
|
const storageFields = {
|
|
|
|
|
managed_directory: 'host-storage-managed', template_directory: 'host-storage-template',
|
|
|
|
|
@ -662,8 +782,9 @@ async function saveHost(event) {
|
|
|
|
|
state.busy = true; byId('host-save').disabled = true; notice('正在保存宿主机面板配置…');
|
|
|
|
|
try {
|
|
|
|
|
const result = await api(`/api/v1/admin/hosts${id ? '/' + encodeURIComponent(id) : ''}`, { method: id ? 'PUT' : 'POST', body });
|
|
|
|
|
state.selectedHost = result?.id ? String(result.id) : '';
|
|
|
|
|
await loadHosts(); renderHosts(); fillHost(result?.id ? profile(result) : null);
|
|
|
|
|
notice('宿主机配置已保存。网络接入变更后请前往 Agent API 重新测试,再启用调度。');
|
|
|
|
|
notice('计算节点配置已保存。网络接入变更后请前往 Agent API 重新测试,再启用调度。');
|
|
|
|
|
}
|
|
|
|
|
catch (error) { byId('host-form-error').textContent = error.message; notice(error.message, true); }
|
|
|
|
|
finally { state.busy = false; byId('host-save').disabled = !canManage; }
|
|
|
|
|
@ -1274,7 +1395,8 @@ async function initialize() {
|
|
|
|
|
try { const session = await api('/api/v1/session'); state.csrf = session.csrf_token || state.csrf; if (!session.admin) throw new Error('需要管理员权限。'); if (!state.csrf) throw new Error('会话令牌缺失,请重新登录。');
|
|
|
|
|
if (root.dataset.cloudAdmin === 'hosts') {
|
|
|
|
|
await Promise.all([loadHosts(), loadUserIsoSettings()]); renderHosts(); fillHost(null);
|
|
|
|
|
byId('host-new').addEventListener('click', () => fillHost(null)); byId('host-form').addEventListener('submit', saveHost); byId('user-iso-settings-form').addEventListener('submit', saveUserIsoSettings);
|
|
|
|
|
byId('host-new').addEventListener('click', () => { state.selectedHost = ''; fillHost(null); renderHosts(); }); byId('host-form').addEventListener('submit', saveHost); byId('user-iso-settings-form').addEventListener('submit', saveUserIsoSettings);
|
|
|
|
|
byId('host-search').addEventListener('input', renderHosts); byId('host-region-filter').addEventListener('change', renderHosts); byId('host-state-filter').addEventListener('change', renderHosts);
|
|
|
|
|
byId('host-network-type').addEventListener('change', updateHostNetworkControls);
|
|
|
|
|
byId('host-storage-form').addEventListener('submit', saveHostStorage); byId('host-storage-close').addEventListener('click', closeHostStorage);
|
|
|
|
|
const storageDrawer = byId('host-storage-drawer'); storageDrawer.addEventListener('cancel', event => { event.preventDefault(); closeHostStorage(); }); storageDrawer.addEventListener('click', event => { if (event.target === storageDrawer) closeHostStorage(); });
|
|
|
|
|
|