mirror of
https://github.com/khoaliber/dockhand.git
synced 2026-03-06 13:21:53 +00:00
Initial commit
This commit is contained in:
40
routes/api/auto-update/+server.ts
Normal file
40
routes/api/auto-update/+server.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { getAutoUpdateSettings } from '$lib/server/db';
|
||||
|
||||
/**
|
||||
* Batch endpoint to get all auto-update settings for an environment.
|
||||
* Returns a map of containerName -> settings for efficient lookup.
|
||||
*/
|
||||
export const GET: RequestHandler = async ({ url }) => {
|
||||
try {
|
||||
const envIdParam = url.searchParams.get('env');
|
||||
const envId = envIdParam ? parseInt(envIdParam) : undefined;
|
||||
|
||||
const settings = await getAutoUpdateSettings(envId);
|
||||
|
||||
// Convert to a map keyed by container name for efficient frontend lookup
|
||||
const settingsMap: Record<string, {
|
||||
enabled: boolean;
|
||||
scheduleType: string;
|
||||
cronExpression: string | null;
|
||||
vulnerabilityCriteria: string;
|
||||
}> = {};
|
||||
|
||||
for (const setting of settings) {
|
||||
if (setting.enabled) {
|
||||
settingsMap[setting.containerName] = {
|
||||
enabled: setting.enabled,
|
||||
scheduleType: setting.scheduleType,
|
||||
cronExpression: setting.cronExpression,
|
||||
vulnerabilityCriteria: setting.vulnerabilityCriteria || 'never'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return json(settingsMap);
|
||||
} catch (error) {
|
||||
console.error('Failed to get auto-update settings:', error);
|
||||
return json({ error: 'Failed to get auto-update settings' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
126
routes/api/auto-update/[containerName]/+server.ts
Normal file
126
routes/api/auto-update/[containerName]/+server.ts
Normal file
@@ -0,0 +1,126 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import {
|
||||
getAutoUpdateSetting,
|
||||
upsertAutoUpdateSetting,
|
||||
deleteAutoUpdateSetting,
|
||||
deleteAutoUpdateSchedule
|
||||
} from '$lib/server/db';
|
||||
import { registerSchedule, unregisterSchedule } from '$lib/server/scheduler';
|
||||
|
||||
export const GET: RequestHandler = async ({ params, url }) => {
|
||||
try {
|
||||
const containerName = decodeURIComponent(params.containerName);
|
||||
const envIdParam = url.searchParams.get('env');
|
||||
const envId = envIdParam ? parseInt(envIdParam) : undefined;
|
||||
|
||||
const setting = await getAutoUpdateSetting(containerName, envId);
|
||||
|
||||
if (!setting) {
|
||||
return json({
|
||||
enabled: false,
|
||||
scheduleType: 'daily',
|
||||
cronExpression: '0 3 * * *',
|
||||
vulnerabilityCriteria: 'never'
|
||||
});
|
||||
}
|
||||
|
||||
// Return with camelCase keys
|
||||
return json({
|
||||
...setting,
|
||||
scheduleType: setting.scheduleType,
|
||||
cronExpression: setting.cronExpression,
|
||||
vulnerabilityCriteria: setting.vulnerabilityCriteria || 'never'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to get auto-update setting:', error);
|
||||
return json({ error: 'Failed to get auto-update setting' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
export const POST: RequestHandler = async ({ params, url, request }) => {
|
||||
try {
|
||||
const containerName = decodeURIComponent(params.containerName);
|
||||
const envIdParam = url.searchParams.get('env');
|
||||
const envId = envIdParam ? parseInt(envIdParam) : undefined;
|
||||
|
||||
const body = await request.json();
|
||||
// Accept both camelCase and snake_case for backward compatibility
|
||||
const enabled = body.enabled;
|
||||
const cronExpression = body.cronExpression ?? body.cron_expression;
|
||||
const vulnerabilityCriteria = body.vulnerabilityCriteria ?? body.vulnerability_criteria;
|
||||
|
||||
// Hard delete when disabled
|
||||
if (enabled === false) {
|
||||
await deleteAutoUpdateSchedule(containerName, envId);
|
||||
return json({ success: true, deleted: true });
|
||||
}
|
||||
|
||||
// Auto-detect schedule type from cron expression for backward compatibility
|
||||
let scheduleType: 'daily' | 'weekly' | 'custom' = 'custom';
|
||||
if (cronExpression) {
|
||||
const parts = cronExpression.split(' ');
|
||||
if (parts.length >= 5) {
|
||||
const [, , day, month, dow] = parts;
|
||||
if (dow !== '*' && day === '*' && month === '*') {
|
||||
scheduleType = 'weekly';
|
||||
} else if (day === '*' && month === '*' && dow === '*') {
|
||||
scheduleType = 'daily';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const setting = await upsertAutoUpdateSetting(
|
||||
containerName,
|
||||
{
|
||||
enabled: Boolean(enabled),
|
||||
scheduleType: scheduleType,
|
||||
cronExpression: cronExpression || null,
|
||||
vulnerabilityCriteria: vulnerabilityCriteria || 'never'
|
||||
},
|
||||
envId
|
||||
);
|
||||
|
||||
// Register or unregister schedule with croner
|
||||
if (setting.enabled && setting.cronExpression) {
|
||||
await registerSchedule(setting.id, 'container_update', setting.environmentId);
|
||||
} else {
|
||||
unregisterSchedule(setting.id, 'container_update');
|
||||
}
|
||||
|
||||
// Return with camelCase keys
|
||||
return json({
|
||||
...setting,
|
||||
scheduleType: setting.scheduleType,
|
||||
cronExpression: setting.cronExpression,
|
||||
vulnerabilityCriteria: setting.vulnerabilityCriteria || 'never'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to save auto-update setting:', error);
|
||||
return json({ error: 'Failed to save auto-update setting' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
export const DELETE: RequestHandler = async ({ params, url }) => {
|
||||
try {
|
||||
const containerName = decodeURIComponent(params.containerName);
|
||||
const envIdParam = url.searchParams.get('env');
|
||||
const envId = envIdParam ? parseInt(envIdParam) : undefined;
|
||||
|
||||
// Get the setting ID before deleting
|
||||
const setting = await getAutoUpdateSetting(containerName, envId);
|
||||
const settingId = setting?.id;
|
||||
|
||||
const deleted = await deleteAutoUpdateSetting(containerName, envId);
|
||||
|
||||
// Unregister schedule from croner
|
||||
if (deleted && settingId) {
|
||||
unregisterSchedule(settingId, 'container_update');
|
||||
}
|
||||
|
||||
return json({ success: deleted });
|
||||
} catch (error) {
|
||||
console.error('Failed to delete auto-update setting:', error);
|
||||
return json({ error: 'Failed to delete auto-update setting' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user