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

269 lines
8.7 KiB
TypeScript
Raw Normal View History

2023-12-19 17:48:20 +00:00
import { AgentSocketHandler } from "../agent-socket-handler";
2023-11-11 14:18:37 +00:00
import { DockgeServer } from "../dockge-server";
import { callbackError, checkLogin, DockgeSocket, ValidationError } from "../util-server";
import { Stack } from "../stack";
2023-12-19 17:48:20 +00:00
import { AgentSocket } from "../../common/agent-socket";
2023-11-11 14:18:37 +00:00
2023-12-19 17:48:20 +00:00
export class DockerSocketHandler extends AgentSocketHandler {
2023-12-20 15:34:17 +00:00
create(socket : DockgeSocket, server : DockgeServer, agentSocket : AgentSocket) {
2023-12-19 17:48:20 +00:00
// Do not call super.create()
2023-11-11 14:18:37 +00:00
2023-12-19 17:48:20 +00:00
agentSocket.on("deployStack", async (name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
2023-12-20 15:34:17 +00:00
checkLogin(socket);
2023-12-20 02:11:00 +00:00
const stack = await this.saveStack(server, name, composeYAML, composeENV, isAdd);
2023-12-20 15:34:17 +00:00
await stack.deploy(socket);
2023-11-11 14:18:37 +00:00
server.sendStackList();
callback({
ok: true,
msg: "Deployed",
});
2023-12-20 15:34:17 +00:00
stack.joinCombinedTerminal(socket);
2023-11-11 14:18:37 +00:00
} catch (e) {
callbackError(e, callback);
}
});
2023-12-19 17:48:20 +00:00
agentSocket.on("saveStack", async (name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
2023-12-20 15:34:17 +00:00
checkLogin(socket);
2023-12-20 02:11:00 +00:00
this.saveStack(server, name, composeYAML, composeENV, isAdd);
2023-11-11 14:18:37 +00:00
callback({
ok: true,
"msg": "Saved"
});
server.sendStackList();
} catch (e) {
callbackError(e, callback);
}
});
2023-12-19 17:48:20 +00:00
agentSocket.on("deleteStack", async (name : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
2023-12-20 15:34:17 +00:00
checkLogin(socket);
2023-11-11 14:18:37 +00:00
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 {
2023-12-20 15:34:17 +00:00
await stack.delete(socket);
2023-11-11 14:18:37 +00:00
} catch (e) {
server.sendStackList();
throw e;
}
server.sendStackList();
callback({
ok: true,
msg: "Deleted"
});
} catch (e) {
callbackError(e, callback);
}
});
2023-12-19 17:48:20 +00:00
agentSocket.on("getStack", async (stackName : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
2023-12-20 15:34:17 +00:00
checkLogin(socket);
2023-11-11 14:18:37 +00:00
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) {
2023-12-20 15:34:17 +00:00
stack.joinCombinedTerminal(socket);
}
2023-11-11 14:18:37 +00:00
callback({
ok: true,
2023-12-20 15:34:17 +00:00
stack: stack.toJSON(socket.endpoint),
2023-11-11 14:18:37 +00:00
});
} catch (e) {
callbackError(e, callback);
}
});
// requestStackList
2023-12-19 17:48:20 +00:00
agentSocket.on("requestStackList", async (callback) => {
2023-11-11 14:18:37 +00:00
try {
2023-12-20 15:34:17 +00:00
checkLogin(socket);
2023-11-11 14:18:37 +00:00
server.sendStackList();
callback({
ok: true,
msg: "Updated"
});
} catch (e) {
callbackError(e, callback);
}
});
// startStack
2023-12-19 17:48:20 +00:00
agentSocket.on("startStack", async (stackName : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
2023-12-20 15:34:17 +00:00
checkLogin(socket);
2023-11-11 14:18:37 +00:00
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
2023-12-20 15:34:17 +00:00
await stack.start(socket);
2023-11-11 14:18:37 +00:00
callback({
ok: true,
msg: "Started"
});
server.sendStackList();
2023-12-20 15:34:17 +00:00
stack.joinCombinedTerminal(socket);
2023-11-11 14:18:37 +00:00
} catch (e) {
callbackError(e, callback);
}
});
// stopStack
2023-12-19 17:48:20 +00:00
agentSocket.on("stopStack", async (stackName : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
2023-12-20 15:34:17 +00:00
checkLogin(socket);
2023-11-11 14:18:37 +00:00
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
2023-12-20 15:34:17 +00:00
await stack.stop(socket);
2023-11-11 14:18:37 +00:00
callback({
ok: true,
msg: "Stopped"
});
server.sendStackList();
} catch (e) {
callbackError(e, callback);
}
});
// restartStack
2023-12-19 17:48:20 +00:00
agentSocket.on("restartStack", async (stackName : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
2023-12-20 15:34:17 +00:00
checkLogin(socket);
2023-11-11 14:18:37 +00:00
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
2023-12-20 15:34:17 +00:00
await stack.restart(socket);
2023-11-11 14:18:37 +00:00
callback({
ok: true,
msg: "Restarted"
});
server.sendStackList();
} catch (e) {
callbackError(e, callback);
}
});
// updateStack
2023-12-19 17:48:20 +00:00
agentSocket.on("updateStack", async (stackName : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
2023-12-20 15:34:17 +00:00
checkLogin(socket);
2023-11-11 14:18:37 +00:00
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
2023-12-20 15:34:17 +00:00
await stack.update(socket);
2023-11-11 14:18:37 +00:00
callback({
ok: true,
msg: "Updated"
});
server.sendStackList();
} catch (e) {
callbackError(e, callback);
}
});
2023-11-21 10:17:11 +00:00
// down stack
2023-12-19 17:48:20 +00:00
agentSocket.on("downStack", async (stackName : unknown, callback) => {
2023-11-21 10:17:11 +00:00
try {
2023-12-20 15:34:17 +00:00
checkLogin(socket);
2023-11-21 10:17:11 +00:00
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
2023-12-20 15:34:17 +00:00
await stack.down(socket);
2023-11-21 10:17:11 +00:00
callback({
ok: true,
msg: "Downed"
});
server.sendStackList();
} catch (e) {
callbackError(e, callback);
}
});
2023-11-11 14:18:37 +00:00
// Services status
2023-12-19 17:48:20 +00:00
agentSocket.on("serviceStatusList", async (stackName : unknown, callback) => {
2023-11-11 14:18:37 +00:00
try {
2023-12-20 15:34:17 +00:00
checkLogin(socket);
2023-11-11 14:18:37 +00:00
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());
callback({
ok: true,
serviceStatusList,
});
} catch (e) {
callbackError(e, callback);
}
});
// getExternalNetworkList
2023-12-19 17:48:20 +00:00
agentSocket.on("getDockerNetworkList", async (callback) => {
2023-11-11 14:18:37 +00:00
try {
2023-12-20 15:34:17 +00:00
checkLogin(socket);
2023-12-04 18:41:25 +00:00
const dockerNetworkList = await server.getDockerNetworkList();
2023-11-11 14:18:37 +00:00
callback({
ok: true,
dockerNetworkList,
});
} catch (e) {
callbackError(e, callback);
}
});
}
2023-12-20 02:11:00 +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;
}
}