diff --git a/static/js/admin.js b/static/js/admin.js index 3fd7dd7..bc8985e 100644 --- a/static/js/admin.js +++ b/static/js/admin.js @@ -4,7 +4,7 @@ import { api } from './api.js'; export async function renderVMAdmin() { const data = await api.listAllVmct(); const arr = Array.isArray(data.all) ? data.all : []; - const nodes = Array.isArray(data.nodes) ? data.nodes : []; + const availableNodes = Array.isArray(data.nodes) ? data.nodes : []; const tbody = document.querySelector('#vm-admin tbody'); if (!arr.length) { setRows(tbody, [rowHTML(['Brak VM/CT'])]); return; } @@ -18,8 +18,8 @@ export async function renderVMAdmin() { `; - const sel = ` + ${availableNodes.map(n => ``).join('')} `; const tools = `
@@ -34,8 +34,10 @@ export async function renderVMAdmin() { Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => { const sid = tr.getAttribute('data-sid'); const colSpan = tr.children.length; - const nodeCell = tr.children[3]; // Node - const badgeCell = tr.children[4]; // Status + const nodeCell = tr.children[3]; // kolumna Node + const badgeCell = tr.children[4]; // kolumna Status + const targetSel = tr.querySelector('.target-node'); + const watchBtn = tr.querySelector('.act-watch'); // subpanel (log) let sub = tr.nextElementSibling; @@ -49,7 +51,7 @@ export async function renderVMAdmin() { const toggleSub = (show) => sub.classList.toggle('d-none', !show); let wsObs = null; // observe websocket - let wsTask = null; // tail websocket (auto z observe) + let wsTask = null; // tail websocket const closeWS = () => { try { wsObs && wsObs.close(); } catch {} @@ -69,12 +71,26 @@ export async function renderVMAdmin() { if (!busy && spin) spin.remove(); }; - const getTarget = () => tr.querySelector('.target-node')?.value || ''; + const rebuildTargetSelect = (currentNode) => { + if (!targetSel) return; + const current = currentNode || (nodeCell?.textContent || '').trim(); + const html = availableNodes.map(n => + ``).join(''); + targetSel.innerHTML = html; + // jeśli selected jest disabled, ustaw zaznaczenie na pierwszy dozwolony + if (targetSel.options.length) { + const idx = Array.from(targetSel.options).findIndex(o => !o.disabled); + if (idx >= 0) targetSel.selectedIndex = idx; + } + }; + + const getTarget = () => targetSel?.value || ''; const openTaskWS = (upid, node) => { if (!upid) return; toggleSub(true); logPre.textContent = `UPID: ${upid} @ ${node}\n`; + try { wsTask && wsTask.close(); } catch {} wsTask = new WebSocket(api.wsTaskURL(upid, node)); wsTask.onmessage = (ev) => { try { @@ -84,13 +100,15 @@ export async function renderVMAdmin() { logPre.scrollTop = logPre.scrollHeight; } else if (msg.type === 'status' && msg.status) { const ok = String(msg.status.exitstatus||'').toUpperCase() === 'OK'; - const stopped = String(msg.status.status||'').toLowerCase() === 'stopped'; - if (badgeCell) badgeCell.innerHTML = ok ? badge('running','ok') : (stopped ? badge('stopped','dark') : badge('working','info')); + const s = String(msg.status.status||'').toLowerCase(); + if (badgeCell) badgeCell.innerHTML = ok ? badge('running','ok') : + (s === 'stopped' ? badge('stopped','dark') : badge('working','info')); } else if (msg.type === 'done') { const ok = !!msg.ok; if (badgeCell) badgeCell.innerHTML = ok ? badge('running','ok') : badge('error','err'); setRowBusy(false); - setTimeout(() => toggleSub(false), 1200); + // nie chowam subpanelu natychmiast, żeby dać doczytać log + setTimeout(() => toggleSub(false), 1500); try { document.getElementById('btnRefresh').click(); } catch {} } } catch {} @@ -98,10 +116,15 @@ export async function renderVMAdmin() { wsTask.onerror = () => {}; }; - const openObserveWS = () => { + const ensureWatchOn = () => { if (wsObs) return; const url = api.wsObserveURL(sid); wsObs = new WebSocket(url); + if (watchBtn) { + watchBtn.classList.remove('btn-outline-info'); + watchBtn.classList.add('btn-info'); + watchBtn.textContent = 'Watching 🔔'; + } wsObs.onmessage = (ev) => { try { const msg = JSON.parse(ev.data); @@ -131,18 +154,17 @@ export async function renderVMAdmin() { else if (msg.type === 'moved' && msg.new_node) { if (nodeCell) nodeCell.textContent = msg.new_node; + rebuildTargetSelect(msg.new_node); try { document.getElementById('btnRefresh').click(); } catch {} } - else if (msg.type === 'done' && msg.upid) { - if (typeof msg.ok === 'boolean') { - if (badgeCell) badgeCell.innerHTML = msg.ok ? badge('running','ok') : badge('error','err'); - } + else if (msg.type === 'done' && typeof msg.ok === 'boolean') { + if (badgeCell) badgeCell.innerHTML = msg.ok ? badge('running','ok') : badge('error','err'); } } catch {} }; - wsObs.onclose = () => { wsObs = null; }; + wsObs.onclose = () => { wsObs = null; if (watchBtn) { watchBtn.classList.remove('btn-info'); watchBtn.classList.add('btn-outline-info'); watchBtn.textContent='Watch 🔔'; } }; wsObs.onerror = () => {}; }; @@ -150,32 +172,38 @@ export async function renderVMAdmin() { setRowBusy(true); try { const target = withTarget ? getTarget() : undefined; + + ensureWatchOn(); + if (action !== 'unlock') toggleSub(true); + const resp = await api.vmAction(sid, action, target); if (!resp.ok) throw new Error(resp.error || 'unknown'); - // unlock – brak UPID + if (!resp.upid) { - showToast('Success', `${action} executed for ${sid}`, 'success'); - setRowBusy(false); toggleSub(false); - try { document.getElementById('btnRefresh').click(); } catch {} + + logPre.textContent = `Waiting for task… (${action})\n`; + showToast('Info', `${action} zainicjowane`, 'info'); + setRowBusy(false); return; } - // UPID → WS tail + openTaskWS(resp.upid, resp.source_node); + } catch (e) { showToast('Error', 'ERROR: ' + (e.message || e), 'danger'); setRowBusy(false); + toggleSub(false); } }; tr.querySelector('.act-unlock')?.addEventListener('click', () => doAction('unlock')); - tr.querySelector('.act-start')?.addEventListener('click', () => { toggleSub(true); doAction('start'); }); - tr.querySelector('.act-stop')?.addEventListener('click', () => { toggleSub(true); doAction('stop'); }); - tr.querySelector('.act-shutdown')?.addEventListener('click', () => { toggleSub(true); doAction('shutdown'); }); - tr.querySelector('.act-migrate')?.addEventListener('click', () => { toggleSub(true); doAction('migrate', true); }); + tr.querySelector('.act-start')?.addEventListener('click', () => doAction('start')); + tr.querySelector('.act-stop')?.addEventListener('click', () => doAction('stop')); + tr.querySelector('.act-shutdown')?.addEventListener('click',() => doAction('shutdown')); + tr.querySelector('.act-migrate')?.addEventListener('click', () => doAction('migrate', true)); tr.querySelector('.act-status')?.addEventListener('click', () => toggleSub(sub.classList.contains('d-none'))); - const watchBtn = tr.querySelector('.act-watch'); if (watchBtn) { watchBtn.addEventListener('click', () => { if (wsObs) { @@ -183,11 +211,11 @@ export async function renderVMAdmin() { watchBtn.classList.remove('btn-info'); watchBtn.classList.add('btn-outline-info'); watchBtn.textContent = 'Watch 🔔'; } else { - openObserveWS(); - watchBtn.classList.remove('btn-outline-info'); watchBtn.classList.add('btn-info'); - watchBtn.textContent = 'Watching 🔔'; + ensureWatchOn(); } }); } + + rebuildTargetSelect(nodeCell?.textContent?.trim()); }); }