2026-05-20 22:59:03 -04:00
|
|
|
import http from 'http';
|
|
|
|
|
import os from 'os';
|
|
|
|
|
import fs from 'fs';
|
2026-05-20 13:48:18 -04:00
|
|
|
|
|
|
|
|
const MAM_API_URL = (process.env.MAM_API_URL || 'http://localhost:3000').replace(/\/$/, '');
|
|
|
|
|
const NODE_TOKEN = process.env.NODE_TOKEN || '';
|
|
|
|
|
const NODE_ROLE = process.env.NODE_ROLE || 'worker';
|
2026-05-21 00:15:03 -04:00
|
|
|
const AGENT_PORT = parseInt(process.env.AGENT_PORT || '7436', 10);
|
2026-05-20 13:48:18 -04:00
|
|
|
const HEARTBEAT_MS = parseInt(process.env.HEARTBEAT_MS || '30000', 10);
|
2026-05-21 00:15:03 -04:00
|
|
|
const VERSION = '1.1.0';
|
|
|
|
|
|
|
|
|
|
// Pick the host's LAN IP. Inside a bridge-mode container,
|
|
|
|
|
// os.networkInterfaces() returns the container's docker-bridge IP (172.x),
|
|
|
|
|
// not the host's LAN address. Two strategies:
|
|
|
|
|
// 1. honour an explicit NODE_IP override (set by onboard-node.sh)
|
|
|
|
|
// 2. filter out interfaces that obviously belong to container/virtual
|
|
|
|
|
// bridges and down-rank the docker bridge range so real LAN IPs win
|
|
|
|
|
//
|
|
|
|
|
// Combine with `network_mode: host` in docker-compose.worker.yml for the
|
|
|
|
|
// most reliable result — that lets os.networkInterfaces() see the host's
|
|
|
|
|
// real adapters directly.
|
|
|
|
|
function getIp() {
|
|
|
|
|
if (process.env.NODE_IP) return process.env.NODE_IP;
|
|
|
|
|
|
|
|
|
|
const SKIP_IFACE = /^(lo|docker\d*|br-|veth|cni|flannel|cali|tun|tap|virbr|kube)/i;
|
|
|
|
|
const isContainerBridge = (ip) => /^172\.(1[6-9]|2\d|3[01])\./.test(ip);
|
|
|
|
|
|
|
|
|
|
const candidates = [];
|
|
|
|
|
for (const [name, addrs] of Object.entries(os.networkInterfaces())) {
|
|
|
|
|
if (SKIP_IFACE.test(name)) continue;
|
|
|
|
|
for (const a of (addrs || [])) {
|
|
|
|
|
if (a.family !== 'IPv4' || a.internal) continue;
|
|
|
|
|
candidates.push({ name, address: a.address, rank: isContainerBridge(a.address) ? 1 : 0 });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
candidates.sort((a, b) => a.rank - b.rank);
|
|
|
|
|
return candidates.length ? candidates[0].address : null;
|
|
|
|
|
}
|
2026-05-20 13:48:18 -04:00
|
|
|
|
2026-05-20 22:59:03 -04:00
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
|
if (req.method === 'GET' && req.url === '/health') {
|
|
|
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
|
|
|
res.end(JSON.stringify({
|
|
|
|
|
ok: true,
|
|
|
|
|
hostname: os.hostname(),
|
|
|
|
|
uptime: Math.round(process.uptime()),
|
|
|
|
|
version: VERSION,
|
|
|
|
|
role: NODE_ROLE,
|
2026-05-21 00:15:03 -04:00
|
|
|
ip: getIp(),
|
2026-05-20 22:59:03 -04:00
|
|
|
}));
|
|
|
|
|
} else {
|
|
|
|
|
res.writeHead(404);
|
|
|
|
|
res.end();
|
|
|
|
|
}
|
2026-05-20 13:48:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ── CPU sampling (500ms window) ───────────────────────────────────────────
|
|
|
|
|
function sampleCpu() {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
const s1 = os.cpus();
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
const s2 = os.cpus();
|
|
|
|
|
let idle = 0, total = 0;
|
|
|
|
|
s2.forEach((cpu, i) => {
|
|
|
|
|
for (const t of Object.keys(cpu.times)) {
|
|
|
|
|
const d = cpu.times[t] - (s1[i]?.times[t] ?? 0);
|
|
|
|
|
total += d;
|
|
|
|
|
if (t === 'idle') idle += d;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
resolve(total > 0 ? Math.round((1 - idle / total) * 10000) / 100 : 0);
|
|
|
|
|
}, 500);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 14:18:07 -04:00
|
|
|
// ── Hardware detection ────────────────────────────────────────────────────
|
2026-05-20 22:59:03 -04:00
|
|
|
// GPU_COUNT / BMD_COUNT env vars override filesystem detection when /dev isn't mapped
|
2026-05-20 14:18:07 -04:00
|
|
|
function detectHardware() {
|
|
|
|
|
const capabilities = { gpus: [], blackmagic: [] };
|
|
|
|
|
|
|
|
|
|
const gpuOverride = parseInt(process.env.GPU_COUNT || '-1', 10);
|
|
|
|
|
if (gpuOverride >= 0) {
|
|
|
|
|
for (let i = 0; i < gpuOverride; i++) {
|
|
|
|
|
capabilities.gpus.push({ device: `/dev/nvidia${i}`, type: 'nvidia', index: i });
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (let i = 0; i < 16; i++) {
|
|
|
|
|
try {
|
|
|
|
|
fs.accessSync(`/dev/nvidia${i}`, fs.constants.F_OK);
|
|
|
|
|
capabilities.gpus.push({ device: `/dev/nvidia${i}`, type: 'nvidia', index: i });
|
|
|
|
|
} catch (_) { break; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bmdOverride = parseInt(process.env.BMD_COUNT || '-1', 10);
|
|
|
|
|
if (bmdOverride >= 0) {
|
|
|
|
|
for (let i = 0; i < bmdOverride; i++) {
|
|
|
|
|
capabilities.blackmagic.push({ device: `/dev/blackmagic/dv${i}`, index: i });
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
const bmdEntries = fs.readdirSync('/dev/blackmagic');
|
2026-05-21 00:15:03 -04:00
|
|
|
capabilities.blackmagic = bmdEntries.map((d, i) => ({ device: `/dev/blackmagic/${d}`, index: i }));
|
2026-05-20 14:18:07 -04:00
|
|
|
} catch (_) {}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 00:15:03 -04:00
|
|
|
// Best-effort model name from BMD_MODEL env (set manually) — used by the UI
|
|
|
|
|
// to render the correct card layout (Duo 2, Quad 2, Mini Recorder, ...).
|
|
|
|
|
if (process.env.BMD_MODEL) capabilities.blackmagic_model = process.env.BMD_MODEL;
|
|
|
|
|
|
2026-05-20 14:18:07 -04:00
|
|
|
return capabilities;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 13:48:18 -04:00
|
|
|
// ── Heartbeat ─────────────────────────────────────────────────────────────
|
|
|
|
|
async function heartbeat() {
|
2026-05-20 14:18:07 -04:00
|
|
|
const cpu_usage = await sampleCpu();
|
|
|
|
|
const totalMem = os.totalmem();
|
|
|
|
|
const freeMem = os.freemem();
|
|
|
|
|
const ip_address = getIp();
|
|
|
|
|
const capabilities = detectHardware();
|
2026-05-20 13:48:18 -04:00
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
hostname: os.hostname(),
|
|
|
|
|
ip_address,
|
|
|
|
|
role: NODE_ROLE,
|
|
|
|
|
version: VERSION,
|
|
|
|
|
api_url: `http://${ip_address || os.hostname()}:${AGENT_PORT}`,
|
|
|
|
|
cpu_usage,
|
|
|
|
|
mem_used_mb: Math.round((totalMem - freeMem) / 1048576),
|
|
|
|
|
mem_total_mb: Math.round(totalMem / 1048576),
|
2026-05-20 14:18:07 -04:00
|
|
|
capabilities,
|
2026-05-20 13:48:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const headers = { 'Content-Type': 'application/json' };
|
|
|
|
|
if (NODE_TOKEN) headers['Authorization'] = `Bearer ${NODE_TOKEN}`;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch(`${MAM_API_URL}/api/v1/cluster/heartbeat`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers,
|
|
|
|
|
body: JSON.stringify(payload),
|
|
|
|
|
signal: AbortSignal.timeout(8000),
|
|
|
|
|
});
|
|
|
|
|
if (res.ok) {
|
2026-05-20 14:18:07 -04:00
|
|
|
const gpuStr = capabilities.gpus.length ? ` gpu=${capabilities.gpus.length}` : '';
|
|
|
|
|
const bmdStr = capabilities.blackmagic.length ? ` bmd=${capabilities.blackmagic.length}` : '';
|
2026-05-20 13:48:18 -04:00
|
|
|
process.stdout.write(
|
2026-05-21 00:15:03 -04:00
|
|
|
`[hb] ${payload.hostname} ip=${ip_address || '?'} cpu=${cpu_usage}% ` +
|
2026-05-20 14:18:07 -04:00
|
|
|
`mem=${payload.mem_used_mb}/${payload.mem_total_mb}MB${gpuStr}${bmdStr}\n`
|
2026-05-20 13:48:18 -04:00
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
const txt = await res.text().catch(() => '');
|
|
|
|
|
console.error(`[hb] ${res.status} ${txt.slice(0, 120)}`);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`[hb] failed — ${err.message}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
heartbeat();
|
|
|
|
|
setInterval(heartbeat, HEARTBEAT_MS);
|
|
|
|
|
|
2026-05-20 22:59:03 -04:00
|
|
|
server.listen(AGENT_PORT, () => {
|
2026-05-20 13:48:18 -04:00
|
|
|
console.log(`wild-dragon-node-agent v${VERSION}`);
|
|
|
|
|
console.log(` Listening :${AGENT_PORT}`);
|
2026-05-21 00:15:03 -04:00
|
|
|
console.log(` Host IP ${getIp() || '(unresolved)'}`);
|
2026-05-20 13:48:18 -04:00
|
|
|
console.log(` Primary ${MAM_API_URL}`);
|
|
|
|
|
console.log(` Role ${NODE_ROLE}`);
|
|
|
|
|
console.log(` Heartbeat every ${HEARTBEAT_MS / 1000}s`);
|
|
|
|
|
});
|