Compare commits

...

6 Commits

Author SHA1 Message Date
e9f1bde8cc Check some fs operation to async 2023-12-04 16:24:35 +08:00
3537b842ec Update 2023-12-04 15:59:01 +08:00
8053e9940e Create the env file only if not empty 2023-12-04 15:15:11 +08:00
0df3fee3f4 Minor 2023-12-03 21:57:27 +08:00
05b79ba50e Fix freeze issue (#227)
* Fix freeze issue

* Fix
2023-12-03 21:30:50 +08:00
a3c4082800 Sort non managed stack to the end (#228) 2023-12-03 21:24:06 +08:00
12 changed files with 155 additions and 76 deletions

View File

@ -1,5 +1,4 @@
title: "❓ Ask for help"
labels: [help]
labels: [help]
body:
- type: checkboxes
id: no-duplicate-issues

View File

@ -1,4 +1,3 @@
title: 🚀 Feature Request
labels: [feature-request]
body:
- type: checkboxes
@ -52,4 +51,4 @@ body:
attributes:
label: "📝 Additional Context"
description: "Add any other context or screenshots about the feature request here."
placeholder: "..."
placeholder: "..."

View File

@ -1,14 +1,14 @@
name: " Ask for help"
description: "Please go to the Discussions tab to submit a Help Request"
name: "⚠️ Ask for help (Please go to the \"Discussions\" tab to submit a Help Request)"
description: "⚠️ Please go to the \"Discussions\" tab to submit a Help Request"
body:
- type: markdown
attributes:
value: |
Please go to https://github.com/louislam/dockge/discussions/new?category=ask-for-help
⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ Please go to https://github.com/louislam/dockge/discussions/new?category=ask-for-help
- type: checkboxes
id: no-duplicate-issues
attributes:
label: "Issues are for bug reports only"
label: "Issues are for bug reports only, please go to the \"Discussions\" tab to submit a Feature Request"
options:
- label: "I understand"
required: true

View File

@ -1,14 +1,14 @@
name: 🚀 Feature Request
description: "Please go to the Discussions tab to submit a Feature Request"
name: 🚀 Feature Request (Please go to the "Discussions" tab to submit a Feature Request)
description: "⚠️ Please go to the \"Discussions\" tab to submit a Feature Request"
body:
- type: markdown
attributes:
value: |
Please go to https://github.com/louislam/dockge/discussions/new?category=feature-request
⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ Please go to https://github.com/louislam/dockge/discussions/new?category=ask-for-help
- type: checkboxes
id: no-duplicate-issues
attributes:
label: "Issues are for bug reports only"
label: "Issues are for bug reports only, please go to the \"Discussions\" tab to submit a Feature Request"
options:
- label: "I understand"
required: true

View File

@ -29,9 +29,11 @@ import { Stack } from "./stack";
import { Cron } from "croner";
import gracefulShutdown from "http-graceful-shutdown";
import User from "./models/user";
import childProcess from "child_process";
import childProcessAsync from "promisify-child-process";
import { Terminal } from "./terminal";
import "dotenv/config";
export class DockgeServer {
app : Express;
httpServer : http.Server;
@ -483,7 +485,7 @@ export class DockgeServer {
return jwtSecretBean;
}
sendStackList(useCache = false) {
async sendStackList(useCache = false) {
let roomList = this.io.sockets.adapter.rooms.keys();
let map : Map<string, object> | undefined;
@ -494,7 +496,7 @@ export class DockgeServer {
// Get the list only if there is a room
if (!map) {
map = new Map();
let stackList = Stack.getStackList(this, useCache);
let stackList = await Stack.getStackList(this, useCache);
for (let [ stackName, stack ] of stackList) {
map.set(stackName, stack.toSimpleJSON());
@ -510,8 +512,8 @@ export class DockgeServer {
}
}
sendStackStatusList() {
let statusList = Stack.getStatusList();
async sendStackStatusList() {
let statusList = await Stack.getStatusList();
let roomList = this.io.sockets.adapter.rooms.keys();
@ -529,8 +531,15 @@ export class DockgeServer {
}
}
getDockerNetworkList() : string[] {
let res = childProcess.spawnSync("docker", [ "network", "ls", "--format", "{{.Name}}" ]);
async getDockerNetworkList() : Promise<string[]> {
let res = await childProcessAsync.spawn("docker", [ "network", "ls", "--format", "{{.Name}}" ], {
encoding: "utf-8",
});
if (!res.stdout) {
return [];
}
let list = res.stdout.toString().split("\n");
// Remove empty string item

View File

@ -12,7 +12,7 @@ export class DockerSocketHandler extends SocketHandler {
socket.on("deployStack", async (name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown, callback) => {
try {
checkLogin(socket);
const stack = this.saveStack(socket, server, name, composeYAML, composeENV, isAdd);
const stack = await this.saveStack(socket, server, name, composeYAML, composeENV, isAdd);
await stack.deploy(socket);
server.sendStackList();
callback({
@ -45,7 +45,7 @@ export class DockerSocketHandler extends SocketHandler {
if (typeof(name) !== "string") {
throw new ValidationError("Name must be a string");
}
const stack = Stack.getStack(server, name);
const stack = await Stack.getStack(server, name);
try {
await stack.delete(socket);
@ -65,7 +65,7 @@ export class DockerSocketHandler extends SocketHandler {
}
});
socket.on("getStack", (stackName : unknown, callback) => {
socket.on("getStack", async (stackName : unknown, callback) => {
try {
checkLogin(socket);
@ -73,7 +73,7 @@ export class DockerSocketHandler extends SocketHandler {
throw new ValidationError("Stack name must be a string");
}
const stack = Stack.getStack(server, stackName);
const stack = await Stack.getStack(server, stackName);
if (stack.isManagedByDockge) {
stack.joinCombinedTerminal(socket);
@ -111,7 +111,7 @@ export class DockerSocketHandler extends SocketHandler {
throw new ValidationError("Stack name must be a string");
}
const stack = Stack.getStack(server, stackName);
const stack = await Stack.getStack(server, stackName);
await stack.start(socket);
callback({
ok: true,
@ -135,7 +135,7 @@ export class DockerSocketHandler extends SocketHandler {
throw new ValidationError("Stack name must be a string");
}
const stack = Stack.getStack(server, stackName);
const stack = await Stack.getStack(server, stackName);
await stack.stop(socket);
callback({
ok: true,
@ -156,7 +156,7 @@ export class DockerSocketHandler extends SocketHandler {
throw new ValidationError("Stack name must be a string");
}
const stack = Stack.getStack(server, stackName);
const stack = await Stack.getStack(server, stackName);
await stack.restart(socket);
callback({
ok: true,
@ -177,7 +177,7 @@ export class DockerSocketHandler extends SocketHandler {
throw new ValidationError("Stack name must be a string");
}
const stack = Stack.getStack(server, stackName);
const stack = await Stack.getStack(server, stackName);
await stack.update(socket);
callback({
ok: true,
@ -198,7 +198,7 @@ export class DockerSocketHandler extends SocketHandler {
throw new ValidationError("Stack name must be a string");
}
const stack = Stack.getStack(server, stackName);
const stack = await Stack.getStack(server, stackName);
await stack.down(socket);
callback({
ok: true,
@ -219,7 +219,7 @@ export class DockerSocketHandler extends SocketHandler {
throw new ValidationError("Stack name must be a string");
}
const stack = Stack.getStack(server, stackName, true);
const stack = await Stack.getStack(server, stackName, true);
const serviceStatusList = Object.fromEntries(await stack.getServiceStatusList());
callback({
ok: true,
@ -264,7 +264,7 @@ export class DockerSocketHandler extends SocketHandler {
});
}
saveStack(socket : DockgeSocket, server : DockgeServer, name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown) : Stack {
async saveStack(socket : DockgeSocket, server : DockgeServer, name : unknown, composeYAML : unknown, composeENV : unknown, isAdd : unknown) : Promise<Stack> {
// Check types
if (typeof(name) !== "string") {
throw new ValidationError("Name must be a string");
@ -280,7 +280,7 @@ export class DockerSocketHandler extends SocketHandler {
}
const stack = new Stack(server, name, composeYAML, composeENV, false);
stack.save(isAdd);
await stack.save(isAdd);
return stack;
}

View File

@ -101,7 +101,7 @@ export class TerminalSocketHandler extends SocketHandler {
log.debug("interactiveTerminal", "Service name: " + serviceName);
// Get stack
const stack = Stack.getStack(server, stackName);
const stack = await Stack.getStack(server, stackName);
stack.joinContainerTerminal(socket, serviceName, shell);
callback({
@ -151,7 +151,7 @@ export class TerminalSocketHandler extends SocketHandler {
throw new ValidationError("Stack name must be a string.");
}
const stack = Stack.getStack(server, stackName);
const stack = await Stack.getStack(server, stackName);
await stack.leaveCombinedTerminal(socket);
callback({

View File

@ -1,8 +1,8 @@
import { DockgeServer } from "./dockge-server";
import fs from "fs";
import fs, { promises as fsAsync } from "fs";
import { log } from "./log";
import yaml from "yaml";
import { DockgeSocket, ValidationError } from "./util-server";
import { DockgeSocket, fileExists, ValidationError } from "./util-server";
import path from "path";
import {
COMBINED_TERMINAL_COLS,
@ -16,7 +16,7 @@ import {
UNKNOWN
} from "./util-common";
import { InteractiveTerminal, Terminal } from "./terminal";
import childProcess from "child_process";
import childProcessAsync from "promisify-child-process";
export class Stack {
@ -72,11 +72,15 @@ export class Stack {
/**
* Get the status of the stack from `docker compose ps --format json`
*/
ps() : object {
let res = childProcess.execSync("docker compose ps --format json", {
cwd: this.path
async ps() : Promise<object> {
let res = await childProcessAsync.spawn("docker", [ "compose", "ps", "--format", "json" ], {
cwd: this.path,
encoding: "utf-8",
});
return JSON.parse(res.toString());
if (!res.stdout) {
return {};
}
return JSON.parse(res.stdout.toString());
}
get isManagedByDockge() : boolean {
@ -95,6 +99,15 @@ export class Stack {
// Check YAML format
yaml.parse(this.composeYAML);
let lines = this.composeENV.split("\n");
// Check if the .env is able to pass docker-compose
// Prevent "setenv: The parameter is incorrect"
// It only happens when there is one line and it doesn't contain "="
if (lines.length === 1 && !lines[0].includes("=") && lines[0].length > 0) {
throw new ValidationError("Invalid .env format");
}
}
get composeYAML() : string {
@ -142,29 +155,35 @@ export class Stack {
* Save the stack to the disk
* @param isAdd
*/
save(isAdd : boolean) {
async save(isAdd : boolean) {
this.validate();
let dir = this.path;
// Check if the name is used if isAdd
if (isAdd) {
if (fs.existsSync(dir)) {
if (await fileExists(dir)) {
throw new ValidationError("Stack name already exists");
}
// Create the stack folder
fs.mkdirSync(dir);
await fsAsync.mkdir(dir);
} else {
if (!fs.existsSync(dir)) {
if (!await fileExists(dir)) {
throw new ValidationError("Stack not found");
}
}
// Write or overwrite the compose.yaml
fs.writeFileSync(path.join(dir, this._composeFileName), this.composeYAML);
await fsAsync.writeFile(path.join(dir, this._composeFileName), this.composeYAML);
const envPath = path.join(dir, ".env");
// Write or overwrite the .env
fs.writeFileSync(path.join(dir, ".env"), this.composeENV);
// If .env is not existing and the composeENV is empty, we don't need to write it
if (await fileExists(envPath) || this.composeENV.trim() !== "") {
await fsAsync.writeFile(envPath, this.composeENV);
}
}
async deploy(socket? : DockgeSocket) : Promise<number> {
@ -184,7 +203,7 @@ export class Stack {
}
// Remove the stack folder
fs.rmSync(this.path, {
await fsAsync.rm(this.path, {
recursive: true,
force: true
});
@ -192,8 +211,8 @@ export class Stack {
return exitCode;
}
updateStatus() {
let statusList = Stack.getStatusList();
async updateStatus() {
let statusList = await Stack.getStatusList();
let status = statusList.get(this.name);
if (status) {
@ -203,7 +222,7 @@ export class Stack {
}
}
static getStackList(server : DockgeServer, useCacheForManaged = false) : Map<string, Stack> {
static async getStackList(server : DockgeServer, useCacheForManaged = false) : Promise<Map<string, Stack>> {
let stacksDir = server.stacksDir;
let stackList : Map<string, Stack>;
@ -214,16 +233,16 @@ export class Stack {
stackList = new Map<string, Stack>();
// Scan the stacks directory, and get the stack list
let filenameList = fs.readdirSync(stacksDir);
let filenameList = await fsAsync.readdir(stacksDir);
for (let filename of filenameList) {
try {
// Check if it is a directory
let stat = fs.statSync(path.join(stacksDir, filename));
let stat = await fsAsync.stat(path.join(stacksDir, filename));
if (!stat.isDirectory()) {
continue;
}
let stack = this.getStack(server, filename);
let stack = await this.getStack(server, filename);
stack._status = CREATED_FILE;
stackList.set(filename, stack);
} catch (e) {
@ -238,8 +257,15 @@ export class Stack {
}
// Get status from docker compose ls
let res = childProcess.execSync("docker compose ls --all --format json");
let composeList = JSON.parse(res.toString());
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);
@ -265,10 +291,12 @@ export class Stack {
* 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 getStatusList() : Map<string, number> {
static async getStatusList() : Promise<Map<string, number>> {
let statusList = new Map<string, number>();
let res = childProcess.execSync("docker compose ls --all --format json");
let res = await childProcessAsync.spawn("docker", [ "compose", "ls", "--all", "--format", "json" ], {
encoding: "utf-8",
});
let composeList = JSON.parse(res.toString());
for (let composeStack of composeList) {
@ -297,13 +325,13 @@ export class Stack {
}
}
static getStack(server: DockgeServer, stackName: string, skipFSOperations = false) : Stack {
static async getStack(server: DockgeServer, stackName: string, skipFSOperations = false) : Promise<Stack> {
let dir = path.join(server.stacksDir, stackName);
if (!skipFSOperations) {
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
if (!await fileExists(dir) || !(await fsAsync.stat(dir)).isDirectory()) {
// Maybe it is a stack managed by docker compose directly
let stackList = this.getStackList(server, true);
let stackList = await this.getStackList(server, true);
let stack = stackList.get(stackName);
if (stack) {
@ -374,7 +402,7 @@ export class Stack {
}
// If the stack is not running, we don't need to restart it
this.updateStatus();
await this.updateStatus();
log.debug("update", "Status: " + this.status);
if (this.status !== RUNNING) {
return exitCode;
@ -422,24 +450,35 @@ export class Stack {
async getServiceStatusList() {
let statusList = new Map<string, number>();
let res = childProcess.spawnSync("docker", [ "compose", "ps", "--format", "json" ], {
cwd: this.path,
});
try {
let res = await childProcessAsync.spawn("docker", [ "compose", "ps", "--format", "json" ], {
cwd: this.path,
encoding: "utf-8",
});
let lines = res.stdout.toString().split("\n");
for (let line of lines) {
try {
let obj = JSON.parse(line);
if (obj.Health === "") {
statusList.set(obj.Service, obj.State);
} else {
statusList.set(obj.Service, obj.Health);
}
} catch (e) {
if (!res.stdout) {
return statusList;
}
let lines = res.stdout?.toString().split("\n");
for (let line of lines) {
try {
let obj = JSON.parse(line);
if (obj.Health === "") {
statusList.set(obj.Service, obj.State);
} else {
statusList.set(obj.Service, obj.Health);
}
} catch (e) {
}
}
return statusList;
} catch (e) {
log.error("getServiceStatusList", e);
return statusList;
}
return statusList;
}
}

View File

@ -5,6 +5,7 @@ import { log } from "./log";
import { ERROR_TYPE_VALIDATION } from "./util-common";
import { R } from "redbean-node";
import { verifyPassword } from "./password-hash";
import fs from "fs";
export interface JWTDecoded {
username : string;
@ -82,3 +83,9 @@ export async function doubleCheckPassword(socket : DockgeSocket, currentPassword
return user;
}
export function fileExists(file : string) {
return fs.promises.access(file, fs.constants.F_OK)
.then(() => true)
.catch(() => false);
}

View File

@ -152,6 +152,14 @@ export default {
});
result.sort((m1, m2) => {
// sort by managed by dockge
if (m1.isManagedByDockge && !m2.isManagedByDockge) {
return -1;
} else if (!m1.isManagedByDockge && m2.isManagedByDockge) {
return 1;
}
if (m1.status !== m2.status) {
if (m2.status === RUNNING) {
return 1;

View File

@ -33,6 +33,7 @@
"composerize": "~1.4.1",
"croner": "~7.0.5",
"dayjs": "~1.11.10",
"dotenv": "~16.3.1",
"express": "~4.18.2",
"express-static-gzip": "~2.1.7",
"http-graceful-shutdown": "~3.1.13",
@ -41,6 +42,7 @@
"knex": "~2.5.1",
"limiter-es6-compat": "~2.1.2",
"mysql2": "~3.6.3",
"promisify-child-process": "~4.1.2",
"redbean-node": "~0.3.3",
"socket.io": "~4.7.2",
"socket.io-client": "~4.7.2",

16
pnpm-lock.yaml generated
View File

@ -32,6 +32,9 @@ dependencies:
dayjs:
specifier: ~1.11.10
version: 1.11.10
dotenv:
specifier: ~16.3.1
version: 16.3.1
express:
specifier: ~4.18.2
version: 4.18.2
@ -56,6 +59,9 @@ dependencies:
mysql2:
specifier: ~3.6.3
version: 3.6.3
promisify-child-process:
specifier: ~4.1.2
version: 4.1.2
redbean-node:
specifier: ~0.3.3
version: 0.3.3(mysql2@3.6.3)
@ -2151,6 +2157,11 @@ packages:
esutils: 2.0.3
dev: true
/dotenv@16.3.1:
resolution: {integrity: sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==}
engines: {node: '>=12'}
dev: false
/eastasianwidth@0.2.0:
resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==}
dev: false
@ -3887,6 +3898,11 @@ packages:
dev: false
optional: true
/promisify-child-process@4.1.2:
resolution: {integrity: sha512-APnkIgmaHNJpkAn7k+CrJSi9WMuff5ctYFbD0CO2XIPkM8yO7d/ShouU2clywbpHV/DUsyc4bpJCsNgddNtx4g==}
engines: {node: '>=8'}
dev: false
/proxy-addr@2.0.7:
resolution: {integrity: sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==}
engines: {node: '>= 0.10'}