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";
|
2024-03-30 20:40:58 +00:00
|
|
|
import { Terminal } from "../terminal";
|
|
|
|
import { getComposeTerminalName } from "../../common/util-common";
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-30 20:40:58 +00:00
|
|
|
agentSocket.on("gitDeployStack", async (stackName : unknown, gitUrl : unknown, branch : unknown, isAdd : unknown, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
|
|
|
if (typeof(stackName) !== "string") {
|
|
|
|
throw new ValidationError("Stack name must be a string");
|
|
|
|
}
|
|
|
|
if (typeof(gitUrl) !== "string") {
|
|
|
|
throw new ValidationError("Git URL must be a string");
|
|
|
|
}
|
|
|
|
if (typeof(branch) !== "string") {
|
|
|
|
throw new ValidationError("Git Ref must be a string");
|
|
|
|
}
|
|
|
|
|
|
|
|
const terminalName = getComposeTerminalName(socket.endpoint, stackName);
|
|
|
|
|
|
|
|
// TODO: this could be done smarter.
|
|
|
|
if (!isAdd) {
|
|
|
|
const stack = await Stack.getStack(server, stackName);
|
|
|
|
await stack.delete(socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
let exitCode = await Terminal.exec(server, socket, terminalName, "git", [ "clone", "-b", branch, gitUrl, stackName ], server.stacksDir);
|
|
|
|
if (exitCode !== 0) {
|
|
|
|
throw new Error("Failed to clone git repo");
|
|
|
|
}
|
|
|
|
|
|
|
|
const stack = await Stack.getStack(server, stackName);
|
|
|
|
await stack.deploy(socket);
|
|
|
|
|
|
|
|
server.sendStackList();
|
|
|
|
callbackResult({
|
|
|
|
ok: true,
|
|
|
|
msg: "Deployed"
|
|
|
|
}, callback);
|
|
|
|
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");
|
|
|
|
}
|
2023-12-03 13:30:50 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2023-12-03 13:30:50 +00:00
|
|
|
const stack = await Stack.getStack(server, stackName);
|
2023-11-11 14:18:37 +00:00
|
|
|
|
2023-11-24 18:04:16 +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");
|
|
|
|
}
|
|
|
|
|
2023-12-03 13:30:50 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2023-12-03 13:30:50 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2023-12-03 13:30:50 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2023-12-03 13:30:50 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-30 20:40:58 +00:00
|
|
|
// gitSync
|
|
|
|
agentSocket.on("gitSync", async (stackName : unknown, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
|
|
|
if (typeof(stackName) !== "string") {
|
|
|
|
throw new ValidationError("Stack name must be a string");
|
|
|
|
}
|
|
|
|
|
|
|
|
const stack = await Stack.getStack(server, stackName);
|
|
|
|
await stack.gitSync(socket);
|
|
|
|
callbackResult({
|
|
|
|
ok: true,
|
|
|
|
msg: "Synced"
|
|
|
|
}, callback);
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2023-12-03 13:30:50 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2023-12-03 13:30:50 +00:00
|
|
|
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");
|
|
|
|
}
|
2023-12-03 10:12:54 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2023-12-03 10:12:54 +00:00
|
|
|
const stack = new Stack(server, name, composeYAML, composeENV, false);
|
2023-12-04 10:40:37 +00:00
|
|
|
await stack.save(isAdd);
|
2023-11-11 14:18:37 +00:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|