#!/bin/bash cd "/app" || exit 1 export PYTHONPATH="$PYTHONPATH:/app/" # Ensure we can write the logs chown -R appuser:appuser /app/logs # Wait for the database to be ready echo "Waiting for database to be ready" until pg_isready -h $DB_HOST -p $DB_PORT; do echo "Postgres is unavailable - sleeping" sleep 2 done echo "Postgres is up - executing commands" export PGPASSWORD=$DB_PASS # Check if the database exists and initialize if not if ! psql -U $DB_USER -h $DB_HOST -p $DB_PORT -d $DB_NAME -c '\dt' | grep -q 'No relations found'; then echo "Database eveai does not exist or is empty. Initializing..." psql -U $DB_USER -h $DB_HOST -p $DB_PORT -d postgres -c "CREATE DATABASE $DB_NAME;" psql -U $DB_USER -h $DB_HOST -p $DB_PORT -d $DB_NAME -c "CREATE EXTENSION IF NOT EXISTS vector;" fi echo "Applying migrations to the public and tenant schema..." # Set FLASK_APP environment variables PROJECT_DIR="/app" export FLASK_APP=${PROJECT_DIR}/scripts/run_eveai_app.py # Adjust the path to your Flask app entry point export PYTHONPATH="$PROJECT_DIR/patched_packages:$PYTHONPATH:$PROJECT_DIR" # Include the app directory in the Python path & patched packages # Run Alembic upgrade for the public schema echo "Applying migrations to the public schema..." flask db upgrade -d "${PROJECT_DIR}/migrations/public" # Run Alembic upgrade for the tenant schema echo "Applying migrations to the tenant schema..." flask db upgrade -d "${PROJECT_DIR}/migrations/tenant" # Set flask environment variables #export FLASK_ENV=development # Use 'production' as appropriate #export FLASK_DEBUG=1 # Use 0 for production # Initialize initial data (tenant and user) echo "Initializing initial tenant and user..." python ${PROJECT_DIR}/scripts/initialize_data.py # Adjust the path to your initialization script # Start Flask app gunicorn -w 4 -k gevent -b 0.0.0.0:5001 scripts.run_eveai_app:app