/** * Load central fleet.json + per-service JSON configs. * @module load */ import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { loadMachines } from './machines.js'; const HERE = path.dirname(fileURLToPath(import.meta.url)); export const FLEET_ROOT = path.resolve(HERE, '..'); export function readJson(file) { return JSON.parse(fs.readFileSync(file, 'utf8')); } /** * @param {string} [root] */ export function loadFleet(root = FLEET_ROOT, opts = {}) { const fleetPath = path.join(root, 'fleet.json'); const fleet = readJson(fleetPath); const dir = path.join(root, 'services'); const services = {}; const unmanaged = []; for (const name of fs.readdirSync(dir).sort()) { if (!name.endsWith('.json')) continue; const spec = readJson(path.join(dir, name)); const configPath = path.join('services', name); if (spec.id === '_unmanaged') { for (const e of spec.entries || []) { unmanaged.push({ ...e, managed: false, configPath }); } continue; } const central = fleet.services?.[spec.id] || {}; services[spec.id] = { ...spec, configPath, min: central.min ?? 0, max: central.max ?? 1, keepFloor: central.keepFloor !== false, enabled: central.enabled !== false, managed: central.managed === false ? false : spec.managed !== false, }; } const overlay = opts.overlay === false ? null : path.join(root, 'data', 'runtime', 'machines-overlay.json'); const machines = loadMachines(root, overlay); return { root, fleetPath: path.relative(root, fleetPath) || 'fleet.json', machinesPath: 'machines.json', machinesOverlay: overlay, control: fleet.control, nats: fleet.nats, machines, services, unmanaged, }; } export function listServices(loaded) { const rows = Object.values(loaded.services).map((s) => ({ id: s.id, title: s.title, kind: s.kind, managed: s.managed, enabled: s.enabled, min: s.min, max: s.max, keepFloor: s.keepFloor, configPath: s.configPath, package: s.package, runtime: s.runtime, })); for (const u of loaded.unmanaged) { rows.push({ id: u.id, title: u.id, kind: 'unmanaged', managed: false, enabled: false, min: 0, max: 0, keepFloor: false, configPath: u.configPath, package: u.package, runtime: u.runtime, }); } return rows; }