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

View File

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

View File

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

View File

@ -18,6 +18,7 @@ import {
} from "../common/util-common"; } from "../common/util-common";
import { InteractiveTerminal, Terminal } from "./terminal"; import { InteractiveTerminal, Terminal } from "./terminal";
import childProcessAsync from "promisify-child-process"; import childProcessAsync from "promisify-child-process";
import { Settings } from "./settings";
export class Stack { 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); let obj = this.toSimpleJSON(endpoint);
return { return {
...obj, ...obj,
composeYAML: this.composeYAML, composeYAML: this.composeYAML,
composeENV: this.composeENV, composeENV: this.composeENV,
primaryHostname,
}; };
} }

View File

@ -35,7 +35,7 @@ export class Terminal {
public enableKeepAlive : boolean = false; public enableKeepAlive : boolean = false;
protected keepAliveInterval? : NodeJS.Timeout; protected keepAliveInterval? : NodeJS.Timeout;
protected socketList : DockgeSocket[] = []; protected socketList : Record<string, DockgeSocket> = {};
constructor(server : DockgeServer, name : string, file : string, args : string | string[], cwd : string) { constructor(server : DockgeServer, name : string, file : string, args : string | string[], cwd : string) {
this.server = server; this.server = server;
@ -89,7 +89,7 @@ export class Terminal {
// Close if there is no clients // Close if there is no clients
this.keepAliveInterval = setInterval(() => { this.keepAliveInterval = setInterval(() => {
const numClients = this.socketList.length; const numClients = Object.keys(this.socketList).length;
if (numClients === 0) { if (numClients === 0) {
log.debug("Terminal", "Terminal " + this.name + " has no client, closing..."); log.debug("Terminal", "Terminal " + this.name + " has no client, closing...");
@ -114,7 +114,8 @@ export class Terminal {
this._ptyProcess.onData((data) => { this._ptyProcess.onData((data) => {
this.buffer.pushItem(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); socket.emitAgent("terminalWrite", this.name, data);
} }
}); });
@ -139,12 +140,13 @@ export class Terminal {
* @param res * @param res
*/ */
protected exit = (res : {exitCode: number, signal?: number | undefined}) => { 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); socket.emitAgent("terminalExit", this.name, res.exitCode);
} }
// Remove all clients // Remove all clients
this.socketList = []; this.socketList = {};
Terminal.terminalMap.delete(this.name); Terminal.terminalMap.delete(this.name);
log.debug("Terminal", "Terminal " + this.name + " exited with code " + res.exitCode); log.debug("Terminal", "Terminal " + this.name + " exited with code " + res.exitCode);
@ -161,14 +163,11 @@ export class Terminal {
} }
public join(socket : DockgeSocket) { public join(socket : DockgeSocket) {
this.socketList.push(socket); this.socketList[socket.id] = socket;
} }
public leave(socket : DockgeSocket) { public leave(socket : DockgeSocket) {
const index = this.socketList.indexOf(socket); delete this.socketList[socket.id];
if (index !== -1) {
this.socketList.splice(index, 1);
}
} }
public get ptyProcess() { 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) { export async function doubleCheckPassword(socket : DockgeSocket, currentPassword : unknown) {
if (typeof currentPassword !== "string") { if (typeof currentPassword !== "string") {
throw new Error("Wrong data type?"); 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" * - "127.0.0.1:5000-5010:5000-5010"
* - "6060:6060/udp" * - "6060:6060/udp"
* @param input * @param input
* @param defaultHostname * @param hostname
*/ */
export function parseDockerPort(input : string, defaultHostname : string = "localhost") { export function parseDockerPort(input : string, hostname) {
let hostname = defaultHostname;
let port; let port;
let display; let display;
@ -407,3 +406,4 @@ function traverseYAML(pair : Pair, env : DotenvParseOutput) : void {
pair.value.value = envsubst(pair.value.value, env); pair.value.value = envsubst(pair.value.value, env);
} }
} }

View File

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

View File

@ -7,7 +7,6 @@
<script> <script>
import { Terminal } from "@xterm/xterm"; import { Terminal } from "@xterm/xterm";
import { FitAddon } from "@xterm/addon-fit"; import { FitAddon } from "@xterm/addon-fit";
import { WebLinksAddon } from "xterm-addon-web-links";
import { TERMINAL_COLS, TERMINAL_ROWS } from "../../../common/util-common"; import { TERMINAL_COLS, TERMINAL_ROWS } from "../../../common/util-common";
export default { export default {
@ -115,14 +114,14 @@ export default {
// Create a new Terminal // Create a new Terminal
if (this.mode === "mainTerminal") { if (this.mode === "mainTerminal") {
this.$root.getSocket().emit("mainTerminal", this.name, (res) => { this.$root.emitAgent(this.endpoint, "mainTerminal", this.name, (res) => {
if (!res.ok) { if (!res.ok) {
this.$root.toastRes(res); this.$root.toastRes(res);
} }
}); });
} else if (this.mode === "interactive") { } else if (this.mode === "interactive") {
console.debug("Create Interactive terminal:", this.name); 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) { if (!res.ok) {
this.$root.toastRes(res); this.$root.toastRes(res);
} }
@ -178,7 +177,7 @@ export default {
// Remove the input from the terminal // Remove the input from the terminal
this.removeInput(); 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); this.$root.toastError(err.msg);
}); });
@ -197,7 +196,7 @@ export default {
// TODO // TODO
} else if (e.key === "\u0003") { // Ctrl + C } else if (e.key === "\u0003") { // Ctrl + C
console.debug("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(); this.removeInput();
} else { } else {
this.cursorPosition++; this.cursorPosition++;
@ -210,7 +209,7 @@ export default {
interactiveTerminalConfig() { interactiveTerminalConfig() {
this.terminal.onKey(e => { 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) { if (!res.ok) {
this.$root.toastRes(res); this.$root.toastRes(res);
} }
@ -239,7 +238,7 @@ export default {
this.terminalFitAddOn.fit(); this.terminalFitAddOn.fit();
let rows = this.terminal.rows; let rows = this.terminal.rows;
let cols = this.terminal.cols; 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"> <div class="mt-3">
<label for="name" class="form-label">{{ $t("dockgeAgent") }}</label> <label for="name" class="form-label">{{ $t("dockgeAgent") }}</label>
<select v-model="stack.endpoint" class="form-select"> <select v-model="stack.endpoint" class="form-select">
<option value="">{{ $t("currentEndpoint") }}</option> <option v-for="(agent, endpoint) in $root.agentList" :key="endpoint" :value="endpoint" :disabled="$root.agentStatusList[endpoint] != 'online'">
<option value="rs-debian:5001">rs-debian:5001</option> ({{ $root.agentStatusList[endpoint] }}) {{ (endpoint) ? endpoint : $t("currentEndpoint") }}
</option>
</select> </select>
</div> </div>
</div> </div>
@ -375,9 +376,16 @@ export default {
}, },
endpoint() { 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: { watch: {
"stack.composeYAML": { "stack.composeYAML": {
@ -556,7 +564,7 @@ export default {
if (res.ok) { if (res.ok) {
this.isEditMode = false; this.isEditMode = false;
this.$router.push("/compose/" + this.stack.name); this.$router.push(this.url);
} }
}); });
}, },
@ -570,7 +578,7 @@ export default {
if (res.ok) { if (res.ok) {
this.isEditMode = false; this.isEditMode = false;
this.$router.push("/compose/" + this.stack.name); this.$router.push(this.url);
} }
}); });
}, },

View File

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

View File

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

View File

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