Initial commit of AthonetTools
This commit is contained in:
60
static/js/wizard/step0_target.js
Normal file
60
static/js/wizard/step0_target.js
Normal file
@@ -0,0 +1,60 @@
|
||||
// static/js/graph/wizard/step0_target.js
|
||||
import { setTarget, bootstrapAccess, getTarget } from './api.js';
|
||||
|
||||
export function mountTargetControls() {
|
||||
const row = document.querySelector('#target-host-row');
|
||||
if (!row) return;
|
||||
|
||||
// Use explicit IDs so we always hit the right elements
|
||||
const ipInput = document.getElementById('target-ip-input');
|
||||
const btn = document.getElementById('btn-enable-access');
|
||||
const badge = document.getElementById('access-badge');
|
||||
|
||||
if (!ipInput || !btn || !badge) return;
|
||||
|
||||
// Prefill: localStorage first, then try backend GET /api/host/target
|
||||
const saved = localStorage.getItem('targetHostIp');
|
||||
if (saved) ipInput.value = saved;
|
||||
getTarget()
|
||||
.then(({ ip }) => { if (ip && !saved) ipInput.value = ip; })
|
||||
.catch(() => { /* ignore if endpoint missing */ });
|
||||
|
||||
btn.addEventListener('click', async () => {
|
||||
const ip = (ipInput.value || '').trim();
|
||||
if (!ip) {
|
||||
badge.className = 'badge bg-warning';
|
||||
badge.textContent = 'Enter IP first';
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
btn.disabled = true;
|
||||
const old = btn.textContent;
|
||||
btn.textContent = 'Enabling…';
|
||||
badge.className = 'badge bg-secondary';
|
||||
badge.textContent = 'Working';
|
||||
|
||||
await setTarget(ip); // POST /api/host/target
|
||||
await bootstrapAccess(); // POST /api/host/bootstrap_access (SSH+webconsole)
|
||||
// Persist for Stage 2 (and page reloads)
|
||||
localStorage.setItem('targetHostIp', ip);
|
||||
|
||||
badge.className = 'badge bg-success';
|
||||
badge.textContent = 'SSH & Webconsole Ready';
|
||||
btn.textContent = old;
|
||||
btn.disabled = false;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
badge.className = 'badge bg-danger';
|
||||
badge.textContent = 'Failed';
|
||||
btn.disabled = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Helper for other steps to read the latest target IP
|
||||
export function getTargetHostData() {
|
||||
const inputIp = (document.getElementById('target-ip-input')?.value || '').trim();
|
||||
const stored = localStorage.getItem('targetHostIp') || '';
|
||||
return { ip: inputIp || stored || '' };
|
||||
}
|
||||
Reference in New Issue
Block a user