AI-admin: bruk Docker Engine API via socket for gateway-restart

docker CLI finnes ikke i web-containeren. Bruker http.request mot
/var/run/docker.sock i stedet for execSync('docker restart ...').

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
vegard 2026-03-16 14:33:36 +01:00
parent c3d81b97fe
commit f7308d21e8

View file

@ -3,7 +3,23 @@ import type { RequestHandler } from './$types';
import { sql } from '$lib/server/db'; import { sql } from '$lib/server/db';
import { writeFileSync } from 'node:fs'; import { writeFileSync } from 'node:fs';
import { join } from 'node:path'; import { join } from 'node:path';
import { execSync } from 'node:child_process'; import http from 'node:http';
/** Docker Engine API via Unix socket */
function dockerRequest(method: string, path: string): Promise<{ status: number; body: string }> {
return new Promise((resolve, reject) => {
const req = http.request(
{ socketPath: '/var/run/docker.sock', path, method, timeout: 30000 },
(res) => {
let body = '';
res.on('data', (chunk: Buffer) => { body += chunk.toString(); });
res.on('end', () => resolve({ status: res.statusCode ?? 0, body }));
}
);
req.on('error', reject);
req.end();
});
}
/** /**
* POST /api/admin/ai/generate-config Generer LiteLLM config.yaml fra PG. * POST /api/admin/ai/generate-config Generer LiteLLM config.yaml fra PG.
@ -70,14 +86,18 @@ export const POST: RequestHandler = async ({ locals, url }) => {
if (shouldRestart) { if (shouldRestart) {
try { try {
// Finn container med "ai-gateway" i navnet // Docker Engine API via Unix socket — ingen docker CLI nødvendig
const name = execSync('docker ps --format "{{.Names}}" | grep ai-gateway', { const listRes = await dockerRequest('GET',
encoding: 'utf-8', '/containers/json?filters=' + encodeURIComponent(JSON.stringify({ name: ['ai-gateway'] }))
timeout: 5000 );
}).trim(); if (listRes.status !== 200) throw new Error(`Docker list feilet: ${listRes.status}`);
const containers = JSON.parse(listRes.body) as Array<{ Id: string }>;
if (name) { if (containers.length > 0) {
execSync(`docker restart ${name}`, { timeout: 30000 }); const restartRes = await dockerRequest('POST',
`/containers/${containers[0].Id}/restart?t=10`
);
if (restartRes.status !== 204) throw new Error(`Docker restart feilet: ${restartRes.status}`);
restarted = true; restarted = true;
} else { } else {
restartError = 'ai-gateway container ikke funnet'; restartError = 'ai-gateway container ikke funnet';