Runtimes: Docker connection via socket proxy, runtime images, host info
- docker-proxy service (tecnativa/docker-socket-proxy) in both compose files; worker uses it via DOCKER_HOST with a dependency-free client. - runtimes table with presets (llama.cpp CPU, vLLM CPU) and custom images; validation of image refs, env, args, devices (/dev/dri, /dev/kfd). - Worker reports Docker/CPU/memory facts and detects its network and models mount; pulls images with progress; image state per runtime. - Settings -> Runtimes page with host card, list, editor, pull/update. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
76683ffd7b
commit
b68b4a2b23
23 files changed
+2622
-2
No files matched your search
@@ -0,0 +1,97 @@
|
||||
import "server-only";
|
||||
import { asc, eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { type DockerHostInfo, runtimes, workerState } from "@/lib/db/schema";
|
||||
import { type RuntimeInput, RUNTIME_PRESETS, RuntimeValidationError, validateRuntime } from "@/lib/runtimes-shared";
|
||||
|
||||
export type Runtime = typeof runtimes.$inferSelect;
|
||||
|
||||
/** Request body for creating/updating a runtime (validated further by validateRuntime). */
|
||||
export const RuntimeBody = z.object({
|
||||
name: z.string().max(200),
|
||||
engine: z.enum(["llama.cpp", "vllm"]),
|
||||
image: z.string().max(512),
|
||||
description: z.string().max(2000).nullable().optional(),
|
||||
env: z.record(z.string(), z.string().max(4000)).optional(),
|
||||
extraArgs: z.array(z.string().max(4000)).max(200).optional(),
|
||||
devices: z.array(z.string().max(200)).max(20).optional(),
|
||||
shmSizeMb: z.number().int().nullable().optional(),
|
||||
numaTuning: z.boolean().optional(),
|
||||
});
|
||||
|
||||
export class RuntimeError extends Error {
|
||||
constructor(
|
||||
public code: "invalid" | "not_found",
|
||||
message: string,
|
||||
public status: number,
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
function validated(input: RuntimeInput) {
|
||||
try {
|
||||
return validateRuntime(input);
|
||||
} catch (err) {
|
||||
if (err instanceof RuntimeValidationError) throw new RuntimeError("invalid", err.message, 400);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function listRuntimes(): Promise<Runtime[]> {
|
||||
return db().select().from(runtimes).orderBy(asc(runtimes.createdAt));
|
||||
}
|
||||
|
||||
export async function getRuntime(id: string): Promise<Runtime> {
|
||||
const r = await db().query.runtimes.findFirst({ where: eq(runtimes.id, id) });
|
||||
if (!r) throw new RuntimeError("not_found", "Runtime not found.", 404);
|
||||
return r;
|
||||
}
|
||||
|
||||
export async function createRuntime(input: RuntimeInput & { preset?: string }): Promise<Runtime> {
|
||||
const v = validated(input);
|
||||
const preset = input.preset && RUNTIME_PRESETS.some((p) => p.id === input.preset) ? input.preset : null;
|
||||
const [row] = await db()
|
||||
.insert(runtimes)
|
||||
.values({ ...v, preset })
|
||||
.returning();
|
||||
return row;
|
||||
}
|
||||
|
||||
export async function updateRuntime(id: string, input: RuntimeInput): Promise<Runtime> {
|
||||
const current = await getRuntime(id);
|
||||
const v = validated(input);
|
||||
const imageChanged = v.image !== current.image;
|
||||
const [row] = await db()
|
||||
.update(runtimes)
|
||||
.set({
|
||||
...v,
|
||||
// A different image has to be looked up (and maybe pulled) again.
|
||||
...(imageChanged ? { imageState: "unknown", imageId: null, imageSize: null, imageCreated: null, pullError: null } : {}),
|
||||
})
|
||||
.where(eq(runtimes.id, id))
|
||||
.returning();
|
||||
return row;
|
||||
}
|
||||
|
||||
export async function deleteRuntime(id: string): Promise<void> {
|
||||
await getRuntime(id);
|
||||
await db().delete(runtimes).where(eq(runtimes.id, id));
|
||||
}
|
||||
|
||||
/** Ask the worker to pull (or re-pull, to update a moving tag) the runtime's image. */
|
||||
export async function requestPull(id: string): Promise<void> {
|
||||
await getRuntime(id);
|
||||
await db().update(runtimes).set({ pullRequestedAt: new Date(), pullError: null }).where(eq(runtimes.id, id));
|
||||
}
|
||||
|
||||
/** Docker facts as last reported by the worker; null when DOCKER_HOST isn't configured. */
|
||||
export async function getDockerInfo(): Promise<DockerHostInfo | null> {
|
||||
try {
|
||||
const row = await db().query.workerState.findFirst({ columns: { docker: true }, where: eq(workerState.id, "worker") });
|
||||
return row?.docker ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user