| #!/bin/bash |
|
|
| |
| |
| |
|
|
| set -e |
|
|
| echo "π Starting Auto-Analyst Backend..." |
|
|
| |
| init_production_database() { |
| echo "π§ Running SAFE database initialization..." |
| |
| |
| python scripts/init_production_db.py |
| |
| |
| if [ $? -eq 0 ]; then |
| echo "β
Database initialization completed successfully" |
| else |
| echo "β οΈ Database initialization had issues, but continuing..." |
| echo "π App will start but some features may not work properly" |
| fi |
| } |
|
|
| |
| verify_app_imports() { |
| echo "π Verifying application imports..." |
| python -c " |
| try: |
| from app import app |
| print('β
Main application imports successful') |
| except Exception as e: |
| print(f'β Application import failed: {e}') |
| exit(1) |
| " || { |
| echo "β Critical application import failure - cannot start" |
| exit 1 |
| } |
| } |
|
|
| |
| verify_database_connectivity() { |
| echo "π Testing database connectivity..." |
| python -c " |
| try: |
| from src.db.init_db import get_session, is_postgres_db |
| from src.db.schemas.models import AgentTemplate |
| |
| db_type = 'PostgreSQL/RDS' if is_postgres_db() else 'SQLite' |
| print(f'ποΈ Database type: {db_type}') |
| |
| session = get_session() |
| |
| # Try to query templates if table exists |
| try: |
| template_count = session.query(AgentTemplate).count() |
| print(f'β
Database connected. Found {template_count} templates.') |
| except Exception as table_error: |
| print(f'β οΈ Database connected but template table issue: {table_error}') |
| print('π Template functionality may not work') |
| finally: |
| session.close() |
| |
| except Exception as e: |
| print(f'β οΈ Database connectivity issue: {e}') |
| print('π App will start but database features may not work') |
| " |
| |
| } |
|
|
| |
| |
| populate_agents_templates() { |
| echo "π§ Checking if agents/templates need to be populated..." |
| python -c " |
| try: |
| from src.db.init_db import DATABASE_URL |
| from src.db.schemas.models import AgentTemplate |
| from src.db.init_db import session_factory |
| |
| # Check database type |
| if DATABASE_URL.startswith('sqlite'): |
| print('π SQLite database detected - checking template population') |
| |
| session = session_factory() |
| try: |
| template_count = session.query(AgentTemplate).count() |
| |
| if template_count == 0: |
| print('π No templates found - populating agents and templates...') |
| session.close() |
| exit(1) # Signal that population is needed |
| else: |
| print(f'β
Found {template_count} templates - population not needed') |
| session.close() |
| exit(0) # Signal that population is not needed |
| except Exception as e: |
| print(f'β οΈ Error checking templates: {e}') |
| print('π Will attempt to populate anyway') |
| session.close() |
| exit(1) # Signal that population is needed |
| else: |
| print('π PostgreSQL/RDS detected - skipping auto-population') |
| exit(0) # Signal that population is not needed |
| |
| except Exception as e: |
| print(f'β Error during template check: {e}') |
| exit(0) # Don't fail startup, just skip population |
| " |
| |
| |
| if [ $? -eq 1 ]; then |
| echo "π Running agent/template population for SQLite..." |
| |
| |
| if [ -f "agents_config.json" ] || [ -f "/app/agents_config.json" ] || [ -f "../agents_config.json" ]; then |
| echo "π Found agents_config.json - validating configuration..." |
| |
| |
| python scripts/populate_agent_templates.py validate |
| validation_result=$? |
| |
| if [ $validation_result -eq 0 ]; then |
| echo "β
Configuration valid - proceeding with sync" |
| python scripts/populate_agent_templates.py sync |
| else |
| echo "β οΈ Configuration validation failed - attempting sync anyway" |
| python scripts/populate_agent_templates.py sync |
| fi |
| else |
| echo "β οΈ agents_config.json not found - trying legacy method" |
| python scripts/populate_agent_templates.py |
| fi |
| |
| if [ $? -eq 0 ]; then |
| echo "β
Agent/template population completed successfully" |
| else |
| echo "β οΈ Agent/template population had issues, but continuing..." |
| echo "π You may need to populate templates manually" |
| echo "π‘ Tip: Ensure agents_config.json exists in the backend directory" |
| fi |
| fi |
| } |
|
|
| |
| if [ ! -f "/app/agents_config.json" ]; then |
| echo "β οΈ agents_config.json not found in container - checking build issues" |
| echo "π Files in /app directory:" |
| ls -la /app/ | head -10 |
| else |
| echo "β
agents_config.json found in container" |
| fi |
|
|
| |
| echo "π§ Initializing production environment..." |
|
|
| |
| verify_app_imports |
|
|
| |
| init_production_database |
|
|
| |
| verify_database_connectivity |
|
|
| |
| populate_agents_templates |
|
|
| echo "π― Starting FastAPI application..." |
| echo "π Application will be available on port 7860" |
|
|
| |
| exec uvicorn app:app --host 0.0.0.0 --port 7860 |