- Added api-security-hardening (helmet, rate limits) - Added nodejs-backend-patterns (error handling) - Added observability-monitoring (pino logging) - Added websocket-engineer (socket.io real-time updates) - Added docker (Multi-stage build, compose) - Added vitest (testing configuration and store tests) - Added data-visualizer (MetricsBar and HostChart) - Added infrastructure-monitoring/proxmox-admin/network-engineer types - Fixed UI accessibility and styling - Cleaned up node_modules tracking
77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import { homedir } from 'os';
|
|
import { HostConfig } from './types';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
const CONFIG_FILE = path.join(__dirname, 'hosts.json');
|
|
|
|
function parseEnvHosts(): HostConfig[] {
|
|
const hostsEnv = process.env.SSH_HOSTS;
|
|
if (!hostsEnv) return [];
|
|
|
|
const hosts: HostConfig[] = [];
|
|
const entries = hostsEnv.split(',').map(h => h.trim()).filter(Boolean);
|
|
|
|
for (const entry of entries) {
|
|
const [name, ip] = entry.split(':');
|
|
if (name && ip) {
|
|
hosts.push({
|
|
name: name.trim(),
|
|
ip: ip.trim(),
|
|
sshUser: process.env.SSH_USER || 'bear',
|
|
sshKeyPath: process.env.SSH_KEY,
|
|
sshPort: process.env.SSH_PORT ? parseInt(process.env.SSH_PORT, 10) : 22,
|
|
});
|
|
}
|
|
}
|
|
|
|
return hosts;
|
|
}
|
|
|
|
function parseJsonConfig(): HostConfig[] {
|
|
if (!fs.existsSync(CONFIG_FILE)) {
|
|
console.error('Config file not found:', CONFIG_FILE);
|
|
return [];
|
|
}
|
|
|
|
try {
|
|
const content = fs.readFileSync(CONFIG_FILE, 'utf-8');
|
|
const data = JSON.parse(content);
|
|
|
|
if (!data.hosts || !Array.isArray(data.hosts)) {
|
|
console.error('No hosts array in config');
|
|
return [];
|
|
}
|
|
|
|
const hosts = data.hosts.map((h: Partial<HostConfig>) => ({
|
|
name: h.name || '',
|
|
ip: h.ip || '',
|
|
sshUser: h.sshUser || 'bear',
|
|
sshKeyPath: h.sshKeyPath?.replace(/^~/, homedir()),
|
|
sshPort: h.sshPort || 22,
|
|
})).filter((h: HostConfig) => h.name && h.ip);
|
|
|
|
console.error('Loaded hosts:', JSON.stringify(hosts));
|
|
return hosts;
|
|
} catch (e: any) {
|
|
console.error('Config parse error:', e.message);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export function getHostConfigs(): HostConfig[] {
|
|
const envHosts = parseEnvHosts();
|
|
if (envHosts.length > 0) {
|
|
return envHosts;
|
|
}
|
|
|
|
return parseJsonConfig();
|
|
}
|
|
|
|
export function hasConfig(): boolean {
|
|
return getHostConfigs().length > 0;
|
|
}
|