Files
GNX-WEB/docker-start.sh
Iliyan Angelov 0b1cabcfaf updates
2025-11-24 16:47:37 +02:00

241 lines
7.9 KiB
Bash
Executable File

#!/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"