This commit is contained in:
Louis Lam
2023-12-20 10:11:00 +08:00
parent 0f79b46769
commit d99f21fe93
16 changed files with 185 additions and 125 deletions

View File

@@ -11,14 +11,14 @@ export class DockerSocketHandler extends AgentSocketHandler {
agentSocket.on("deployStack", async (name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown, callback) => {
try {
checkLogin(s);
const stack = await this.saveStack(socket, server, name, composeYAML, composeENV, isAdd);
await stack.deploy(socket);
const stack = await this.saveStack(server, name, composeYAML, composeENV, isAdd);
await stack.deploy(s);
server.sendStackList();
callback({
ok: true,
msg: "Deployed",
});
stack.joinCombinedTerminal(socket);
stack.joinCombinedTerminal(s);
} catch (e) {
callbackError(e, callback);
}
@@ -26,8 +26,8 @@ export class DockerSocketHandler extends AgentSocketHandler {
agentSocket.on("saveStack", async (name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown, callback) => {
try {
checkLogin(socket);
this.saveStack(socket, server, name, composeYAML, composeENV, isAdd);
checkLogin(s);
this.saveStack(server, name, composeYAML, composeENV, isAdd);
callback({
ok: true,
"msg": "Saved"
@@ -40,14 +40,14 @@ export class DockerSocketHandler extends AgentSocketHandler {
agentSocket.on("deleteStack", async (name : unknown, callback) => {
try {
checkLogin(socket);
checkLogin(s);
if (typeof(name) !== "string") {
throw new ValidationError("Name must be a string");
}
const stack = await Stack.getStack(server, name);
try {
await stack.delete(socket);
await stack.delete(s);
} catch (e) {
server.sendStackList();
throw e;
@@ -80,7 +80,7 @@ export class DockerSocketHandler extends AgentSocketHandler {
callback({
ok: true,
stack: stack.toJSON(),
stack: stack.toJSON(s.endpoint),
});
} catch (e) {
callbackError(e, callback);
@@ -90,7 +90,7 @@ export class DockerSocketHandler extends AgentSocketHandler {
// requestStackList
agentSocket.on("requestStackList", async (callback) => {
try {
checkLogin(socket);
checkLogin(s);
server.sendStackList();
callback({
ok: true,
@@ -104,21 +104,21 @@ export class DockerSocketHandler extends AgentSocketHandler {
// startStack
agentSocket.on("startStack", async (stackName : unknown, callback) => {
try {
checkLogin(socket);
checkLogin(s);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
await stack.start(socket);
await stack.start(s);
callback({
ok: true,
msg: "Started"
});
server.sendStackList();
stack.joinCombinedTerminal(socket);
stack.joinCombinedTerminal(s);
} catch (e) {
callbackError(e, callback);
@@ -128,14 +128,14 @@ export class DockerSocketHandler extends AgentSocketHandler {
// stopStack
agentSocket.on("stopStack", async (stackName : unknown, callback) => {
try {
checkLogin(socket);
checkLogin(s);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
await stack.stop(socket);
await stack.stop(s);
callback({
ok: true,
msg: "Stopped"
@@ -170,14 +170,14 @@ export class DockerSocketHandler extends AgentSocketHandler {
// updateStack
agentSocket.on("updateStack", async (stackName : unknown, callback) => {
try {
checkLogin(socket);
checkLogin(s);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
await stack.update(socket);
await stack.update(s);
callback({
ok: true,
msg: "Updated"
@@ -191,14 +191,14 @@ export class DockerSocketHandler extends AgentSocketHandler {
// down stack
agentSocket.on("downStack", async (stackName : unknown, callback) => {
try {
checkLogin(socket);
checkLogin(s);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string");
}
const stack = await Stack.getStack(server, stackName);
await stack.down(socket);
await stack.down(s);
callback({
ok: true,
msg: "Downed"
@@ -232,7 +232,7 @@ export class DockerSocketHandler extends AgentSocketHandler {
// getExternalNetworkList
agentSocket.on("getDockerNetworkList", async (callback) => {
try {
checkLogin(socket);
checkLogin(s);
const dockerNetworkList = await server.getDockerNetworkList();
callback({
ok: true,
@@ -244,7 +244,7 @@ export class DockerSocketHandler extends AgentSocketHandler {
});
}
async saveStack(socket : DockgeSocket, server : DockgeServer, name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown) : Promise<Stack> {
async saveStack(server : DockgeServer, name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown) : Promise<Stack> {
// Check types
if (typeof(name) !== "string") {
throw new ValidationError("Name must be a string");

View File

@@ -1,26 +1,17 @@
import { SocketHandler } from "../socket-handler.js";
import { DockgeServer } from "../dockge-server";
import { callbackError, checkLogin, DockgeSocket, ValidationError } from "../util-server";
import { log } from "../log";
import yaml from "yaml";
import path from "path";
import fs from "fs";
import {
allowedCommandList,
allowedRawKeys,
getComposeTerminalName, getContainerExecTerminalName,
isDev,
PROGRESS_TERMINAL_ROWS
} from "../../common/util-common";
import { InteractiveTerminal, MainTerminal, Terminal } from "../terminal";
import { Stack } from "../stack";
import { AgentSocketHandler } from "../agent-socket-handler";
import { AgentSocket } from "../../common/agent-socket";
export class TerminalSocketHandler extends SocketHandler {
create(socket : DockgeSocket, server : DockgeServer) {
export class TerminalSocketHandler extends AgentSocketHandler {
create(s : DockgeSocket, server : DockgeServer, agentSocket : AgentSocket) {
socket.on("terminalInput", async (terminalName : unknown, cmd : unknown, errorCallback) => {
agentSocket.on("terminalInput", async (terminalName : unknown, cmd : unknown, errorCallback) => {
try {
checkLogin(socket);
checkLogin(s);
if (typeof(terminalName) !== "string") {
throw new Error("Terminal name must be a string.");
@@ -48,9 +39,9 @@ export class TerminalSocketHandler extends SocketHandler {
});
// Main Terminal
socket.on("mainTerminal", async (terminalName : unknown, callback) => {
agentSocket.on("mainTerminal", async (terminalName : unknown, callback) => {
try {
checkLogin(socket);
checkLogin(s);
// TODO: Reset the name here, force one main terminal for now
terminalName = "console";
@@ -81,7 +72,7 @@ export class TerminalSocketHandler extends SocketHandler {
});
// Interactive Terminal for containers
socket.on("interactiveTerminal", async (stackName : unknown, serviceName : unknown, shell : unknown, callback) => {
agentSocket.on("interactiveTerminal", async (stackName : unknown, serviceName : unknown, shell : unknown, callback) => {
try {
checkLogin(socket);
@@ -113,14 +104,14 @@ export class TerminalSocketHandler extends SocketHandler {
});
// Join Output Terminal
socket.on("terminalJoin", async (terminalName : unknown, callback) => {
agentSocket.on("terminalJoin", async (terminalName : unknown, callback) => {
if (typeof(callback) !== "function") {
log.debug("console", "Callback is not a function.");
return;
}
try {
checkLogin(socket);
checkLogin(s);
if (typeof(terminalName) !== "string") {
throw new ValidationError("Terminal name must be a string.");
}
@@ -141,9 +132,9 @@ export class TerminalSocketHandler extends SocketHandler {
});
// Leave Combined Terminal
socket.on("leaveCombinedTerminal", async (stackName : unknown, callback) => {
agentSocket.on("leaveCombinedTerminal", async (stackName : unknown, callback) => {
try {
checkLogin(socket);
checkLogin(s);
log.debug("leaveCombinedTerminal", "Stack name: " + stackName);
@@ -152,7 +143,7 @@ export class TerminalSocketHandler extends SocketHandler {
}
const stack = await Stack.getStack(server, stackName);
await stack.leaveCombinedTerminal(socket);
await stack.leaveCombinedTerminal(s);
callback({
ok: true,
@@ -163,43 +154,39 @@ export class TerminalSocketHandler extends SocketHandler {
});
// Resize Terminal
socket.on(
"terminalResize",
async (terminalName: unknown, rows: unknown, cols: unknown) => {
log.info("terminalResize", `Terminal: ${terminalName}`);
try {
checkLogin(socket);
if (typeof terminalName !== "string") {
throw new Error("Terminal name must be a string.");
}
agentSocket.on("terminalResize", async (terminalName: unknown, rows: unknown, cols: unknown) => {
log.info("terminalResize", `Terminal: ${terminalName}`);
try {
checkLogin(socket);
if (typeof terminalName !== "string") {
throw new Error("Terminal name must be a string.");
}
if (typeof rows !== "number") {
throw new Error("Command must be a number.");
}
if (typeof cols !== "number") {
throw new Error("Command must be a number.");
}
if (typeof rows !== "number") {
throw new Error("Command must be a number.");
}
if (typeof cols !== "number") {
throw new Error("Command must be a number.");
}
let terminal = Terminal.getTerminal(terminalName);
let terminal = Terminal.getTerminal(terminalName);
// log.info("terminal", terminal);
if (terminal instanceof Terminal) {
//log.debug("terminalInput", "Terminal found, writing to terminal.");
terminal.rows = rows;
terminal.cols = cols;
} else {
throw new Error(`${terminalName} Terminal not found.`);
}
} catch (e) {
log.debug(
"terminalResize",
// log.info("terminal", terminal);
if (terminal instanceof Terminal) {
//log.debug("terminalInput", "Terminal found, writing to terminal.");
terminal.rows = rows;
terminal.cols = cols;
} else {
throw new Error(`${terminalName} Terminal not found.`);
}
} catch (e) {
log.debug("terminalResize",
// Added to prevent the lint error when adding the type
// and ts type checker saying type is unknown.
// @ts-ignore
`Error on ${terminalName}: ${e.message}`
);
}
);
}
);
});
}
}

View File

@@ -111,4 +111,11 @@ export class DockgeInstanceManager {
client?.emit("agent", endpoint, eventName, ...args);
}
emitToAllEndpoints(eventName: string, ...args : unknown[]) {
log.debug("INSTANCEMANAGER", "Emitting event to all endpoints");
for (let endpoint in this.instanceSocketList) {
this.emitToEndpoint(endpoint, eventName, ...args);
}
}
}

View File

@@ -25,7 +25,7 @@ import { Arguments, Config, DockgeSocket } from "./util-server";
import { DockerSocketHandler } from "./agent-socket-handlers/docker-socket-handler";
import expressStaticGzip from "express-static-gzip";
import path from "path";
import { TerminalSocketHandler } from "./socket-handlers/terminal-socket-handler";
import { TerminalSocketHandler } from "./agent-socket-handlers/terminal-socket-handler";
import { Stack } from "./stack";
import { Cron } from "croner";
import gracefulShutdown from "http-graceful-shutdown";
@@ -52,17 +52,20 @@ export class DockgeServer {
];
/**
* List of socket handlers
* List of socket handlers (no agent support)
*/
socketHandlerList : SocketHandler[] = [
new MainSocketHandler(),
new TerminalSocketHandler(),
];
agentProxySocketHandler = new AgentProxySocketHandler();
/**
* List of socket handlers (support agent)
*/
agentSocketHandlerList : AgentSocketHandler[] = [
new DockerSocketHandler(),
new TerminalSocketHandler(),
];
/**

View File

@@ -3,6 +3,7 @@ import { DockgeServer } from "../dockge-server";
import { log } from "../log";
import { checkLogin, DockgeSocket } from "../util-server";
import { AgentSocket } from "../../common/agent-socket";
import { ALL_ENDPOINTS } from "../../common/util-common";
export class AgentProxySocketHandler extends SocketHandler {
@@ -14,19 +15,22 @@ export class AgentProxySocketHandler extends SocketHandler {
// Check Type
if (typeof(endpoint) !== "string") {
throw new Error("Endpoint must be a string");
throw new Error("Endpoint must be a string: " + endpoint);
}
if (typeof(eventName) !== "string") {
throw new Error("Event name must be a string");
}
log.debug("agent", "Proxying request to " + endpoint + " for " + eventName);
if (endpoint === ALL_ENDPOINTS) { // Send to all endpoints
log.debug("agent", "Sending to all endpoints: " + eventName);
socket.instanceManager.emitToAllEndpoints(eventName, ...args);
// Direct connection or matching endpoint
if (!endpoint || endpoint === socket.endpoint) {
log.debug("agent", "Direct connection");
} else if (!endpoint || endpoint === socket.endpoint) { // Direct connection or matching endpoint
log.debug("agent", "Matched endpoint: " + eventName);
agentSocket.call(eventName, ...args);
} else {
log.debug("agent", "Proxying request to " + endpoint + " for " + eventName);
socket.instanceManager.emitToEndpoint(endpoint, eventName, ...args);
}
} catch (e) {

View File

@@ -35,6 +35,8 @@ export class Terminal {
public enableKeepAlive : boolean = false;
protected keepAliveInterval? : NodeJS.Timeout;
protected socketList : DockgeSocket[] = [];
constructor(server : DockgeServer, name : string, file : string, args : string | string[], cwd : string) {
this.server = server;
this._name = name;
@@ -87,8 +89,7 @@ export class Terminal {
// Close if there is no clients
this.keepAliveInterval = setInterval(() => {
const clients = this.server.io.sockets.adapter.rooms.get(this.name);
const numClients = clients ? clients.size : 0;
const numClients = this.socketList.length;
if (numClients === 0) {
log.debug("Terminal", "Terminal " + this.name + " has no client, closing...");
@@ -112,8 +113,9 @@ export class Terminal {
// On Data
this._ptyProcess.onData((data) => {
this.buffer.pushItem(data);
if (this.server.io) {
this.server.io.to(this.name).emit("terminalWrite", this.name, data);
for (const socket of this.socketList) {
socket.emitAgent("terminalWrite", this.name, data);
}
});
@@ -137,10 +139,12 @@ export class Terminal {
* @param res
*/
protected exit = (res : {exitCode: number, signal?: number | undefined}) => {
this.server.io.to(this.name).emit("terminalExit", this.name, res.exitCode);
for (const socket of this.socketList) {
socket.emitAgent("terminalExit", this.name, res.exitCode);
}
// Remove room
this.server.io.in(this.name).socketsLeave(this.name);
// Remove all clients
this.socketList = [];
Terminal.terminalMap.delete(this.name);
log.debug("Terminal", "Terminal " + this.name + " exited with code " + res.exitCode);
@@ -157,11 +161,14 @@ export class Terminal {
}
public join(socket : DockgeSocket) {
socket.join(this.name);
this.socketList.push(socket);
}
public leave(socket : DockgeSocket) {
socket.leave(this.name);
const index = this.socketList.indexOf(socket);
if (index !== -1) {
this.socketList.splice(index, 1);
}
}
public get ptyProcess() {