feat: add Semaphore Sync job #50
11
js/ui.js
11
js/ui.js
@@ -483,17 +483,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 '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 ───────────────────────────────────────────────────────────────────
|
||||||
|
|||||||
@@ -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 ────────────────────────────────────────────────────────────────
|
||||||
|
|||||||
@@ -585,4 +585,21 @@ 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('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