Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
84 changes: 84 additions & 0 deletions docsite/docs/usage/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -251,6 +252,89 @@ Komodo Import will **log** which files it detects and indicate which ones it wil

</details>

##### 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.

<details>

<summary>Example: Multiple Servers with Same Stacks</summary>

**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).

</details>

<details>

<summary>Example: Environment-Based Naming</summary>

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`

</details>

There is additional configuration available depending on the [Stack Source](#stack-sources):

Expand Down
9 changes: 8 additions & 1 deletion src/builders/stack/filesOnServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const buildFileStack = async (path: string, options: BuildFileStackOption
autoUpdate,
pollForUpdate,
server,
stackNamePrefix,
hostParentPath,
composeFiles = [],
projectName,
Expand All @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion src/builders/stack/gitStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const buildGitStack = async (path: string, options: BuildGitStackOptions)
autoUpdate,
pollForUpdate,
server,
stackNamePrefix,
projectName,
composeFiles = [],
writeEnv = false,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/common/infrastructure/config/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface CommonImportOptions {
server: string
stackNamePrefix?: string
imageRegistryProvider?: string
imageRegistryAccount?: string
autoUpdate?: boolean
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down