#!/bin/bash # Docker startup script for GNX Web Application # This script handles automatic setup, permissions, and startup set -e echo "๐Ÿš€ Starting GNX Web Application..." echo "" # Set proper permissions for scripts and directories echo "๐Ÿ”ง Setting up permissions..." # Make scripts executable chmod +x docker-start.sh 2>/dev/null || true chmod +x migrate-data.sh 2>/dev/null || true chmod +x migrate-sqlite-to-postgres.sh 2>/dev/null || true # Set permissions for directories mkdir -p backEnd/media backEnd/staticfiles backEnd/logs backups chmod 755 backEnd/media backEnd/staticfiles backEnd/logs backups 2>/dev/null || true # Set permissions for database file if it exists if [ -f "backEnd/db.sqlite3" ]; then chmod 644 backEnd/db.sqlite3 2>/dev/null || true fi # Set permissions for .env files if [ -f ".env.production" ]; then chmod 600 .env.production 2>/dev/null || true fi echo "โœ… Permissions set" echo "" # Check if .env.production exists if [ ! -f .env.production ]; then echo "โš ๏ธ Warning: .env.production not found. Creating from example..." if [ -f .env.production.example ]; then cp .env.production.example .env.production echo "๐Ÿ“ Please edit .env.production with your actual values before continuing." exit 1 else echo "โŒ Error: .env.production.example not found!" exit 1 fi fi # Load environment variables export $(cat .env.production | grep -v '^#' | xargs) # Configure Nginx echo "๐Ÿ”ง Configuring Nginx..." # Check for existing nginx configs for gnxsoft NGINX_AVAILABLE="/etc/nginx/sites-available/gnxsoft" NGINX_ENABLED="/etc/nginx/sites-enabled/gnxsoft" NGINX_CONF="nginx.conf" # Check if nginx.conf exists if [ ! -f "$NGINX_CONF" ]; then echo "โŒ Error: nginx.conf not found in current directory!" exit 1 fi # Backup and remove old configs if they exist if [ -f "$NGINX_AVAILABLE" ]; then echo "๐Ÿ“ฆ Backing up existing nginx config..." sudo cp "$NGINX_AVAILABLE" "${NGINX_AVAILABLE}.backup.$(date +%Y%m%d_%H%M%S)" echo "โœ… Old config backed up" fi if [ -L "$NGINX_ENABLED" ]; then echo "๐Ÿ”— Removing old symlink..." sudo rm -f "$NGINX_ENABLED" fi # Check for other gnxsoft configs and remove them for file in /etc/nginx/sites-available/gnxsoft* /etc/nginx/sites-enabled/gnxsoft*; do if [ -f "$file" ] || [ -L "$file" ]; then if [ "$file" != "$NGINX_AVAILABLE" ] && [ "$file" != "$NGINX_ENABLED" ]; then echo "๐Ÿ—‘๏ธ Removing old config: $file" sudo rm -f "$file" fi fi done # Copy new nginx config echo "๐Ÿ“‹ Installing new nginx configuration..." sudo cp "$NGINX_CONF" "$NGINX_AVAILABLE" # Create symlink echo "๐Ÿ”— Creating symlink..." sudo ln -sf "$NGINX_AVAILABLE" "$NGINX_ENABLED" # Update paths in nginx config if needed (using current directory) CURRENT_DIR=$(pwd) echo "๐Ÿ“ Updating paths in nginx config..." sudo sed -i "s|/home/gnx/Desktop/GNX-WEB|$CURRENT_DIR|g" "$NGINX_AVAILABLE" # Generate or get INTERNAL_API_KEY if [ -z "$INTERNAL_API_KEY" ] || [ "$INTERNAL_API_KEY" = "your-generated-key-here" ]; then echo "๐Ÿ”‘ Generating new INTERNAL_API_KEY..." INTERNAL_API_KEY=$(python3 -c "import secrets; print(secrets.token_urlsafe(32))" 2>/dev/null || openssl rand -base64 32 | tr -d "=+/" | cut -c1-32) # Update .env.production with the generated key if [ -f .env.production ]; then if grep -q "INTERNAL_API_KEY=" .env.production; then sed -i "s|INTERNAL_API_KEY=.*|INTERNAL_API_KEY=$INTERNAL_API_KEY|" .env.production else echo "INTERNAL_API_KEY=$INTERNAL_API_KEY" >> .env.production fi echo "โœ… Updated .env.production with generated INTERNAL_API_KEY" fi # Export for use in this script export INTERNAL_API_KEY fi # Set INTERNAL_API_KEY in nginx config echo "๐Ÿ”‘ Setting INTERNAL_API_KEY in nginx config..." sudo sed -i "s|PLACEHOLDER_INTERNAL_API_KEY|$INTERNAL_API_KEY|g" "$NGINX_AVAILABLE" echo "โœ… INTERNAL_API_KEY configured in nginx" # Test nginx configuration echo "๐Ÿงช Testing nginx configuration..." if sudo nginx -t; then echo "โœ… Nginx configuration is valid" echo "๐Ÿ”„ Reloading nginx..." sudo systemctl reload nginx echo "โœ… Nginx reloaded successfully" else echo "โŒ Nginx configuration test failed!" echo "โš ๏ธ Please check the configuration manually" exit 1 fi # Build images echo "๐Ÿ”จ Building Docker images..." docker-compose build # Start containers echo "โ–ถ๏ธ Starting containers..." docker-compose up -d # Wait for services to be ready echo "โณ Waiting for services to start..." sleep 10 # Wait for PostgreSQL to be ready (if using PostgreSQL) if echo "$DATABASE_URL" | grep -q "postgresql://"; then echo "โณ Waiting for PostgreSQL to be ready..." timeout=30 while [ $timeout -gt 0 ]; do if docker-compose exec -T postgres pg_isready -U ${POSTGRES_USER:-gnx} > /dev/null 2>&1; then echo "โœ… PostgreSQL is ready" break fi echo " Waiting for PostgreSQL... ($timeout seconds remaining)" sleep 2 timeout=$((timeout - 2)) done if [ $timeout -le 0 ]; then echo "โš ๏ธ Warning: PostgreSQL may not be ready, but continuing..." fi # Check if we need to migrate from SQLite if [ -f "./backEnd/db.sqlite3" ] && [ ! -f ".migrated_to_postgres" ]; then echo "" echo "๐Ÿ”„ SQLite database detected. Checking if migration is needed..." # Check if PostgreSQL database is empty (only has default tables) POSTGRES_TABLES=$(docker-compose exec -T backend python manage.py shell -c " from django.db import connection cursor = connection.cursor() cursor.execute(\"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public' AND table_name NOT LIKE 'django_%'\") print(cursor.fetchone()[0]) " 2>/dev/null | tail -1 || echo "0") # Check if SQLite has data SQLITE_HAS_DATA=$(docker-compose exec -T backend bash -c " export DATABASE_URL=sqlite:///db.sqlite3 python manage.py shell -c \" from django.contrib.auth.models import User from django.db import connection cursor = connection.cursor() cursor.execute('SELECT name FROM sqlite_master WHERE type=\"table\" AND name NOT LIKE \"sqlite_%\" AND name NOT LIKE \"django_%\"') tables = cursor.fetchall() has_data = False for table in tables: cursor.execute(f'SELECT COUNT(*) FROM {table[0]}') if cursor.fetchone()[0] > 0: has_data = True break print('1' if has_data else '0') \" 2>/dev/null " | tail -1 || echo "0") if [ "$SQLITE_HAS_DATA" = "1" ] && [ "$POSTGRES_TABLES" = "0" ] || [ "$POSTGRES_TABLES" -lt 5 ]; then echo "๐Ÿ“ฆ SQLite database has data. Starting migration to PostgreSQL..." echo " This may take a few minutes..." echo "" # Run migration script if [ -f "./migrate-sqlite-to-postgres.sh" ]; then ./migrate-sqlite-to-postgres.sh else echo "โš ๏ธ Migration script not found. Please run manually:" echo " ./migrate-sqlite-to-postgres.sh" fi else echo "โœ… No migration needed (PostgreSQL already has data or SQLite is empty)" touch .migrated_to_postgres fi fi fi # Run migrations echo "๐Ÿ“ฆ Running database migrations..." docker-compose exec -T backend python manage.py migrate --noinput # Collect static files echo "๐Ÿ“ Collecting static files..." docker-compose exec -T backend python manage.py collectstatic --noinput # Check health echo "๐Ÿฅ Checking service health..." docker-compose ps echo "" echo "โœ… GNX Web Application is running!" echo "" echo "Backend: http://localhost:1086" echo "Frontend: http://localhost:1087" echo "Nginx: Configured and running" echo "" echo "View logs: docker-compose logs -f" echo "Stop services: docker-compose down" echo "" echo "๐Ÿ“‹ Nginx config location: $NGINX_AVAILABLE"