diff --git a/compose.yaml b/compose.yaml index 418f44f..7e645eb 100644 --- a/compose.yaml +++ b/compose.yaml @@ -14,6 +14,8 @@ services: ## optional #- TZ=America/New_York + ## Prefix to add to stack names (useful for multi-server deployments) + #- STACK_NAME_PREFIX=server-1 #- IMAGE_REGISTRY_PROVIDER= #- IMAGE_REGISTRY_ACCOUNT= #- AUTO_UPDATE=false diff --git a/docsite/docs/usage/overview.mdx b/docsite/docs/usage/overview.mdx index 1d30910..d6c522d 100644 --- a/docsite/docs/usage/overview.mdx +++ b/docsite/docs/usage/overview.mdx @@ -203,6 +203,7 @@ The configuration and behaviors below are applicable to all Stacks: | `SERVER_NAME` | ☑️ | | The name of the Komodo [Server](https://komo.do/docs/resources#server) where a Stack is located | | `STACKS_FROM` | ☑️ | `dir` | What [**Stack Sources**](#stack-sources) to generate Stacks from? `dir` or `compose` | | `HOST_DIR` | ☑️ | | The parent directory on the **host** that mounted into the Komodo Import container | +| `STACK_NAME_PREFIX` | - | | Prefix to add to all stack names. Useful for multi-server deployments to ensure unique stack names. Original name is preserved in `project_name` field. | | `WRITE_ENV` | - | `true` | Write the contents of found .env files to Komodo **Environment**. Likely want this as `true`. | | `COMPOSE_FILE_GLOB` | - | `**/{compose,docker-compose}*.y?(a)ml` | The [glob](https://github.com/isaacs/node-glob?tab=readme-ov-file#glob-primer) pattern to use for finding files for **Files Paths** in Stack config | | `ENV_FILE_GLOB` | - | `**/.env` | The [glob](https://github.com/isaacs/node-glob?tab=readme-ov-file#glob-primer) pattern to use for finding files for **Additional Env Files** in Stack config | @@ -251,6 +252,89 @@ Komodo Import will **log** which files it detects and indicate which ones it wil +##### Multi-Server Deployments + +When importing stacks from multiple servers running identical services (e.g., `crowdsec`, `traefik`, etc.), you may encounter naming conflicts where each server generates stacks with the same name. Use `STACK_NAME_PREFIX` to create unique stack names per server while preserving the original name for identification. + +
+ +Example: Multiple Servers with Same Stacks + +**Problem:** You have 3 servers, each running `crowdsec` and `traefik`. Without prefixes, all generate identical stack names: + +```toml +[[stack]] +name = "crowdsec" +# ... config +``` + +**Solution:** Use `STACK_NAME_PREFIX` for each server: + +```yaml title="Server 1 - compose.yaml" +services: + komodo-import: + # ... + environment: + - SERVER_NAME=server-1 + - STACK_NAME_PREFIX=server-1 +``` + +```yaml title="Server 2 - compose.yaml" +services: + komodo-import: + # ... + environment: + - SERVER_NAME=server-2 + - STACK_NAME_PREFIX=server-2 +``` + +**Result:** Each server generates unique stack names: + +```toml +# Server 1 +[[stack]] +name = "server-1-crowdsec" + +[stack.config] +server = "server-1" +project_name = "crowdsec" + +# Server 2 +[[stack]] +name = "server-2-crowdsec" + +[stack.config] +server = "server-2" +project_name = "crowdsec" +``` + +The `project_name` field preserves the original stack name for identification and komodo management (it will use the project-name as the stack name when issuing docker compose commands). + +
+ +
+ +Example: Environment-Based Naming + +You can also use prefixes to distinguish stacks by environment: + +```yaml +# Production +environment: + - STACK_NAME_PREFIX=prod + +# Staging +environment: + - STACK_NAME_PREFIX=staging + +# Development +environment: + - STACK_NAME_PREFIX=dev +``` + +Results in: `prod-myapp`, `staging-myapp`, `dev-myapp` + +
There is additional configuration available depending on the [Stack Source](#stack-sources): diff --git a/src/builders/stack/filesOnServer.ts b/src/builders/stack/filesOnServer.ts index f489f42..03d873b 100644 --- a/src/builders/stack/filesOnServer.ts +++ b/src/builders/stack/filesOnServer.ts @@ -20,6 +20,7 @@ export const buildFileStack = async (path: string, options: BuildFileStackOption autoUpdate, pollForUpdate, server, + stackNamePrefix, hostParentPath, composeFiles = [], projectName, @@ -37,10 +38,16 @@ export const buildFileStack = async (path: string, options: BuildFileStackOption let logJson = isDebugMode(); try { + const originalName = projectName ?? folderName; + const prefixedName = stackNamePrefix + ? `${stackNamePrefix}-${originalName}` + : originalName; + stack = { - name: projectName ?? folderName, + name: prefixedName, config: { server, + project_name: stackNamePrefix ? originalName : undefined, run_directory: join(hostParentPath, folderName), files_on_host: true, registry_account: imageRegistryAccount, diff --git a/src/builders/stack/gitStack.ts b/src/builders/stack/gitStack.ts index 88374ec..21cfe99 100644 --- a/src/builders/stack/gitStack.ts +++ b/src/builders/stack/gitStack.ts @@ -22,6 +22,7 @@ export const buildGitStack = async (path: string, options: BuildGitStackOptions) autoUpdate, pollForUpdate, server, + stackNamePrefix, projectName, composeFiles = [], writeEnv = false, @@ -103,10 +104,16 @@ export const buildGitStack = async (path: string, options: BuildGitStackOptions) let logJson = isDebugMode(); try { + const originalName = projectName ?? folderName; + const prefixedName = stackNamePrefix + ? `${stackNamePrefix}-${originalName}` + : originalName; + stack = { - name: projectName ?? folderName, + name: prefixedName, config: { server, + project_name: stackNamePrefix ? originalName : undefined, registry_account: imageRegistryAccount, registry_provider: imageRegistryProvider, auto_update: autoUpdate, diff --git a/src/common/infrastructure/config/common.ts b/src/common/infrastructure/config/common.ts index 850731b..39f5ca3 100644 --- a/src/common/infrastructure/config/common.ts +++ b/src/common/infrastructure/config/common.ts @@ -1,5 +1,6 @@ export interface CommonImportOptions { server: string + stackNamePrefix?: string imageRegistryProvider?: string imageRegistryAccount?: string autoUpdate?: boolean diff --git a/src/index.ts b/src/index.ts index 700da98..d1566db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -58,6 +58,7 @@ try { const importOptions: CommonImportOptions = { server: process.env.SERVER_NAME, + stackNamePrefix: isUndefinedOrEmptyString(process.env.STACK_NAME_PREFIX) ? undefined : process.env.STACK_NAME_PREFIX.trim(), imageRegistryProvider: process.env.IMAGE_REGISTRY_PROVIDER, imageRegistryAccount: process.env.IMAGE_REGISTRY_ACCOUNT, autoUpdate: isUndefinedOrEmptyString(process.env.AUTO_UPDATE) ? undefined : parseBool(process.env.AUTO_UPDATE),