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:
parent
c3d81b97fe
commit
f7308d21e8
1 changed files with 28 additions and 8 deletions
|
|
@ -3,7 +3,23 @@ import type { RequestHandler } from './$types';
|
|||
import { sql } from '$lib/server/db';
|
||||
import { writeFileSync } from 'node:fs';
|
||||
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.
|
||||
|
|
@ -70,14 +86,18 @@ export const POST: RequestHandler = async ({ locals, url }) => {
|
|||
|
||||
if (shouldRestart) {
|
||||
try {
|
||||
// Finn container med "ai-gateway" i navnet
|
||||
const name = execSync('docker ps --format "{{.Names}}" | grep ai-gateway', {
|
||||
encoding: 'utf-8',
|
||||
timeout: 5000
|
||||
}).trim();
|
||||
// Docker Engine API via Unix socket — ingen docker CLI nødvendig
|
||||
const listRes = await dockerRequest('GET',
|
||||
'/containers/json?filters=' + encodeURIComponent(JSON.stringify({ name: ['ai-gateway'] }))
|
||||
);
|
||||
if (listRes.status !== 200) throw new Error(`Docker list feilet: ${listRes.status}`);
|
||||
const containers = JSON.parse(listRes.body) as Array<{ Id: string }>;
|
||||
|
||||
if (name) {
|
||||
execSync(`docker restart ${name}`, { timeout: 30000 });
|
||||
if (containers.length > 0) {
|
||||
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;
|
||||
} else {
|
||||
restartError = 'ai-gateway container ikke funnet';
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue