Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/FoxxMD/komodo-import. Import existing compose stacks into Komodo foxxmd.github.io/komodo-import
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107import { Logger, childLogger } from "@foxxmd/logging";import { FilesOnServerConfig } from "../../common/infrastructure/config/stackConfig.js";import { _PartialStackConfig } from 'komodo_client/dist/types.js';import { parse, ParsedPath, sep, join } from 'path';import { TomlStack } from "../../common/infrastructure/tomlObjects.js";import { findFilesRecurive, readText, sortComposePaths } from "../../common/utils/io.js";import { stripIndents } from "common-tags";import { isDebugMode, removeUndefinedKeys } from "../../common/utils/utils.js";import { DEFAULT_COMPOSE_GLOB, DEFAULT_ENV_GLOB, selectComposeFiles, selectEnvFiles } from "./stackUtils.js";
export type BuildFileStackOptions = FilesOnServerConfig & { logger: Logger };
export const buildFileStack = async (path: string, options: BuildFileStackOptions): Promise<TomlStack> => {
const { composeFileGlob = DEFAULT_COMPOSE_GLOB, envFileGlob = DEFAULT_ENV_GLOB, komodoEnvName = '.komodoEnv', imageRegistryAccount, imageRegistryProvider, autoUpdate = false, pollForUpdate = false, server, hostParentPath, writeEnv = false, } = options;
const pathInfo: ParsedPath = parse(path);
const folderName = `${pathInfo.name}${pathInfo.ext !== '' ? pathInfo.ext : ''}`;
const logger = childLogger(options.logger, [folderName, 'Files On Server']); logger.info(`Found Stack '${folderName}' at dir ${path}`);
let stack: TomlStack; let logJson = isDebugMode();
try { stack = { name: folderName, config: { server, run_directory: join(hostParentPath, folderName), files_on_host: true, registry_account: imageRegistryAccount, registry_provider: imageRegistryProvider, auto_update: autoUpdate, poll_for_updates: pollForUpdate } };
stack.config.file_paths = await selectComposeFiles(composeFileGlob, path, logger);
const envFiles = await selectEnvFiles(envFileGlob, path, logger); if (envFiles !== undefined) { if (writeEnv) { logger.verbose('Writing env file(s) contents to Komodo Environmnent'); const envContents: string[] = []; for (const f of envFiles) { envContents.push(await readText(envFiles)) } stack.config.environment = envContents.join('\n'); } else { stack.config.env_file_path = komodoEnvName logger.info(`Using ${komodoEnvName} for Komodo-written env file`); stack.config.additional_env_files = envFiles; } }
logger.info('Stack config complete');
return removeUndefinedKeys(stack); } catch (e) { logJson = true; throw new Error(`Error occurred while processing Stack for folder ${folderName}`, { cause: e }); } finally { if (logJson) { logger.debug(`Stack Config: ${JSON.stringify(stack)}}`); } }}
export const buildFileStacks = async (dirs: string[], options: FilesOnServerConfig & { logger: Logger }): Promise<TomlStack[]> => { const logger = childLogger(options.logger, 'Files On Server');
const { composeFileGlob = DEFAULT_COMPOSE_GLOB, envFileGlob = DEFAULT_ENV_GLOB, } = options;
logger.info(`Processing Stacks for ${dirs.length} folders:\n${dirs.join('\n')}`); logger.info(`Compose File Glob: ${composeFileGlob}`); logger.info(`Env Glob: ${envFileGlob}`);
const stacks: TomlStack[] = []; for (const dir of dirs) { try { stacks.push(await buildFileStack(dir, { ...options, logger })); } catch (e) { logger.error(new Error(`Unable to build Stack for folder ${dir}`, { cause: e })); } }
logger.info(`Built Stack configs for ${stacks.length} folders`); return stacks;}