hosting-platform/deploy.sh

123 lines
3.8 KiB
Bash
Executable File

#!/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
echo "🔄 [5/6] Restarting services..."
ssh -i $SSH_KEY $HOST << 'ENDSSH'
supervisorctl restart hosting-backend hosting-frontend
ENDSSH
sleep 3
echo "✅ Services restarted"
echo ""
# 6. Health Check
echo "🏥 [6/6] Running health checks..."
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 ""