dockge/backend/agent-socket-handlers/docker-socket-handler.ts

278 lines
9.2 KiB
TypeScript
Raw Normal View History

2023-12-25 20:12:44 +00:00
import { AgentSocketHandler } from "../agent-socket-handler";
2023-11-11 14:18:37 +00:00
import { DockgeServer } from "../dockge-server";
2023-12-25 20:12:44 +00:00
import { callbackError, callbackResult, checkLogin, DockgeSocket, ValidationError } from "../util-server";
2023-11-11 14:18:37 +00:00
import { Stack } from "../stack";
2023-12-25 20:12:44 +00:00
import { AgentSocket } from "../../common/agent-socket";
2023-11-11 14:18:37 +00:00
2023-12-25 20:12:44 +00:00
export class DockerSocketHandler extends AgentSocketHandler {
create(socket : DockgeSocket, server : DockgeServer, agentSocket : AgentSocket) {
// Do not call super.create()
2023-11-11 14:18:37 +00:00
2023-12-25 20:12:44 +00:00
agentSocket.on("deployStack", async (name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
checkLogin(socket);
2023-12-25 20:12:44 +00:00
const stack = await this.saveStack(server, name, composeYAML, composeENV, isAdd);
2023-11-11 14:18:37 +00:00
await stack.deploy(socket);
server.sendStackList();
2023-12-25 20:12:44 +00:00
callbackResult({
2023-11-11 14:18:37 +00:00
ok: true,
msg: "Deployed",
2024-10-13 16:51:59 +00:00
msgi18n: true,
2023-12-25 20:12:44 +00:00
}, callback);
2023-11-11 14:18:37 +00:00
stack.joinCombinedTerminal(socket);
} catch (e) {
callbackError(e, callback);
}
});
2023-12-25 20:12:44 +00:00
agentSocket.on("saveStack", async (name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
checkLogin(socket);
2023-12-25 20:12:44 +00:00
await this.saveStack(server, name, composeYAML, composeENV, isAdd);
callbackResult({
2023-11-11 14:18:37 +00:00
ok: true,
2024-10-13 16:51:59 +00:00
msg: "Saved",
msgi18n: true,
2023-12-25 20:12:44 +00:00
}, callback);
2023-11-11 14:18:37 +00:00
server.sendStackList();
} catch (e) {
callbackError(e, callback);
}
});
2023-12-25 20:12:44 +00:00
agentSocket.on("deleteStack", async (name : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
checkLogin(socket);
if (typeof(name) !== "string") {
throw new ValidationError("Name must be a string");
}
const stack = await Stack.getStack(server, name);
2023-11-11 14:18:37 +00:00
try {
await stack.delete(socket);
} catch (e) {
server.sendStackList();
throw e;
}
server.sendStackList();
2023-12-25 20:12:44 +00:00
callbackResult({
2023-11-11 14:18:37 +00:00
ok: true,
2024-10-13 16:51:59 +00:00
msg: "Deleted",
msgi18n: true,
2023-12-25 20:12:44 +00:00
}, callback);
2023-11-11 14:18:37 +00:00
} catch (e) {
callbackError(e, callback);
}
});
2023-12-25 20:12:44 +00:00
agentSocket.on("getStack", async (stackName : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
checkLogin(socket);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
2023-11-11 14:18:37 +00:00
if (stack.isManagedByDockge) {
stack.joinCombinedTerminal(socket);
}
2023-11-11 14:18:37 +00:00
2023-12-25 20:12:44 +00:00
callbackResult({
2023-11-11 14:18:37 +00:00
ok: true,
2023-12-25 20:12:44 +00:00
stack: await stack.toJSON(socket.endpoint),
}, callback);
2023-11-11 14:18:37 +00:00
} catch (e) {
callbackError(e, callback);
}
});
// requestStackList
2023-12-25 20:12:44 +00:00
agentSocket.on("requestStackList", async (callback) => {
2023-11-11 14:18:37 +00:00
try {
checkLogin(socket);
server.sendStackList();
2023-12-25 20:12:44 +00:00
callbackResult({
2023-11-11 14:18:37 +00:00
ok: true,
2024-10-13 16:51:59 +00:00
msg: "Updated",
msgi18n: true,
2023-12-25 20:12:44 +00:00
}, callback);
2023-11-11 14:18:37 +00:00
} catch (e) {
callbackError(e, callback);
}
});
// startStack
2023-12-25 20:12:44 +00:00
agentSocket.on("startStack", async (stackName : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
checkLogin(socket);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
2023-11-11 14:18:37 +00:00
await stack.start(socket);
2023-12-25 20:12:44 +00:00
callbackResult({
2023-11-11 14:18:37 +00:00
ok: true,
2024-10-13 16:51:59 +00:00
msg: "Started",
msgi18n: true,
2023-12-25 20:12:44 +00:00
}, callback);
2023-11-11 14:18:37 +00:00
server.sendStackList();
stack.joinCombinedTerminal(socket);
} catch (e) {
callbackError(e, callback);
}
});
// stopStack
2023-12-25 20:12:44 +00:00
agentSocket.on("stopStack", async (stackName : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
checkLogin(socket);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
2023-11-11 14:18:37 +00:00
await stack.stop(socket);
2023-12-25 20:12:44 +00:00
callbackResult({
2023-11-11 14:18:37 +00:00
ok: true,
2024-10-13 16:51:59 +00:00
msg: "Stopped",
msgi18n: true,
2023-12-25 20:12:44 +00:00
}, callback);
2023-11-11 14:18:37 +00:00
server.sendStackList();
} catch (e) {
callbackError(e, callback);
}
});
// restartStack
2023-12-25 20:12:44 +00:00
agentSocket.on("restartStack", async (stackName : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
checkLogin(socket);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
2023-11-11 14:18:37 +00:00
await stack.restart(socket);
2023-12-25 20:12:44 +00:00
callbackResult({
2023-11-11 14:18:37 +00:00
ok: true,
2024-10-13 16:51:59 +00:00
msg: "Restarted",
msgi18n: true,
2023-12-25 20:12:44 +00:00
}, callback);
2023-11-11 14:18:37 +00:00
server.sendStackList();
} catch (e) {
callbackError(e, callback);
}
});
// updateStack
2023-12-25 20:12:44 +00:00
agentSocket.on("updateStack", async (stackName : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
checkLogin(socket);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
2023-11-11 14:18:37 +00:00
await stack.update(socket);
2023-12-25 20:12:44 +00:00
callbackResult({
2023-11-11 14:18:37 +00:00
ok: true,
2024-10-13 16:51:59 +00:00
msg: "Updated",
msgi18n: true,
2023-12-25 20:12:44 +00:00
}, callback);
2023-11-11 14:18:37 +00:00
server.sendStackList();
} catch (e) {
callbackError(e, callback);
}
});
2023-11-21 10:17:11 +00:00
// down stack
2023-12-25 20:12:44 +00:00
agentSocket.on("downStack", async (stackName : unknown, callback) => {
2023-11-21 10:17:11 +00:00
try {
checkLogin(socket);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
2023-11-21 10:17:11 +00:00
await stack.down(socket);
2023-12-25 20:12:44 +00:00
callbackResult({
2023-11-21 10:17:11 +00:00
ok: true,
2024-10-13 16:51:59 +00:00
msg: "Downed",
msgi18n: true,
2023-12-25 20:12:44 +00:00
}, callback);
2023-11-21 10:17:11 +00:00
server.sendStackList();
} catch (e) {
callbackError(e, callback);
}
});
2023-11-11 14:18:37 +00:00
// Services status
2023-12-25 20:12:44 +00:00
agentSocket.on("serviceStatusList", async (stackName : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
checkLogin(socket);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName, true);
2023-11-11 14:18:37 +00:00
const serviceStatusList = Object.fromEntries(await stack.getServiceStatusList());
2023-12-25 20:12:44 +00:00
callbackResult({
2023-11-11 14:18:37 +00:00
ok: true,
serviceStatusList,
2023-12-25 20:12:44 +00:00
}, callback);
2023-11-11 14:18:37 +00:00
} catch (e) {
callbackError(e, callback);
}
});
// getExternalNetworkList
2023-12-25 20:12:44 +00:00
agentSocket.on("getDockerNetworkList", async (callback) => {
2023-11-11 14:18:37 +00:00
try {
checkLogin(socket);
2023-12-04 18:41:25 +00:00
const dockerNetworkList = await server.getDockerNetworkList();
2023-12-25 20:12:44 +00:00
callbackResult({
2023-11-11 14:18:37 +00:00
ok: true,
dockerNetworkList,
2023-12-25 20:12:44 +00:00
}, callback);
2023-11-11 14:18:37 +00:00
} catch (e) {
callbackError(e, callback);
}
});
}
2023-12-25 20:12:44 +00:00
async saveStack(server : DockgeServer, name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown) : Promise<Stack> {
2023-11-11 14:18:37 +00:00
// Check types
if (typeof(name) !== "string") {
throw new ValidationError("Name must be a string");
}
if (typeof(composeYAML) !== "string") {
throw new ValidationError("Compose YAML must be a string");
}
if (typeof(composeENV) !== "string") {
throw new ValidationError("Compose ENV must be a string");
}
2023-11-11 14:18:37 +00:00
if (typeof(isAdd) !== "boolean") {
throw new ValidationError("isAdd must be a boolean");
}
const stack = new Stack(server, name, composeYAML, composeENV, false);
await stack.save(isAdd);
2023-11-11 14:18:37 +00:00
return stack;
}
}