Multiple Dockge instances (#200)

This commit is contained in:
Louis Lam 2023-12-26 04:12:44 +08:00 committed by GitHub
parent 80e885e85d
commit de2de0573b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
38 changed files with 1525 additions and 597 deletions

View File

@ -14,20 +14,17 @@ View Video: https://youtu.be/AWAlOQeNpgU?t=48
## ⭐ Features ## ⭐ Features
- Manage `compose.yaml` - 🧑‍💼 Manage your `compose.yaml` files
- Create/Edit/Start/Stop/Restart/Delete - Create/Edit/Start/Stop/Restart/Delete
- Update Docker Images - Update Docker Images
- Interactive Editor for `compose.yaml` - ⌨️ Interactive Editor for `compose.yaml`
- Interactive Web Terminal - 🦦 Interactive Web Terminal
- Reactive - 🕷️ (1.4.0 🆕) Multiple agents support - You can manage multiple stacks from different Docker hosts in one single interface
- Everything is just responsive. Progress (Pull/Up/Down) and terminal output are in real-time - 🏪 Convert `docker run ...` commands into `compose.yaml`
- Easy-to-use & fancy UI - 📙 File based structure - Dockge won't kidnap your compose files, they are stored on your drive as usual. You can interact with them using normal `docker compose` commands
- If you love Uptime Kuma's UI/UX, you will love this one too
- Convert `docker run ...` commands into `compose.yaml`
- File based structure
- Dockge won't kidnap your compose files, they are stored on your drive as usual. You can interact with them using normal `docker compose` commands
<img src="https://github.com/louislam/dockge/assets/1336778/cc071864-592e-4909-b73a-343a57494002" width=300 /> <img src="https://github.com/louislam/dockge/assets/1336778/cc071864-592e-4909-b73a-343a57494002" width=300 />
- 🚄 Reactive - Everything is just responsive. Progress (Pull/Up/Down) and terminal output are in real-time
- 🐣 Easy-to-use & fancy UI - If you love Uptime Kuma's UI/UX, you will love this one too
![](https://github.com/louislam/dockge/assets/1336778/89fc1023-b069-42c0-a01c-918c495f1a6a) ![](https://github.com/louislam/dockge/assets/1336778/89fc1023-b069-42c0-a01c-918c495f1a6a)

291
backend/agent-manager.ts Normal file
View File

@ -0,0 +1,291 @@
import { DockgeSocket } from "./util-server";
import { io, Socket as SocketClient } from "socket.io-client";
import { log } from "./log";
import { Agent } from "./models/agent";
import { isDev, LooseObject, sleep } from "../common/util-common";
import semver from "semver";
import { R } from "redbean-node";
import dayjs, { Dayjs } from "dayjs";
/**
* Dockge Instance Manager
* One AgentManager per Socket connection
*/
export class AgentManager {
protected socket : DockgeSocket;
protected agentSocketList : Record<string, SocketClient> = {};
protected agentLoggedInList : Record<string, boolean> = {};
protected _firstConnectTime : Dayjs = dayjs();
constructor(socket: DockgeSocket) {
this.socket = socket;
}
get firstConnectTime() : Dayjs {
return this._firstConnectTime;
}
test(url : string, username : string, password : string) : Promise<void> {
return new Promise((resolve, reject) => {
let obj = new URL(url);
let endpoint = obj.host;
if (!endpoint) {
reject(new Error("Invalid Dockge URL"));
}
if (this.agentSocketList[endpoint]) {
reject(new Error("The Dockge URL already exists"));
}
let client = io(url, {
reconnection: false,
extraHeaders: {
endpoint,
}
});
client.on("connect", () => {
client.emit("login", {
username: username,
password: password,
}, (res : LooseObject) => {
if (res.ok) {
resolve();
} else {
reject(new Error(res.msg));
}
client.disconnect();
});
});
client.on("connect_error", (err) => {
if (err.message === "xhr poll error") {
reject(new Error("Unable to connect to the Dockge instance"));
} else {
reject(err);
}
client.disconnect();
});
});
}
/**
*
* @param url
* @param username
* @param password
*/
async add(url : string, username : string, password : string) : Promise<Agent> {
let bean = R.dispense("agent") as Agent;
bean.url = url;
bean.username = username;
bean.password = password;
await R.store(bean);
return bean;
}
/**
*
* @param url
*/
async remove(url : string) {
let bean = await R.findOne("agent", " url = ? ", [
url,
]);
if (bean) {
await R.trash(bean);
let endpoint = bean.endpoint;
delete this.agentSocketList[endpoint];
} else {
throw new Error("Agent not found");
}
}
connect(url : string, username : string, password : string) {
let obj = new URL(url);
let endpoint = obj.host;
this.socket.emit("agentStatus", {
endpoint: endpoint,
status: "connecting",
});
if (!endpoint) {
log.error("agent-manager", "Invalid endpoint: " + endpoint + " URL: " + url);
return;
}
if (this.agentSocketList[endpoint]) {
log.debug("agent-manager", "Already connected to the socket server: " + endpoint);
return;
}
log.info("agent-manager", "Connecting to the socket server: " + endpoint);
let client = io(url, {
extraHeaders: {
endpoint,
}
});
client.on("connect", () => {
log.info("agent-manager", "Connected to the socket server: " + endpoint);
client.emit("login", {
username: username,
password: password,
}, (res : LooseObject) => {
if (res.ok) {
log.info("agent-manager", "Logged in to the socket server: " + endpoint);
this.agentLoggedInList[endpoint] = true;
this.socket.emit("agentStatus", {
endpoint: endpoint,
status: "online",
});
} else {
log.error("agent-manager", "Failed to login to the socket server: " + endpoint);
this.agentLoggedInList[endpoint] = false;
this.socket.emit("agentStatus", {
endpoint: endpoint,
status: "offline",
});
}
});
});
client.on("connect_error", (err) => {
log.error("agent-manager", "Error from the socket server: " + endpoint);
this.socket.emit("agentStatus", {
endpoint: endpoint,
status: "offline",
});
});
client.on("disconnect", () => {
log.info("agent-manager", "Disconnected from the socket server: " + endpoint);
this.socket.emit("agentStatus", {
endpoint: endpoint,
status: "offline",
});
});
client.on("agent", (...args : unknown[]) => {
this.socket.emit("agent", ...args);
});
client.on("info", (res) => {
log.debug("agent-manager", res);
// Disconnect if the version is lower than 1.4.0
if (!isDev && semver.satisfies(res.version, "< 1.4.0")) {
this.socket.emit("agentStatus", {
endpoint: endpoint,
status: "offline",
msg: `${endpoint}: Unsupported version: ` + res.version,
});
client.disconnect();
}
});
this.agentSocketList[endpoint] = client;
}
disconnect(endpoint : string) {
let client = this.agentSocketList[endpoint];
client?.disconnect();
}
async connectAll() {
this._firstConnectTime = dayjs();
if (this.socket.endpoint) {
log.info("agent-manager", "This connection is connected as an agent, skip connectAll()");
return;
}
let list : Record<string, Agent> = await Agent.getAgentList();
if (Object.keys(list).length !== 0) {
log.info("agent-manager", "Connecting to all instance socket server(s)...");
}
for (let endpoint in list) {
let agent = list[endpoint];
this.connect(agent.url, agent.username, agent.password);
}
}
disconnectAll() {
for (let endpoint in this.agentSocketList) {
this.disconnect(endpoint);
}
}
async 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);
}
if (!client.connected || !this.agentLoggedInList[endpoint]) {
// Maybe the request is too quick, the socket is not connected yet, check firstConnectTime
// If it is within 10 seconds, we should apply retry logic here
let diff = dayjs().diff(this.firstConnectTime, "second");
log.debug("agent-manager", endpoint + ": diff: " + diff);
let ok = false;
while (diff < 10) {
if (client.connected && this.agentLoggedInList[endpoint]) {
log.debug("agent-manager", `${endpoint}: Connected & Logged in`);
ok = true;
break;
}
log.debug("agent-manager", endpoint + ": not ready yet, retrying in 1 second...");
await sleep(1000);
diff = dayjs().diff(this.firstConnectTime, "second");
}
if (!ok) {
log.error("agent-manager", `${endpoint}: Socket client not connected`);
throw new Error("Socket client not connected for endpoint: " + endpoint);
}
}
client.emit("agent", endpoint, eventName, ...args);
}
emitToAllEndpoints(eventName: string, ...args : unknown[]) {
log.debug("agent-manager", "Emitting event to all endpoints");
for (let endpoint in this.agentSocketList) {
this.emitToEndpoint(endpoint, eventName, ...args).catch((e) => {
log.warn("agent-manager", e.message);
});
}
}
async sendAgentList() {
let list = await Agent.getAgentList();
let result : Record<string, LooseObject> = {};
// Myself
result[""] = {
url: "",
username: "",
endpoint: "",
};
for (let endpoint in list) {
let agent = list[endpoint];
result[endpoint] = agent.toJSON();
}
this.socket.emit("agentList", {
ok: true,
agentList: result,
});
}
}

View File

@ -0,0 +1,7 @@
import { DockgeServer } from "./dockge-server";
import { AgentSocket } from "../common/agent-socket";
import { DockgeSocket } from "./util-server";
export abstract class AgentSocketHandler {
abstract create(socket : DockgeSocket, server : DockgeServer, agentSocket : AgentSocket): void;
}

View File

@ -1,45 +1,44 @@
import { SocketHandler } from "../socket-handler.js"; 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";
// @ts-ignore export class DockerSocketHandler extends AgentSocketHandler {
import composerize from "composerize"; create(socket : DockgeSocket, server : DockgeServer, agentSocket : AgentSocket) {
// Do not call super.create()
export class DockerSocketHandler extends SocketHandler { agentSocket.on("deployStack", async (name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown, callback) => {
create(socket : DockgeSocket, server : DockgeServer) {
socket.on("deployStack", async (name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown, callback) => {
try { try {
checkLogin(socket); checkLogin(socket);
const stack = await this.saveStack(socket, 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);
} }
}); });
socket.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(socket, 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);
} }
}); });
socket.on("deleteStack", async (name : unknown, callback) => { agentSocket.on("deleteStack", async (name : unknown, callback) => {
try { try {
checkLogin(socket); checkLogin(socket);
if (typeof(name) !== "string") { if (typeof(name) !== "string") {
@ -55,17 +54,17 @@ export class DockerSocketHandler extends SocketHandler {
} }
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);
} }
}); });
socket.on("getStack", async (stackName : unknown, callback) => { agentSocket.on("getStack", async (stackName : unknown, callback) => {
try { try {
checkLogin(socket); checkLogin(socket);
@ -79,31 +78,31 @@ export class DockerSocketHandler extends SocketHandler {
stack.joinCombinedTerminal(socket); stack.joinCombinedTerminal(socket);
} }
callback({ callbackResult({
ok: true, ok: true,
stack: stack.toJSON(), stack: await stack.toJSON(socket.endpoint),
}); }, callback);
} catch (e) { } catch (e) {
callbackError(e, callback); callbackError(e, callback);
} }
}); });
// requestStackList // requestStackList
socket.on("requestStackList", async (callback) => { agentSocket.on("requestStackList", async (callback) => {
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);
} }
}); });
// startStack // startStack
socket.on("startStack", async (stackName : unknown, callback) => { agentSocket.on("startStack", async (stackName : unknown, callback) => {
try { try {
checkLogin(socket); checkLogin(socket);
@ -113,10 +112,10 @@ export class DockerSocketHandler extends SocketHandler {
const stack = await Stack.getStack(server, stackName); const stack = await Stack.getStack(server, stackName);
await stack.start(socket); await stack.start(socket);
callback({ callbackResult({
ok: true, ok: true,
msg: "Started" msg: "Started"
}); }, callback);
server.sendStackList(); server.sendStackList();
stack.joinCombinedTerminal(socket); stack.joinCombinedTerminal(socket);
@ -127,7 +126,7 @@ export class DockerSocketHandler extends SocketHandler {
}); });
// stopStack // stopStack
socket.on("stopStack", async (stackName : unknown, callback) => { agentSocket.on("stopStack", async (stackName : unknown, callback) => {
try { try {
checkLogin(socket); checkLogin(socket);
@ -137,10 +136,10 @@ export class DockerSocketHandler extends SocketHandler {
const stack = await Stack.getStack(server, stackName); const stack = await Stack.getStack(server, stackName);
await stack.stop(socket); await stack.stop(socket);
callback({ callbackResult({
ok: true, ok: true,
msg: "Stopped" msg: "Stopped"
}); }, callback);
server.sendStackList(); server.sendStackList();
} catch (e) { } catch (e) {
callbackError(e, callback); callbackError(e, callback);
@ -148,7 +147,7 @@ export class DockerSocketHandler extends SocketHandler {
}); });
// restartStack // restartStack
socket.on("restartStack", async (stackName : unknown, callback) => { agentSocket.on("restartStack", async (stackName : unknown, callback) => {
try { try {
checkLogin(socket); checkLogin(socket);
@ -158,10 +157,10 @@ export class DockerSocketHandler extends SocketHandler {
const stack = await Stack.getStack(server, stackName); const stack = await Stack.getStack(server, stackName);
await stack.restart(socket); await stack.restart(socket);
callback({ callbackResult({
ok: true, ok: true,
msg: "Restarted" msg: "Restarted"
}); }, callback);
server.sendStackList(); server.sendStackList();
} catch (e) { } catch (e) {
callbackError(e, callback); callbackError(e, callback);
@ -169,7 +168,7 @@ export class DockerSocketHandler extends SocketHandler {
}); });
// updateStack // updateStack
socket.on("updateStack", async (stackName : unknown, callback) => { agentSocket.on("updateStack", async (stackName : unknown, callback) => {
try { try {
checkLogin(socket); checkLogin(socket);
@ -179,10 +178,10 @@ export class DockerSocketHandler extends SocketHandler {
const stack = await Stack.getStack(server, stackName); const stack = await Stack.getStack(server, stackName);
await stack.update(socket); await stack.update(socket);
callback({ callbackResult({
ok: true, ok: true,
msg: "Updated" msg: "Updated"
}); }, callback);
server.sendStackList(); server.sendStackList();
} catch (e) { } catch (e) {
callbackError(e, callback); callbackError(e, callback);
@ -190,7 +189,7 @@ export class DockerSocketHandler extends SocketHandler {
}); });
// down stack // down stack
socket.on("downStack", async (stackName : unknown, callback) => { agentSocket.on("downStack", async (stackName : unknown, callback) => {
try { try {
checkLogin(socket); checkLogin(socket);
@ -200,10 +199,10 @@ export class DockerSocketHandler extends SocketHandler {
const stack = await Stack.getStack(server, stackName); const stack = await Stack.getStack(server, stackName);
await stack.down(socket); await stack.down(socket);
callback({ callbackResult({
ok: true, ok: true,
msg: "Downed" msg: "Downed"
}); }, callback);
server.sendStackList(); server.sendStackList();
} catch (e) { } catch (e) {
callbackError(e, callback); callbackError(e, callback);
@ -211,7 +210,7 @@ export class DockerSocketHandler extends SocketHandler {
}); });
// Services status // Services status
socket.on("serviceStatusList", async (stackName : unknown, callback) => { agentSocket.on("serviceStatusList", async (stackName : unknown, callback) => {
try { try {
checkLogin(socket); checkLogin(socket);
@ -221,50 +220,31 @@ export class DockerSocketHandler extends SocketHandler {
const stack = await Stack.getStack(server, stackName, true); const stack = await Stack.getStack(server, stackName, true);
const serviceStatusList = Object.fromEntries(await stack.getServiceStatusList()); const serviceStatusList = Object.fromEntries(await stack.getServiceStatusList());
callback({ callbackResult({
ok: true, ok: true,
serviceStatusList, serviceStatusList,
}); }, callback);
} catch (e) { } catch (e) {
callbackError(e, callback); callbackError(e, callback);
} }
}); });
// getExternalNetworkList // getExternalNetworkList
socket.on("getDockerNetworkList", async (callback) => { agentSocket.on("getDockerNetworkList", async (callback) => {
try { try {
checkLogin(socket); checkLogin(socket);
const dockerNetworkList = await server.getDockerNetworkList(); const dockerNetworkList = await server.getDockerNetworkList();
callback({ callbackResult({
ok: true, ok: true,
dockerNetworkList, dockerNetworkList,
}); }, callback);
} catch (e) {
callbackError(e, callback);
}
});
// composerize
socket.on("composerize", async (dockerRunCommand : unknown, callback) => {
try {
checkLogin(socket);
if (typeof(dockerRunCommand) !== "string") {
throw new ValidationError("dockerRunCommand must be a string");
}
const composeTemplate = composerize(dockerRunCommand);
callback({
ok: true,
composeTemplate,
});
} catch (e) { } catch (e) {
callbackError(e, callback); callbackError(e, callback);
} }
}); });
} }
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 // Check types
if (typeof(name) !== "string") { if (typeof(name) !== "string") {
throw new ValidationError("Name must be a string"); throw new ValidationError("Name must be a string");

View File

@ -1,24 +1,15 @@
import { SocketHandler } from "../socket-handler.js";
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 yaml from "yaml";
import path from "path";
import fs from "fs";
import {
allowedCommandList,
allowedRawKeys,
getComposeTerminalName, getContainerExecTerminalName,
isDev,
PROGRESS_TERMINAL_ROWS
} from "../util-common";
import { InteractiveTerminal, MainTerminal, Terminal } from "../terminal"; import { InteractiveTerminal, MainTerminal, Terminal } from "../terminal";
import { Stack } from "../stack"; import { Stack } from "../stack";
import { AgentSocketHandler } from "../agent-socket-handler";
import { AgentSocket } from "../../common/agent-socket";
export class TerminalSocketHandler extends SocketHandler { export class TerminalSocketHandler extends AgentSocketHandler {
create(socket : DockgeSocket, server : DockgeServer) { create(socket : DockgeSocket, server : DockgeServer, agentSocket : AgentSocket) {
socket.on("terminalInput", async (terminalName : unknown, cmd : unknown, errorCallback) => { agentSocket.on("terminalInput", async (terminalName : unknown, cmd : unknown, callback) => {
try { try {
checkLogin(socket); checkLogin(socket);
@ -38,17 +29,12 @@ export class TerminalSocketHandler extends SocketHandler {
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,
});
}
} }
}); });
// Main Terminal // Main Terminal
socket.on("mainTerminal", async (terminalName : unknown, callback) => { agentSocket.on("mainTerminal", async (terminalName : unknown, callback) => {
try { try {
checkLogin(socket); checkLogin(socket);
@ -59,29 +45,29 @@ export class TerminalSocketHandler extends SocketHandler {
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(socket);
terminal.start(); terminal.start();
callback({ callbackResult({
ok: true, ok: true,
}); }, callback);
} catch (e) { } catch (e) {
callbackError(e, callback); callbackError(e, callback);
} }
}); });
// Interactive Terminal for containers // 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 { try {
checkLogin(socket); checkLogin(socket);
@ -104,16 +90,16 @@ export class TerminalSocketHandler extends SocketHandler {
const stack = await Stack.getStack(server, stackName); const stack = await Stack.getStack(server, stackName);
stack.joinContainerTerminal(socket, serviceName, shell); stack.joinContainerTerminal(socket, serviceName, shell);
callback({ callbackResult({
ok: true, ok: true,
}); }, callback);
} catch (e) { } catch (e) {
callbackError(e, callback); callbackError(e, callback);
} }
}); });
// Join Output Terminal // Join Output Terminal
socket.on("terminalJoin", async (terminalName : unknown, callback) => { agentSocket.on("terminalJoin", async (terminalName : unknown, callback) => {
if (typeof(callback) !== "function") { if (typeof(callback) !== "function") {
log.debug("console", "Callback is not a function."); log.debug("console", "Callback is not a function.");
return; return;
@ -141,7 +127,7 @@ export class TerminalSocketHandler extends SocketHandler {
}); });
// Leave Combined Terminal // Leave Combined Terminal
socket.on("leaveCombinedTerminal", async (stackName : unknown, callback) => { agentSocket.on("leaveCombinedTerminal", async (stackName : unknown, callback) => {
try { try {
checkLogin(socket); checkLogin(socket);
@ -154,18 +140,16 @@ export class TerminalSocketHandler extends SocketHandler {
const stack = await Stack.getStack(server, stackName); const stack = await Stack.getStack(server, stackName);
await stack.leaveCombinedTerminal(socket); await stack.leaveCombinedTerminal(socket);
callback({ callbackResult({
ok: true, ok: true,
}); }, callback);
} catch (e) { } catch (e) {
callbackError(e, callback); callbackError(e, callback);
} }
}); });
// Resize Terminal // Resize Terminal
socket.on( agentSocket.on("terminalResize", async (terminalName: unknown, rows: unknown, cols: unknown) => {
"terminalResize",
async (terminalName: unknown, rows: unknown, cols: unknown) => {
log.info("terminalResize", `Terminal: ${terminalName}`); log.info("terminalResize", `Terminal: ${terminalName}`);
try { try {
checkLogin(socket); checkLogin(socket);
@ -191,15 +175,13 @@ export class TerminalSocketHandler extends SocketHandler {
throw new Error(`${terminalName} Terminal not found.`); throw new Error(`${terminalName} Terminal not found.`);
} }
} catch (e) { } catch (e) {
log.debug( log.debug("terminalResize",
"terminalResize",
// Added to prevent the lint error when adding the type // Added to prevent the lint error when adding the type
// and ts type checker saying type is unknown. // and ts type checker saying type is unknown.
// @ts-ignore // @ts-ignore
`Error on ${terminalName}: ${e.message}` `Error on ${terminalName}: ${e.message}`
); );
} }
} });
);
} }
} }

View File

@ -9,7 +9,7 @@ import knex from "knex";
import Dialect from "knex/lib/dialects/sqlite3/index.js"; import Dialect from "knex/lib/dialects/sqlite3/index.js";
import sqlite from "@louislam/sqlite3"; import sqlite from "@louislam/sqlite3";
import { sleep } from "./util-common"; import { sleep } from "../common/util-common";
interface DBConfig { interface DBConfig {
type?: "sqlite" | "mysql"; type?: "sqlite" | "mysql";

View File

@ -1,3 +1,4 @@
import "dotenv/config";
import { MainRouter } from "./routers/main-router"; import { MainRouter } from "./routers/main-router";
import * as fs from "node:fs"; import * as fs from "node:fs";
import { PackageJson } from "type-fest"; import { PackageJson } from "type-fest";
@ -17,23 +18,26 @@ import { Settings } from "./settings";
import checkVersion from "./check-version"; import checkVersion from "./check-version";
import dayjs from "dayjs"; import dayjs from "dayjs";
import { R } from "redbean-node"; import { R } from "redbean-node";
import { genSecret, isDev } from "./util-common"; import { genSecret, isDev, LooseObject } from "../common/util-common";
import { generatePasswordHash } from "./password-hash"; import { generatePasswordHash } from "./password-hash";
import { Bean } from "redbean-node/dist/bean"; import { Bean } from "redbean-node/dist/bean";
import { Arguments, Config, DockgeSocket } from "./util-server"; import { Arguments, Config, DockgeSocket } from "./util-server";
import { DockerSocketHandler } from "./socket-handlers/docker-socket-handler"; import { DockerSocketHandler } from "./agent-socket-handlers/docker-socket-handler";
import expressStaticGzip from "express-static-gzip"; import expressStaticGzip from "express-static-gzip";
import path from "path"; 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 { Stack } from "./stack";
import { Cron } from "croner"; import { Cron } from "croner";
import gracefulShutdown from "http-graceful-shutdown"; import gracefulShutdown from "http-graceful-shutdown";
import User from "./models/user"; import User from "./models/user";
import childProcessAsync from "promisify-child-process"; import childProcessAsync from "promisify-child-process";
import { AgentManager } from "./agent-manager";
import { AgentProxySocketHandler } from "./socket-handlers/agent-proxy-socket-handler";
import { AgentSocketHandler } from "./agent-socket-handler";
import { AgentSocket } from "../common/agent-socket";
import { ManageAgentSocketHandler } from "./socket-handlers/manage-agent-socket-handler";
import { Terminal } from "./terminal"; import { Terminal } from "./terminal";
import "dotenv/config";
export class DockgeServer { export class DockgeServer {
app : Express; app : Express;
httpServer : http.Server; httpServer : http.Server;
@ -50,10 +54,19 @@ export class DockgeServer {
]; ];
/** /**
* List of socket handlers * List of socket handlers (no agent support)
*/ */
socketHandlerList : SocketHandler[] = [ socketHandlerList : SocketHandler[] = [
new MainSocketHandler(), new MainSocketHandler(),
new ManageAgentSocketHandler(),
];
agentProxySocketHandler = new AgentProxySocketHandler();
/**
* List of socket handlers (support agent)
*/
agentSocketHandlerList : AgentSocketHandler[] = [
new DockerSocketHandler(), new DockerSocketHandler(),
new TerminalSocketHandler(), new TerminalSocketHandler(),
]; ];
@ -196,7 +209,7 @@ export class DockgeServer {
cors, cors,
allowRequest: (req, callback) => { allowRequest: (req, callback) => {
let isOriginValid = true; let isOriginValid = true;
const bypass = isDev; const bypass = isDev || process.env.UPTIME_KUMA_WS_ORIGIN_CHECK === "bypass";
if (!bypass) { if (!bypass) {
let host = req.headers.host; let host = req.headers.host;
@ -230,20 +243,52 @@ export class DockgeServer {
}); });
this.io.on("connection", async (socket: Socket) => { this.io.on("connection", async (socket: Socket) => {
log.info("server", "Socket connected!"); let dockgeSocket = socket as DockgeSocket;
dockgeSocket.instanceManager = new AgentManager(dockgeSocket);
dockgeSocket.emitAgent = (event : string, ...args : unknown[]) => {
let obj = args[0];
if (typeof(obj) === "object") {
let obj2 = obj as LooseObject;
obj2.endpoint = dockgeSocket.endpoint;
}
dockgeSocket.emit("agent", event, ...args);
};
this.sendInfo(socket, true); if (typeof(socket.request.headers.endpoint) === "string") {
dockgeSocket.endpoint = socket.request.headers.endpoint;
} else {
dockgeSocket.endpoint = "";
}
if (dockgeSocket.endpoint) {
log.info("server", "Socket connected (agent), as endpoint " + dockgeSocket.endpoint);
} else {
log.info("server", "Socket connected (direct)");
}
this.sendInfo(dockgeSocket, true);
if (this.needSetup) { if (this.needSetup) {
log.info("server", "Redirect to setup page"); log.info("server", "Redirect to setup page");
socket.emit("setup"); dockgeSocket.emit("setup");
} }
// Create socket handlers // Create socket handlers (original, no agent support)
for (const socketHandler of this.socketHandlerList) { for (const socketHandler of this.socketHandlerList) {
socketHandler.create(socket as DockgeSocket, this); socketHandler.create(dockgeSocket, this);
} }
// Create Agent Socket
let agentSocket = new AgentSocket();
// Create agent socket handlers
for (const socketHandler of this.agentSocketHandlerList) {
socketHandler.create(dockgeSocket, this, agentSocket);
}
// Create agent proxy socket handlers
this.agentProxySocketHandler.create2(dockgeSocket, this, agentSocket);
// *************************** // ***************************
// Better do anything after added all socket handlers here // Better do anything after added all socket handlers here
// *************************** // ***************************
@ -251,12 +296,18 @@ export class DockgeServer {
log.debug("auth", "check auto login"); log.debug("auth", "check auto login");
if (await Settings.get("disableAuth")) { if (await Settings.get("disableAuth")) {
log.info("auth", "Disabled Auth: auto login to admin"); log.info("auth", "Disabled Auth: auto login to admin");
this.afterLogin(socket as DockgeSocket, await R.findOne("user") as User); this.afterLogin(dockgeSocket, await R.findOne("user") as User);
socket.emit("autoLogin"); dockgeSocket.emit("autoLogin");
} else { } else {
log.debug("auth", "need auth"); log.debug("auth", "need auth");
} }
// Socket disconnect
dockgeSocket.on("disconnect", () => {
log.info("server", "Socket disconnected!");
dockgeSocket.instanceManager.disconnectAll();
});
}); });
this.io.on("disconnect", () => { this.io.on("disconnect", () => {
@ -281,6 +332,11 @@ export class DockgeServer {
} catch (e) { } catch (e) {
log.error("server", e); log.error("server", e);
} }
socket.instanceManager.sendAgentList();
// Also connect to other dockge instances
socket.instanceManager.connectAll();
} }
/** /**
@ -519,26 +575,34 @@ export class DockgeServer {
return jwtSecretBean; return jwtSecretBean;
} }
/**
* Send stack list to all connected sockets
* @param useCache
*/
async sendStackList(useCache = false) { async sendStackList(useCache = false) {
let roomList = this.io.sockets.adapter.rooms.keys(); let socketList = this.io.sockets.sockets.values();
let map : Map<string, object> | undefined;
let stackList;
for (let socket of socketList) {
let dockgeSocket = socket as DockgeSocket;
for (let room of roomList) {
// Check if the room is a number (user id) // Check if the room is a number (user id)
if (Number(room)) { if (dockgeSocket.userID) {
// Get the list only if there is a room // Get the list only if there is a logged in user
if (!map) { if (!stackList) {
map = new Map(); stackList = await Stack.getStackList(this, useCache);
let stackList = await Stack.getStackList(this, useCache); }
let map : Map<string, object> = new Map();
for (let [ stackName, stack ] of stackList) { for (let [ stackName, stack ] of stackList) {
map.set(stackName, stack.toSimpleJSON()); map.set(stackName, stack.toSimpleJSON(dockgeSocket.endpoint));
}
} }
log.debug("server", "Send stack list to room " + room); log.debug("server", "Send stack list to user: " + dockgeSocket.id + " (" + dockgeSocket.endpoint + ")");
this.io.to(room).emit("stackList", { dockgeSocket.emitAgent("stackList", {
ok: true, ok: true,
stackList: Object.fromEntries(map), stackList: Object.fromEntries(map),
}); });
@ -546,25 +610,6 @@ export class DockgeServer {
} }
} }
async sendStackStatusList() {
let statusList = await Stack.getStatusList();
let roomList = this.io.sockets.adapter.rooms.keys();
for (let room of roomList) {
// Check if the room is a number (user id)
if (Number(room)) {
log.debug("server", "Send stack status list to room " + room);
this.io.to(room).emit("stackStatusList", {
ok: true,
stackStatusList: Object.fromEntries(statusList),
});
} else {
log.debug("server", "Skip sending stack status list to room " + room);
}
}
}
async getDockerNetworkList() : Promise<string[]> { async getDockerNetworkList() : Promise<string[]> {
let res = await childProcessAsync.spawn("docker", [ "network", "ls", "--format", "{{.Name}}" ], { let res = await childProcessAsync.spawn("docker", [ "network", "ls", "--format", "{{.Name}}" ], {
encoding: "utf-8", encoding: "utf-8",
@ -618,10 +663,10 @@ export class DockgeServer {
* @param {string} userID * @param {string} userID
* @param {string?} currentSocketID * @param {string?} currentSocketID
*/ */
disconnectAllSocketClients(userID: number, currentSocketID? : string) { disconnectAllSocketClients(userID: number | undefined, currentSocketID? : string) {
for (const rawSocket of this.io.sockets.sockets.values()) { for (const rawSocket of this.io.sockets.sockets.values()) {
let socket = rawSocket as DockgeSocket; let socket = rawSocket as DockgeSocket;
if (socket.userID === userID && socket.id !== currentSocketID) { if ((!userID || socket.userID === userID) && socket.id !== currentSocketID) {
try { try {
socket.emit("refresh"); socket.emit("refresh");
socket.disconnect(); socket.disconnect();

View File

@ -1,6 +1,6 @@
// Console colors // Console colors
// https://stackoverflow.com/questions/9781218/how-to-change-node-jss-console-font-color // https://stackoverflow.com/questions/9781218/how-to-change-node-jss-console-font-color
import { intHash, isDev } from "./util-common"; import { intHash, isDev } from "../common/util-common";
import dayjs from "dayjs"; import dayjs from "dayjs";
export const CONSOLE_STYLE_Reset = "\x1b[0m"; export const CONSOLE_STYLE_Reset = "\x1b[0m";

View File

@ -0,0 +1,16 @@
import { Knex } from "knex";
export async function up(knex: Knex): Promise<void> {
// Create the user table
return knex.schema.createTable("agent", (table) => {
table.increments("id");
table.string("url", 255).notNullable().unique();
table.string("username", 255).notNullable();
table.string("password", 255).notNullable();
table.boolean("active").notNullable().defaultTo(true);
});
}
export async function down(knex: Knex): Promise<void> {
return knex.schema.dropTable("agent");
}

31
backend/models/agent.ts Normal file
View File

@ -0,0 +1,31 @@
import { BeanModel } from "redbean-node/dist/bean-model";
import { R } from "redbean-node";
import { LooseObject } from "../../common/util-common";
export class Agent extends BeanModel {
static async getAgentList() : Promise<Record<string, Agent>> {
let list = await R.findAll("agent") as Agent[];
let result : Record<string, Agent> = {};
for (let agent of list) {
result[agent.endpoint] = agent;
}
return result;
}
get endpoint() : string {
let obj = new URL(this.url);
return obj.host;
}
toJSON() : LooseObject {
return {
url: this.url,
username: this.username,
endpoint: this.endpoint,
};
}
}
export default Agent;

View File

@ -1,6 +1,6 @@
import { R } from "redbean-node"; import { R } from "redbean-node";
import { log } from "./log"; import { log } from "./log";
import { LooseObject } from "./util-common"; import { LooseObject } from "../common/util-common";
export class Settings { export class Settings {

View File

@ -0,0 +1,47 @@
import { SocketHandler } from "../socket-handler.js";
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 {
create2(socket : DockgeSocket, server : DockgeServer, agentSocket : AgentSocket) {
// Agent - proxying requests if needed
socket.on("agent", async (endpoint : unknown, eventName : unknown, ...args : unknown[]) => {
try {
checkLogin(socket);
// Check Type
if (typeof(endpoint) !== "string") {
throw new Error("Endpoint must be a string: " + endpoint);
}
if (typeof(eventName) !== "string") {
throw new Error("Event name must be a string");
}
if (endpoint === ALL_ENDPOINTS) { // Send to all endpoints
log.debug("agent", "Sending to all endpoints: " + eventName);
socket.instanceManager.emitToAllEndpoints(eventName, ...args);
} 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);
await socket.instanceManager.emitToEndpoint(endpoint, eventName, ...args);
}
} catch (e) {
if (e instanceof Error) {
log.warn("agent", e.message);
}
}
});
}
create(socket : DockgeSocket, server : DockgeServer) {
throw new Error("Method not implemented. Please use create2 instead.");
}
}

View File

@ -1,3 +1,5 @@
// @ts-ignore
import composerize from "composerize";
import { SocketHandler } from "../socket-handler.js"; import { SocketHandler } from "../socket-handler.js";
import { DockgeServer } from "../dockge-server"; import { DockgeServer } from "../dockge-server";
import { log } from "../log"; import { log } from "../log";
@ -5,7 +7,14 @@ import { R } from "redbean-node";
import { loginRateLimiter, twoFaRateLimiter } from "../rate-limiter"; import { loginRateLimiter, twoFaRateLimiter } from "../rate-limiter";
import { generatePasswordHash, needRehashPassword, shake256, SHAKE256_LENGTH, verifyPassword } from "../password-hash"; import { generatePasswordHash, needRehashPassword, shake256, SHAKE256_LENGTH, verifyPassword } from "../password-hash";
import { User } from "../models/user"; import { User } from "../models/user";
import { checkLogin, DockgeSocket, doubleCheckPassword, JWTDecoded } from "../util-server"; import {
callbackError,
checkLogin,
DockgeSocket,
doubleCheckPassword,
JWTDecoded,
ValidationError
} from "../util-server";
import { passwordStrength } from "check-password-strength"; import { passwordStrength } from "check-password-strength";
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
import { Settings } from "../settings"; import { Settings } from "../settings";
@ -262,8 +271,6 @@ export class MainSocketHandler extends SocketHandler {
await doubleCheckPassword(socket, currentPassword); await doubleCheckPassword(socket, currentPassword);
} }
console.log(data);
await Settings.setSettings("general", data); await Settings.setSettings("general", data);
callback({ callback({
@ -294,6 +301,25 @@ export class MainSocketHandler extends SocketHandler {
} }
} }
}); });
// composerize
socket.on("composerize", async (dockerRunCommand : unknown, callback) => {
try {
checkLogin(socket);
if (typeof(dockerRunCommand) !== "string") {
throw new ValidationError("dockerRunCommand must be a string");
}
const composeTemplate = composerize(dockerRunCommand);
callback({
ok: true,
composeTemplate,
});
} catch (e) {
callbackError(e, callback);
}
});
} }
async login(username : string, password : string) : Promise<User | null> { async login(username : string, password : string) : Promise<User | null> {

View File

@ -0,0 +1,70 @@
import { SocketHandler } from "../socket-handler.js";
import { DockgeServer } from "../dockge-server";
import { log } from "../log";
import { callbackError, callbackResult, checkLogin, DockgeSocket } from "../util-server";
import { LooseObject } from "../../common/util-common";
export class ManageAgentSocketHandler extends SocketHandler {
create(socket : DockgeSocket, server : DockgeServer) {
// addAgent
socket.on("addAgent", async (requestData : unknown, callback : unknown) => {
try {
log.debug("manage-agent-socket-handler", "addAgent");
checkLogin(socket);
if (typeof(requestData) !== "object") {
throw new Error("Data must be an object");
}
let data = requestData as LooseObject;
let manager = socket.instanceManager;
await manager.test(data.url, data.username, data.password);
await manager.add(data.url, data.username, data.password);
// connect to the agent
manager.connect(data.url, data.username, data.password);
// Refresh another sockets
// It is a bit difficult to control another browser sessions to connect/disconnect agents, so force them to refresh the page will be easier.
server.disconnectAllSocketClients(undefined, socket.id);
manager.sendAgentList();
callbackResult({
ok: true,
msg: "agentAddedSuccessfully",
msgi18n: true,
}, callback);
} catch (e) {
callbackError(e, callback);
}
});
// removeAgent
socket.on("removeAgent", async (url : unknown, callback : unknown) => {
try {
log.debug("manage-agent-socket-handler", "removeAgent");
checkLogin(socket);
if (typeof(url) !== "string") {
throw new Error("URL must be a string");
}
let manager = socket.instanceManager;
await manager.remove(url);
server.disconnectAllSocketClients(undefined, socket.id);
manager.sendAgentList();
callbackResult({
ok: true,
msg: "agentRemovedSuccessfully",
msgi18n: true,
}, callback);
} catch (e) {
callbackError(e, callback);
}
});
}
}

View File

@ -15,9 +15,10 @@ import {
PROGRESS_TERMINAL_ROWS, PROGRESS_TERMINAL_ROWS,
RUNNING, TERMINAL_ROWS, RUNNING, TERMINAL_ROWS,
UNKNOWN UNKNOWN
} from "./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,22 +51,41 @@ export class Stack {
} }
} }
toJSON() : object { async toJSON(endpoint : string) : Promise<object> {
let obj = this.toSimpleJSON();
// 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 { return {
...obj, ...obj,
composeYAML: this.composeYAML, composeYAML: this.composeYAML,
composeENV: this.composeENV, composeENV: this.composeENV,
primaryHostname,
}; };
} }
toSimpleJSON() : object { toSimpleJSON(endpoint : string) : object {
return { return {
name: this.name, name: this.name,
status: this._status, status: this._status,
tags: [], tags: [],
isManagedByDockge: this.isManagedByDockge, isManagedByDockge: this.isManagedByDockge,
composeFileName: this._composeFileName, composeFileName: this._composeFileName,
endpoint,
}; };
} }
@ -186,8 +206,8 @@ export class Stack {
} }
} }
async deploy(socket? : DockgeSocket) : Promise<number> { async deploy(socket : DockgeSocket) : Promise<number> {
const terminalName = getComposeTerminalName(this.name); const terminalName = getComposeTerminalName(socket.endpoint, this.name);
let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "up", "-d", "--remove-orphans" ], this.path); let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "up", "-d", "--remove-orphans" ], this.path);
if (exitCode !== 0) { if (exitCode !== 0) {
throw new Error("Failed to deploy, please check the terminal output for more information."); throw new Error("Failed to deploy, please check the terminal output for more information.");
@ -195,8 +215,8 @@ export class Stack {
return exitCode; return exitCode;
} }
async delete(socket?: DockgeSocket) : Promise<number> { async delete(socket: DockgeSocket) : Promise<number> {
const terminalName = getComposeTerminalName(this.name); const terminalName = getComposeTerminalName(socket.endpoint, this.name);
let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "down", "--remove-orphans" ], this.path); let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "down", "--remove-orphans" ], this.path);
if (exitCode !== 0) { if (exitCode !== 0) {
throw new Error("Failed to delete, please check the terminal output for more information."); throw new Error("Failed to delete, please check the terminal output for more information.");
@ -388,7 +408,7 @@ export class Stack {
} }
async start(socket: DockgeSocket) { async start(socket: DockgeSocket) {
const terminalName = getComposeTerminalName(this.name); const terminalName = getComposeTerminalName(socket.endpoint, this.name);
let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "up", "-d", "--remove-orphans" ], this.path); let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "up", "-d", "--remove-orphans" ], this.path);
if (exitCode !== 0) { if (exitCode !== 0) {
throw new Error("Failed to start, please check the terminal output for more information."); throw new Error("Failed to start, please check the terminal output for more information.");
@ -397,7 +417,7 @@ export class Stack {
} }
async stop(socket: DockgeSocket) : Promise<number> { async stop(socket: DockgeSocket) : Promise<number> {
const terminalName = getComposeTerminalName(this.name); const terminalName = getComposeTerminalName(socket.endpoint, this.name);
let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "stop" ], this.path); let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "stop" ], this.path);
if (exitCode !== 0) { if (exitCode !== 0) {
throw new Error("Failed to stop, please check the terminal output for more information."); throw new Error("Failed to stop, please check the terminal output for more information.");
@ -406,7 +426,7 @@ export class Stack {
} }
async restart(socket: DockgeSocket) : Promise<number> { async restart(socket: DockgeSocket) : Promise<number> {
const terminalName = getComposeTerminalName(this.name); const terminalName = getComposeTerminalName(socket.endpoint, this.name);
let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "restart" ], this.path); let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "restart" ], this.path);
if (exitCode !== 0) { if (exitCode !== 0) {
throw new Error("Failed to restart, please check the terminal output for more information."); throw new Error("Failed to restart, please check the terminal output for more information.");
@ -415,7 +435,7 @@ export class Stack {
} }
async down(socket: DockgeSocket) : Promise<number> { async down(socket: DockgeSocket) : Promise<number> {
const terminalName = getComposeTerminalName(this.name); const terminalName = getComposeTerminalName(socket.endpoint, this.name);
let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "down" ], this.path); let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "down" ], this.path);
if (exitCode !== 0) { if (exitCode !== 0) {
throw new Error("Failed to down, please check the terminal output for more information."); throw new Error("Failed to down, please check the terminal output for more information.");
@ -424,7 +444,7 @@ export class Stack {
} }
async update(socket: DockgeSocket) { async update(socket: DockgeSocket) {
const terminalName = getComposeTerminalName(this.name); const terminalName = getComposeTerminalName(socket.endpoint, this.name);
let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "pull" ], this.path); let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "pull" ], this.path);
if (exitCode !== 0) { if (exitCode !== 0) {
throw new Error("Failed to pull, please check the terminal output for more information."); throw new Error("Failed to pull, please check the terminal output for more information.");
@ -445,7 +465,7 @@ export class Stack {
} }
async joinCombinedTerminal(socket: DockgeSocket) { async joinCombinedTerminal(socket: DockgeSocket) {
const terminalName = getCombinedTerminalName(this.name); const terminalName = getCombinedTerminalName(socket.endpoint, this.name);
const terminal = Terminal.getOrCreateTerminal(this.server, terminalName, "docker", [ "compose", "logs", "-f", "--tail", "100" ], this.path); const terminal = Terminal.getOrCreateTerminal(this.server, terminalName, "docker", [ "compose", "logs", "-f", "--tail", "100" ], this.path);
terminal.enableKeepAlive = true; terminal.enableKeepAlive = true;
terminal.rows = COMBINED_TERMINAL_ROWS; terminal.rows = COMBINED_TERMINAL_ROWS;
@ -455,7 +475,7 @@ export class Stack {
} }
async leaveCombinedTerminal(socket: DockgeSocket) { async leaveCombinedTerminal(socket: DockgeSocket) {
const terminalName = getCombinedTerminalName(this.name); const terminalName = getCombinedTerminalName(socket.endpoint, this.name);
const terminal = Terminal.getTerminal(terminalName); const terminal = Terminal.getTerminal(terminalName);
if (terminal) { if (terminal) {
terminal.leave(socket); terminal.leave(socket);
@ -463,7 +483,7 @@ export class Stack {
} }
async joinContainerTerminal(socket: DockgeSocket, serviceName: string, shell : string = "sh", index: number = 0) { async joinContainerTerminal(socket: DockgeSocket, serviceName: string, shell : string = "sh", index: number = 0) {
const terminalName = getContainerExecTerminalName(this.name, serviceName, index); const terminalName = getContainerExecTerminalName(socket.endpoint, this.name, serviceName, index);
let terminal = Terminal.getTerminal(terminalName); let terminal = Terminal.getTerminal(terminalName);
if (!terminal) { if (!terminal) {

View File

@ -8,7 +8,7 @@ import {
PROGRESS_TERMINAL_ROWS, PROGRESS_TERMINAL_ROWS,
TERMINAL_COLS, TERMINAL_COLS,
TERMINAL_ROWS TERMINAL_ROWS
} from "./util-common"; } from "../common/util-common";
import { sync as commandExistsSync } from "command-exists"; import { sync as commandExistsSync } from "command-exists";
import { log } from "./log"; import { log } from "./log";
@ -34,6 +34,9 @@ export class Terminal {
public enableKeepAlive : boolean = false; public enableKeepAlive : boolean = false;
protected keepAliveInterval? : NodeJS.Timeout; protected keepAliveInterval? : NodeJS.Timeout;
protected kickDisconnectedClientsInterval? : NodeJS.Timeout;
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;
@ -82,13 +85,22 @@ export class Terminal {
return; return;
} }
this.kickDisconnectedClientsInterval = setInterval(() => {
for (const socketID in this.socketList) {
const socket = this.socketList[socketID];
if (!socket.connected) {
log.debug("Terminal", "Kicking disconnected client " + socket.id + " from terminal " + this.name);
this.leave(socket);
}
}
}, 60 * 1000);
if (this.enableKeepAlive) { if (this.enableKeepAlive) {
log.debug("Terminal", "Keep alive enabled for terminal " + this.name); log.debug("Terminal", "Keep alive enabled for terminal " + this.name);
// Close if there is no clients // Close if there is no clients
this.keepAliveInterval = setInterval(() => { this.keepAliveInterval = setInterval(() => {
const clients = this.server.io.sockets.adapter.rooms.get(this.name); const numClients = Object.keys(this.socketList).length;
const numClients = clients ? clients.size : 0;
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...");
@ -112,8 +124,10 @@ export class Terminal {
// On Data // On Data
this._ptyProcess.onData((data) => { this._ptyProcess.onData((data) => {
this.buffer.pushItem(data); this.buffer.pushItem(data);
if (this.server.io) {
this.server.io.to(this.name).emit("terminalWrite", this.name, data); for (const socketID in this.socketList) {
const socket = this.socketList[socketID];
socket.emitAgent("terminalWrite", this.name, data);
} }
}); });
@ -137,15 +151,19 @@ export class Terminal {
* @param res * @param res
*/ */
protected exit = (res : {exitCode: number, signal?: number | undefined}) => { protected exit = (res : {exitCode: number, signal?: number | undefined}) => {
this.server.io.to(this.name).emit("terminalExit", this.name, res.exitCode); for (const socketID in this.socketList) {
const socket = this.socketList[socketID];
socket.emitAgent("terminalExit", this.name, res.exitCode);
}
// Remove room // Remove all clients
this.server.io.in(this.name).socketsLeave(this.name); 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);
clearInterval(this.keepAliveInterval); clearInterval(this.keepAliveInterval);
clearInterval(this.kickDisconnectedClientsInterval);
if (this.callback) { if (this.callback) {
this.callback(res.exitCode); this.callback(res.exitCode);
@ -157,11 +175,11 @@ export class Terminal {
} }
public join(socket : DockgeSocket) { public join(socket : DockgeSocket) {
socket.join(this.name); this.socketList[socket.id] = socket;
} }
public leave(socket : DockgeSocket) { public leave(socket : DockgeSocket) {
socket.leave(this.name); delete this.socketList[socket.id];
} }
public get ptyProcess() { public get ptyProcess() {

View File

@ -2,10 +2,11 @@ import { Socket } from "socket.io";
import { Terminal } from "./terminal"; import { Terminal } from "./terminal";
import { randomBytes } from "crypto"; import { randomBytes } from "crypto";
import { log } from "./log"; import { log } from "./log";
import { ERROR_TYPE_VALIDATION } from "./util-common"; import { ERROR_TYPE_VALIDATION } from "../common/util-common";
import { R } from "redbean-node"; import { R } from "redbean-node";
import { verifyPassword } from "./password-hash"; import { verifyPassword } from "./password-hash";
import fs from "fs"; import fs from "fs";
import { AgentManager } from "./agent-manager";
export interface JWTDecoded { export interface JWTDecoded {
username : string; username : string;
@ -15,6 +16,9 @@ export interface JWTDecoded {
export interface DockgeSocket extends Socket { export interface DockgeSocket extends Socket {
userID: number; userID: number;
consoleTerminal? : Terminal; consoleTerminal? : Terminal;
instanceManager : AgentManager;
endpoint : string;
emitAgent : (eventName : string, ...args : unknown[]) => void;
} }
// For command line arguments, so they are nullable // For command line arguments, so they are nullable
@ -56,18 +60,28 @@ export function callbackError(error : unknown, callback : unknown) {
callback({ callback({
ok: false, ok: false,
msg: error.message, msg: error.message,
msgi18n: true,
}); });
} else if (error instanceof ValidationError) { } else if (error instanceof ValidationError) {
callback({ callback({
ok: false, ok: false,
type: ERROR_TYPE_VALIDATION, type: ERROR_TYPE_VALIDATION,
msg: error.message, msg: error.message,
msgi18n: true,
}); });
} else { } else {
log.debug("console", "Unknown error: " + error); log.debug("console", "Unknown error: " + error);
} }
} }
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?");

15
common/agent-socket.ts Normal file
View File

@ -0,0 +1,15 @@
export class AgentSocket {
eventList : Map<string, (...args : unknown[]) => void> = new Map();
on(event : string, callback : (...args : unknown[]) => void) {
this.eventList.set(event, callback);
}
call(eventName : string, ...args : unknown[]) {
const callback = this.eventList.get(eventName);
if (callback) {
callback(...args);
}
}
}

View File

@ -43,6 +43,8 @@ async function initRandomBytes() {
} }
} }
export const ALL_ENDPOINTS = "##ALL_DOCKGE_ENDPOINTS##";
// Stack Status // Stack Status
export const UNKNOWN = 0; export const UNKNOWN = 0;
export const CREATED_FILE = 1; export const CREATED_FILE = 1;
@ -206,20 +208,20 @@ export function getCryptoRandomInt(min: number, max: number):number {
} }
} }
export function getComposeTerminalName(stack : string) { export function getComposeTerminalName(endpoint : string, stack : string) {
return "compose-" + stack; return "compose-" + endpoint + "-" + stack;
} }
export function getCombinedTerminalName(stack : string) { export function getCombinedTerminalName(endpoint : string, stack : string) {
return "combined-" + stack; return "combined-" + endpoint + "-" + stack;
} }
export function getContainerTerminalName(container : string) { export function getContainerTerminalName(endpoint : string, container : string) {
return "container-" + container; return "container-" + endpoint + "-" + container;
} }
export function getContainerExecTerminalName(stackName : string, container : string, index : number) { export function getContainerExecTerminalName(endpoint : string, stackName : string, container : string, index : number) {
return "container-exec-" + stackName + "-" + container + "-" + index; return "container-exec-" + endpoint + "-" + stackName + "-" + container + "-" + index;
} }
export function copyYAMLComments(doc : Document, src : Document) { export function copyYAMLComments(doc : Document, src : Document) {
@ -289,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 : string) {
let hostname = defaultHostname;
let port; let port;
let display; let display;
@ -405,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

@ -5,7 +5,7 @@ import { User } from "../backend/models/user";
import { DockgeServer } from "../backend/dockge-server"; import { DockgeServer } from "../backend/dockge-server";
import { log } from "../backend/log"; import { log } from "../backend/log";
import { io } from "socket.io-client"; import { io } from "socket.io-client";
import { BaseRes } from "../backend/util-common"; import { BaseRes } from "../common/util-common";
console.log("== Dockge Reset Password Tool =="); console.log("== Dockge Reset Password Tool ==");
@ -92,7 +92,6 @@ function disconnectAllSocketClients(username : string, password : string) : Prom
// Disconnect all socket connections // Disconnect all socket connections
const socket = io(url, { const socket = io(url, {
transports: [ "websocket" ],
reconnection: false, reconnection: false,
timeout: 5000, timeout: 5000,
}); });

View File

@ -137,7 +137,7 @@
<script> <script>
import { defineComponent } from "vue"; import { defineComponent } from "vue";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
import { parseDockerPort } from "../../../backend/util-common"; import { parseDockerPort } from "../../../common/util-common";
export default defineComponent({ export default defineComponent({
components: { components: {
@ -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

@ -65,6 +65,10 @@ export default {
editorFocus() { editorFocus() {
return this.$parent.$parent.editorFocus; return this.$parent.$parent.editorFocus;
}, },
endpoint() {
return this.$parent.$parent.endpoint;
},
}, },
watch: { watch: {
"jsonConfig.networks": { "jsonConfig.networks": {
@ -134,7 +138,7 @@ export default {
}, },
loadExternalNetworkList() { loadExternalNetworkList() {
this.$root.getSocket().emit("getDockerNetworkList", (res) => { this.$root.emitAgent(this.endpoint, "getDockerNetworkList", (res) => {
if (res.ok) { if (res.ok) {
this.externalNetworkList = res.dockerNetworkList.filter((n) => { this.externalNetworkList = res.dockerNetworkList.filter((n) => {
// Filter out this stack networks // Filter out this stack networks

View File

@ -43,7 +43,7 @@
</div> </div>
</div> </div>
<div ref="stackList" class="stack-list" :class="{ scrollbar: scrollbar }" :style="stackListStyle"> <div ref="stackList" class="stack-list" :class="{ scrollbar: scrollbar }" :style="stackListStyle">
<div v-if="Object.keys($root.stackList).length === 0" class="text-center mt-3"> <div v-if="Object.keys(sortedStackList).length === 0" class="text-center mt-3">
<router-link to="/compose">{{ $t("addFirstStackMsg") }}</router-link> <router-link to="/compose">{{ $t("addFirstStackMsg") }}</router-link>
</div> </div>
@ -67,7 +67,7 @@
<script> <script>
import Confirm from "../components/Confirm.vue"; import Confirm from "../components/Confirm.vue";
import StackListItem from "../components/StackListItem.vue"; import StackListItem from "../components/StackListItem.vue";
import { CREATED_FILE, CREATED_STACK, EXITED, RUNNING, UNKNOWN } from "../../../backend/util-common"; import { CREATED_FILE, CREATED_STACK, EXITED, RUNNING, UNKNOWN } from "../../../common/util-common";
export default { export default {
components: { components: {
@ -120,7 +120,7 @@ export default {
* @returns {Array} The sorted list of stacks. * @returns {Array} The sorted list of stacks.
*/ */
sortedStackList() { sortedStackList() {
let result = Object.values(this.$root.stackList); let result = Object.values(this.$root.completeStackList);
result = result.filter(stack => { result = result.filter(stack => {
// filter by search text // filter by search text
@ -160,6 +160,7 @@ export default {
return 1; return 1;
} }
// sort by status
if (m1.status !== m2.status) { if (m1.status !== m2.status) {
if (m2.status === RUNNING) { if (m2.status === RUNNING) {
return 1; return 1;

View File

@ -1,12 +1,14 @@
<template> <template>
<router-link :to="`/compose/${stack.name}`" :class="{ 'dim' : !stack.isManagedByDockge }" class="item"> <router-link :to="url" :class="{ 'dim' : !stack.isManagedByDockge }" class="item">
<Uptime :stack="stack" :fixed-width="true" class="me-2" /> <Uptime :stack="stack" :fixed-width="true" class="me-2" />
<span class="title">{{ stackName }}</span> <div class="title">
<span>{{ stackName }}</span>
<div v-if="$root.agentCount > 1" class="endpoint">{{ endpointDisplay }}</div>
</div>
</router-link> </router-link>
</template> </template>
<script> <script>
import Uptime from "./Uptime.vue"; import Uptime from "./Uptime.vue";
export default { export default {
@ -51,6 +53,16 @@ export default {
}; };
}, },
computed: { computed: {
endpointDisplay() {
return this.$root.endpointDisplayFunction(this.stack.endpoint);
},
url() {
if (this.stack.endpoint) {
return `/compose/${this.stack.name}/${this.stack.endpoint}`;
} else {
return `/compose/${this.stack.name}`;
}
},
depthMargin() { depthMargin() {
return { return {
marginLeft: `${31 * this.depth}px`, marginLeft: `${31 * this.depth}px`,
@ -117,16 +129,31 @@ export default {
padding-right: 2px !important; padding-right: 2px !important;
} }
// .stack-item { .item {
// width: 100%; text-decoration: none;
// }
.tags {
margin-top: 4px;
padding-left: 67px;
display: flex; display: flex;
flex-wrap: wrap; align-items: center;
gap: 0; min-height: 52px;
border-radius: 10px;
transition: all ease-in-out 0.15s;
width: 100%;
padding: 5px 8px;
&.disabled {
opacity: 0.3;
}
&:hover {
background-color: $highlight-white;
}
&.active {
background-color: #cdf8f4;
}
.title {
margin-top: -4px;
}
.endpoint {
font-size: 12px;
color: $dark-font-color3;
}
} }
.collapsed { .collapsed {

View File

@ -7,8 +7,7 @@
<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 "../../../backend/util-common";
export default { export default {
/** /**
@ -24,6 +23,11 @@ export default {
require: true, require: true,
}, },
endpoint: {
type: String,
require: true,
},
// Require if mode is interactive // Require if mode is interactive
stackName: { stackName: {
type: String, type: String,
@ -110,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);
} }
@ -134,15 +138,15 @@ export default {
}, },
methods: { methods: {
bind(name) { bind(endpoint, name) {
// Workaround: normally this.name should be set, but it is not sometimes, so we use the parameter, but eventually this.name and name must be the same name // Workaround: normally this.name should be set, but it is not sometimes, so we use the parameter, but eventually this.name and name must be the same name
if (name) { if (name) {
this.$root.unbindTerminal(name); this.$root.unbindTerminal(name);
this.$root.bindTerminal(name, this.terminal); this.$root.bindTerminal(endpoint, name, this.terminal);
console.debug("Terminal bound via parameter: " + name); console.debug("Terminal bound via parameter: " + name);
} else if (this.name) { } else if (this.name) {
this.$root.unbindTerminal(this.name); this.$root.unbindTerminal(this.name);
this.$root.bindTerminal(this.name, this.terminal); this.$root.bindTerminal(this.endpoint, this.name, this.terminal);
console.debug("Terminal bound: " + this.name); console.debug("Terminal bound: " + this.name);
} else { } else {
console.debug("Terminal name not set"); console.debug("Terminal name not set");
@ -173,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);
}); });
@ -192,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++;
@ -205,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);
} }
@ -234,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

@ -3,7 +3,7 @@
</template> </template>
<script> <script>
import { statusColor, statusNameShort } from "../../../backend/util-common"; import { statusColor, statusNameShort } from "../../../common/util-common";
export default { export default {
props: { props: {

View File

@ -99,5 +99,17 @@
"connecting...": "Connecting to the socket server…", "connecting...": "Connecting to the socket server…",
"url": "URL | URLs", "url": "URL | URLs",
"extra": "Extra", "extra": "Extra",
"newUpdate": "New Update" "newUpdate": "New Update",
"dockgeAgent": "Dockge Agent | Dockge Agents",
"currentEndpoint": "Current",
"dockgeURL": "Dockge URL (e.g. http://127.0.0.1:5001)",
"agentOnline": "Online",
"agentOffline": "Offline",
"connecting": "Connecting",
"connect": "Connect",
"addAgent": "Add Agent",
"agentAddedSuccessfully": "Agent added successfully.",
"agentRemovedSuccessfully": "Agent removed successfully.",
"removeAgent": "Remove Agent",
"removeAgentMsg": "Are you sure you want to remove this agent?"
} }

View File

@ -98,6 +98,7 @@
<script> <script>
import Login from "../components/Login.vue"; import Login from "../components/Login.vue";
import { compareVersions } from "compare-versions"; import { compareVersions } from "compare-versions";
import { ALL_ENDPOINTS } from "../../../common/util-common";
export default { export default {
@ -145,7 +146,7 @@ export default {
methods: { methods: {
scanFolder() { scanFolder() {
this.$root.getSocket().emit("requestStackList", (res) => { this.$root.emitAgent(ALL_ENDPOINTS, "requestStackList", (res) => {
this.$root.toastRes(res); this.$root.toastRes(res);
}); });
}, },

View File

@ -1,5 +1,5 @@
// Dayjs init inside this, so it has to be the first import // Dayjs init inside this, so it has to be the first import
import "../../backend/util-common"; import "../../common/util-common";
import { createApp, defineComponent, h } from "vue"; import { createApp, defineComponent, h } from "vue";
import App from "./App.vue"; import App from "./App.vue";

View File

@ -3,6 +3,7 @@ import { Socket } from "socket.io-client";
import { defineComponent } from "vue"; import { defineComponent } from "vue";
import jwtDecode from "jwt-decode"; import jwtDecode from "jwt-decode";
import { Terminal } from "@xterm/xterm"; import { Terminal } from "@xterm/xterm";
import { AgentSocket } from "../../../common/agent-socket";
let socket : Socket; let socket : Socket;
@ -28,16 +29,51 @@ export default defineComponent({
loggedIn: false, loggedIn: false,
allowLoginDialog: false, allowLoginDialog: false,
username: null, username: null,
stackList: {},
composeTemplate: "", composeTemplate: "",
stackList: {},
// All stack list from all agents
allAgentStackList: {} as Record<string, object>,
// online / offline / connecting
agentStatusList: {
},
// Agent List
agentList: {
},
}; };
}, },
computed: { computed: {
agentCount() {
return Object.keys(this.agentList).length;
},
completeStackList() {
let list : Record<string, object> = {};
for (let stackName in this.stackList) {
list[stackName + "_"] = this.stackList[stackName];
}
for (let endpoint in this.allAgentStackList) {
let instance = this.allAgentStackList[endpoint];
for (let stackName in instance.stackList) {
list[stackName + "_" + endpoint] = instance.stackList[stackName];
}
}
return list;
},
usernameFirstChar() { usernameFirstChar() {
if (typeof this.username == "string" && this.username.length >= 1) { if (typeof this.username == "string" && this.username.length >= 1) {
return this.username.charAt(0).toUpperCase(); return this.username.charAt(0).toUpperCase();
} else { } else {
return "🐻"; return "🐬";
} }
}, },
@ -65,6 +101,15 @@ export default defineComponent({
}, },
watch: { watch: {
"socketIO.connected"() {
if (this.socketIO.connected) {
this.agentStatusList[""] = "online";
} else {
this.agentStatusList[""] = "offline";
}
},
remember() { remember() {
localStorage.remember = (this.remember) ? "1" : "0"; localStorage.remember = (this.remember) ? "1" : "0";
}, },
@ -84,6 +129,15 @@ export default defineComponent({
}, },
methods: { methods: {
endpointDisplayFunction(endpoint : string) {
if (endpoint) {
return endpoint;
} else {
return this.$t("currentEndpoint");
}
},
/** /**
* Initialize connection to socket server * Initialize connection to socket server
* @param bypass Should the check for if we * @param bypass Should the check for if we
@ -108,8 +162,12 @@ export default defineComponent({
this.socketIO.connecting = true; this.socketIO.connecting = true;
}, 1500); }, 1500);
socket = io(url, { socket = io(url);
transports: [ "websocket", "polling" ]
// Handling events from agents
let agentSocket = new AgentSocket();
socket.on("agent", (eventName : unknown, ...args : unknown[]) => {
agentSocket.call(eventName, ...args);
}); });
socket.on("connect", () => { socket.on("connect", () => {
@ -177,7 +235,7 @@ export default defineComponent({
this.$router.push("/setup"); this.$router.push("/setup");
}); });
socket.on("terminalWrite", (terminalName, data) => { agentSocket.on("terminalWrite", (terminalName, data) => {
const terminal = terminalMap.get(terminalName); const terminal = terminalMap.get(terminalName);
if (!terminal) { if (!terminal) {
//console.error("Terminal not found: " + terminalName); //console.error("Terminal not found: " + terminalName);
@ -186,9 +244,18 @@ export default defineComponent({
terminal.write(data); terminal.write(data);
}); });
socket.on("stackList", (res) => { agentSocket.on("stackList", (res) => {
if (res.ok) { if (res.ok) {
if (!res.endpoint) {
this.stackList = res.stackList; this.stackList = res.stackList;
} else {
if (!this.allAgentStackList[res.endpoint]) {
this.allAgentStackList[res.endpoint] = {
stackList: {},
};
}
this.allAgentStackList[res.endpoint].stackList = res.stackList;
}
} }
}); });
@ -203,6 +270,21 @@ export default defineComponent({
} }
}); });
socket.on("agentStatus", (res) => {
this.agentStatusList[res.endpoint] = res.status;
if (res.msg) {
this.toastError(res.msg);
}
});
socket.on("agentList", (res) => {
console.log(res);
if (res.ok) {
this.agentList = res.agentList;
}
});
socket.on("refresh", () => { socket.on("refresh", () => {
location.reload(); location.reload();
}); });
@ -220,6 +302,10 @@ export default defineComponent({
return socket; return socket;
}, },
emitAgent(endpoint : string, eventName : string, ...args : unknown[]) {
this.getSocket().emit("agent", endpoint, eventName, ...args);
},
/** /**
* Get payload of JWT cookie * Get payload of JWT cookie
* @returns {(object | undefined)} JWT payload * @returns {(object | undefined)} JWT payload
@ -310,9 +396,9 @@ export default defineComponent({
}, },
bindTerminal(terminalName : string, terminal : Terminal) { bindTerminal(endpoint : string, terminalName : string, terminal : Terminal) {
// Load terminal, get terminal screen // Load terminal, get terminal screen
socket.emit("terminalJoin", terminalName, (res) => { this.emitAgent(endpoint, "terminalJoin", terminalName, (res) => {
if (res.ok) { if (res.ok) {
terminal.write(res.buffer); terminal.write(res.buffer);
terminalMap.set(terminalName, terminal); terminalMap.set(terminalName, terminal);

View File

@ -2,7 +2,12 @@
<transition name="slide-fade" appear> <transition name="slide-fade" appear>
<div> <div>
<h1 v-if="isAdd" class="mb-3">Compose</h1> <h1 v-if="isAdd" class="mb-3">Compose</h1>
<h1 v-else class="mb-3"><Uptime :stack="globalStack" :pill="true" /> {{ stack.name }}</h1> <h1 v-else class="mb-3">
<Uptime :stack="globalStack" :pill="true" /> {{ stack.name }}
<span v-if="$root.agentCount > 1" class="agent-name">
({{ endpointDisplay }})
</span>
</h1>
<div v-if="stack.isManagedByDockge" class="mb-3"> <div v-if="stack.isManagedByDockge" class="mb-3">
<div class="btn-group me-2" role="group"> <div class="btn-group me-2" role="group">
@ -70,6 +75,7 @@
ref="progressTerminal" ref="progressTerminal"
class="mb-3 terminal" class="mb-3 terminal"
:name="terminalName" :name="terminalName"
:endpoint="endpoint"
:rows="progressTerminalRows" :rows="progressTerminalRows"
@has-data="showProgressTerminal = true; submitted = true;" @has-data="showProgressTerminal = true; submitted = true;"
></Terminal> ></Terminal>
@ -87,6 +93,16 @@
<input id="name" v-model="stack.name" type="text" class="form-control" required @blur="stackNameToLowercase"> <input id="name" v-model="stack.name" type="text" class="form-control" required @blur="stackNameToLowercase">
<div class="form-text">{{ $t("Lowercase only") }}</div> <div class="form-text">{{ $t("Lowercase only") }}</div>
</div> </div>
<!-- Endpoint -->
<div class="mt-3">
<label for="name" class="form-label">{{ $t("dockgeAgent") }}</label>
<select v-model="stack.endpoint" class="form-select">
<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> </div>
</div> </div>
@ -139,6 +155,7 @@
ref="combinedTerminal" ref="combinedTerminal"
class="mb-3 terminal" class="mb-3 terminal"
:name="combinedTerminalName" :name="combinedTerminalName"
:endpoint="endpoint"
:rows="combinedTerminalRows" :rows="combinedTerminalRows"
:cols="combinedTerminalCols" :cols="combinedTerminalCols"
style="height: 350px;" style="height: 350px;"
@ -236,7 +253,7 @@ import {
getComposeTerminalName, getComposeTerminalName,
PROGRESS_TERMINAL_ROWS, PROGRESS_TERMINAL_ROWS,
RUNNING RUNNING
} from "../../../backend/util-common"; } from "../../../common/util-common";
import { BModal } from "bootstrap-vue-next"; import { BModal } from "bootstrap-vue-next";
import NetworkInput from "../components/NetworkInput.vue"; import NetworkInput from "../components/NetworkInput.vue";
import dotenv from "dotenv"; import dotenv from "dotenv";
@ -298,6 +315,10 @@ export default {
}, },
computed: { computed: {
endpointDisplay() {
return this.$root.endpointDisplayFunction(this.endpoint);
},
urls() { urls() {
if (!this.envsubstJSONConfig["x-dockge"] || !this.envsubstJSONConfig["x-dockge"].urls || !Array.isArray(this.envsubstJSONConfig["x-dockge"].urls)) { if (!this.envsubstJSONConfig["x-dockge"] || !this.envsubstJSONConfig["x-dockge"].urls || !Array.isArray(this.envsubstJSONConfig["x-dockge"].urls)) {
return []; return [];
@ -334,7 +355,7 @@ export default {
* @return {*} * @return {*}
*/ */
globalStack() { globalStack() {
return this.$root.stackList[this.stack.name]; return this.$root.completeStackList[this.stack.name + "_" + this.endpoint];
}, },
status() { status() {
@ -349,20 +370,31 @@ export default {
if (!this.stack.name) { if (!this.stack.name) {
return ""; return "";
} }
return getComposeTerminalName(this.stack.name); return getComposeTerminalName(this.endpoint, this.stack.name);
}, },
combinedTerminalName() { combinedTerminalName() {
if (!this.stack.name) { if (!this.stack.name) {
return ""; return "";
} }
return getCombinedTerminalName(this.stack.name); return getCombinedTerminalName(this.endpoint, this.stack.name);
}, },
networks() { networks() {
return this.jsonConfig.networks; return this.jsonConfig.networks;
} },
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": {
@ -405,9 +437,7 @@ export default {
}, },
$route(to, from) { $route(to, from) {
// Leave Combined Terminal
console.debug("leaveCombinedTerminal", from.params.stackName);
this.$root.getSocket().emit("leaveCombinedTerminal", this.stack.name, () => {});
} }
}, },
mounted() { mounted() {
@ -437,6 +467,7 @@ export default {
composeYAML, composeYAML,
composeENV, composeENV,
isManagedByDockge: true, isManagedByDockge: true,
endpoint: "",
}; };
this.yamlCodeChange(); this.yamlCodeChange();
@ -449,11 +480,9 @@ export default {
this.requestServiceStatus(); this.requestServiceStatus();
}, },
unmounted() { unmounted() {
this.stopServiceStatusTimeout = true;
clearTimeout(serviceStatusTimeout);
}, },
methods: { methods: {
startServiceStatusTimeout() { startServiceStatusTimeout() {
clearTimeout(serviceStatusTimeout); clearTimeout(serviceStatusTimeout);
serviceStatusTimeout = setTimeout(async () => { serviceStatusTimeout = setTimeout(async () => {
@ -462,7 +491,7 @@ export default {
}, },
requestServiceStatus() { requestServiceStatus() {
this.$root.getSocket().emit("serviceStatusList", this.stack.name, (res) => { this.$root.emitAgent(this.endpoint, "serviceStatusList", this.stack.name, (res) => {
if (res.ok) { if (res.ok) {
this.serviceStatusList = res.serviceStatusList; this.serviceStatusList = res.serviceStatusList;
} }
@ -475,22 +504,34 @@ export default {
exitConfirm(next) { exitConfirm(next) {
if (this.isEditMode) { if (this.isEditMode) {
if (confirm("You are currently editing a stack. Are you sure you want to leave?")) { if (confirm("You are currently editing a stack. Are you sure you want to leave?")) {
this.exitAction();
next(); next();
} else { } else {
next(false); next(false);
} }
} else { } else {
this.exitAction();
next(); next();
} }
}, },
exitAction() {
console.log("exitAction");
this.stopServiceStatusTimeout = true;
clearTimeout(serviceStatusTimeout);
// Leave Combined Terminal
console.debug("leaveCombinedTerminal", this.endpoint, this.stack.name);
this.$root.emitAgent(this.endpoint, "leaveCombinedTerminal", this.stack.name, () => {});
},
bindTerminal() { bindTerminal() {
this.$refs.progressTerminal?.bind(this.terminalName); this.$refs.progressTerminal?.bind(this.endpoint, this.terminalName);
}, },
loadStack() { loadStack() {
this.processing = true; this.processing = true;
this.$root.getSocket().emit("getStack", this.stack.name, (res) => { this.$root.emitAgent(this.endpoint, "getStack", this.stack.name, (res) => {
if (res.ok) { if (res.ok) {
this.stack = res.stack; this.stack = res.stack;
this.yamlCodeChange(); this.yamlCodeChange();
@ -532,15 +573,15 @@ export default {
} }
} }
this.bindTerminal(this.terminalName); this.bindTerminal();
this.$root.getSocket().emit("deployStack", this.stack.name, this.stack.composeYAML, this.stack.composeENV, this.isAdd, (res) => { this.$root.emitAgent(this.stack.endpoint, "deployStack", this.stack.name, this.stack.composeYAML, this.stack.composeENV, this.isAdd, (res) => {
this.processing = false; this.processing = false;
this.$root.toastRes(res); this.$root.toastRes(res);
if (res.ok) { if (res.ok) {
this.isEditMode = false; this.isEditMode = false;
this.$router.push("/compose/" + this.stack.name); this.$router.push(this.url);
} }
}); });
}, },
@ -548,13 +589,13 @@ export default {
saveStack() { saveStack() {
this.processing = true; this.processing = true;
this.$root.getSocket().emit("saveStack", this.stack.name, this.stack.composeYAML, this.stack.composeENV, this.isAdd, (res) => { this.$root.emitAgent(this.stack.endpoint, "saveStack", this.stack.name, this.stack.composeYAML, this.stack.composeENV, this.isAdd, (res) => {
this.processing = false; this.processing = false;
this.$root.toastRes(res); this.$root.toastRes(res);
if (res.ok) { if (res.ok) {
this.isEditMode = false; this.isEditMode = false;
this.$router.push("/compose/" + this.stack.name); this.$router.push(this.url);
} }
}); });
}, },
@ -562,7 +603,7 @@ export default {
startStack() { startStack() {
this.processing = true; this.processing = true;
this.$root.getSocket().emit("startStack", this.stack.name, (res) => { this.$root.emitAgent(this.endpoint, "startStack", this.stack.name, (res) => {
this.processing = false; this.processing = false;
this.$root.toastRes(res); this.$root.toastRes(res);
}); });
@ -571,7 +612,7 @@ export default {
stopStack() { stopStack() {
this.processing = true; this.processing = true;
this.$root.getSocket().emit("stopStack", this.stack.name, (res) => { this.$root.emitAgent(this.endpoint, "stopStack", this.stack.name, (res) => {
this.processing = false; this.processing = false;
this.$root.toastRes(res); this.$root.toastRes(res);
}); });
@ -580,7 +621,7 @@ export default {
downStack() { downStack() {
this.processing = true; this.processing = true;
this.$root.getSocket().emit("downStack", this.stack.name, (res) => { this.$root.emitAgent(this.endpoint, "downStack", this.stack.name, (res) => {
this.processing = false; this.processing = false;
this.$root.toastRes(res); this.$root.toastRes(res);
}); });
@ -589,7 +630,7 @@ export default {
restartStack() { restartStack() {
this.processing = true; this.processing = true;
this.$root.getSocket().emit("restartStack", this.stack.name, (res) => { this.$root.emitAgent(this.endpoint, "restartStack", this.stack.name, (res) => {
this.processing = false; this.processing = false;
this.$root.toastRes(res); this.$root.toastRes(res);
}); });
@ -598,14 +639,14 @@ export default {
updateStack() { updateStack() {
this.processing = true; this.processing = true;
this.$root.getSocket().emit("updateStack", this.stack.name, (res) => { this.$root.emitAgent(this.endpoint, "updateStack", this.stack.name, (res) => {
this.processing = false; this.processing = false;
this.$root.toastRes(res); this.$root.toastRes(res);
}); });
}, },
deleteDialog() { deleteDialog() {
this.$root.getSocket().emit("deleteStack", this.stack.name, (res) => { this.$root.emitAgent(this.endpoint, "deleteStack", this.stack.name, (res) => {
this.$root.toastRes(res); this.$root.toastRes(res);
if (res.ok) { if (res.ok) {
this.$router.push("/"); this.$router.push("/");
@ -750,6 +791,8 @@ export default {
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
@import "../styles/vars.scss";
.terminal { .terminal {
height: 200px; height: 200px;
} }
@ -761,4 +804,9 @@ export default {
background-color: #2c2f38 !important; background-color: #2c2f38 !important;
} }
} }
.agent-name {
font-size: 13px;
color: $dark-font-color3;
}
</style> </style>

View File

@ -15,14 +15,14 @@
</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>
<script> <script>
import { allowedCommandList } from "../../../backend/util-common"; import { allowedCommandList } from "../../../common/util-common";
export default { export default {
components: { components: {
@ -32,6 +32,11 @@ export default {
allowedCommandList, allowedCommandList,
}; };
}, },
computed: {
endpoint() {
return this.$route.params.endpoint || "";
},
},
mounted() { mounted() {
}, },

View File

@ -7,13 +7,13 @@
<router-link :to="sh" class="btn btn-normal me-2">Switch to sh</router-link> <router-link :to="sh" class="btn btn-normal me-2">Switch to sh</router-link>
</div> </div>
<Terminal class="terminal" :rows="20" mode="interactive" :name="terminalName" :stack-name="stackName" :service-name="serviceName" :shell="shell"></Terminal> <Terminal class="terminal" :rows="20" mode="interactive" :name="terminalName" :stack-name="stackName" :service-name="serviceName" :shell="shell" :endpoint="endpoint"></Terminal>
</div> </div>
</transition> </transition>
</template> </template>
<script> <script>
import { getContainerExecTerminalName } from "../../../backend/util-common"; import { getContainerExecTerminalName } from "../../../common/util-common";
export default { export default {
components: { components: {
@ -27,6 +27,9 @@ export default {
stackName() { stackName() {
return this.$route.params.stackName; return this.$route.params.stackName;
}, },
endpoint() {
return this.$route.params.endpoint || "";
},
shell() { shell() {
return this.$route.params.type; return this.$route.params.type;
}, },
@ -34,10 +37,12 @@ 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() {
return { let endpoint = this.$route.params.endpoint;
let data = {
name: "containerTerminal", name: "containerTerminal",
params: { params: {
stackName: this.stackName, stackName: this.stackName,
@ -45,6 +50,13 @@ export default {
type: "sh", type: "sh",
}, },
}; };
if (endpoint) {
data.name = "containerTerminalEndpoint";
data.params.endpoint = endpoint;
}
return data;
}, },
}, },
mounted() { mounted() {

View File

@ -5,6 +5,10 @@
{{ $t("home") }} {{ $t("home") }}
</h1> </h1>
<div class="row first-row">
<!-- Left -->
<div class="col-md-7">
<!-- Stats -->
<div class="shadow-box big-padding text-center mb-4"> <div class="shadow-box big-padding text-center mb-4">
<div class="row"> <div class="row">
<div class="col"> <div class="col">
@ -22,19 +26,76 @@
</div> </div>
</div> </div>
<!-- Docker Run -->
<h2 class="mb-3">{{ $t("Docker Run") }}</h2> <h2 class="mb-3">{{ $t("Docker Run") }}</h2>
<div class="mb-3"> <div class="mb-3">
<textarea id="name" v-model="dockerRunCommand" type="text" class="form-control docker-run" required placeholder="docker run ..."></textarea> <textarea id="name" v-model="dockerRunCommand" type="text" class="form-control docker-run" required placeholder="docker run ..."></textarea>
</div> </div>
<button class="btn-normal btn" @click="convertDockerRun">{{ $t("Convert to Compose") }}</button> <button class="btn-normal btn mb-4" @click="convertDockerRun">{{ $t("Convert to Compose") }}</button>
</div>
<!-- Right -->
<div class="col-md-5">
<!-- Agent List -->
<div class="shadow-box big-padding">
<h4 class="mb-3">{{ $tc("dockgeAgent", 2) }} <span class="badge bg-warning" style="font-size: 12px;">beta</span></h4>
<div v-for="(agent, endpoint) in $root.agentList" :key="endpoint" class="mb-3 agent">
<!-- Agent Status -->
<template v-if="$root.agentStatusList[endpoint]">
<span v-if="$root.agentStatusList[endpoint] === 'online'" class="badge bg-primary me-2">{{ $t("agentOnline") }}</span>
<span v-else-if="$root.agentStatusList[endpoint] === 'offline'" class="badge bg-danger me-2">{{ $t("agentOffline") }}</span>
<span v-else class="badge bg-secondary me-2">{{ $t($root.agentStatusList[endpoint]) }}</span>
</template>
<!-- Agent Display Name -->
<span v-if="endpoint === ''">{{ $t("currentEndpoint") }}</span>
<a v-else :href="agent.url" target="_blank">{{ endpoint }}</a>
<!-- Remove Button -->
<font-awesome-icon v-if="endpoint !== ''" class="ms-2 remove-agent" icon="trash" @click="showRemoveAgentDialog[agent.url] = !showRemoveAgentDialog[agent.url]" />
<!-- Remoe Agent Dialog -->
<BModal v-model="showRemoveAgentDialog[agent.url]" :okTitle="$t('removeAgent')" okVariant="danger" @ok="removeAgent(agent.url)">
<p>{{ agent.url }}</p>
{{ $t("removeAgentMsg") }}
</BModal>
</div>
<button v-if="!showAgentForm" class="btn btn-normal" @click="showAgentForm = !showAgentForm">{{ $t("addAgent") }}</button>
<!-- Add Agent Form -->
<form v-if="showAgentForm" @submit.prevent="addAgent">
<div class="mb-3">
<label for="url" class="form-label">{{ $t("dockgeURL") }}</label>
<input id="url" v-model="agent.url" type="url" class="form-control" required placeholder="http://">
</div>
<div class="mb-3">
<label for="username" class="form-label">{{ $t("Username") }}</label>
<input id="username" v-model="agent.username" type="text" class="form-control" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">{{ $t("Password") }}</label>
<input id="password" v-model="agent.password" type="password" class="form-control" required autocomplete="new-password">
</div>
<button type="submit" class="btn btn-primary" :disabled="connectingAgent">
<template v-if="connectingAgent">{{ $t("connecting") }}</template>
<template v-else>{{ $t("connect") }}</template>
</button>
</form>
</div>
</div>
</div>
</div> </div>
</transition> </transition>
<router-view ref="child" /> <router-view ref="child" />
</template> </template>
<script> <script>
import { statusNameShort } from "../../../backend/util-common"; import { statusNameShort } from "../../../common/util-common";
export default { export default {
components: { components: {
@ -58,6 +119,14 @@ export default {
importantHeartBeatListLength: 0, importantHeartBeatListLength: 0,
displayedRecords: [], displayedRecords: [],
dockerRunCommand: "", dockerRunCommand: "",
showAgentForm: false,
showRemoveAgentDialog: {},
connectingAgent: false,
agent: {
url: "http://",
username: "",
password: "",
}
}; };
}, },
@ -98,11 +167,43 @@ export default {
methods: { methods: {
addAgent() {
this.connectingAgent = true;
this.$root.getSocket().emit("addAgent", this.agent, (res) => {
this.$root.toastRes(res);
if (res.ok) {
this.showAgentForm = false;
this.agent = {
url: "http://",
username: "",
password: "",
};
}
this.connectingAgent = false;
});
},
removeAgent(url) {
this.$root.getSocket().emit("removeAgent", url, (res) => {
if (res.ok) {
this.$root.toastRes(res);
let urlObj = new URL(url);
let endpoint = urlObj.host;
// Remove the stack list and status list of the removed agent
delete this.$root.allAgentStackList[endpoint];
}
});
},
getStatusNum(statusName) { getStatusNum(statusName) {
let num = 0; let num = 0;
for (let stackName in this.$root.stackList) { for (let stackName in this.$root.completeStackList) {
const stack = this.$root.stackList[stackName]; const stack = this.$root.completeStackList[stackName];
if (statusNameShort(stack.status) === statusName) { if (statusNameShort(stack.status) === statusName) {
num += 1; num += 1;
} }
@ -230,4 +331,20 @@ table {
font-family: 'JetBrains Mono', monospace; font-family: 'JetBrains Mono', monospace;
font-size: 15px; font-size: 15px;
} }
.first-row .shadow-box {
}
.remove-agent {
cursor: pointer;
color: rgba(255, 255, 255, 0.3);
}
.agent {
a {
text-decoration: none;
}
}
</style> </style>

View File

@ -35,22 +35,33 @@ const routes = [
component: Compose, component: Compose,
}, },
{ {
path: "/compose/:stackName", path: "/compose/:stackName/:endpoint",
name: "compose", component: Compose,
},
{
path: "/compose/:stackName",
component: Compose, component: Compose,
props: true,
}, },
{ {
path: "/terminal/:stackName/:serviceName/:type", path: "/terminal/:stackName/:serviceName/:type",
component: ContainerTerminal, component: ContainerTerminal,
name: "containerTerminal", name: "containerTerminal",
}, },
{
path: "/terminal/:stackName/:serviceName/:type/:endpoint",
component: ContainerTerminal,
name: "containerTerminalEndpoint",
},
] ]
}, },
{ {
path: "/console", path: "/console",
component: Console, component: Console,
}, },
{
path: "/console/:endpoint",
component: Console,
},
{ {
path: "/settings", path: "/settings",
component: Settings, component: Settings,

View File

@ -14,9 +14,11 @@
"dev:backend": "cross-env NODE_ENV=development tsx watch --inspect ./backend/index.ts", "dev:backend": "cross-env NODE_ENV=development tsx watch --inspect ./backend/index.ts",
"dev:frontend": "cross-env NODE_ENV=development vite --host --config ./frontend/vite.config.ts", "dev:frontend": "cross-env NODE_ENV=development vite --host --config ./frontend/vite.config.ts",
"release-final": "tsx ./extra/test-docker.ts && tsx extra/update-version.ts && pnpm run build:frontend && npm run build:docker", "release-final": "tsx ./extra/test-docker.ts && tsx extra/update-version.ts && pnpm run build:frontend && npm run build:docker",
"release-beta": "tsx ./extra/test-docker.ts && tsx extra/update-version.ts && pnpm run build:frontend && npm run build:docker-beta",
"build:frontend": "vite build --config ./frontend/vite.config.ts", "build:frontend": "vite build --config ./frontend/vite.config.ts",
"build:docker-base": "docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/dockge:base -f ./docker/Base.Dockerfile . --push", "build:docker-base": "docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/dockge:base -f ./docker/Base.Dockerfile . --push",
"build:docker": "node ./extra/env2arg.js docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/dockge:latest -t louislam/dockge:1 -t louislam/dockge:$VERSION --target release -f ./docker/Dockerfile . --push", "build:docker": "node ./extra/env2arg.js docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/dockge:latest -t louislam/dockge:1 -t louislam/dockge:$VERSION --target release -f ./docker/Dockerfile . --push",
"build:docker-beta": "node ./extra/env2arg.js docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/dockge:beta -t louislam/dockge:$VERSION --target release -f ./docker/Dockerfile . --push",
"build:docker-nightly": "pnpm run build:frontend && docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/dockge:nightly --target nightly -f ./docker/Dockerfile . --push", "build:docker-nightly": "pnpm run build:frontend && docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/dockge:nightly --target nightly -f ./docker/Dockerfile . --push",
"build:healthcheck": "docker buildx build -f docker/BuildHealthCheck.Dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/dockge:build-healthcheck . --push", "build:healthcheck": "docker buildx build -f docker/BuildHealthCheck.Dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/dockge:build-healthcheck . --push",
"start-docker": "docker run --rm -p 5001:5001 --name dockge louislam/dockge:latest", "start-docker": "docker run --rm -p 5001:5001 --name dockge louislam/dockge:latest",
@ -46,6 +48,7 @@
"mysql2": "~3.6.5", "mysql2": "~3.6.5",
"promisify-child-process": "~4.1.2", "promisify-child-process": "~4.1.2",
"redbean-node": "~0.3.3", "redbean-node": "~0.3.3",
"semver": "^7.5.4",
"socket.io": "~4.7.2", "socket.io": "~4.7.2",
"socket.io-client": "~4.7.2", "socket.io-client": "~4.7.2",
"timezones-list": "~3.0.2", "timezones-list": "~3.0.2",
@ -66,6 +69,7 @@
"@types/command-exists": "~1.2.3", "@types/command-exists": "~1.2.3",
"@types/express": "~4.17.21", "@types/express": "~4.17.21",
"@types/jsonwebtoken": "~9.0.5", "@types/jsonwebtoken": "~9.0.5",
"@types/semver": "^7.5.6",
"@typescript-eslint/eslint-plugin": "~6.8.0", "@typescript-eslint/eslint-plugin": "~6.8.0",
"@typescript-eslint/parser": "~6.8.0", "@typescript-eslint/parser": "~6.8.0",
"@vitejs/plugin-vue": "~4.5.2", "@vitejs/plugin-vue": "~4.5.2",
@ -82,9 +86,9 @@
"sass": "~1.68.0", "sass": "~1.68.0",
"typescript": "~5.2.2", "typescript": "~5.2.2",
"unplugin-vue-components": "~0.25.2", "unplugin-vue-components": "~0.25.2",
"vite": "~5.0.7", "vite": "~5.0.10",
"vite-plugin-compression": "~0.5.1", "vite-plugin-compression": "~0.5.1",
"vue": "~3.3.11", "vue": "~3.3.13",
"vue-eslint-parser": "~9.3.2", "vue-eslint-parser": "~9.3.2",
"vue-i18n": "~9.5.0", "vue-i18n": "~9.5.0",
"vue-prism-editor": "2.0.0-alpha.2", "vue-prism-editor": "2.0.0-alpha.2",

File diff suppressed because it is too large Load Diff

View File

@ -7,6 +7,7 @@
"skipLibCheck": true "skipLibCheck": true
}, },
"include": [ "include": [
"backend/**/*" "backend/**/*",
"common/**/*"
] ]
} }