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 {
create(s : DockgeSocket, server : DockgeServer, agentSocket : AgentSocket) {
// 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-19 17:48:20 +00:00
checkLogin(s);
const stack = await this.saveStack(socket, server, name, composeYAML, composeENV, isAdd);
2023-11-11 14:18:37 +00:00
await stack.deploy(socket);
server.sendStackList();
callback({
ok: true,
msg: "Deployed",
});
stack.joinCombinedTerminal(socket);
} 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 {
checkLogin(socket);
this.saveStack(socket, 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 {
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();
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-19 17:48:20 +00:00
checkLogin(s);
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-19 17:48:20 +00:00
stack.joinCombinedTerminal(s);
}
2023-11-11 14:18:37 +00:00
callback({
ok: true,
stack: stack.toJSON(),
});
} 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 {
checkLogin(socket);
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 {
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);
callback({
ok: true,
msg: "Started"
});
server.sendStackList();
stack.joinCombinedTerminal(socket);
} 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 {
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);
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-19 17:48:20 +00:00
checkLogin(s);
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-19 17:48:20 +00:00
await stack.restart(s);
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 {
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);
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 {
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);
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-19 17:48:20 +00:00
checkLogin(s);
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 {
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);
}
});
}
async saveStack(socket : DockgeSocket, 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;
}
}