From 30e4c9407b3362aeae624acd0868eec6019501d3 Mon Sep 17 00:00:00 2001 From: Yury Kossakovsky Date: Thu, 25 Dec 2025 13:15:07 -0700 Subject: [PATCH] fix: initialize databases before starting dependent services - start postgresql first, then run init_databases.sh, then other services - add depends_on postgres for lightrag service - remove unused nocodb from databases list - document database initialization in add-new-service workflow --- .claude/commands/add-new-service.md | 42 ++++++++++++++++++++++++++--- CLAUDE.md | 12 +++++---- docker-compose.yml | 3 +++ scripts/apply_update.sh | 11 +++++--- scripts/init_databases.sh | 5 ++-- scripts/install.sh | 10 +++++-- 6 files changed, 66 insertions(+), 17 deletions(-) diff --git a/.claude/commands/add-new-service.md b/.claude/commands/add-new-service.md index 5ee4ede..86b1768 100644 --- a/.claude/commands/add-new-service.md +++ b/.claude/commands/add-new-service.md @@ -87,7 +87,40 @@ Common dependencies: - `minio` - S3-compatible object storage - `clickhouse` - Analytics database (for Langfuse) -### 1.5 Proxy Configuration (for outbound AI API calls) +### 1.5 Database Initialization (if using PostgreSQL) + +If service requires its own PostgreSQL database, add it to `scripts/init_databases.sh`: + +```bash +# List of databases to create (add new services here) +DATABASES=( + "langfuse" + "lightrag" + "postiz" + "waha" + "$ARGUMENTS" # Add your service here +) +``` + +**File:** `scripts/init_databases.sh` + +This script: +- Runs automatically during install/update (BEFORE services start) +- Creates database if it doesn't exist (idempotent) +- Waits for PostgreSQL to be healthy first + +**Important:** Database name should match what's configured in docker-compose.yml environment variables. + +Example in docker-compose.yml: +```yaml +environment: + DATABASE_URL: postgresql://postgres:${POSTGRES_PASSWORD}@postgres:5432/$ARGUMENTS + # OR individual vars: + POSTGRES_HOST: postgres + POSTGRES_DATABASE: $ARGUMENTS +``` + +### 1.6 Proxy Configuration (for outbound AI API calls) If service makes HTTP requests to external AI APIs (OpenAI, Anthropic, Google, etc.), add proxy support: @@ -102,7 +135,7 @@ The `x-proxy-env` anchor (defined at top of docker-compose.yml) provides: - `HTTP_PROXY`, `HTTPS_PROXY`, `http_proxy`, `https_proxy` → `${GOST_PROXY_URL:-}` - `NO_PROXY`, `no_proxy` → `${GOST_NO_PROXY:-}` -### 1.6 Healthcheck Proxy Bypass +### 1.7 Healthcheck Proxy Bypass **CRITICAL:** If using `<<: *proxy-env`, healthcheck MUST bypass proxy: @@ -116,7 +149,7 @@ healthcheck: The `http_proxy= https_proxy= HTTP_PROXY= HTTPS_PROXY=` prefix clears proxy vars for healthcheck only. -### 1.7 Multi-service Profiles +### 1.8 Multi-service Profiles For services with multiple containers, use same profile for all: @@ -160,7 +193,7 @@ Examples in project: - `ragflow` → ragflow + ragflow-mysql + ragflow-redis + ragflow-minio + ragflow-elasticsearch - `monitoring` → prometheus + grafana + cadvisor + node-exporter -### 1.8 Hardware/GPU Profiles +### 1.9 Hardware/GPU Profiles For services with CPU/GPU variants, use mutually exclusive profiles: @@ -660,6 +693,7 @@ bash -n scripts/07_final_report.sh ### If Database Required - [ ] `docker-compose.yml`: `depends_on` with `condition: service_healthy` +- [ ] `scripts/init_databases.sh`: add database name to `DATABASES` array ### If First-Run Setup Needed - [ ] `scripts/generate_welcome_page.sh`: `QUICK_START_ARRAY` entry diff --git a/CLAUDE.md b/CLAUDE.md index ae2afd7..7338328 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -22,6 +22,7 @@ This is **n8n-install**, a Docker Compose-based installer that provides a compre - `scripts/install.sh`: Main installation orchestrator - `scripts/04_wizard.sh`: Interactive service selection using whiptail - `scripts/03_generate_secrets.sh`: Secret generation and bcrypt hashing +- `scripts/init_databases.sh`: Creates isolated PostgreSQL databases for services - `scripts/07_final_report.sh`: Post-install credential summary ## Common Development Commands @@ -56,11 +57,12 @@ Follow this workflow when adding a new optional service (refer to `.claude/comma 3. **.env.example**: Add `MYSERVICE_HOSTNAME=myservice.yourdomain.com` and credentials if using basic auth. 4. **scripts/03_generate_secrets.sh**: Generate passwords and bcrypt hashes. Add to `VARS_TO_GENERATE` map. 5. **scripts/04_wizard.sh**: Add service to `base_services_data` array for wizard selection. -6. **scripts/generate_welcome_page.sh**: Add service to `SERVICES_ARRAY` for welcome dashboard. -7. **welcome/app.js**: Add `SERVICE_METADATA` entry with name, description, icon, color, category. -8. **scripts/07_final_report.sh**: Add service URL and credentials output using `is_profile_active "myservice"`. -9. **README.md**: Add one-line description under "What's Included". -10. **CHANGELOG.md**: Add entry under `## [Unreleased]` → `### Added`. +6. **scripts/init_databases.sh**: If service uses PostgreSQL, add database name to `DATABASES` array. +7. **scripts/generate_welcome_page.sh**: Add service to `SERVICES_ARRAY` for welcome dashboard. +8. **welcome/app.js**: Add `SERVICE_METADATA` entry with name, description, icon, color, category. +9. **scripts/07_final_report.sh**: Add service URL and credentials output using `is_profile_active "myservice"`. +10. **README.md**: Add one-line description under "What's Included". +11. **CHANGELOG.md**: Add entry under `## [Unreleased]` → `### Added`. **Always ask users if the new service requires Caddy basic auth protection.** diff --git a/docker-compose.yml b/docker-compose.yml index d3f8877..4593c4c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1056,6 +1056,9 @@ services: container_name: lightrag profiles: ["lightrag"] restart: unless-stopped + depends_on: + postgres: + condition: service_healthy environment: <<: *proxy-env # Server Configuration diff --git a/scripts/apply_update.sh b/scripts/apply_update.sh index b87f268..1867468 100755 --- a/scripts/apply_update.sh +++ b/scripts/apply_update.sh @@ -92,13 +92,18 @@ $COMPOSE_CMD -p "localai" "${COMPOSE_FILES_FOR_PULL[@]}" pull --ignore-buildable exit 1 } -# Start services using the 06_run_services.sh script -log_info "Running Services..." -bash "$RUN_SERVICES_SCRIPT" || { log_error "Failed to start services. Check logs for details."; exit 1; } +# Start PostgreSQL first to initialize databases before other services +log_info "Starting PostgreSQL..." +$COMPOSE_CMD -p "localai" up -d postgres || { log_error "Failed to start PostgreSQL"; exit 1; } # Initialize PostgreSQL databases for services (creates if not exist) +# This must run BEFORE other services that depend on these databases bash "$SCRIPT_DIR/init_databases.sh" || { log_warning "Database initialization had issues, but continuing..."; } +# Start all services using the 06_run_services.sh script (postgres is already running) +log_info "Running Services..." +bash "$RUN_SERVICES_SCRIPT" || { log_error "Failed to start services. Check logs for details."; exit 1; } + log_success "Update application completed successfully!" # --- Fix file permissions --- diff --git a/scripts/init_databases.sh b/scripts/init_databases.sh index 883e714..9e1d72c 100755 --- a/scripts/init_databases.sh +++ b/scripts/init_databases.sh @@ -5,7 +5,7 @@ # This script runs during install/update and creates databases if they don't exist. # Safe to run multiple times - only creates missing databases. # -# Usage: Called automatically from install.sh and update.sh +# Usage: Called automatically from install.sh and apply_update.sh # ============================================================================= source "$(dirname "$0")/utils.sh" && init_paths @@ -15,7 +15,6 @@ source "$(dirname "$0")/utils.sh" && init_paths DATABASES=( "langfuse" "lightrag" - "nocodb" "postiz" "waha" ) @@ -44,7 +43,7 @@ EXISTING=0 for db in "${DATABASES[@]}"; do # Check if database exists - EXISTS=$(docker exec postgres psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname = '$db'" 2>/dev/null) + EXISTS=$(docker exec postgres psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname = '$db'" 2>/dev/null | tr -d ' ') if [ "$EXISTS" = "1" ]; then log_info "Database '$db' already exists" diff --git a/scripts/install.sh b/scripts/install.sh index e4c205f..8a01809 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -120,12 +120,18 @@ bash "$SCRIPT_DIR/05_configure_services.sh" || { log_error "Configure Services f log_success "Configure Services complete!" show_step 6 8 "Running Services" -bash "$SCRIPT_DIR/06_run_services.sh" || { log_error "Running Services failed"; exit 1; } -log_success "Running Services complete!" +# Start PostgreSQL first to initialize databases before other services +log_info "Starting PostgreSQL..." +docker compose -p localai up -d postgres || { log_error "Failed to start PostgreSQL"; exit 1; } # Initialize PostgreSQL databases for services (creates if not exist) +# This must run BEFORE other services that depend on these databases bash "$SCRIPT_DIR/init_databases.sh" || { log_warning "Database initialization had issues, but continuing..."; } +# Now start all services (postgres is already running) +bash "$SCRIPT_DIR/06_run_services.sh" || { log_error "Running Services failed"; exit 1; } +log_success "Running Services complete!" + show_step 7 8 "Generating Final Report" # --- Installation Summary --- log_info "Installation Summary:"