2025-05-24 18:33:51 +02:00

167 lines
5.0 KiB
Bash

#!/bin/bash
# Setup Local Environment Script
# This script extracts configuration from terraform and sets up local development
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
TERRAFORM_DIR="$PROJECT_ROOT/deployment/terraform"
echo "Setting up local development environment..."
# Check if terraform directory exists
if [ ! -d "$TERRAFORM_DIR" ]; then
echo "Error: Terraform directory not found at $TERRAFORM_DIR"
exit 1
fi
# Change to terraform directory
cd "$TERRAFORM_DIR"
# Check if terraform state exists
if [ ! -f "terraform.tfstate" ]; then
echo "Error: Terraform state not found. Please run 'terraform apply' first."
exit 1
fi
echo "Extracting configuration from terraform..."
# Get terraform outputs
QDRANT_HOST=$(terraform output -raw vector_db_vm_external_ip 2>/dev/null || echo "")
QDRANT_PORT="6333"
FIRESTORE_PROJECT_ID=$(terraform output -raw firestore_database_id 2>/dev/null | cut -d'/' -f2 || echo "")
GCS_BUCKET_NAME=$(terraform output -raw storage_bucket_name 2>/dev/null || echo "")
if [ -z "$QDRANT_HOST" ]; then
echo "Error: Could not extract Qdrant host from terraform outputs"
exit 1
fi
echo "Configuration extracted:"
echo " Qdrant Host: $QDRANT_HOST"
echo " Qdrant Port: $QDRANT_PORT"
echo " Firestore Project: $FIRESTORE_PROJECT_ID"
echo " GCS Bucket: $GCS_BUCKET_NAME"
# Go back to project root
cd "$PROJECT_ROOT"
# Update start_dev.sh with extracted values
echo "Updating start_dev.sh..."
cat > start_dev.sh << EOF
#!/bin/bash
# Development startup script for Sereact API
# This script sets the environment variables and starts the application
# Auto-generated by deployment/scripts/setup_local_env.sh
# Activate virtual environment
source venv/Scripts/activate
# Set environment variables from deployed infrastructure
export QDRANT_HOST=$QDRANT_HOST
export QDRANT_PORT=$QDRANT_PORT
export FIRESTORE_PROJECT_ID=$FIRESTORE_PROJECT_ID
export GCS_BUCKET_NAME=$GCS_BUCKET_NAME
export ENVIRONMENT=development
# Start the application
echo "Starting Sereact API with deployed infrastructure..."
echo "Qdrant endpoint: http://\$QDRANT_HOST:\$QDRANT_PORT"
echo "Firestore project: \$FIRESTORE_PROJECT_ID"
echo "GCS bucket: \$GCS_BUCKET_NAME"
echo "API will be available at: http://localhost:8000"
echo "API documentation: http://localhost:8000/docs"
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
EOF
chmod +x start_dev.sh
# Update docker-compose.yml with extracted values
echo "Updating docker-compose.yml..."
cat > docker-compose.yml << EOF
version: '3.8'
services:
api:
build: .
ports:
- "8000:8000"
volumes:
- .:/app
- \${GOOGLE_APPLICATION_CREDENTIALS:-./firestore-credentials.json}:/app/firestore-credentials.json:ro
environment:
- PYTHONUNBUFFERED=1
- ENVIRONMENT=development
- FIRESTORE_CREDENTIALS_FILE=/app/firestore-credentials.json
- GOOGLE_APPLICATION_CREDENTIALS=/app/firestore-credentials.json
- FIRESTORE_PROJECT_ID=\${FIRESTORE_PROJECT_ID:-$FIRESTORE_PROJECT_ID}
- QDRANT_HOST=$QDRANT_HOST
- QDRANT_PORT=$QDRANT_PORT
- GCS_BUCKET_NAME=$GCS_BUCKET_NAME
command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload
EOF
# Update test script
echo "Updating test_qdrant_connection.py..."
cat > test_qdrant_connection.py << EOF
#!/usr/bin/env python3
"""
Simple test script to verify Qdrant connection
Auto-generated by deployment/scripts/setup_local_env.sh
"""
import os
import sys
from src.services.vector_db import VectorDatabaseService
def test_qdrant_connection():
"""Test the connection to Qdrant"""
# Set environment variables from deployed infrastructure
os.environ['QDRANT_HOST'] = '$QDRANT_HOST'
os.environ['QDRANT_PORT'] = '$QDRANT_PORT'
try:
print("Testing Qdrant connection...")
print(f"Host: {os.environ['QDRANT_HOST']}")
print(f"Port: {os.environ['QDRANT_PORT']}")
# Initialize the service
vector_db = VectorDatabaseService()
# Test health check
is_healthy = vector_db.health_check()
print(f"Health check: {'✓ PASSED' if is_healthy else '✗ FAILED'}")
# Get collection info
collection_info = vector_db.get_collection_info()
print(f"Collection info: {collection_info}")
print("\n✓ Qdrant connection test PASSED!")
return True
except Exception as e:
print(f"\n✗ Qdrant connection test FAILED: {e}")
return False
if __name__ == "__main__":
success = test_qdrant_connection()
sys.exit(0 if success else 1)
EOF
echo ""
echo "✓ Local development environment setup complete!"
echo ""
echo "You can now:"
echo " 1. Run './start_dev.sh' to start the API with deployed infrastructure"
echo " 2. Run 'docker-compose up' to use Docker with deployed Qdrant"
echo " 3. Run 'python test_qdrant_connection.py' to test Qdrant connection"
echo ""
echo "All configuration has been automatically extracted from your terraform deployment."