Skip to content

Commit 34f73f0

Browse files
author
Phillip Simonds
committed
Make it easy to run multiple infrahub project instances locally
- Update invoke get_compose_command task to use project name based on repo or env var - Update docker-compose.override.yml to easily take manual ports for various services as env vars
1 parent b1d2cb7 commit 34f73f0

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

.env.example

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ export INFRAHUB_ENTERPRISE=false
3636
# - Bootstrap script loads objects/git-repo/github.yml
3737
export INFRAHUB_GIT_LOCAL=false
3838

39+
# =============================================================================
40+
# Parallel Instance Configuration
41+
# =============================================================================
42+
# Use these settings to run multiple instances of this project simultaneously.
43+
# Each instance needs a unique project name and different ports.
44+
#
45+
# Example for a second instance:
46+
# INFRAHUB_PROJECT_NAME=infrahub-2
47+
# INFRAHUB_PORT=8100
48+
# PREFECT_PORT=4201
49+
# STREAMLIT_PORT=8502
50+
# INFRAHUB_ADDRESS=http://localhost:8100
51+
# INFRAHUB_UI_URL=http://localhost:8100
52+
53+
# Docker Compose project name (defaults to directory name)
54+
# export INFRAHUB_PROJECT_NAME=infrahub-bundle-dc
55+
56+
# Port mappings for Docker services
57+
# export INFRAHUB_PORT=8000
58+
# export PREFECT_PORT=4200
59+
# export STREAMLIT_PORT=8501
60+
3961
# =============================================================================
4062
# Service Catalog Configuration
4163
# =============================================================================
@@ -62,10 +84,6 @@ export API_TIMEOUT=30
6284
# The service catalog will retry failed requests this many times before giving up
6385
export API_RETRY_COUNT=3
6486

65-
# Streamlit server port (optional)
66-
# Port on which the Streamlit application will run
67-
# STREAMLIT_PORT=8501
68-
6987
# =============================================================================
7088
# Additional Notes
7189
# =============================================================================

docker-compose.override.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
---
2+
# Override file for bundle-dc project
3+
# Port configuration via environment variables for running multiple instances:
4+
# INFRAHUB_PORT (default: 8000) - Infrahub server
5+
# PREFECT_PORT (default: 4200) - Prefect task manager UI
6+
# STREAMLIT_PORT (default: 8501) - Service catalog UI
7+
28
services:
39
database:
410
environment:
@@ -9,10 +15,15 @@ services:
915
- NEO4J_dbms_memory_heap_max__size=4g
1016
- NEO4J_dbms_memory_pagecache_size=2g
1117

18+
# Override Infrahub server port for parallel instances
19+
infrahub-server:
20+
ports:
21+
- "${INFRAHUB_PORT:-8000}:8000"
22+
1223
# Expose prefect UI
1324
task-manager:
1425
ports:
15-
- "4200:4200"
26+
- "${PREFECT_PORT:-4200}:4200"
1627

1728
# Mount local repository for local development
1829
# Used when INFRAHUB_GIT_LOCAL=true to work with local generator/transform changes
@@ -26,10 +37,10 @@ services:
2637
dockerfile: Dockerfile
2738
container_name: service-catalog
2839
ports:
29-
- "8501:8501"
40+
- "${STREAMLIT_PORT:-8501}:8501"
3041
environment:
3142
- INFRAHUB_ADDRESS=http://infrahub-server:8000
32-
- INFRAHUB_UI_URL=http://localhost:8000
43+
- INFRAHUB_UI_URL=${INFRAHUB_UI_URL:-http://localhost:8000}
3344
- INFRAHUB_API_TOKEN=${INFRAHUB_API_TOKEN:-06438eb2-8019-4776-878c-0941b1f1d1ec}
3445
- DEFAULT_BRANCH=main
3546
- GENERATOR_WAIT_TIME=60

tasks.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,42 @@
2626
INFRAHUB_SERVICE_CATALOG = os.getenv("INFRAHUB_SERVICE_CATALOG", "false").lower() == "true"
2727
INFRAHUB_GIT_LOCAL = os.getenv("INFRAHUB_GIT_LOCAL", "false").lower() == "true"
2828
MAIN_DIRECTORY_PATH = Path(__file__).parent
29+
INFRAHUB_PROJECT_NAME = os.getenv("INFRAHUB_PROJECT_NAME", MAIN_DIRECTORY_PATH.name)
2930

3031

3132
# Download compose file and use with override
3233
def get_compose_command() -> str:
33-
"""Generate docker compose command with override support."""
34+
"""Generate docker compose command with override support.
35+
36+
Supports layered compose files:
37+
1. docker-compose.yml (base, downloaded or local)
38+
2. docker-compose.override.yml (committed overrides, uses env vars for ports)
39+
40+
The project name is controlled by INFRAHUB_PROJECT_NAME env var,
41+
defaulting to the directory name.
42+
"""
3443
local_compose_file = MAIN_DIRECTORY_PATH / "docker-compose.yml"
3544
override_file = MAIN_DIRECTORY_PATH / "docker-compose.override.yml"
3645

37-
# Check if local docker-compose.yml exists
46+
base_cmd = f"docker compose -p {INFRAHUB_PROJECT_NAME}"
47+
48+
# If we have a local docker-compose.yml, use it directly
3849
if local_compose_file.exists():
39-
# Use local docker-compose.yml file
50+
cmd = f"{base_cmd} -f {local_compose_file}"
4051
if override_file.exists():
41-
return f"docker compose -p infrahub -f {local_compose_file} -f {override_file}"
42-
return f"docker compose -p infrahub-bundle-dc -f {local_compose_file}"
52+
cmd += f" -f {override_file}"
53+
return cmd
4354

4455
# Fall back to downloading from infrahub.opsmill.io
45-
# Determine the base URL based on edition
4656
if INFRAHUB_ENTERPRISE:
4757
base_url = f"https://infrahub.opsmill.io/enterprise/{INFRAHUB_VERSION}"
4858
else:
4959
base_url = f"https://infrahub.opsmill.io/{INFRAHUB_VERSION}"
5060

61+
cmd = f"curl -s {base_url} | {base_cmd} -f -"
5162
if override_file.exists():
52-
return f"curl -s {base_url} | docker compose -p infrahub -f - -f {override_file}"
53-
return f"curl -s {base_url} | docker compose -p infrahub -f -"
63+
cmd += f" -f {override_file}"
64+
return cmd
5465

5566

5667
def get_compose_source() -> str:
@@ -119,6 +130,7 @@ def info(context: Context) -> None:
119130
info_msg = (
120131
f"[cyan]Edition:[/cyan] {edition}\n"
121132
f"[cyan]Version:[/cyan] {INFRAHUB_VERSION}\n"
133+
f"[cyan]Project Name:[/cyan] {INFRAHUB_PROJECT_NAME}\n"
122134
f"[cyan]Compose Source:[/cyan] {COMPOSE_SOURCE}\n"
123135
f"[cyan]Service Catalog:[/cyan] {'Enabled' if INFRAHUB_SERVICE_CATALOG else 'Disabled'}\n"
124136
f"[cyan]Local Git Repository:[/cyan] {'Enabled' if INFRAHUB_GIT_LOCAL else 'Disabled'}\n"

0 commit comments

Comments
 (0)