Compare commits
42 Commits
ea4c5f7c95
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| edf6f674b3 | |||
| a8d367b4be | |||
| 5ca0b648ca | |||
| 518ed42f60 | |||
| a9147b0198 | |||
| 2e3484b1d9 | |||
| cb83d11261 | |||
| 047fd0653e | |||
| 027ed52768 | |||
| e2935c58c8 | |||
| 1bbe743dba | |||
| d88b79e9f0 | |||
| 8a9de6d72a | |||
| ddd528a682 | |||
| 03cf2aa9c6 | |||
| d84674b0c6 | |||
| 7999f46ca2 | |||
| 307c5cf9e8 | |||
| 34af8e5a8f | |||
| 76d2bffb4f | |||
| 64de0e432c | |||
| a5b409a348 | |||
| 8f35724bde | |||
| cec82a3347 | |||
| 883e59789b | |||
| 817fdaef13 | |||
| 9295354e72 | |||
| 372cda6a58 | |||
| 3301e942ef | |||
| c4ebb76deb | |||
| bb765453ab | |||
| 88474d1048 | |||
| 954d85ca81 | |||
| 117dfc5f17 | |||
| c39c7a8aef | |||
| a934db1a14 | |||
| 07cef73fae | |||
| e3d089a71f | |||
| 120b61a423 | |||
| cd16b7ea28 | |||
| afbdefa549 | |||
| f1e192c5d4 |
@@ -153,6 +153,8 @@ main { flex: 1; }
|
|||||||
}
|
}
|
||||||
|
|
||||||
.stat-cell:last-child { border-right: none; }
|
.stat-cell:last-child { border-right: none; }
|
||||||
|
.stat-clickable { cursor: pointer; user-select: none; }
|
||||||
|
.stat-clickable:hover { background: var(--bg2); }
|
||||||
|
|
||||||
.stat-label {
|
.stat-label {
|
||||||
font-size: 10px;
|
font-size: 10px;
|
||||||
|
|||||||
@@ -53,4 +53,6 @@ if (VERSION) {
|
|||||||
document.getElementById('nav-version').textContent = label;
|
document.getElementById('nav-version').textContent = label;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fetch('/api/jobs').then(r => r.json()).then(_updateJobsNavDot).catch(() => {});
|
||||||
|
|
||||||
handleRoute();
|
handleRoute();
|
||||||
|
|||||||
79
js/ui.js
79
js/ui.js
@@ -71,10 +71,10 @@ async function renderDashboard() {
|
|||||||
all.forEach(i => { states[i.state] = (states[i.state] || 0) + 1; });
|
all.forEach(i => { states[i.state] = (states[i.state] || 0) + 1; });
|
||||||
|
|
||||||
document.getElementById('stats-bar').innerHTML = `
|
document.getElementById('stats-bar').innerHTML = `
|
||||||
<div class="stat-cell"><div class="stat-label">total</div><div class="stat-value accent">${all.length}</div></div>
|
<div class="stat-cell stat-clickable" onclick="setStateFilter('')"><div class="stat-label">total</div><div class="stat-value accent">${all.length}</div></div>
|
||||||
<div class="stat-cell"><div class="stat-label">deployed</div><div class="stat-value">${states['deployed'] || 0}</div></div>
|
<div class="stat-cell stat-clickable" onclick="setStateFilter('deployed')"><div class="stat-label">deployed</div><div class="stat-value">${states['deployed'] || 0}</div></div>
|
||||||
<div class="stat-cell"><div class="stat-label">testing</div><div class="stat-value amber">${states['testing'] || 0}</div></div>
|
<div class="stat-cell stat-clickable" onclick="setStateFilter('testing')"><div class="stat-label">testing</div><div class="stat-value amber">${states['testing'] || 0}</div></div>
|
||||||
<div class="stat-cell"><div class="stat-label">degraded</div><div class="stat-value red">${states['degraded'] || 0}</div></div>
|
<div class="stat-cell stat-clickable" onclick="setStateFilter('degraded')"><div class="stat-label">degraded</div><div class="stat-value red">${states['degraded'] || 0}</div></div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
await populateStackFilter();
|
await populateStackFilter();
|
||||||
@@ -95,6 +95,11 @@ async function populateStackFilter() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setStateFilter(state) {
|
||||||
|
document.getElementById('filter-state').value = state;
|
||||||
|
filterInstances();
|
||||||
|
}
|
||||||
|
|
||||||
async function filterInstances() {
|
async function filterInstances() {
|
||||||
const search = document.getElementById('search-input').value;
|
const search = document.getElementById('search-input').value;
|
||||||
const state = document.getElementById('filter-state').value;
|
const state = document.getElementById('filter-state').value;
|
||||||
@@ -289,6 +294,10 @@ async function saveInstance() {
|
|||||||
hardware_acceleration: +document.getElementById('f-hardware-accel').checked,
|
hardware_acceleration: +document.getElementById('f-hardware-accel').checked,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Snapshot job state before creation — jobs fire immediately after the 201
|
||||||
|
// so the baseline must be captured before the POST, not after.
|
||||||
|
const jobBaseline = !editingVmid ? await _snapshotJobBaseline() : null;
|
||||||
|
|
||||||
const result = editingVmid
|
const result = editingVmid
|
||||||
? await updateInstance(editingVmid, data)
|
? await updateInstance(editingVmid, data)
|
||||||
: await createInstance(data);
|
: await createInstance(data);
|
||||||
@@ -298,6 +307,8 @@ async function saveInstance() {
|
|||||||
showToast(`${name} ${editingVmid ? 'updated' : 'created'}`, 'success');
|
showToast(`${name} ${editingVmid ? 'updated' : 'created'}`, 'success');
|
||||||
closeModal();
|
closeModal();
|
||||||
|
|
||||||
|
if (jobBaseline) await _waitForOnCreateJobs(jobBaseline);
|
||||||
|
|
||||||
if (currentVmid && document.getElementById('page-detail').classList.contains('active')) {
|
if (currentVmid && document.getElementById('page-detail').classList.contains('active')) {
|
||||||
await renderDetailPage(vmid);
|
await renderDetailPage(vmid);
|
||||||
} else {
|
} else {
|
||||||
@@ -305,6 +316,30 @@ async function saveInstance() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function _snapshotJobBaseline() {
|
||||||
|
const jobs = await fetch('/api/jobs').then(r => r.json());
|
||||||
|
return new Map(jobs.map(j => [j.id, j.last_run_id ?? null]));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function _waitForOnCreateJobs(baseline) {
|
||||||
|
const jobs = await fetch('/api/jobs').then(r => r.json());
|
||||||
|
const relevant = jobs.filter(j => (j.config ?? {}).run_on_create);
|
||||||
|
if (!relevant.length) return;
|
||||||
|
|
||||||
|
const deadline = Date.now() + 30_000;
|
||||||
|
while (Date.now() < deadline) {
|
||||||
|
await new Promise(r => setTimeout(r, 500));
|
||||||
|
const current = await fetch('/api/jobs').then(r => r.json());
|
||||||
|
const allDone = relevant.every(j => {
|
||||||
|
const cur = current.find(c => c.id === j.id);
|
||||||
|
if (!cur) return true;
|
||||||
|
if (cur.last_run_id === baseline.get(j.id)) return false; // new run not started yet
|
||||||
|
return cur.last_status !== 'running'; // new run complete
|
||||||
|
});
|
||||||
|
if (allDone) return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ── Confirm Dialog ────────────────────────────────────────────────────────────
|
// ── Confirm Dialog ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
function confirmDeleteDialog(inst) {
|
function confirmDeleteDialog(inst) {
|
||||||
@@ -382,15 +417,17 @@ async function importDB() {
|
|||||||
document.getElementById('confirm-ok').onclick = async () => {
|
document.getElementById('confirm-ok').onclick = async () => {
|
||||||
closeConfirm();
|
closeConfirm();
|
||||||
try {
|
try {
|
||||||
const { instances, history = [] } = JSON.parse(await file.text());
|
const { instances, history = [], jobs, job_runs } = JSON.parse(await file.text());
|
||||||
const res = await fetch('/api/import', {
|
const res = await fetch('/api/import', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ instances, history }),
|
body: JSON.stringify({ instances, history, jobs, job_runs }),
|
||||||
});
|
});
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
if (!res.ok) { showToast(data.error ?? 'Import failed', 'error'); return; }
|
if (!res.ok) { showToast(data.error ?? 'Import failed', 'error'); return; }
|
||||||
showToast(`Imported ${data.imported} instance${data.imported !== 1 ? 's' : ''}`, 'success');
|
const parts = [`${data.imported} instance${data.imported !== 1 ? 's' : ''}`];
|
||||||
|
if (data.imported_jobs != null) parts.push(`${data.imported_jobs} job${data.imported_jobs !== 1 ? 's' : ''}`);
|
||||||
|
showToast(`Imported ${parts.join(', ')}`, 'success');
|
||||||
closeSettingsModal();
|
closeSettingsModal();
|
||||||
renderDashboard();
|
renderDashboard();
|
||||||
} catch {
|
} catch {
|
||||||
@@ -461,6 +498,13 @@ async function loadJobDetail(jobId) {
|
|||||||
<label class="form-label" for="job-schedule">Poll interval (minutes)</label>
|
<label class="form-label" for="job-schedule">Poll interval (minutes)</label>
|
||||||
<input class="form-input" id="job-schedule" type="number" min="1" value="${job.schedule}" style="max-width:100px">
|
<input class="form-input" id="job-schedule" type="number" min="1" value="${job.schedule}" style="max-width:100px">
|
||||||
</div>
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="form-label" style="display:flex;align-items:center;gap:8px;cursor:pointer">
|
||||||
|
<input type="checkbox" id="job-run-on-create" ${cfg.run_on_create ? 'checked' : ''}
|
||||||
|
style="accent-color:var(--accent);width:13px;height:13px">
|
||||||
|
Run on instance creation
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
${_renderJobConfigFields(job.key, cfg)}
|
${_renderJobConfigFields(job.key, cfg)}
|
||||||
<div class="job-actions">
|
<div class="job-actions">
|
||||||
<button class="btn btn-secondary" onclick="saveJobDetail(${job.id})">Save</button>
|
<button class="btn btn-secondary" onclick="saveJobDetail(${job.id})">Save</button>
|
||||||
@@ -483,17 +527,20 @@ function _renderJobConfigFields(key, cfg) {
|
|||||||
<input class="form-input" id="job-cfg-api-key" type="password"
|
<input class="form-input" id="job-cfg-api-key" type="password"
|
||||||
placeholder="tskey-api-…" value="${esc(cfg.api_key ?? '')}">
|
placeholder="tskey-api-…" value="${esc(cfg.api_key ?? '')}">
|
||||||
</div>`;
|
</div>`;
|
||||||
if (key === 'patchmon_sync') return `
|
if (key === 'patchmon_sync' || key === 'semaphore_sync') {
|
||||||
|
const label = key === 'semaphore_sync' ? 'API Token (Bearer)' : 'API Token (Basic)';
|
||||||
|
return `
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label class="form-label" for="job-cfg-api-url">API URL</label>
|
<label class="form-label" for="job-cfg-api-url">API URL</label>
|
||||||
<input class="form-input" id="job-cfg-api-url" type="text"
|
<input class="form-input" id="job-cfg-api-url" type="text"
|
||||||
placeholder="http://patchmon:3000/api/v1/api/hosts" value="${esc(cfg.api_url ?? '')}">
|
value="${esc(cfg.api_url ?? '')}">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label class="form-label" for="job-cfg-api-token">API Token (Basic)</label>
|
<label class="form-label" for="job-cfg-api-token">${label}</label>
|
||||||
<input class="form-input" id="job-cfg-api-token" type="password"
|
<input class="form-input" id="job-cfg-api-token" type="password"
|
||||||
placeholder="Basic token…" value="${esc(cfg.api_token ?? '')}">
|
value="${esc(cfg.api_token ?? '')}">
|
||||||
</div>`;
|
</div>`;
|
||||||
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -516,10 +563,12 @@ async function saveJobDetail(jobId) {
|
|||||||
const apiKey = document.getElementById('job-cfg-api-key');
|
const apiKey = document.getElementById('job-cfg-api-key');
|
||||||
const apiUrl = document.getElementById('job-cfg-api-url');
|
const apiUrl = document.getElementById('job-cfg-api-url');
|
||||||
const apiToken = document.getElementById('job-cfg-api-token');
|
const apiToken = document.getElementById('job-cfg-api-token');
|
||||||
if (tailnet) cfg.tailnet = tailnet.value.trim();
|
if (tailnet) cfg.tailnet = tailnet.value.trim();
|
||||||
if (apiKey) cfg.api_key = apiKey.value;
|
if (apiKey) cfg.api_key = apiKey.value;
|
||||||
if (apiUrl) cfg.api_url = apiUrl.value.trim();
|
if (apiUrl) cfg.api_url = apiUrl.value.trim();
|
||||||
if (apiToken) cfg.api_token = apiToken.value;
|
if (apiToken) cfg.api_token = apiToken.value;
|
||||||
|
const runOnCreate = document.getElementById('job-run-on-create');
|
||||||
|
if (runOnCreate) cfg.run_on_create = runOnCreate.checked;
|
||||||
const res = await fetch(`/api/jobs/${jobId}`, {
|
const res = await fetch(`/api/jobs/${jobId}`, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
const VERSION = "1.4.0";
|
const VERSION = "1.5.0";
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "catalyst",
|
"name": "catalyst",
|
||||||
"version": "1.4.0",
|
"version": "1.6.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node server/server.js",
|
"start": "node server/server.js",
|
||||||
|
|||||||
47
server/db.js
47
server/db.js
@@ -125,6 +125,10 @@ function seedJobs() {
|
|||||||
upsert.run('patchmon_sync', 'Patchmon Sync',
|
upsert.run('patchmon_sync', 'Patchmon Sync',
|
||||||
'Syncs Patchmon host registration status to instances by matching hostnames.',
|
'Syncs Patchmon host registration status to instances by matching hostnames.',
|
||||||
0, 60, JSON.stringify({ api_url: 'http://patchmon:3000/api/v1/api/hosts', api_token: '' }));
|
0, 60, JSON.stringify({ api_url: 'http://patchmon:3000/api/v1/api/hosts', api_token: '' }));
|
||||||
|
|
||||||
|
upsert.run('semaphore_sync', 'Semaphore Sync',
|
||||||
|
'Syncs Semaphore inventory membership to instances by matching hostnames.',
|
||||||
|
0, 60, JSON.stringify({ api_url: 'http://semaphore:3000/api/project/1/inventory/1', api_token: '' }));
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Queries ───────────────────────────────────────────────────────────────────
|
// ── Queries ───────────────────────────────────────────────────────────────────
|
||||||
@@ -169,7 +173,8 @@ export function createInstance(data) {
|
|||||||
@tailscale, @andromeda, @tailscale_ip, @hardware_acceleration)
|
@tailscale, @andromeda, @tailscale_ip, @hardware_acceleration)
|
||||||
`).run(data);
|
`).run(data);
|
||||||
db.prepare(
|
db.prepare(
|
||||||
`INSERT INTO instance_history (vmid, field, old_value, new_value) VALUES (?, 'created', NULL, NULL)`
|
`INSERT INTO instance_history (vmid, field, old_value, new_value, changed_at)
|
||||||
|
VALUES (?, 'created', NULL, NULL, strftime('%Y-%m-%dT%H:%M:%f', 'now'))`
|
||||||
).run(data.vmid);
|
).run(data.vmid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,12 +185,13 @@ export function updateInstance(vmid, data) {
|
|||||||
name=@name, state=@state, stack=@stack, vmid=@newVmid,
|
name=@name, state=@state, stack=@stack, vmid=@newVmid,
|
||||||
atlas=@atlas, argus=@argus, semaphore=@semaphore, patchmon=@patchmon,
|
atlas=@atlas, argus=@argus, semaphore=@semaphore, patchmon=@patchmon,
|
||||||
tailscale=@tailscale, andromeda=@andromeda, tailscale_ip=@tailscale_ip,
|
tailscale=@tailscale, andromeda=@andromeda, tailscale_ip=@tailscale_ip,
|
||||||
hardware_acceleration=@hardware_acceleration, updated_at=datetime('now')
|
hardware_acceleration=@hardware_acceleration, updated_at=strftime('%Y-%m-%dT%H:%M:%f', 'now')
|
||||||
WHERE vmid=@vmid
|
WHERE vmid=@vmid
|
||||||
`).run({ ...data, newVmid: data.vmid, vmid });
|
`).run({ ...data, newVmid: data.vmid, vmid });
|
||||||
const newVmid = data.vmid;
|
const newVmid = data.vmid;
|
||||||
const insertEvt = db.prepare(
|
const insertEvt = db.prepare(
|
||||||
`INSERT INTO instance_history (vmid, field, old_value, new_value) VALUES (?, ?, ?, ?)`
|
`INSERT INTO instance_history (vmid, field, old_value, new_value, changed_at)
|
||||||
|
VALUES (?, ?, ?, ?, strftime('%Y-%m-%dT%H:%M:%f', 'now'))`
|
||||||
);
|
);
|
||||||
for (const field of HISTORY_FIELDS) {
|
for (const field of HISTORY_FIELDS) {
|
||||||
const oldVal = String(old[field] ?? '');
|
const oldVal = String(old[field] ?? '');
|
||||||
@@ -223,7 +229,7 @@ export function importInstances(rows, historyRows = []) {
|
|||||||
|
|
||||||
export function getInstanceHistory(vmid) {
|
export function getInstanceHistory(vmid) {
|
||||||
return db.prepare(
|
return db.prepare(
|
||||||
'SELECT * FROM instance_history WHERE vmid = ? ORDER BY changed_at DESC'
|
'SELECT * FROM instance_history WHERE vmid = ? ORDER BY changed_at DESC, id DESC'
|
||||||
).all(vmid);
|
).all(vmid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,6 +237,33 @@ export function getAllHistory() {
|
|||||||
return db.prepare('SELECT * FROM instance_history ORDER BY vmid, changed_at').all();
|
return db.prepare('SELECT * FROM instance_history ORDER BY vmid, changed_at').all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getAllJobs() {
|
||||||
|
return db.prepare('SELECT id, key, name, description, enabled, schedule, config FROM jobs ORDER BY id').all();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getAllJobRuns() {
|
||||||
|
return db.prepare('SELECT * FROM job_runs ORDER BY job_id, id').all();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function importJobs(jobRows, jobRunRows = []) {
|
||||||
|
db.exec('BEGIN');
|
||||||
|
db.exec('DELETE FROM job_runs');
|
||||||
|
db.exec('DELETE FROM jobs');
|
||||||
|
const insertJob = db.prepare(`
|
||||||
|
INSERT INTO jobs (id, key, name, description, enabled, schedule, config)
|
||||||
|
VALUES (@id, @key, @name, @description, @enabled, @schedule, @config)
|
||||||
|
`);
|
||||||
|
for (const j of jobRows) insertJob.run(j);
|
||||||
|
if (jobRunRows.length) {
|
||||||
|
const insertRun = db.prepare(`
|
||||||
|
INSERT INTO job_runs (id, job_id, started_at, ended_at, status, result)
|
||||||
|
VALUES (@id, @job_id, @started_at, @ended_at, @status, @result)
|
||||||
|
`);
|
||||||
|
for (const r of jobRunRows) insertRun.run(r);
|
||||||
|
}
|
||||||
|
db.exec('COMMIT');
|
||||||
|
}
|
||||||
|
|
||||||
export function getConfig(key, defaultVal = '') {
|
export function getConfig(key, defaultVal = '') {
|
||||||
const row = db.prepare('SELECT value FROM config WHERE key = ?').get(key);
|
const row = db.prepare('SELECT value FROM config WHERE key = ?').get(key);
|
||||||
return row ? row.value : defaultVal;
|
return row ? row.value : defaultVal;
|
||||||
@@ -278,12 +311,14 @@ export function updateJob(id, { enabled, schedule, config }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function createJobRun(jobId) {
|
export function createJobRun(jobId) {
|
||||||
return Number(db.prepare('INSERT INTO job_runs (job_id) VALUES (?)').run(jobId).lastInsertRowid);
|
return Number(db.prepare(
|
||||||
|
`INSERT INTO job_runs (job_id, started_at) VALUES (?, strftime('%Y-%m-%dT%H:%M:%f', 'now'))`
|
||||||
|
).run(jobId).lastInsertRowid);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function completeJobRun(runId, status, result) {
|
export function completeJobRun(runId, status, result) {
|
||||||
db.prepare(`
|
db.prepare(`
|
||||||
UPDATE job_runs SET ended_at=datetime('now'), status=@status, result=@result WHERE id=@id
|
UPDATE job_runs SET ended_at=strftime('%Y-%m-%dT%H:%M:%f', 'now'), status=@status, result=@result WHERE id=@id
|
||||||
`).run({ id: runId, status, result });
|
`).run({ id: runId, status, result });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,11 +66,46 @@ async function patchmonSyncHandler(cfg) {
|
|||||||
return { summary: `${updated} updated of ${instances.length}` };
|
return { summary: `${updated} updated of ${instances.length}` };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Semaphore Sync ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
async function semaphoreSyncHandler(cfg) {
|
||||||
|
const { api_url, api_token } = cfg;
|
||||||
|
if (!api_url || !api_token) throw new Error('Semaphore not configured — set API URL and token');
|
||||||
|
|
||||||
|
const res = await fetch(api_url, {
|
||||||
|
headers: { Authorization: `Bearer ${api_token}` },
|
||||||
|
});
|
||||||
|
if (!res.ok) throw new Error(`Semaphore API ${res.status}`);
|
||||||
|
|
||||||
|
const data = await res.json();
|
||||||
|
// Inventory is an Ansible INI string; extract bare hostnames
|
||||||
|
const hostSet = new Set(
|
||||||
|
(data.inventory ?? '').split('\n')
|
||||||
|
.map(l => l.trim())
|
||||||
|
.filter(l => l && !l.startsWith('[') && !l.startsWith('#') && !l.startsWith(';'))
|
||||||
|
.map(l => l.split(/[\s=]/)[0])
|
||||||
|
.filter(Boolean)
|
||||||
|
);
|
||||||
|
|
||||||
|
const instances = getInstances();
|
||||||
|
let updated = 0;
|
||||||
|
for (const inst of instances) {
|
||||||
|
const newSemaphore = hostSet.has(inst.name) ? 1 : 0;
|
||||||
|
if (newSemaphore !== inst.semaphore) {
|
||||||
|
const { id: _id, created_at: _ca, updated_at: _ua, ...instData } = inst;
|
||||||
|
updateInstance(inst.vmid, { ...instData, semaphore: newSemaphore });
|
||||||
|
updated++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { summary: `${updated} updated of ${instances.length}` };
|
||||||
|
}
|
||||||
|
|
||||||
// ── Registry ──────────────────────────────────────────────────────────────────
|
// ── Registry ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
const HANDLERS = {
|
const HANDLERS = {
|
||||||
tailscale_sync: tailscaleSyncHandler,
|
tailscale_sync: tailscaleSyncHandler,
|
||||||
patchmon_sync: patchmonSyncHandler,
|
patchmon_sync: patchmonSyncHandler,
|
||||||
|
semaphore_sync: semaphoreSyncHandler,
|
||||||
};
|
};
|
||||||
|
|
||||||
// ── Public API ────────────────────────────────────────────────────────────────
|
// ── Public API ────────────────────────────────────────────────────────────────
|
||||||
@@ -94,6 +129,15 @@ export async function runJob(jobId) {
|
|||||||
|
|
||||||
const _intervals = new Map();
|
const _intervals = new Map();
|
||||||
|
|
||||||
|
export async function runJobsOnCreate() {
|
||||||
|
for (const job of getJobs()) {
|
||||||
|
const cfg = JSON.parse(job.config || '{}');
|
||||||
|
if (cfg.run_on_create) {
|
||||||
|
try { await runJob(job.id); } catch (e) { console.error(`runJobsOnCreate job ${job.id}:`, e); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function restartJobs() {
|
export function restartJobs() {
|
||||||
for (const iv of _intervals.values()) clearInterval(iv);
|
for (const iv of _intervals.values()) clearInterval(iv);
|
||||||
_intervals.clear();
|
_intervals.clear();
|
||||||
|
|||||||
@@ -3,8 +3,9 @@ import {
|
|||||||
getInstances, getInstance, getDistinctStacks,
|
getInstances, getInstance, getDistinctStacks,
|
||||||
createInstance, updateInstance, deleteInstance, importInstances, getInstanceHistory, getAllHistory,
|
createInstance, updateInstance, deleteInstance, importInstances, getInstanceHistory, getAllHistory,
|
||||||
getConfig, setConfig, getJobs, getJob, updateJob, getJobRuns,
|
getConfig, setConfig, getJobs, getJob, updateJob, getJobRuns,
|
||||||
|
getAllJobs, getAllJobRuns, importJobs,
|
||||||
} from './db.js';
|
} from './db.js';
|
||||||
import { runJob, restartJobs } from './jobs.js';
|
import { runJob, restartJobs, runJobsOnCreate } from './jobs.js';
|
||||||
|
|
||||||
export const router = Router();
|
export const router = Router();
|
||||||
|
|
||||||
@@ -101,6 +102,7 @@ router.post('/instances', (req, res) => {
|
|||||||
createInstance(data);
|
createInstance(data);
|
||||||
const created = getInstance(data.vmid);
|
const created = getInstance(data.vmid);
|
||||||
res.status(201).json(created);
|
res.status(201).json(created);
|
||||||
|
runJobsOnCreate().catch(() => {});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
handleDbError('POST /api/instances', e, res);
|
handleDbError('POST /api/instances', e, res);
|
||||||
}
|
}
|
||||||
@@ -127,15 +129,17 @@ router.put('/instances/:vmid', (req, res) => {
|
|||||||
// GET /api/export
|
// GET /api/export
|
||||||
router.get('/export', (_req, res) => {
|
router.get('/export', (_req, res) => {
|
||||||
const instances = getInstances();
|
const instances = getInstances();
|
||||||
const history = getAllHistory();
|
const history = getAllHistory();
|
||||||
|
const jobs = getAllJobs();
|
||||||
|
const job_runs = getAllJobRuns();
|
||||||
const date = new Date().toISOString().slice(0, 10);
|
const date = new Date().toISOString().slice(0, 10);
|
||||||
res.setHeader('Content-Disposition', `attachment; filename="catalyst-backup-${date}.json"`);
|
res.setHeader('Content-Disposition', `attachment; filename="catalyst-backup-${date}.json"`);
|
||||||
res.json({ version: 2, exported_at: new Date().toISOString(), instances, history });
|
res.json({ version: 3, exported_at: new Date().toISOString(), instances, history, jobs, job_runs });
|
||||||
});
|
});
|
||||||
|
|
||||||
// POST /api/import
|
// POST /api/import
|
||||||
router.post('/import', (req, res) => {
|
router.post('/import', (req, res) => {
|
||||||
const { instances, history = [] } = req.body ?? {};
|
const { instances, history = [], jobs, job_runs } = req.body ?? {};
|
||||||
if (!Array.isArray(instances)) {
|
if (!Array.isArray(instances)) {
|
||||||
return res.status(400).json({ error: 'body must contain an instances array' });
|
return res.status(400).json({ error: 'body must contain an instances array' });
|
||||||
}
|
}
|
||||||
@@ -147,7 +151,14 @@ router.post('/import', (req, res) => {
|
|||||||
if (errors.length) return res.status(400).json({ errors });
|
if (errors.length) return res.status(400).json({ errors });
|
||||||
try {
|
try {
|
||||||
importInstances(instances.map(normalise), Array.isArray(history) ? history : []);
|
importInstances(instances.map(normalise), Array.isArray(history) ? history : []);
|
||||||
res.json({ imported: instances.length });
|
if (Array.isArray(jobs)) {
|
||||||
|
importJobs(jobs, Array.isArray(job_runs) ? job_runs : []);
|
||||||
|
try { restartJobs(); } catch (e) { console.error('POST /api/import restartJobs', e); }
|
||||||
|
}
|
||||||
|
res.json({
|
||||||
|
imported: instances.length,
|
||||||
|
imported_jobs: Array.isArray(jobs) ? jobs.length : undefined,
|
||||||
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('POST /api/import', e);
|
console.error('POST /api/import', e);
|
||||||
res.status(500).json({ error: 'internal server error' });
|
res.status(500).json({ error: 'internal server error' });
|
||||||
|
|||||||
@@ -276,9 +276,9 @@ describe('GET /api/export', () => {
|
|||||||
expect(res.body.instances).toEqual([])
|
expect(res.body.instances).toEqual([])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns version 2', async () => {
|
it('returns version 3', async () => {
|
||||||
const res = await request(app).get('/api/export')
|
const res = await request(app).get('/api/export')
|
||||||
expect(res.body.version).toBe(2)
|
expect(res.body.version).toBe(3)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('includes a history array', async () => {
|
it('includes a history array', async () => {
|
||||||
@@ -287,6 +287,21 @@ describe('GET /api/export', () => {
|
|||||||
expect(res.body.history).toBeInstanceOf(Array)
|
expect(res.body.history).toBeInstanceOf(Array)
|
||||||
expect(res.body.history.some(e => e.field === 'created')).toBe(true)
|
expect(res.body.history.some(e => e.field === 'created')).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('includes jobs and job_runs arrays', async () => {
|
||||||
|
createJob(testJob)
|
||||||
|
const res = await request(app).get('/api/export')
|
||||||
|
expect(res.body.jobs).toBeInstanceOf(Array)
|
||||||
|
expect(res.body.jobs).toHaveLength(1)
|
||||||
|
expect(res.body.jobs[0].key).toBe('tailscale_sync')
|
||||||
|
expect(res.body.job_runs).toBeInstanceOf(Array)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('exports raw job config without masking', async () => {
|
||||||
|
createJob(testJob)
|
||||||
|
const res = await request(app).get('/api/export')
|
||||||
|
expect(res.body.jobs[0].config).toContain('tskey-test')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// ── POST /api/import ──────────────────────────────────────────────────────────
|
// ── POST /api/import ──────────────────────────────────────────────────────────
|
||||||
@@ -341,6 +356,28 @@ describe('POST /api/import', () => {
|
|||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
expect(res.body.imported).toBe(1)
|
expect(res.body.imported).toBe(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('imports jobs and job_runs and returns imported_jobs count', async () => {
|
||||||
|
const exp = await request(app).get('/api/export')
|
||||||
|
createJob(testJob)
|
||||||
|
const fullExport = await request(app).get('/api/export')
|
||||||
|
const res = await request(app).post('/api/import').send({
|
||||||
|
instances: fullExport.body.instances,
|
||||||
|
history: fullExport.body.history,
|
||||||
|
jobs: fullExport.body.jobs,
|
||||||
|
job_runs: fullExport.body.job_runs,
|
||||||
|
})
|
||||||
|
expect(res.status).toBe(200)
|
||||||
|
expect(res.body.imported_jobs).toBe(1)
|
||||||
|
expect((await request(app).get('/api/jobs')).body).toHaveLength(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('leaves jobs untouched when no jobs key in payload', async () => {
|
||||||
|
createJob(testJob)
|
||||||
|
await request(app).post('/api/import')
|
||||||
|
.send({ instances: [{ ...base, vmid: 1, name: 'x' }] })
|
||||||
|
expect((await request(app).get('/api/jobs')).body).toHaveLength(1)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// ── Static assets & SPA routing ───────────────────────────────────────────────
|
// ── Static assets & SPA routing ───────────────────────────────────────────────
|
||||||
@@ -585,4 +622,40 @@ describe('POST /api/jobs/:id/run', () => {
|
|||||||
const res = await request(app).post(`/api/jobs/${id}/run`)
|
const res = await request(app).post(`/api/jobs/${id}/run`)
|
||||||
expect(res.status).toBe(500)
|
expect(res.status).toBe(500)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('run_on_create: triggers matching jobs when an instance is created', async () => {
|
||||||
|
createJob({ ...testJob, config: JSON.stringify({ api_key: 'k', tailnet: 't', run_on_create: true }) })
|
||||||
|
const id = (await request(app).get('/api/jobs')).body[0].id
|
||||||
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => ({ devices: [] }) }))
|
||||||
|
await request(app).post('/api/instances').send(base)
|
||||||
|
await new Promise(r => setImmediate(r))
|
||||||
|
const detail = await request(app).get(`/api/jobs/${id}`)
|
||||||
|
expect(detail.body.runs).toHaveLength(1)
|
||||||
|
expect(detail.body.runs[0].status).toBe('success')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('run_on_create: does not trigger jobs without the flag', async () => {
|
||||||
|
createJob(testJob)
|
||||||
|
const id = (await request(app).get('/api/jobs')).body[0].id
|
||||||
|
await request(app).post('/api/instances').send(base)
|
||||||
|
await new Promise(r => setImmediate(r))
|
||||||
|
expect((await request(app).get(`/api/jobs/${id}`)).body.runs).toHaveLength(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('semaphore_sync: parses ansible inventory and updates instances', async () => {
|
||||||
|
const semaphoreJob = {
|
||||||
|
key: 'semaphore_sync', name: 'Semaphore Sync', description: 'test',
|
||||||
|
enabled: 0, schedule: 60,
|
||||||
|
config: JSON.stringify({ api_url: 'http://semaphore:3000/api/project/1/inventory/1', api_token: 'bearer-token' }),
|
||||||
|
}
|
||||||
|
createJob(semaphoreJob)
|
||||||
|
const id = (await request(app).get('/api/jobs')).body[0].id
|
||||||
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValueOnce({
|
||||||
|
ok: true,
|
||||||
|
json: async () => ({ inventory: '[production]\nplex\nhomeassistant\n' }),
|
||||||
|
}))
|
||||||
|
const res = await request(app).post(`/api/jobs/${id}/run`)
|
||||||
|
expect(res.status).toBe(200)
|
||||||
|
expect(res.body.summary).toMatch(/updated of/)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user