This commit is contained in:
jarek committed 2026-02-08 09:59:06 +01:00
1 parent 9d2266dffe
commit 188ba1967d
87 files changed
+2103 -1774

No files matched your search

+9 -18
View File
@@ -21,11 +21,13 @@ export const GET: RequestHandler = async ({ cookies }) => {
});
}
let heartbeatInterval: ReturnType<typeof setInterval>;
let onAuditEvent: (data: AuditEventData) => void;
const stream = new ReadableStream({
start(controller) {
const encoder = new TextEncoder();
// Send SSE event
const sendEvent = (type: string, data: any) => {
const event = `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`;
try {
@@ -35,11 +37,9 @@ export const GET: RequestHandler = async ({ cookies }) => {
}
};
// Send initial connection event
sendEvent('connected', { timestamp: new Date().toISOString() });
// Send heartbeat to keep connection alive (every 5s to prevent Traefik 10s idle timeout)
const heartbeatInterval = setInterval(() => {
heartbeatInterval = setInterval(() => {
try {
sendEvent('heartbeat', { timestamp: new Date().toISOString() });
} catch {
@@ -47,24 +47,15 @@ export const GET: RequestHandler = async ({ cookies }) => {
}
}, 5000);
// Listen for audit events
const onAuditEvent = (data: AuditEventData) => {
onAuditEvent = (data: AuditEventData) => {
sendEvent('audit', data);
};
auditEvents.on('audit', onAuditEvent);
// Cleanup when client disconnects
const cleanup = () => {
clearInterval(heartbeatInterval);
auditEvents.off('audit', onAuditEvent);
};
// Note: SvelteKit doesn't provide a direct way to detect client disconnect
// The cleanup will happen when the stream errors or the server shuts down
// For production, consider using a WebSocket instead for better connection management
return cleanup;
},
cancel() {
clearInterval(heartbeatInterval);
auditEvents.off('audit', onAuditEvent);
}
});
@@ -113,7 +113,10 @@ export const GET: RequestHandler = async ({ params, url, cookies }) => {
if (error instanceof EnvironmentNotFoundError) {
return json({ error: 'Environment not found' }, { status: 404 });
}
console.error('Failed to get container stats:', error);
if (error.statusCode === 404) {
return json({ error: 'Container not found' }, { status: 404 });
}
console.error('Failed to get container stats:', error.message || error);
return json({ error: error.message || 'Failed to get stats' }, { status: 500 });
}
};
@@ -16,6 +16,7 @@ import { getScannerSettings, scanImage } from '$lib/server/scanner';
import { saveVulnerabilityScan, removePendingContainerUpdate, type VulnerabilityCriteria } from '$lib/server/db';
import { parseImageNameAndTag, shouldBlockUpdate, combineScanSummaries, isDockhandContainer } from '$lib/server/scheduler/tasks/update-utils';
import { recreateContainer, updateStackContainer } from '$lib/server/scheduler/tasks/container-update';
import { pullStackService } from '$lib/server/stacks';
export interface ScanResult {
critical: number;
@@ -56,6 +57,15 @@ export interface UpdateProgress {
scannerResults?: ScannerResult[];
blockReason?: string;
scanner?: string;
vulnerabilities?: Array<{
id: string;
severity: string;
package: string;
version: string;
fixedVersion?: string;
link?: string;
scanner: string;
}>;
}
/**
@@ -198,6 +208,13 @@ export const POST: RequestHandler = async (event) => {
continue;
}
// Detect stack membership early (needed for both pull and recreate)
const containerLabels = config.Labels || {};
const composeProject = containerLabels['com.docker.compose.project'];
const composeService = containerLabels['com.docker.compose.service'];
const composeConfigFiles = containerLabels['com.docker.compose.project.config_files'];
const isStackContainer = !!(composeProject && composeService);
// Step 1: Pull latest image
safeEnqueue({
type: 'progress',
@@ -210,19 +227,37 @@ export const POST: RequestHandler = async (event) => {
});
try {
await pullImage(imageName, (data: any) => {
// Send pull progress as log entries
if (data.status) {
safeEnqueue({
type: 'pull_log',
containerId,
containerName,
pullStatus: data.status,
pullId: data.id,
pullProgress: data.progress
});
if (isStackContainer) {
const pullResult = await pullStackService(composeProject, composeService!, envIdNum, composeConfigFiles);
if (!pullResult.success) {
// Fallback to direct pull
await pullImage(imageName, (data: any) => {
if (data.status) {
safeEnqueue({
type: 'pull_log',
containerId,
containerName,
pullStatus: data.status,
pullId: data.id,
pullProgress: data.progress
});
}
}, envIdNum);
}
}, envIdNum);
} else {
await pullImage(imageName, (data: any) => {
if (data.status) {
safeEnqueue({
type: 'pull_log',
containerId,
containerName,
pullStatus: data.status,
pullId: data.id,
pullProgress: data.progress
});
}
}, envIdNum);
}
} catch (pullError: any) {
safeEnqueue({
type: 'progress',
@@ -289,13 +324,13 @@ export const POST: RequestHandler = async (event) => {
try {
const scanResults = await scanImage(tempTag, envIdNum, (progress) => {
if (progress.message) {
if (progress.output || progress.message) {
safeEnqueue({
type: 'scan_log',
containerId,
containerName,
scanner: progress.scanner,
message: progress.message
message: progress.output || progress.message
});
}
});
@@ -352,12 +387,27 @@ export const POST: RequestHandler = async (event) => {
}
}
// Collect vulnerabilities from all scanners (cap at 100)
const vulnerabilities = scanResults
.flatMap(r => r.vulnerabilities || [])
.slice(0, 100)
.map(v => ({
id: v.id,
severity: v.severity,
package: v.package,
version: v.version,
fixedVersion: v.fixedVersion,
link: v.link,
scanner: v.scanner
}));
safeEnqueue({
type: 'scan_complete',
containerId,
containerName,
scanResult: finalScanResult,
scannerResults: individualScannerResults.length > 0 ? individualScannerResults : undefined,
vulnerabilities: vulnerabilities.length > 0 ? vulnerabilities : undefined,
message: finalScanResult
? `Scan complete: ${finalScanResult.critical} critical, ${finalScanResult.high} high, ${finalScanResult.medium} medium, ${finalScanResult.low} low`
: 'Scan complete: no vulnerabilities found'
@@ -415,12 +465,6 @@ export const POST: RequestHandler = async (event) => {
} catch { /* ignore cleanup errors */ }
}
// Detect if container is part of a Docker Compose stack
const containerLabels = config.Labels || {};
const composeProject = containerLabels['com.docker.compose.project'];
const composeService = containerLabels['com.docker.compose.service'];
const isStackContainer = !!composeProject;
// Progress logging function for shared functions
const logProgress = (message: string) => {
safeEnqueue({
@@ -452,7 +496,7 @@ export const POST: RequestHandler = async (event) => {
});
// Try stack-based update first
const stackSuccess = await updateStackContainer(composeProject, composeService!, envIdNum, logProgress);
const stackSuccess = await updateStackContainer(composeProject, composeService!, envIdNum, logProgress, composeConfigFiles);
if (stackSuccess) {
updateSuccess = true;
+1 -1
View File
@@ -170,7 +170,7 @@ export const GET: RequestHandler = async ({ url, cookies }) => {
if (error instanceof EnvironmentNotFoundError) {
return json({ error: 'Environment not found' }, { status: 404 });
}
console.error('Failed to get container stats:', error);
console.error('Failed to get container stats:', error.message || error);
return json([], { status: 200 }); // Return empty array instead of error
}
};
@@ -8,6 +8,18 @@ import {
} from '$lib/server/db';
import { refreshSchedulesForEnvironment } from '$lib/server/scheduler';
/** Map of modern IANA timezone names to their canonical equivalents recognized by Bun/ICU */
const TIMEZONE_ALIASES: Record<string, string> = {
'Europe/Kyiv': 'Europe/Kiev',
'Asia/Ho_Chi_Minh': 'Asia/Saigon',
'America/Nuuk': 'America/Godthab',
'Pacific/Kanton': 'Pacific/Enderbury'
};
function normalizeTimezone(tz: string): string {
return TIMEZONE_ALIASES[tz] || tz;
}
/**
* Get timezone for an environment.
*/
@@ -26,7 +38,8 @@ export const GET: RequestHandler = async ({ params, cookies }) => {
return json({ error: 'Environment not found' }, { status: 404 });
}
const timezone = await getEnvironmentTimezone(id);
const rawTimezone = await getEnvironmentTimezone(id);
const timezone = normalizeTimezone(rawTimezone);
return json({ timezone });
} catch (error) {
@@ -54,7 +67,7 @@ export const POST: RequestHandler = async ({ params, request, cookies }) => {
}
const data = await request.json();
const timezone = data.timezone || 'UTC';
const timezone = normalizeTimezone(data.timezone || 'UTC');
// Validate timezone
const validTimezones = Intl.supportedValuesOf('timeZone');
+15 -8
View File
@@ -33,10 +33,13 @@ export const GET: RequestHandler = async ({ url }) => {
);
}
let heartbeatInterval: ReturnType<typeof setInterval>;
let controllerClosed = false;
let eventReader: ReadableStreamDefaultReader<Uint8Array> | null = null;
const stream = new ReadableStream({
async start(controller) {
const encoder = new TextEncoder();
let controllerClosed = false;
// Safe close helper - prevents "Controller is already closed" errors
const safeClose = () => {
@@ -50,7 +53,7 @@ export const GET: RequestHandler = async ({ url }) => {
}
};
// Send initial connection event
// Send SSE event
const sendEvent = (type: string, data: any) => {
if (controllerClosed) return;
try {
@@ -63,7 +66,7 @@ export const GET: RequestHandler = async ({ url }) => {
};
// Send heartbeat to keep connection alive (every 5s to prevent Traefik 10s idle timeout)
const heartbeatInterval = setInterval(() => {
heartbeatInterval = setInterval(() => {
try {
sendEvent('heartbeat', { timestamp: new Date().toISOString() });
} catch {
@@ -71,6 +74,7 @@ export const GET: RequestHandler = async ({ url }) => {
}
}, 5000);
// Send initial connection event
sendEvent('connected', { timestamp: new Date().toISOString(), envId: envIdNum });
try {
@@ -87,14 +91,14 @@ export const GET: RequestHandler = async ({ url }) => {
return;
}
const reader = eventStream.getReader();
eventReader = eventStream.getReader();
const decoder = new TextDecoder();
let buffer = '';
const processEvents = async () => {
try {
while (true) {
const { done, value } = await reader.read();
const { done, value } = await eventReader!.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
@@ -129,9 +133,7 @@ export const GET: RequestHandler = async ({ url }) => {
} catch (error: any) {
// Don't log full stack trace for expected connection errors
const isConnectionError = error?.code === 'ECONNRESET' || error?.code === 'ECONNREFUSED';
if (isConnectionError) {
// Silent - these are handled by event-subprocess reconnection logic
} else {
if (!isConnectionError) {
console.error('Docker event stream error:', error?.message || error);
}
sendEvent('error', { message: error?.message || 'Stream connection lost' });
@@ -157,6 +159,11 @@ export const GET: RequestHandler = async ({ url }) => {
clearInterval(heartbeatInterval);
safeClose();
}
},
cancel() {
controllerClosed = true;
clearInterval(heartbeatInterval);
eventReader?.cancel().catch(() => {});
}
});
@@ -15,11 +15,14 @@ export const POST: RequestHandler = async ({ params }) => {
return json({ error: 'Notification setting not found' }, { status: 404 });
}
const success = await testNotification(setting);
const result = await testNotification(setting);
return json({
success,
message: success ? 'Test notification sent successfully' : 'Failed to send test notification'
success: result.success,
message: result.success
? 'Test notification sent successfully'
: 'Failed to send test notification',
error: result.error
});
} catch (error: any) {
console.error('Error testing notification:', error);
+5 -2
View File
@@ -17,6 +17,7 @@ interface ValidationResult {
* Extract environment variables from compose YAML content.
* Matches ${VAR_NAME} and ${VAR_NAME:-default} patterns.
* Ignores variables in commented lines (lines starting with #).
* Ignores escaped $$ (Docker Compose escape syntax for literal $).
* Returns { required: [...], optional: [...] }
*/
function extractComposeVars(yaml: string): { required: string[]; optional: string[] } {
@@ -33,7 +34,8 @@ function extractComposeVars(yaml: string): { required: string[]; optional: strin
}
// Match ${VAR_NAME} (required) and ${VAR_NAME:-default} or ${VAR_NAME-default} (optional)
const regex = /\$\{([A-Za-z_][A-Za-z0-9_]*)(?:(:-?)[^}]*)?\}/g;
// Use negative lookbehind (?<!\$) to skip escaped $$ (Docker Compose escape syntax)
const regex = /(?<!\$)\$\{([A-Za-z_][A-Za-z0-9_]*)(?:(:-?)[^}]*)?\}/g;
let match;
while ((match = regex.exec(line)) !== null) {
@@ -57,7 +59,8 @@ function extractComposeVars(yaml: string): { required: string[]; optional: strin
}
// Also match $VAR_NAME (simple variable substitution)
const simpleRegex = /\$([A-Za-z_][A-Za-z0-9_]*)(?![{A-Za-z0-9_])/g;
// Use negative lookbehind (?<!\$) to skip escaped $$
const simpleRegex = /(?<!\$)\$([A-Za-z_][A-Za-z0-9_]*)(?![{A-Za-z0-9_])/g;
while ((match = simpleRegex.exec(line)) !== null) {
const varName = match[1];
if (!required.includes(varName) && !optional.includes(varName)) {