dockge/backend/agent-socket-handlers/resource-socket-handler.ts
Weapie d8fdeba05d
All checks were successful
Docker image / publish (push) Successful in 59s
Improve Dockge workflows and require approval after image scans
Update the shadcn preset and visual Compose editor. Add deployment previews, history, attention notices, and cleanup safeguards. Use the supplied icon in the app header and authentication screens.
2026-11-12 12:15:02 +01:00

48 lines
2.7 KiB
TypeScript

import { AgentSocketHandler } from "../agent-socket-handler";
import type { DockgeServer } from "../dockge-server";
import { callbackError, callbackResult, checkLogin, DockgeSocket, ValidationError } from "../util-server";
import type { AgentSocket } from "../../common/agent-socket";
export class ResourceSocketHandler extends AgentSocketHandler {
create(socket: DockgeSocket, server: DockgeServer, agentSocket: AgentSocket) {
const resources = server.dockerResources;
const on = (event: string, errorKey: string, operation: (...args: unknown[]) => Promise<object>) => {
agentSocket.on(event, async (...args: unknown[]) => {
const callback = args.pop();
try {
checkLogin(socket);
callbackResult({ ok: true,
...await operation(...args) }, callback);
} catch (error) {
callbackError(error instanceof ValidationError ? error : new Error(errorKey), callback);
}
});
};
on("listDockerImages", "resourceLoadFailed", async () => ({ items: await resources.listImages() }));
on("listDockerVolumes", "resourceLoadFailed", async () => ({ items: await resources.listVolumes() }));
on("getResourceCleanup", "resourceLoadFailed", async kind => ({ schedule: await server.resourceCleanup.status(kind) }));
on("setResourceCleanup", "resourceCleanupSaveFailed", async (kind, schedule) => ({
schedule: await server.resourceCleanup.set(kind, schedule),
}));
on("previewDockerCleanup", "resourceCleanupFailed", async kind => ({ preview: await server.resourceCleanup.preview(kind) }));
on("pruneDockerResources", "resourceCleanupFailed", async (kind, id) => ({ result: await server.resourceCleanup.prune(kind, id) }));
on("checkDockerImageTag", "resourceCheckFailed", async (id, tag) => ({ status: await resources.checkImage(id, tag) }));
on("removeDockerImage", "resourceDeleteFailed", async (id, tag) => {
await server.stackDeployments.withImageChanges(async () => {
if (typeof id === "string" && server.stackDeployments.protectedImages().has(id)) {
throw new ValidationError("cleanupProtectedImage");
}
await resources.removeImage(id, tag);
});
return { msg: "resourceDeleted",
msgi18n: true };
});
on("removeDockerVolume", "resourceDeleteFailed", async (name, created) => {
await resources.removeVolume(name, created);
return { msg: "resourceDeleted",
msgi18n: true };
});
}
}