This commit is contained in:
Louis Lam 2023-12-25 18:02:46 +08:00
parent 0c32171acc
commit 17f9ee63f7
13 changed files with 137 additions and 76 deletions

View File

@ -147,7 +147,6 @@ export class AgentManager {
client.on("connect_error", (err) => {
log.error("agent-manager", "Error from the socket server: " + endpoint);
log.debug("agent-manager", err);
this.socket.emit("agentStatus", {
endpoint: endpoint,
status: "offline",
@ -163,7 +162,6 @@ export class AgentManager {
});
client.on("agent", (...args : unknown[]) => {
log.debug("agent-manager", "Forward event");
this.socket.emit("agent", ...args);
});
@ -216,11 +214,18 @@ export class AgentManager {
emitToEndpoint(endpoint: string, eventName: string, ...args : unknown[]) {
log.debug("agent-manager", "Emitting event to endpoint: " + endpoint);
let client = this.agentSocketList[endpoint];
if (!client) {
log.error("agent-manager", "Socket client not found for endpoint: " + endpoint);
throw new Error("Socket client not found for endpoint: " + endpoint);
}
client?.emit("agent", endpoint, eventName, ...args);
if (!client.connected) {
log.error("agent-manager", "Socket client not connected for endpoint: " + endpoint);
throw new Error("Socket client not connected for endpoint: " + endpoint);
}
client.emit("agent", endpoint, eventName, ...args);
}
emitToAllEndpoints(eventName: string, ...args : unknown[]) {

View File

@ -1,6 +1,6 @@
import { AgentSocketHandler } from "../agent-socket-handler";
import { DockgeServer } from "../dockge-server";
import { callbackError, checkLogin, DockgeSocket, ValidationError } from "../util-server";
import { callbackError, callbackResult, checkLogin, DockgeSocket, ValidationError } from "../util-server";
import { Stack } from "../stack";
import { AgentSocket } from "../../common/agent-socket";
@ -14,10 +14,10 @@ export class DockerSocketHandler extends AgentSocketHandler {
const stack = await this.saveStack(server, name, composeYAML, composeENV, isAdd);
await stack.deploy(socket);
server.sendStackList();
callback({
callbackResult({
ok: true,
msg: "Deployed",
});
}, callback);
stack.joinCombinedTerminal(socket);
} catch (e) {
callbackError(e, callback);
@ -27,11 +27,11 @@ export class DockerSocketHandler extends AgentSocketHandler {
agentSocket.on("saveStack", async (name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown, callback) => {
try {
checkLogin(socket);
this.saveStack(server, name, composeYAML, composeENV, isAdd);
callback({
await this.saveStack(server, name, composeYAML, composeENV, isAdd);
callbackResult({
ok: true,
"msg": "Saved"
});
}, callback);
server.sendStackList();
} catch (e) {
callbackError(e, callback);
@ -54,10 +54,10 @@ export class DockerSocketHandler extends AgentSocketHandler {
}
server.sendStackList();
callback({
callbackResult({
ok: true,
msg: "Deleted"
});
}, callback);
} catch (e) {
callbackError(e, callback);
@ -78,10 +78,10 @@ export class DockerSocketHandler extends AgentSocketHandler {
stack.joinCombinedTerminal(socket);
}
callback({
callbackResult({
ok: true,
stack: stack.toJSON(socket.endpoint),
});
stack: await stack.toJSON(socket.endpoint),
}, callback);
} catch (e) {
callbackError(e, callback);
}
@ -92,10 +92,10 @@ export class DockerSocketHandler extends AgentSocketHandler {
try {
checkLogin(socket);
server.sendStackList();
callback({
callbackResult({
ok: true,
msg: "Updated"
});
}, callback);
} catch (e) {
callbackError(e, callback);
}

View File

@ -1,5 +1,5 @@
import { DockgeServer } from "../dockge-server";
import { callbackError, checkLogin, DockgeSocket, ValidationError } from "../util-server";
import { callbackError, callbackResult, checkLogin, DockgeSocket, ValidationError } from "../util-server";
import { log } from "../log";
import { InteractiveTerminal, MainTerminal, Terminal } from "../terminal";
import { Stack } from "../stack";
@ -9,7 +9,7 @@ import { AgentSocket } from "../../common/agent-socket";
export class TerminalSocketHandler extends AgentSocketHandler {
create(s : DockgeSocket, server : DockgeServer, agentSocket : AgentSocket) {
agentSocket.on("terminalInput", async (terminalName : unknown, cmd : unknown, errorCallback) => {
agentSocket.on("terminalInput", async (terminalName : unknown, cmd : unknown, callback) => {
try {
checkLogin(s);
@ -29,12 +29,7 @@ export class TerminalSocketHandler extends AgentSocketHandler {
throw new Error("Terminal not found or it is not a Interactive Terminal.");
}
} catch (e) {
if (e instanceof Error) {
errorCallback({
ok: false,
msg: e.message,
});
}
callbackError(e, callback);
}
});
@ -50,22 +45,22 @@ export class TerminalSocketHandler extends AgentSocketHandler {
throw new ValidationError("Terminal name must be a string.");
}
log.debug("deployStack", "Terminal name: " + terminalName);
log.debug("mainTerminal", "Terminal name: " + terminalName);
let terminal = Terminal.getTerminal(terminalName);
if (!terminal) {
terminal = new MainTerminal(server, terminalName);
terminal.rows = 50;
log.debug("deployStack", "Terminal created");
log.debug("mainTerminal", "Terminal created");
}
terminal.join(socket);
terminal.join(s);
terminal.start();
callback({
callbackResult({
ok: true,
});
}, callback);
} catch (e) {
callbackError(e, callback);
}
@ -74,7 +69,7 @@ export class TerminalSocketHandler extends AgentSocketHandler {
// Interactive Terminal for containers
agentSocket.on("interactiveTerminal", async (stackName : unknown, serviceName : unknown, shell : unknown, callback) => {
try {
checkLogin(socket);
checkLogin(s);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string.");
@ -93,11 +88,11 @@ export class TerminalSocketHandler extends AgentSocketHandler {
// Get stack
const stack = await Stack.getStack(server, stackName);
stack.joinContainerTerminal(socket, serviceName, shell);
stack.joinContainerTerminal(s, serviceName, shell);
callback({
callbackResult({
ok: true,
});
}, callback);
} catch (e) {
callbackError(e, callback);
}
@ -145,9 +140,9 @@ export class TerminalSocketHandler extends AgentSocketHandler {
const stack = await Stack.getStack(server, stackName);
await stack.leaveCombinedTerminal(s);
callback({
callbackResult({
ok: true,
});
}, callback);
} catch (e) {
callbackError(e, callback);
}
@ -157,7 +152,7 @@ export class TerminalSocketHandler extends AgentSocketHandler {
agentSocket.on("terminalResize", async (terminalName: unknown, rows: unknown, cols: unknown) => {
log.info("terminalResize", `Terminal: ${terminalName}`);
try {
checkLogin(socket);
checkLogin(s);
if (typeof terminalName !== "string") {
throw new Error("Terminal name must be a string.");
}

View File

@ -18,6 +18,7 @@ import {
} from "../common/util-common";
import { InteractiveTerminal, Terminal } from "./terminal";
import childProcessAsync from "promisify-child-process";
import { Settings } from "./settings";
export class Stack {
@ -50,12 +51,30 @@ export class Stack {
}
}
toJSON(endpoint : string) : object {
async toJSON(endpoint : string) : Promise<object> {
// Since we have multiple agents now, embed primary hostname in the stack object too.
let primaryHostname = await Settings.get("primaryHostname");
if (!primaryHostname) {
if (!endpoint) {
primaryHostname = "localhost";
} else {
// Use the endpoint as the primary hostname
try {
primaryHostname = (new URL("https://" + endpoint).hostname);
} catch (e) {
// Just in case if the endpoint is in a incorrect format
primaryHostname = "localhost";
}
}
}
let obj = this.toSimpleJSON(endpoint);
return {
...obj,
composeYAML: this.composeYAML,
composeENV: this.composeENV,
primaryHostname,
};
}

View File

@ -35,7 +35,7 @@ export class Terminal {
public enableKeepAlive : boolean = false;
protected keepAliveInterval? : NodeJS.Timeout;
protected socketList : DockgeSocket[] = [];
protected socketList : Record<string, DockgeSocket> = {};
constructor(server : DockgeServer, name : string, file : string, args : string | string[], cwd : string) {
this.server = server;
@ -89,7 +89,7 @@ export class Terminal {
// Close if there is no clients
this.keepAliveInterval = setInterval(() => {
const numClients = this.socketList.length;
const numClients = Object.keys(this.socketList).length;
if (numClients === 0) {
log.debug("Terminal", "Terminal " + this.name + " has no client, closing...");
@ -114,7 +114,8 @@ export class Terminal {
this._ptyProcess.onData((data) => {
this.buffer.pushItem(data);
for (const socket of this.socketList) {
for (const socketID in this.socketList) {
const socket = this.socketList[socketID];
socket.emitAgent("terminalWrite", this.name, data);
}
});
@ -139,12 +140,13 @@ export class Terminal {
* @param res
*/
protected exit = (res : {exitCode: number, signal?: number | undefined}) => {
for (const socket of this.socketList) {
for (const socketID in this.socketList) {
const socket = this.socketList[socketID];
socket.emitAgent("terminalExit", this.name, res.exitCode);
}
// Remove all clients
this.socketList = [];
this.socketList = {};
Terminal.terminalMap.delete(this.name);
log.debug("Terminal", "Terminal " + this.name + " exited with code " + res.exitCode);
@ -161,14 +163,11 @@ export class Terminal {
}
public join(socket : DockgeSocket) {
this.socketList.push(socket);
this.socketList[socket.id] = socket;
}
public leave(socket : DockgeSocket) {
const index = this.socketList.indexOf(socket);
if (index !== -1) {
this.socketList.splice(index, 1);
}
delete this.socketList[socket.id];
}
public get ptyProcess() {

View File

@ -74,6 +74,14 @@ export function callbackError(error : unknown, callback : unknown) {
}
}
export function callbackResult(result : unknown, callback : unknown) {
if (typeof(callback) !== "function") {
log.error("console", "Callback is not a function");
return;
}
callback(result);
}
export async function doubleCheckPassword(socket : DockgeSocket, currentPassword : unknown) {
if (typeof currentPassword !== "string") {
throw new Error("Wrong data type?");

View File

@ -291,10 +291,9 @@ function copyYAMLCommentsItems(items : any, srcItems : any) {
* - "127.0.0.1:5000-5010:5000-5010"
* - "6060:6060/udp"
* @param input
* @param defaultHostname
* @param hostname
*/
export function parseDockerPort(input : string, defaultHostname : string = "localhost") {
let hostname = defaultHostname;
export function parseDockerPort(input : string, hostname) {
let port;
let display;
@ -407,3 +406,4 @@ function traverseYAML(pair : Pair, env : DotenvParseOutput) : void {
pair.value.value = envsubst(pair.value.value, env);
}
}

View File

@ -189,6 +189,17 @@ export default defineComponent({
},
terminalRouteLink() {
if (this.endpoint) {
return {
name: "containerTerminalEndpoint",
params: {
endpoint: this.endpoint,
stackName: this.stackName,
serviceName: this.name,
type: "bash",
},
};
} else {
return {
name: "containerTerminal",
params: {
@ -197,6 +208,15 @@ export default defineComponent({
type: "bash",
},
};
}
},
endpoint() {
return this.$parent.$parent.endpoint;
},
stack() {
return this.$parent.$parent.stack;
},
stackName() {
@ -254,8 +274,7 @@ export default defineComponent({
},
methods: {
parsePort(port) {
let hostname = this.$root.info.primaryHostname || location.hostname;
return parseDockerPort(port, hostname);
return parseDockerPort(port, this.stack.primaryHostname);
},
remove() {
delete this.jsonObject.services[this.name];

View File

@ -7,7 +7,6 @@
<script>
import { Terminal } from "@xterm/xterm";
import { FitAddon } from "@xterm/addon-fit";
import { WebLinksAddon } from "xterm-addon-web-links";
import { TERMINAL_COLS, TERMINAL_ROWS } from "../../../common/util-common";
export default {
@ -115,14 +114,14 @@ export default {
// Create a new Terminal
if (this.mode === "mainTerminal") {
this.$root.getSocket().emit("mainTerminal", this.name, (res) => {
this.$root.emitAgent(this.endpoint, "mainTerminal", this.name, (res) => {
if (!res.ok) {
this.$root.toastRes(res);
}
});
} else if (this.mode === "interactive") {
console.debug("Create Interactive terminal:", this.name);
this.$root.getSocket().emit("interactiveTerminal", this.stackName, this.serviceName, this.shell, (res) => {
this.$root.emitAgent(this.endpoint, "interactiveTerminal", this.stackName, this.serviceName, this.shell, (res) => {
if (!res.ok) {
this.$root.toastRes(res);
}
@ -178,7 +177,7 @@ export default {
// Remove the input from the terminal
this.removeInput();
this.$root.getSocket().emit("terminalInput", this.name, buffer + e.key, (err) => {
this.$root.emitAgent(this.endpoint, "terminalInput", this.name, buffer + e.key, (err) => {
this.$root.toastError(err.msg);
});
@ -197,7 +196,7 @@ export default {
// TODO
} else if (e.key === "\u0003") { // Ctrl + C
console.debug("Ctrl + C");
this.$root.getSocket().emit("terminalInput", this.name, e.key);
this.$root.emitAgent(this.endpoint, "terminalInput", this.name, e.key);
this.removeInput();
} else {
this.cursorPosition++;
@ -210,7 +209,7 @@ export default {
interactiveTerminalConfig() {
this.terminal.onKey(e => {
this.$root.getSocket().emit("terminalInput", this.name, e.key, (res) => {
this.$root.emitAgent(this.endpoint, "terminalInput", this.name, e.key, (res) => {
if (!res.ok) {
this.$root.toastRes(res);
}
@ -239,7 +238,7 @@ export default {
this.terminalFitAddOn.fit();
let rows = this.terminal.rows;
let cols = this.terminal.cols;
this.$root.getSocket().emit("terminalResize", this.name, rows, cols);
this.$root.emitAgent(this.endpoint, "terminalResize", this.name, rows, cols);
}
}
};

View File

@ -93,8 +93,9 @@
<div class="mt-3">
<label for="name" class="form-label">{{ $t("dockgeAgent") }}</label>
<select v-model="stack.endpoint" class="form-select">
<option value="">{{ $t("currentEndpoint") }}</option>
<option value="rs-debian:5001">rs-debian:5001</option>
<option v-for="(agent, endpoint) in $root.agentList" :key="endpoint" :value="endpoint" :disabled="$root.agentStatusList[endpoint] != 'online'">
({{ $root.agentStatusList[endpoint] }}) {{ (endpoint) ? endpoint : $t("currentEndpoint") }}
</option>
</select>
</div>
</div>
@ -375,9 +376,16 @@ export default {
},
endpoint() {
return this.$route.params.endpoint || "";
}
return this.stack.endpoint || this.$route.params.endpoint || "";
},
url() {
if (this.stack.endpoint) {
return `/compose/${this.stack.name}/${this.stack.endpoint}`;
} else {
return `/compose/${this.stack.name}`;
}
},
},
watch: {
"stack.composeYAML": {
@ -556,7 +564,7 @@ export default {
if (res.ok) {
this.isEditMode = false;
this.$router.push("/compose/" + this.stack.name);
this.$router.push(this.url);
}
});
},
@ -570,7 +578,7 @@ export default {
if (res.ok) {
this.isEditMode = false;
this.$router.push("/compose/" + this.stack.name);
this.$router.push(this.url);
}
});
},

View File

@ -15,7 +15,7 @@
</p>
</div>
<Terminal class="terminal" :rows="20" mode="mainTerminal" name="console"></Terminal>
<Terminal class="terminal" :rows="20" mode="mainTerminal" name="console" :endpoint="endpoint"></Terminal>
</div>
</transition>
</template>
@ -32,6 +32,11 @@ export default {
allowedCommandList,
};
},
computed: {
endpoint() {
return this.$route.params.endpoint || "";
},
},
mounted() {
},

View File

@ -28,7 +28,7 @@ export default {
return this.$route.params.stackName;
},
endpoint() {
// TODO
return this.$route.params.endpoint || "";
},
shell() {
return this.$route.params.type;
@ -37,7 +37,7 @@ export default {
return this.$route.params.serviceName;
},
terminalName() {
return getContainerExecTerminalName(this.stackName, this.serviceName, 0);
return getContainerExecTerminalName(this.endpoint, this.stackName, this.serviceName, 0);
},
sh() {
let endpoint = this.$route.params.endpoint;

View File

@ -58,6 +58,10 @@ const routes = [
path: "/console",
component: Console,
},
{
path: "/console/:endpoint",
component: Console,
},
{
path: "/settings",
component: Settings,