|
|
|
|
@ -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: [], 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() };
|
|
|
|
|
const state = { csrf: window.ADMIN_CSRF_TOKEN || '', hosts: [], hostRegions: [], hostSummary: {}, expandedHostRegions: new Set(), hostRegionsInitialized: false, selectedHost: '', busy: false, epoch: 0, configBusy: false, configOpener: null, configScroll: null, 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';
|
|
|
|
|
@ -136,7 +136,7 @@ function hostNetworkLabel(value) {
|
|
|
|
|
return network ? `${network.type === 'bridge' ? 'Linux 网桥' : 'libvirt 网络'} · ${network.name}` : `待重新配置(${value || '未配置'})`;
|
|
|
|
|
}
|
|
|
|
|
function fillHost(host) {
|
|
|
|
|
byId('host-form').reset(); const h = host || {}; byId('host-id').value = h.id || ''; byId('host-form-title').textContent = host ? `编辑宿主机 · ${h.name}` : '添加宿主机';
|
|
|
|
|
byId('host-form').reset(); const h = host || {}; byId('host-id').value = h.id || ''; byId('host-form-title').textContent = host ? `编辑节点 · ${h.name}` : '添加计算节点'; byId('host-save').lastChild.textContent = host ? '保存节点' : '创建节点';
|
|
|
|
|
const network = parseHostNetwork(host ? h.default_network : 'bridge:br0');
|
|
|
|
|
const fields = { 'host-name': h.name || '', 'host-country-code': h.country_code || '', 'host-region': h.region || '', 'host-network-type': network?.type || '', 'host-network-name': network?.name || '' };
|
|
|
|
|
for (const [id, value] of Object.entries(fields)) byId(id).value = value;
|
|
|
|
|
@ -214,15 +214,7 @@ function renderHostNode(host) {
|
|
|
|
|
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); }
|
|
|
|
|
});
|
|
|
|
|
button.addEventListener('click', () => openHostConfig(host.id, button));
|
|
|
|
|
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));
|
|
|
|
|
@ -312,6 +304,42 @@ function closeHostDrawer(id, drawer) {
|
|
|
|
|
if (closed) finishHostDrawerClose(drawer);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
function closeHostConfig() { closeHostDrawer('host-config-drawer', 'config'); }
|
|
|
|
|
function showHostConfig(opener) {
|
|
|
|
|
state.configOpener = opener || null;
|
|
|
|
|
const dialog = byId('host-config-drawer');
|
|
|
|
|
if (!dialog.open) {
|
|
|
|
|
lockHostDrawerScroll('config');
|
|
|
|
|
window.VenixDrawerMotion.openDrawerWithMotion(dialog);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
function openNewHostConfig(opener) {
|
|
|
|
|
if (state.configBusy) return;
|
|
|
|
|
state.selectedHost = '';
|
|
|
|
|
fillHost(null);
|
|
|
|
|
renderHosts();
|
|
|
|
|
showHostConfig(opener);
|
|
|
|
|
}
|
|
|
|
|
async function openHostConfig(hostId, opener) {
|
|
|
|
|
if (state.configBusy) return;
|
|
|
|
|
state.configBusy = true;
|
|
|
|
|
showHostConfig(opener);
|
|
|
|
|
byId('host-form-title').textContent = '正在读取计算节点…';
|
|
|
|
|
byId('host-save').disabled = true;
|
|
|
|
|
try {
|
|
|
|
|
const host = profile(await api(`/api/v1/admin/hosts/${encodeURIComponent(hostId)}`));
|
|
|
|
|
state.selectedHost = String(host.id);
|
|
|
|
|
fillHost(host);
|
|
|
|
|
renderHosts();
|
|
|
|
|
notice('已读取计算节点配置。Agent 地址和密钥请在“Agent API”页面维护。');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
notice(error.message, true);
|
|
|
|
|
closeHostConfig();
|
|
|
|
|
} finally {
|
|
|
|
|
state.configBusy = false;
|
|
|
|
|
byId('host-save').disabled = !canManage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
function renderHostStorage(payload) {
|
|
|
|
|
if (!payload?.host_id) throw new Error('宿主机存储响应无效。');
|
|
|
|
|
byId('host-storage-title').textContent = `${payload.host_name || `宿主机 #${payload.host_id}`} · 存储目录`;
|
|
|
|
|
@ -779,11 +807,12 @@ async function saveHost(event) {
|
|
|
|
|
try { network = hostNetworkValue(); }
|
|
|
|
|
catch (error) { byId('host-form-error').textContent = error.message; notice(error.message, true); return; }
|
|
|
|
|
const body = { name: byId('host-name').value.trim(), enabled: byId('host-enabled').checked, country_code: byId('host-country-code').value, region: byId('host-region').value.trim(), default_network: network };
|
|
|
|
|
state.busy = true; byId('host-save').disabled = true; notice('正在保存宿主机面板配置…');
|
|
|
|
|
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);
|
|
|
|
|
closeHostConfig();
|
|
|
|
|
notice('计算节点配置已保存。网络接入变更后请前往 Agent API 重新测试,再启用调度。');
|
|
|
|
|
}
|
|
|
|
|
catch (error) { byId('host-form-error').textContent = error.message; notice(error.message, true); }
|
|
|
|
|
@ -1395,7 +1424,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', () => { state.selectedHost = ''; fillHost(null); renderHosts(); }); byId('host-form').addEventListener('submit', saveHost); byId('user-iso-settings-form').addEventListener('submit', saveUserIsoSettings);
|
|
|
|
|
byId('host-new').addEventListener('click', event => openNewHostConfig(event.currentTarget)); byId('host-form').addEventListener('submit', saveHost); byId('user-iso-settings-form').addEventListener('submit', saveUserIsoSettings);
|
|
|
|
|
byId('host-config-close').addEventListener('click', closeHostConfig); const configDrawer = byId('host-config-drawer'); configDrawer.addEventListener('cancel', event => { event.preventDefault(); closeHostConfig(); }); configDrawer.addEventListener('click', event => { if (event.target === configDrawer) closeHostConfig(); });
|
|
|
|
|
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);
|
|
|
|
|
@ -1406,7 +1436,7 @@ async function initialize() {
|
|
|
|
|
const networkDrawer = byId('host-network-pools-drawer'); networkDrawer.addEventListener('cancel', event => { event.preventDefault(); closeHostNetworkPools(); }); networkDrawer.addEventListener('click', event => { if (event.target === networkDrawer) closeHostNetworkPools(); });
|
|
|
|
|
byId('host-vpc-transport-close').addEventListener('click', closeHostVpcTransport); byId('host-vpc-transport-form').addEventListener('submit', saveHostVpcTransport); byId('host-vpc-transport-discover').addEventListener('click', discoverHostVpcTransport); byId('host-vpc-transport-test').addEventListener('click', testHostVpcTransport); byId('host-vpc-transport-candidate').addEventListener('change', selectHostVpcTransportCandidate);
|
|
|
|
|
const vpcTransportDrawer = byId('host-vpc-transport-drawer'); vpcTransportDrawer.addEventListener('cancel', event => { event.preventDefault(); closeHostVpcTransport(); }); vpcTransportDrawer.addEventListener('click', event => { if (event.target === vpcTransportDrawer) closeHostVpcTransport(); });
|
|
|
|
|
if (!canManage) { byId('host-form').querySelectorAll('input,select,textarea,button').forEach(input => { input.disabled = true; }); byId('user-iso-settings-form').querySelectorAll('input,button').forEach(input => { input.disabled = true; }); }
|
|
|
|
|
if (!canManage) { byId('host-form').querySelectorAll('input,select,textarea,button').forEach(input => { input.disabled = true; }); byId('host-enabled').disabled = true; byId('user-iso-settings-form').querySelectorAll('input,button').forEach(input => { input.disabled = true; }); }
|
|
|
|
|
notice(canManage ? '请选择宿主机编辑,或添加新的宿主机。' : '当前管理员仅可查看配置。');
|
|
|
|
|
}
|
|
|
|
|
else if (root.dataset.cloudAdmin === 'templates') { byId('template-import')?.addEventListener('submit', importTemplate); byId('template-upload')?.addEventListener('submit', uploadCloudTemplate); byId('template-catalog-refresh')?.addEventListener('click', loadCatalog); byId('template-startup-form').addEventListener('submit', saveStartupScript); byId('template-startup-close').addEventListener('click', closeStartupScript); byId('template-startup-script').addEventListener('input', updateStartupScriptControls); byId('template-startup-dialog').addEventListener('cancel', event => { event.preventDefault(); closeStartupScript(); }); await loadCatalog(); }
|
|
|
|
|
|