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

206 lines
6.3 KiB
TypeScript
Raw Normal View History

2023-10-23 11:30:58 +00:00
import { SocketHandler } from "../socket-handler.js";
import { DockgeServer } from "../dockge-server";
2023-10-26 05:23:45 +00:00
import { callbackError, checkLogin, DockgeSocket, ValidationError } from "../util-server";
import { Stack } from "../stack";
2023-10-23 11:30:58 +00:00
export class DockerSocketHandler extends SocketHandler {
create(socket : DockgeSocket, server : DockgeServer) {
2023-10-26 05:23:45 +00:00
socket.on("deployStack", async (name : unknown, composeYAML : unknown, isAdd : unknown, callback) => {
2023-10-23 11:30:58 +00:00
try {
checkLogin(socket);
2023-10-26 05:23:45 +00:00
const stack = this.saveStack(socket, server, name, composeYAML, isAdd);
await stack.deploy(socket);
2023-10-29 07:25:52 +00:00
server.sendStackList();
2023-10-26 05:23:45 +00:00
callback({
ok: true,
2023-11-05 17:18:02 +00:00
msg: "Deployed",
2023-10-23 11:30:58 +00:00
});
2023-10-26 05:23:45 +00:00
} catch (e) {
callbackError(e, callback);
2023-10-23 11:30:58 +00:00
}
});
2023-10-26 05:23:45 +00:00
socket.on("saveStack", async (name : unknown, composeYAML : unknown, isAdd : unknown, callback) => {
2023-10-23 11:30:58 +00:00
try {
checkLogin(socket);
2023-10-26 05:23:45 +00:00
this.saveStack(socket, server, name, composeYAML, isAdd);
2023-10-23 11:30:58 +00:00
callback({
ok: true,
2023-10-26 05:23:45 +00:00
"msg": "Saved"
2023-10-23 11:30:58 +00:00
});
2023-10-29 07:25:52 +00:00
server.sendStackList();
} catch (e) {
callbackError(e, callback);
}
});
socket.on("deleteStack", async (name : unknown, callback) => {
try {
checkLogin(socket);
if (typeof(name) !== "string") {
throw new ValidationError("Name must be a string");
}
const stack = Stack.getStack(server, name);
try {
await stack.delete(socket);
} catch (e) {
server.sendStackList();
throw e;
}
server.sendStackList();
callback({
ok: true,
msg: "Deleted"
});
2023-10-23 11:30:58 +00:00
} catch (e) {
2023-10-26 05:23:45 +00:00
callbackError(e, callback);
}
});
socket.on("getStack", (stackName : unknown, callback) => {
try {
checkLogin(socket);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = Stack.getStack(server, stackName);
2023-11-05 17:18:02 +00:00
2023-11-06 07:15:55 +00:00
stack.joinCombinedTerminal(socket);
2023-11-05 17:18:02 +00:00
2023-10-23 11:30:58 +00:00
callback({
2023-10-26 05:23:45 +00:00
ok: true,
stack: stack.toJSON(),
2023-10-23 11:30:58 +00:00
});
2023-10-26 05:23:45 +00:00
} catch (e) {
callbackError(e, callback);
2023-10-23 11:30:58 +00:00
}
});
2023-11-05 17:18:02 +00:00
// requestStackList
socket.on("requestStackList", async (callback) => {
try {
checkLogin(socket);
server.sendStackList();
callback({
ok: true,
msg: "Updated"
});
} catch (e) {
callbackError(e, callback);
}
});
// startStack
socket.on("startStack", async (stackName : unknown, callback) => {
try {
checkLogin(socket);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = Stack.getStack(server, stackName);
await stack.start(socket);
callback({
ok: true,
msg: "Started"
});
server.sendStackList();
2023-11-06 07:15:55 +00:00
stack.joinCombinedTerminal(socket);
2023-11-05 17:18:02 +00:00
} catch (e) {
callbackError(e, callback);
}
});
// stopStack
socket.on("stopStack", async (stackName : unknown, callback) => {
try {
checkLogin(socket);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = Stack.getStack(server, stackName);
await stack.stop(socket);
callback({
ok: true,
msg: "Stopped"
});
server.sendStackList();
} catch (e) {
callbackError(e, callback);
}
});
// restartStack
socket.on("restartStack", async (stackName : unknown, callback) => {
try {
checkLogin(socket);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = Stack.getStack(server, stackName);
await stack.restart(socket);
callback({
ok: true,
msg: "Restarted"
});
server.sendStackList();
} catch (e) {
callbackError(e, callback);
}
});
// updateStack
socket.on("updateStack", async (stackName : unknown, callback) => {
try {
checkLogin(socket);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = Stack.getStack(server, stackName);
await stack.update(socket);
callback({
ok: true,
msg: "Updated"
});
server.sendStackList();
} catch (e) {
callbackError(e, callback);
}
});
2023-10-23 11:30:58 +00:00
}
2023-10-26 05:23:45 +00:00
saveStack(socket : DockgeSocket, server : DockgeServer, name : unknown, composeYAML : unknown, isAdd : unknown) : Stack {
// 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(isAdd) !== "boolean") {
throw new ValidationError("isAdd must be a boolean");
}
const stack = new Stack(server, name, composeYAML);
stack.save(isAdd);
return stack;
}
2023-11-05 17:18:02 +00:00
2023-10-23 11:30:58 +00:00
}
2023-10-26 05:23:45 +00:00