#!/bin/bash # Hosting Platform Deployment Script # Usage: ./deploy.sh set -e HOST="root@176.96.129.77" SSH_KEY="~/.ssh/id_rsa" echo "╔══════════════════════════════════════════════════════════════╗" echo "║ 🚀 Hosting Platform Deployment Script 🚀 ║" echo "╚══════════════════════════════════════════════════════════════╝" echo "" # 1. Backup and Git Pull echo "📥 [1/6] Backing up .env and pulling latest code..." ssh -i $SSH_KEY $HOST << 'ENDSSH' # Backup .env file mkdir -p /opt/backups/hosting-platform if [ -f /opt/hosting-platform/backend/.env ]; then cp /opt/hosting-platform/backend/.env /opt/backups/hosting-platform/.env.backup.$(date +%Y%m%d_%H%M%S) fi # Pull latest code cd /opt/hosting-platform git fetch origin git reset --hard origin/main # Restore .env file LATEST_BACKUP=$(ls -t /opt/backups/hosting-platform/.env.backup.* 2>/dev/null | head -1) if [ -n "$LATEST_BACKUP" ]; then cp "$LATEST_BACKUP" /opt/hosting-platform/backend/.env fi # Ensure ADMIN_API config exists if ! grep -q "ADMIN_API_URL" /opt/hosting-platform/backend/.env; then cat >> /opt/hosting-platform/backend/.env << 'EOF' # Admin Panel API (for fetching CF accounts) ADMIN_API_URL=http://localhost:5001 ADMIN_API_INTERNAL_KEY=internal-api-key-1c2f72e80d1bc5f9bfee7e3726319ac7 EOF fi ENDSSH echo "✅ Git pull complete and .env restored" echo "" # 2. Backend Dependencies echo "📦 [2/6] Installing backend dependencies..." ssh -i $SSH_KEY $HOST << 'ENDSSH' cd /opt/hosting-platform/backend source venv/bin/activate pip install -q -r requirements.txt ENDSSH echo "✅ Backend dependencies installed" echo "" # 3. Database Migration echo "🗄️ [3/6] Running database migrations..." ssh -i $SSH_KEY $HOST << 'ENDSSH' cd /opt/hosting-platform/backend source venv/bin/activate python -c "from app.main import app, db; app.app_context().push(); db.create_all()" ENDSSH echo "✅ Database migrations complete" echo "" # 4. Frontend Build echo "🎨 [4/6] Building frontend..." ssh -i $SSH_KEY $HOST << 'ENDSSH' cd /opt/hosting-platform/frontend npm install --silent npm run build ENDSSH echo "✅ Frontend built" echo "" # 5. Restart Services with Proper Cleanup echo "🔄 [5/6] Restarting services..." ssh -i $SSH_KEY $HOST << 'ENDSSH' # Kill all existing backend processes echo " 🔪 Stopping all backend processes..." pkill -f "python.*hosting-platform.*main.py" || true sleep 2 # Force kill if still running if pgrep -f "python.*hosting-platform.*main.py" > /dev/null; then echo " ⚠️ Force killing remaining processes..." pkill -9 -f "python.*hosting-platform.*main.py" || true sleep 1 fi # Update supervisor config with proper settings cat > /etc/supervisor/conf.d/hosting-backend.conf << 'EOF' [program:hosting-backend] command=/opt/hosting-platform/backend/venv/bin/python /opt/hosting-platform/backend/app/main.py directory=/opt/hosting-platform/backend user=root autostart=true autorestart=true startsecs=5 stopwaitsecs=10 stopsignal=TERM killasgroup=true stopasgroup=true redirect_stderr=true stdout_logfile=/var/log/hosting-backend.log stdout_logfile_maxbytes=50MB stdout_logfile_backups=5 environment=PYTHONPATH="/opt/hosting-platform/backend",FLASK_APP="app.main",FLASK_ENV="production",SECRET_KEY="cfef4ad2f52832def87c20ebddb5067c44379c5ab366ebeb50217b5f484a92df",DATABASE_URL="postgresql://hosting:hosting_519c6c66a8e2695ce704ccba@localhost:5432/hosting",REDIS_URL="redis://localhost:6379/0",ENCRYPTION_KEY="tThpEL7KeYwGSg9isM7LUbxv-Lju325c2gtIf56DHV4",PLATFORM_CF_API_TOKEN="",PLATFORM_CF_ACCOUNT_ID="",LB_IPS="185.123.45.67,185.123.45.68,185.123.45.69",API_HOST="0.0.0.0",API_PORT="5000" EOF # Reload supervisor echo " 🔄 Reloading supervisor..." supervisorctl reread supervisorctl update sleep 2 # Start backend echo " ▶️ Starting backend..." supervisorctl start hosting-backend || true sleep 3 # Restart frontend supervisorctl restart hosting-frontend || true ENDSSH sleep 3 echo "✅ Services restarted" echo "" # 6. Health Check echo "🏥 [6/6] Running health checks..." # Wait for backend to be ready MAX_RETRIES=15 RETRY_COUNT=0 echo " ⏳ Waiting for backend to be ready..." while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do HEALTH=$(ssh -i $SSH_KEY $HOST "curl -s http://localhost:5000/health" 2>/dev/null || echo "") if echo "$HEALTH" | grep -q "ok"; then echo " ✅ Backend is healthy!" break fi RETRY_COUNT=$((RETRY_COUNT + 1)) echo " ⏳ Attempt $RETRY_COUNT/$MAX_RETRIES..." sleep 2 done if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then echo " ❌ Backend failed to start! Checking logs..." ssh -i $SSH_KEY $HOST "tail -30 /var/log/hosting-backend.log" exit 1 fi # Check public endpoints sleep 2 HEALTH=$(curl -s https://api.argeict.net/health) if echo "$HEALTH" | grep -q "ok"; then echo "✅ API Health: OK" else echo "❌ API Health: FAILED" exit 1 fi ADMIN=$(curl -s https://api.argeict.net/api/admin/cf-accounts) if echo "$ADMIN" | grep -q "success"; then echo "✅ Admin Endpoints: OK" else echo "❌ Admin Endpoints: FAILED" exit 1 fi echo "" echo "╔══════════════════════════════════════════════════════════════╗" echo "║ ✅ DEPLOYMENT SUCCESSFUL! ✅ ║" echo "╚══════════════════════════════════════════════════════════════╝" echo "" echo "🌐 URLs:" echo " Frontend: https://argeict.net" echo " API: https://api.argeict.net" echo " Gitea: https://gitea.argeict.net" echo "" echo "📝 Next steps:" echo " - Test the new features in the admin panel" echo " - Check logs: ssh $HOST 'tail -f /var/log/hosting-backend.log'" echo ""