feat(alerts,api): implement AlertEngine, webhooks, silences, and Phase 4 REST/WS routes

This commit is contained in:
tyuXX 2026-11-27 06:15:07 +00:00
parent f21699963f
commit 7272ad58bd
5 changed files with 1507 additions and 2 deletions

View file

@ -3,6 +3,10 @@ import http from 'node:http';
import path from 'node:path';
import type Database from 'better-sqlite3';
import express, { type Express } from 'express';
import { AlertEngine } from './alerts/alert-engine.js';
import { BroadcastService } from './automation/broadcast-service.js';
import { TransferEngine } from './automation/transfer-engine.js';
import { WorkflowEngine } from './automation/workflow-engine.js';
import { ConfigManager } from './config/config-manager.js';
import { openDatabase } from './db/database.js';
import { ServerInspector } from './monitoring/inspector.js';
@ -24,15 +28,18 @@ import { SftpService } from './ssh/sftp-service.js';
import { SSHPool } from './ssh/ssh-pool.js';
import { TunnelManager } from './ssh/tunnel-manager.js';
import { CustomSystemsEngine } from './systems/systems-engine.js';
import { createAlertsRouter } from './api/routes/alerts.routes.js';
import { createAuditRouter } from './api/routes/audit.routes.js';
import { createAuthRouter } from './api/routes/auth.routes.js';
import { createConfigRouter } from './api/routes/config.routes.js';
import { createServersRouter } from './api/routes/servers.routes.js';
import { createSftpRouter } from './api/routes/sftp.routes.js';
import { createSnippetsRouter } from './api/routes/snippets.routes.js';
import { createSystemsRouter } from './api/routes/systems.routes.js';
import { createTerminalRouter } from './api/routes/terminal.routes.js';
import { createProxyRouter, createTunnelsRouter } from './api/routes/tunnels.routes.js';
import { createVaultRouter } from './api/routes/vault.routes.js';
import { createWorkflowsRouter } from './api/routes/workflows.routes.js';
import { WsHub } from './api/ws-hub.js';
export interface ServerOptions {
@ -64,6 +71,10 @@ export interface GCoordinatorInstance {
customSystemsEngine: CustomSystemsEngine;
serverInspector: ServerInspector;
monitoringService: MonitoringService;
transferEngine: TransferEngine;
workflowEngine: WorkflowEngine;
broadcastService: BroadcastService;
alertEngine: AlertEngine;
wsHub: WsHub;
port: number;
close(): Promise<void>;
@ -144,6 +155,36 @@ export async function createGCoordinatorServer(
sshPool,
probeRunner,
);
const transferEngine = new TransferEngine(sshPool, sftpService, configManager);
const workflowEngine = new WorkflowEngine(
db,
configManager,
vaultService,
sshPool,
sftpService,
customSystemsEngine,
transferEngine,
wsHub,
auditService,
);
const broadcastService = new BroadcastService(
configManager,
vaultService,
sshPool,
wsHub,
auditService,
);
const alertEngine = new AlertEngine(
db,
configManager,
vaultService,
monitoringService,
sshPool,
customSystemsEngine,
workflowEngine,
wsHub,
auditService,
);
wsHub.registerUpgradeHandler('/proxy/', (req, socket, head) => {
tunnelManager.handleWsProxyUpgrade(req, socket, head);
@ -178,7 +219,12 @@ export async function createGCoordinatorServer(
}
} else if (channel.startsWith('stream:')) {
const jobId = channel.slice('stream:'.length);
for (const frame of customSystemsEngine.getStreamReplayFrames(jobId)) {
const replay = jobId.startsWith('wf:')
? workflowEngine.getStreamReplayFrames(jobId)
: jobId.startsWith('bcast-')
? broadcastService.getStreamReplayFrames(jobId)
: customSystemsEngine.getStreamReplayFrames(jobId);
for (const frame of replay) {
ctx.send(frame);
}
} else if (channel === 'metrics') {
@ -218,7 +264,8 @@ export async function createGCoordinatorServer(
if (s.hostKeyMismatch) {
wsHub.broadcastChannel('alerts', {
type: 'alerts:update',
activeCount: knownHosts.listPendingMismatches().length,
activeCount:
alertEngine.getActiveCount() + knownHosts.listPendingMismatches().length,
hostKeyMismatch: s.hostKeyMismatch,
});
}
@ -238,8 +285,13 @@ export async function createGCoordinatorServer(
monitoringService.start();
void tunnelManager.syncAutoStart();
customSystemsEngine.syncBackgroundChecks();
workflowEngine.start();
alertEngine.start();
vaultService.onPanicLock(() => {
alertEngine.handlePanicLock();
workflowEngine.handlePanicLock();
broadcastService.cancelAll();
monitoringService.stopAll();
customSystemsEngine.stopBackgroundChecks();
customSystemsEngine.cancelAllStreams();
@ -261,6 +313,7 @@ export async function createGCoordinatorServer(
if (
event.entityType === 'server' ||
event.entityType === 'system' ||
event.entityType === 'workflow' ||
event.entityType === 'settings' ||
event.entityType === 'all'
) {
@ -279,6 +332,7 @@ export async function createGCoordinatorServer(
}
monitoringService.syncServers();
customSystemsEngine.syncBackgroundChecks();
workflowEngine.syncCronJobs();
void tunnelManager.syncAutoStart();
}
});
@ -320,6 +374,7 @@ export async function createGCoordinatorServer(
onVaultUnlocked: () => {
monitoringService.syncServers();
customSystemsEngine.syncBackgroundChecks();
workflowEngine.syncCronJobs();
void tunnelManager.syncAutoStart();
},
onVaultChanged: (snapshotReason) => {
@ -338,6 +393,24 @@ export async function createGCoordinatorServer(
'/api/systems',
createSystemsRouter({ authService, auditService, systemsEngine: customSystemsEngine }),
);
app.use(
'/api/workflows',
createWorkflowsRouter({
authService,
configManager,
auditService,
workflowEngine,
broadcastService,
}),
);
app.use(
'/api/snippets',
createSnippetsRouter({ authService, configManager, auditService }),
);
app.use(
'/api/alerts',
createAlertsRouter({ authService, configManager, auditService, alertEngine }),
);
app.use(
'/api/servers/:id/sftp',
createSftpRouter({ authService, configManager, auditService, sftpService }),
@ -409,9 +482,16 @@ export async function createGCoordinatorServer(
customSystemsEngine,
serverInspector,
monitoringService,
transferEngine,
workflowEngine,
broadcastService,
alertEngine,
wsHub,
port: actualPort,
async close() {
alertEngine.close();
workflowEngine.close();
broadcastService.cancelAll();
monitoringService.close();
customSystemsEngine.close();
tunnelManager.stopAll();