dockge/backend/stack.ts
Weapie d8fdeba05d
All checks were successful
Docker image / publish (push) Successful in 59s
Improve Dockge workflows and require approval after image scans
Update the shadcn preset and visual Compose editor. Add deployment previews, history, attention notices, and cleanup safeguards. Use the supplied icon in the app header and authentication screens.
2026-11-12 12:15:02 +01:00

603 lines
21 KiB
TypeScript

import { DockgeServer } from "./dockge-server";
import fs, { promises as fsAsync } from "fs";
import { createHash } from "node:crypto";
import { log } from "./log";
import yaml from "yaml";
import { DockgeSocket, fileExists, ValidationError } from "./util-server";
import path from "path";
import {
acceptedComposeFileNames,
COMBINED_TERMINAL_COLS,
COMBINED_TERMINAL_ROWS,
CREATED_FILE,
CREATED_STACK,
EXITED, getCombinedTerminalName,
getComposeTerminalName, getContainerExecTerminalName,
RUNNING, TERMINAL_ROWS,
UNKNOWN
} from "../common/util-common";
import { InteractiveTerminal, Terminal } from "./terminal";
import childProcessAsync from "promisify-child-process";
import { Settings } from "./settings";
import { analyzeEnv } from "../common/env-file";
import { saveStackFiles } from "./stack-files";
export class Stack {
name: string;
protected _status: number = UNKNOWN;
protected _composeYAML?: string;
protected _composeENV?: string;
protected _configFilePath?: string;
protected _composeFileName: string = "compose.yaml";
protected server: DockgeServer;
protected combinedTerminal? : Terminal;
protected static managedStackList: Map<string, Stack> = new Map();
private static operations = new Set<string>();
/**
* Hold the stack lock across filesystem changes and all Compose steps.
* @param server Dockge instance that owns the stack
* @param name Stack directory name
* @param action Operation to run
* @returns The operation result
*/
static async withOperation<T>(server: DockgeServer, name: string, action: () => Promise<T>): Promise<T> {
const directory = path.resolve(server.stacksDir, name);
if (this.operations.has(directory)) {
throw new ValidationError("Another operation is already running, please try again later.");
}
this.operations.add(directory);
try {
return await action();
} finally {
this.operations.delete(directory);
}
}
constructor(server : DockgeServer, name : string, composeYAML? : string, composeENV? : string, skipFSOperations = false) {
this.name = name;
this.server = server;
this._composeYAML = composeYAML;
this._composeENV = composeENV;
if (!skipFSOperations) {
// Check if compose file name is different from compose.yaml
for (const filename of acceptedComposeFileNames) {
if (fs.existsSync(path.join(this.path, filename))) {
this._composeFileName = filename;
break;
}
}
}
}
async toJSON(endpoint : string) : Promise<object> {
// Since we have multiple agents now, embed primary hostname in the stack object too.
let primaryHostname = await Settings.get("primaryHostname");
if (!primaryHostname) {
if (!endpoint) {
primaryHostname = "localhost";
} else {
// Use the endpoint as the primary hostname
try {
primaryHostname = (new URL("https://" + endpoint).hostname);
} catch (e) {
// Just in case if the endpoint is in a incorrect format
primaryHostname = "localhost";
}
}
}
let obj = this.toSimpleJSON(endpoint);
return {
...obj,
composeYAML: this.composeYAML,
composeENV: this.composeENV,
primaryHostname,
revision: this.revision,
};
}
toSimpleJSON(endpoint : string) : object {
return {
name: this.name,
status: this._status,
tags: [],
isManagedByDockge: this.isManagedByDockge,
composeFileName: this._composeFileName,
endpoint,
};
}
/**
* Get the status of the stack from `docker compose ps --format json`
*/
async ps() : Promise<object> {
let res = await childProcessAsync.spawn("docker", this.getComposeOptions("ps", "--format", "json"), {
cwd: this.path,
encoding: "utf-8",
});
if (!res.stdout) {
return {};
}
return JSON.parse(res.stdout.toString());
}
get isManagedByDockge() : boolean {
return fs.existsSync(this.path) && fs.statSync(this.path).isDirectory();
}
get status() : number {
return this._status;
}
get revision(): string {
return createHash("sha256").update(JSON.stringify([ this._composeFileName, this.composeYAML, this.composeENV ])).digest("hex");
}
validate() {
// Check name, allows [a-z][0-9] _ - only
if (!this.name.match(/^[a-z0-9_-]+$/)) {
throw new ValidationError("Stack name can only contain [a-z][0-9] _ - only");
}
// Check YAML format
const config = yaml.parse(this.composeYAML);
if (config !== null && (typeof config !== "object" || Array.isArray(config))) {
throw new ValidationError("editorYamlMapping");
}
if (config?.services && (typeof config.services !== "object" || Array.isArray(config.services))) {
throw new ValidationError("editorServicesMapping");
}
if (analyzeEnv(this.composeENV).some(issue => issue.severity === "error")) {
throw new ValidationError("editorInvalidEnv");
}
}
get composeYAML() : string {
if (this._composeYAML === undefined) {
try {
this._composeYAML = fs.readFileSync(path.join(this.path, this._composeFileName), "utf-8");
} catch (e) {
this._composeYAML = "";
}
}
return this._composeYAML;
}
get composeENV() : string {
if (this._composeENV === undefined) {
try {
this._composeENV = fs.readFileSync(path.join(this.path, ".env"), "utf-8");
} catch (e) {
this._composeENV = "";
}
}
return this._composeENV;
}
get path() : string {
return path.join(this.server.stacksDir, this.name);
}
get fullPath() : string {
let dir = this.path;
// Compose up via node-pty
let fullPathDir;
// if dir is relative, make it absolute
if (!path.isAbsolute(dir)) {
fullPathDir = path.join(process.cwd(), dir);
} else {
fullPathDir = dir;
}
return fullPathDir;
}
/**
* Save the stack to the disk
* @param isAdd
* @param expectedRevision Version loaded by the editor, omitted by legacy clients
*/
async save(isAdd : boolean, expectedRevision?: string) {
this.validate();
let dir = this.path;
let owner;
if (process.env.PUID && process.env.PGID) {
const uid = Number(process.env.PUID);
const gid = Number(process.env.PGID);
if (!Number.isSafeInteger(uid) || uid < 0 || !Number.isSafeInteger(gid) || gid < 0) {
throw new ValidationError("Invalid PUID or PGID");
}
owner = {
uid,
gid,
};
}
const checkRevision = () => {
if (!isAdd && expectedRevision !== undefined) {
const current = new Stack(this.server, this.name);
if (current.revision !== expectedRevision && current.revision !== this.revision) {
throw new ValidationError("editorConflict");
}
}
};
// Check if the name is used if isAdd
if (isAdd) {
if (await fileExists(dir)) {
throw new ValidationError("Stack name already exists");
}
// Create the stack folder
await fsAsync.mkdir(dir);
} else {
if (!await fileExists(dir)) {
throw new ValidationError("Stack not found");
}
checkRevision();
}
try {
if (owner) {
await fsAsync.lchown(dir, owner.uid, owner.gid);
}
await saveStackFiles([
{
path: path.join(dir, this._composeFileName),
content: this.composeYAML,
mode: 0o644,
},
{
path: path.join(dir, ".env"),
content: this.composeENV,
mode: 0o600,
},
], owner, checkRevision);
} catch (error) {
if (isAdd) {
await fsAsync.rmdir(dir).catch(() => {});
}
throw error;
}
}
async delete(socket: DockgeSocket) : Promise<number> {
const terminalName = getComposeTerminalName(socket.endpoint, this.name);
let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", this.getComposeOptions("down", "--remove-orphans"), this.path);
if (exitCode !== 0) {
throw new Error("Failed to delete, please check the terminal output for more information.");
}
// Remove the stack folder
await fsAsync.rm(this.path, {
recursive: true,
force: true
});
return exitCode;
}
async updateStatus() {
let statusList = await Stack.getStatusList();
let status = statusList.get(this.name);
if (status) {
this._status = status;
} else {
this._status = UNKNOWN;
}
}
/**
* Checks if a compose file exists in the specified directory.
* @async
* @static
* @param {string} stacksDir - The directory of the stack.
* @param {string} filename - The name of the directory to check for the compose file.
* @returns {Promise<boolean>} A promise that resolves to a boolean indicating whether any compose file exists.
*/
static async composeFileExists(stacksDir : string, filename : string) : Promise<boolean> {
let filenamePath = path.join(stacksDir, filename);
// Check if any compose file exists
for (const filename of acceptedComposeFileNames) {
let composeFile = path.join(filenamePath, filename);
if (await fileExists(composeFile)) {
return true;
}
}
return false;
}
static async getStackList(server : DockgeServer, useCacheForManaged = false) : Promise<Map<string, Stack>> {
let stacksDir = server.stacksDir;
let stackList : Map<string, Stack>;
// Use cached stack list?
if (useCacheForManaged && this.managedStackList.size > 0) {
stackList = this.managedStackList;
} else {
stackList = new Map<string, Stack>();
// Scan the stacks directory, and get the stack list
let filenameList = await fsAsync.readdir(stacksDir);
for (let filename of filenameList) {
try {
// Check if it is a directory
let stat = await fsAsync.stat(path.join(stacksDir, filename));
if (!stat.isDirectory()) {
continue;
}
// If no compose file exists, skip it
if (!await Stack.composeFileExists(stacksDir, filename)) {
continue;
}
let stack = await this.getStack(server, filename);
stack._status = CREATED_FILE;
stackList.set(filename, stack);
} catch (e) {
if (e instanceof Error) {
log.warn("getStackList", `Failed to get stack ${filename}, error: ${e.message}`);
}
}
}
// Cache by copying
this.managedStackList = new Map(stackList);
}
// Get status from docker compose ls
let res = await childProcessAsync.spawn("docker", [ "compose", "ls", "--all", "--format", "json" ], {
encoding: "utf-8",
});
if (!res.stdout) {
return stackList;
}
let composeList = JSON.parse(res.stdout.toString());
for (let composeStack of composeList) {
let stack = stackList.get(composeStack.Name);
// This stack probably is not managed by Dockge, but we still want to show it
if (!stack) {
// Skip the dockge stack if it is not managed by Dockge
if (composeStack.Name === "dockge") {
continue;
}
stack = new Stack(server, composeStack.Name);
stackList.set(composeStack.Name, stack);
}
stack._status = this.statusConvert(composeStack.Status);
stack._configFilePath = composeStack.ConfigFiles;
}
return stackList;
}
/**
* Get the status list, it will be used to update the status of the stacks
* Not all status will be returned, only the stack that is deployed or created to `docker compose` will be returned
*/
static async getStatusList() : Promise<Map<string, number>> {
let statusList = new Map<string, number>();
let res = await childProcessAsync.spawn("docker", [ "compose", "ls", "--all", "--format", "json" ], {
encoding: "utf-8",
});
if (!res.stdout) {
return statusList;
}
let composeList = JSON.parse(res.stdout.toString());
for (let composeStack of composeList) {
statusList.set(composeStack.Name, this.statusConvert(composeStack.Status));
}
return statusList;
}
/**
* Convert the status string from `docker compose ls` to the status number
* Input Example: "exited(1), running(1)"
* @param status
*/
static statusConvert(status : string) : number {
if (status.startsWith("created")) {
return CREATED_STACK;
} else if (status.includes("exited")) {
// If one of the service is exited, we consider the stack is exited
return EXITED;
} else if (status.startsWith("running")) {
// If there is no exited services, there should be only running services
return RUNNING;
} else {
return UNKNOWN;
}
}
static async getStack(server: DockgeServer, stackName: string, skipFSOperations = false) : Promise<Stack> {
let dir = path.join(server.stacksDir, stackName);
if (!skipFSOperations) {
if (!await fileExists(dir) || !(await fsAsync.stat(dir)).isDirectory()) {
// Maybe it is a stack managed by docker compose directly
let stackList = await this.getStackList(server, true);
let stack = stackList.get(stackName);
if (stack) {
return stack;
} else {
// Really not found
throw new ValidationError("Stack not found");
}
}
} else {
//log.debug("getStack", "Skip FS operations");
}
let stack : Stack;
if (!skipFSOperations) {
stack = new Stack(server, stackName);
} else {
stack = new Stack(server, stackName, undefined, undefined, true);
}
stack._status = UNKNOWN;
stack._configFilePath = path.resolve(dir);
return stack;
}
getComposeOptions(command : string, ...extraOptions : string[]) {
//--env-file ./../global.env --env-file .env
let options = [ "compose", command, ...extraOptions ];
if (fs.existsSync(path.join(this.server.stacksDir, "global.env"))) {
if (fs.existsSync(path.join(this.path, ".env"))) {
options.splice(1, 0, "--env-file", "./.env");
}
options.splice(1, 0, "--env-file", "../global.env");
}
console.log(options);
return options;
}
async stop(socket: DockgeSocket) : Promise<number> {
const terminalName = getComposeTerminalName(socket.endpoint, this.name);
let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", this.getComposeOptions("stop"), this.path);
if (exitCode !== 0) {
throw new Error("Failed to stop, please check the terminal output for more information.");
}
return exitCode;
}
async restart(socket: DockgeSocket) : Promise<number> {
const terminalName = getComposeTerminalName(socket.endpoint, this.name);
let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", this.getComposeOptions("restart"), this.path);
if (exitCode !== 0) {
throw new Error("Failed to restart, please check the terminal output for more information.");
}
return exitCode;
}
async down(socket: DockgeSocket) : Promise<number> {
const terminalName = getComposeTerminalName(socket.endpoint, this.name);
let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", this.getComposeOptions("down"), this.path);
if (exitCode !== 0) {
throw new Error("Failed to down, please check the terminal output for more information.");
}
return exitCode;
}
async joinCombinedTerminal(socket: DockgeSocket) {
const terminalName = getCombinedTerminalName(socket.endpoint, this.name);
const terminal = Terminal.getOrCreateTerminal(this.server, terminalName, "docker", this.getComposeOptions("logs", "-f", "--tail", "100"), this.path);
terminal.enableKeepAlive = true;
terminal.rows = COMBINED_TERMINAL_ROWS;
terminal.cols = COMBINED_TERMINAL_COLS;
terminal.join(socket);
terminal.start();
}
async leaveCombinedTerminal(socket: DockgeSocket) {
const terminalName = getCombinedTerminalName(socket.endpoint, this.name);
const terminal = Terminal.getTerminal(terminalName);
if (terminal) {
terminal.leave(socket);
}
}
async joinContainerTerminal(socket: DockgeSocket, serviceName: string, shell : string = "sh", index: number = 0) {
const terminalName = getContainerExecTerminalName(socket.endpoint, this.name, serviceName, index);
let terminal = Terminal.getTerminal(terminalName);
if (!terminal) {
terminal = new InteractiveTerminal(this.server, terminalName, "docker", this.getComposeOptions("exec", serviceName, shell), this.path);
terminal.rows = TERMINAL_ROWS;
log.debug("joinContainerTerminal", "Terminal created");
}
terminal.join(socket);
terminal.start();
}
async getServiceStatusList() {
let statusList = new Map<string, Array<object>>();
try {
let res = await childProcessAsync.spawn("docker", this.getComposeOptions("ps", "--all", "--no-trunc", "--orphans=false", "--format", "json"), {
cwd: this.path,
encoding: "utf-8",
});
if (!res.stdout) {
return statusList;
}
let lines = res.stdout?.toString().split("\n");
const addLine = (obj: { ID: string, Service: string, State: string, Name: string, Health: string, ExitCode?: number, Publishers?: unknown[] }) => {
if (!statusList.has(obj.Service)) {
statusList.set(obj.Service, []);
}
statusList.get(obj.Service)?.push({
id: obj.ID,
state: obj.State,
status: obj.Health || obj.State,
name: obj.Name,
exitCode: obj.ExitCode,
ports: obj.Publishers ?? [],
});
};
for (let line of lines) {
try {
let obj = JSON.parse(line);
if (obj instanceof Array) {
obj.forEach(addLine);
} else {
addLine(obj);
}
} catch (e) {
}
}
return statusList;
} catch (e) {
log.error("getServiceStatusList", e);
return statusList;
}
}
async stopService(socket: DockgeSocket, serviceName: string): Promise<number> {
const terminalName = getComposeTerminalName(socket.endpoint, this.name);
const exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "stop", serviceName ], this.path);
if (exitCode !== 0) {
throw new Error(`Failed to stop service ${serviceName}, please check logs for more information.`);
}
return exitCode;
}
async restartService(socket: DockgeSocket, serviceName: string): Promise<number> {
const terminalName = getComposeTerminalName(socket.endpoint, this.name);
const exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "restart", serviceName ], this.path);
if (exitCode !== 0) {
throw new Error(`Failed to restart service ${serviceName}, please check logs for more information.`);
}
return exitCode;
}
}