vm management
This commit is contained in:
15
app.py
15
app.py
@@ -488,7 +488,10 @@ def api_vm_action():
|
|||||||
base = f"/nodes/{node}/{typ}/{vmid}/migrate"
|
base = f"/nodes/{node}/{typ}/{vmid}/migrate"
|
||||||
cmd = ["pvesh", "create", base, "-target", target, "-online", "0"]
|
cmd = ["pvesh", "create", base, "-target", target, "-online", "0"]
|
||||||
res = post_json(cmd)
|
res = post_json(cmd)
|
||||||
return jsonify(ok=True, result=res or {})
|
upid = None
|
||||||
|
if isinstance(res, dict):
|
||||||
|
upid = res.get("data") or res.get("upid")
|
||||||
|
return jsonify(ok=True, result=res or {}, upid=upid, source_node=node)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return jsonify(ok=False, error="unknown action"), 400
|
return jsonify(ok=False, error="unknown action"), 400
|
||||||
@@ -497,6 +500,16 @@ def api_vm_action():
|
|||||||
return jsonify(ok=False, error=str(e)), 500
|
return jsonify(ok=False, error=str(e)), 500
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/task-status")
|
||||||
|
def api_task_status():
|
||||||
|
upid = request.args.get("upid", "").strip()
|
||||||
|
node = request.args.get("node", "").strip()
|
||||||
|
if not upid or not node:
|
||||||
|
return jsonify(ok=False, error="upid and node required"), 400
|
||||||
|
st = get_json(["pvesh", "get", f"/nodes/{node}/tasks/{upid}/status"]) or {}
|
||||||
|
return jsonify(ok=True, status=st)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import argparse
|
import argparse
|
||||||
p = argparse.ArgumentParser()
|
p = argparse.ArgumentParser()
|
||||||
|
@@ -673,12 +673,13 @@ async function vmAction(sid, action, target) {
|
|||||||
if (!d.ok) throw new Error(d.error || 'action failed');
|
if (!d.ok) throw new Error(d.error || 'action failed');
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function renderVMAdmin() {
|
async function renderVMAdmin() {
|
||||||
const data = await fetchAllVmct();
|
const data = await fetchAllVmct();
|
||||||
const arr = ensureArr(data.all);
|
const arr = ensureArr(data.all);
|
||||||
const nodes = ensureArr(data.nodes);
|
const nodes = ensureArr(data.nodes);
|
||||||
const tbody = document.querySelector('#vm-admin tbody');
|
const tbody = document.querySelector('#vm-admin tbody');
|
||||||
if (!arr.length) { setRows(tbody, [rowHTML(['No VMs/CTs'])]); return; }
|
if (!arr.length) { setRows(tbody, [rowHTML(['Brak VM/CT'])]); return; }
|
||||||
|
|
||||||
const rows = arr.map(x => {
|
const rows = arr.map(x => {
|
||||||
const sid = safe(x.sid), type = safe(x.type), name = safe(x.name), node = safe(x.node);
|
const sid = safe(x.sid), type = safe(x.type), name = safe(x.name), node = safe(x.node);
|
||||||
@@ -689,12 +690,12 @@ async function renderVMAdmin() {
|
|||||||
<button class="btn btn-outline-success act-start">Start</button>
|
<button class="btn btn-outline-success act-start">Start</button>
|
||||||
<button class="btn btn-outline-warning act-shutdown">Shutdown</button>
|
<button class="btn btn-outline-warning act-shutdown">Shutdown</button>
|
||||||
<button class="btn btn-outline-danger act-stop">Stop</button>
|
<button class="btn btn-outline-danger act-stop">Stop</button>
|
||||||
<button class="btn btn-outline-primary act-migrate">Migrate (offline)</button>
|
|
||||||
</div>`;
|
</div>`;
|
||||||
const sel = `<select class="form-select form-select-sm target-node" style="min-width:140px">
|
const sel = `<select class="form-select form-select-sm target-node" style="min-width:140px">
|
||||||
${nodes.map(n => `<option value="${n}" ${n === x.node ? 'disabled' : ''}>${n}${n === x.node ? ' (src)' : ''}</option>`).join('')}
|
${nodes.map(n => `<option value="${n}" ${n === x.node ? 'disabled' : ''}>${n}${n === x.node ? ' (src)' : ''}</option>`).join('')}
|
||||||
</select>`;
|
</select>`;
|
||||||
return rowHTML([sid, type.toUpperCase(), name, node, st, actions, sel], `data-sid="${sid}"`);
|
const migrateBtn = `<button class="btn btn-outline-primary btn-sm act-migrate">Migrate (offline)</button>`;
|
||||||
|
return rowHTML([sid, type.toUpperCase(), name, node, st, actions, sel, migrateBtn], `data-sid="${sid}"`);
|
||||||
});
|
});
|
||||||
|
|
||||||
setRows(tbody, rows);
|
setRows(tbody, rows);
|
||||||
@@ -702,14 +703,51 @@ async function renderVMAdmin() {
|
|||||||
Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => {
|
Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => {
|
||||||
const sid = tr.getAttribute('data-sid');
|
const sid = tr.getAttribute('data-sid');
|
||||||
const getTarget = () => tr.querySelector('.target-node')?.value || '';
|
const getTarget = () => tr.querySelector('.target-node')?.value || '';
|
||||||
|
const colSpan = tr.children.length; // for status row width
|
||||||
const bind = (sel, action, needsTarget = false) => {
|
const bind = (sel, action, needsTarget = false) => {
|
||||||
const btn = tr.querySelector(sel);
|
const btn = tr.querySelector(sel);
|
||||||
if (!btn) return;
|
if (!btn) return;
|
||||||
btn.onclick = async () => {
|
btn.onclick = async () => {
|
||||||
btn.disabled = true;
|
btn.disabled = true;
|
||||||
try {
|
try {
|
||||||
await vmAction(sid, action, needsTarget ? getTarget() : undefined);
|
if (action === 'migrate') {
|
||||||
|
const target = getTarget();
|
||||||
|
const resp = await vmAction(sid, action, target);
|
||||||
|
// show expandable status row
|
||||||
|
const row = ensureMigRow(tr, colSpan);
|
||||||
|
setMigRowVisible(row, true);
|
||||||
|
const log = row.querySelector('.mig-log');
|
||||||
|
const srcNode = resp.source_node;
|
||||||
|
const upid = resp.upid;
|
||||||
|
log.textContent = `Starting offline migrate to ${target} (UPID: ${upid || '—'})...`;
|
||||||
|
// update badge immediately
|
||||||
|
const badgeCell = tr.children[4];
|
||||||
|
if (badgeCell) badgeCell.innerHTML = badge('migrating', 'info');
|
||||||
|
// start background polling; update log on each tick
|
||||||
|
await pollTask(upid, srcNode, (st) => {
|
||||||
|
const lines = [];
|
||||||
|
for (const k of ['type', 'status', 'pid', 'starttime', 'user', 'node', 'endtime', 'exitstatus']) {
|
||||||
|
if (st[k] !== undefined) lines.push(`${k}: ${st[k]}`);
|
||||||
|
}
|
||||||
|
log.textContent = lines.join('
|
||||||
|
') || '—';
|
||||||
|
}, async (finalSt) => {
|
||||||
|
const exit = (finalSt && finalSt.exitstatus) ? String(finalSt.exitstatus) : '';
|
||||||
|
const ok = exit.toUpperCase() === 'OK';
|
||||||
|
const badgeCell2 = tr.children[4];
|
||||||
|
if (badgeCell2) badgeCell2.innerHTML = ok ? badge('running', 'ok') : badge('migrate error', 'err');
|
||||||
|
log.textContent += (log.textContent ? '\n' : '') + (ok ? 'Migration finished successfully.' : ('Migration failed: ' + (exit || 'unknown error')));
|
||||||
await doRefresh();
|
await doRefresh();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await vmAction(sid, action, needsTarget ? getTarget() : undefined);
|
||||||
|
const exit = (finalSt && finalSt.exitstatus) ? String(finalSt.exitstatus) : '';
|
||||||
|
const ok = exit.toUpperCase() === 'OK';
|
||||||
|
const badgeCell2 = tr.children[4];
|
||||||
|
if (badgeCell2) badgeCell2.innerHTML = ok ? badge('running', 'ok') : badge('migrate error', 'err');
|
||||||
|
log.textContent += (log.textContent ? '\n' : '') + (ok ? 'Migration finished successfully.' : ('Migration failed: ' + (exit || 'unknown error')));
|
||||||
|
await doRefresh();
|
||||||
|
}
|
||||||
} catch (e) { alert('ERROR: ' + e.message); }
|
} catch (e) { alert('ERROR: ' + e.message); }
|
||||||
btn.disabled = false;
|
btn.disabled = false;
|
||||||
};
|
};
|
||||||
@@ -721,3 +759,44 @@ async function renderVMAdmin() {
|
|||||||
bind('.act-migrate', 'migrate', true);
|
bind('.act-migrate', 'migrate', true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
async function pollTask(upid, node, onUpdate, onDone) {
|
||||||
|
let lastSt = null;
|
||||||
|
if (!upid || !node) return;
|
||||||
|
const started = Date.now();
|
||||||
|
const maxMs = 5 * 60 * 1000;
|
||||||
|
const delay = (ms) => new Promise(r => setTimeout(r, ms));
|
||||||
|
while (Date.now() - started < maxMs) {
|
||||||
|
try {
|
||||||
|
const r = await fetch('/api/task-status?upid=' + encodeURIComponent(upid) + '&node=' + encodeURIComponent(node));
|
||||||
|
const d = await r.json();
|
||||||
|
if (d && d.ok) {
|
||||||
|
const st = d.status || {}; lastSt = st; if (onUpdate) { try { onUpdate(st); } catch { } }
|
||||||
|
const s = (st.status || '').toLowerCase();
|
||||||
|
if (s === 'stopped' || st.exitstatus) break;
|
||||||
|
}
|
||||||
|
} catch (e) { }
|
||||||
|
await delay(2000);
|
||||||
|
}
|
||||||
|
try { if (onDone) onDone(lastSt); } catch { }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function ensureMigRow(mainTr, colSpan) {
|
||||||
|
let nxt = mainTr.nextElementSibling;
|
||||||
|
if (!nxt || !nxt.classList.contains('mig-row')) {
|
||||||
|
nxt = document.createElement('tr');
|
||||||
|
nxt.className = 'mig-row d-none';
|
||||||
|
const td = document.createElement('td');
|
||||||
|
td.colSpan = colSpan;
|
||||||
|
td.innerHTML = '<div class="p-3 border rounded bg-body-tertiary"><div class="fw-semibold mb-1">Migration status</div><pre class="small mb-0 mig-log">—</pre></div>';
|
||||||
|
nxt.appendChild(td);
|
||||||
|
mainTr.parentNode.insertBefore(nxt, mainTr.nextSibling);
|
||||||
|
}
|
||||||
|
return nxt;
|
||||||
|
}
|
||||||
|
function setMigRowVisible(row, vis) {
|
||||||
|
row.classList.toggle('d-none', !vis);
|
||||||
|
}
|
@@ -243,12 +243,13 @@
|
|||||||
<th>Node</th>
|
<th>Node</th>
|
||||||
<th>Status</th>
|
<th>Status</th>
|
||||||
<th>Actions</th>
|
<th>Actions</th>
|
||||||
<th>Target (migrate)</th>
|
<th>Target</th>
|
||||||
|
<th>Migrate</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="7" class="text-muted">Loading…</td>
|
<td colspan="8" class="text-muted">Loading…</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
@@ -275,7 +276,7 @@
|
|||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="7" class="text-muted">Loading…</td>
|
<td colspan="8" class="text-muted">Loading…</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
Reference in New Issue
Block a user