draft support for nested stacks directory

This commit is contained in:
Martin 2024-12-12 21:52:37 -05:00
parent 01906205f0
commit fc4ad7ff29
No known key found for this signature in database
GPG key ID: 2FFD190F91104F3E

View file

@ -104,7 +104,7 @@ export class Stack {
} }
get isManagedByDockge() : boolean { get isManagedByDockge() : boolean {
return fs.existsSync(this.path) && fs.statSync(this.path).isDirectory(); return !!this._configFilePath && this._configFilePath.startsWith(this.server.stacksDir);
} }
get status() : number { get status() : number {
@ -153,7 +153,7 @@ export class Stack {
} }
get path() : string { get path() : string {
return path.join(this.server.stacksDir, this.name); return this._configFilePath || "";
} }
get fullPath() : string { get fullPath() : string {
@ -263,41 +263,12 @@ export class Stack {
} }
static async getStackList(server : DockgeServer, useCacheForManaged = false) : Promise<Map<string, Stack>> { static async getStackList(server : DockgeServer, useCacheForManaged = false) : Promise<Map<string, Stack>> {
let stacksDir = server.stacksDir; let stackList : Map<string, Stack> = new Map<string, Stack>();
let stackList : Map<string, Stack>;
// Use cached stack list? // Use cached stack list?
if (useCacheForManaged && this.managedStackList.size > 0) { if (useCacheForManaged && this.managedStackList.size > 0) {
stackList = this.managedStackList; stackList = this.managedStackList;
} else { return stackList;
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 // Get status from docker compose ls
@ -306,28 +277,33 @@ export class Stack {
}); });
if (!res.stdout) { if (!res.stdout) {
log.warn("getStackList", "No response from docker compose daemon when attempting to retrieve list of stacks");
return stackList; return stackList;
} }
let composeList = JSON.parse(res.stdout.toString()); let composeList = JSON.parse(res.stdout.toString());
for (let composeStack of composeList) { for (let composeStack of composeList) {
let stack = stackList.get(composeStack.Name); try {
let stack = new Stack(server, composeStack.Name);
stack._status = this.statusConvert(composeStack.Status);
// This stack probably is not managed by Dockge, but we still want to show it let composeFiles = composeStack.ConfigFiles.split(","); // it is possible for a project to have more than one config file
if (!stack) { stack._configFilePath = path.dirname(composeFiles[0]);
// Skip the dockge stack if it is not managed by Dockge stack._composeFileName = path.basename(composeFiles[0]);
if (composeStack.Name === "dockge") { if (stack.name === "dockge" && !stack.isManagedByDockge) {
// skip dockge if not managed by dockge
continue; continue;
} }
stack = new Stack(server, composeStack.Name);
stackList.set(composeStack.Name, stack); stackList.set(composeStack.Name, stack);
} catch (e) {
if (e instanceof Error) {
log.warn("getStackList", `Failed to get stack ${composeStack.Name}, error: ${e.message}`);
}
}
} }
stack._status = this.statusConvert(composeStack.Status); this.managedStackList = stackList;
stack._configFilePath = composeStack.ConfigFiles;
}
return stackList; return stackList;
} }
@ -375,35 +351,24 @@ export class Stack {
} }
static async getStack(server: DockgeServer, stackName: string, skipFSOperations = false) : Promise<Stack> { static async getStack(server: DockgeServer, stackName: string, skipFSOperations = false) : Promise<Stack> {
let dir = path.join(server.stacksDir, stackName); let stack: Stack | undefined;
if (!skipFSOperations) { 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 stackList = await this.getStackList(server, true);
let stack = stackList.get(stackName); stack = stackList.get(stackName);
if (!stack || !await fileExists(stack.path) || !(await fsAsync.stat(stack.path)).isDirectory() ) {
if (stack) { throw new ValidationError(`getStack; Stack ${stackName} not found`);
return stack;
} else {
// Really not found
throw new ValidationError("Stack not found");
}
} }
} else { } else {
//log.debug("getStack", "Skip FS operations"); // search for known stack with this name
if (this.managedStackList) {
stack = this.managedStackList.get(stackName);
} }
if (!this.managedStackList || !stack) {
let stack : Stack;
if (!skipFSOperations) {
stack = new Stack(server, stackName);
} else {
stack = new Stack(server, stackName, undefined, undefined, true); stack = new Stack(server, stackName, undefined, undefined, true);
}
stack._status = UNKNOWN; stack._status = UNKNOWN;
stack._configFilePath = path.resolve(dir); stack._configFilePath = path.resolve(server.stacksDir, stackName);
}
}
return stack; return stack;
} }
@ -522,7 +487,6 @@ export class Stack {
} catch (e) { } catch (e) {
} }
} }
return statusList; return statusList;
} catch (e) { } catch (e) {
log.error("getServiceStatusList", e); log.error("getServiceStatusList", e);