diff --git a/backend/app/services/admin_api_service.py b/backend/app/services/admin_api_service.py index 53e5b85..506c130 100644 --- a/backend/app/services/admin_api_service.py +++ b/backend/app/services/admin_api_service.py @@ -118,3 +118,92 @@ class AdminAPIService: 'error': f'Admin API error: {str(e)}' } + def increment_domain_count(self, account_id: int) -> Dict: + """ + Increment domain count for CF account in Admin Panel + Called after successfully creating a domain + + Args: + account_id: CF account ID + + Returns: + { + "status": "success" | "error", + "message": str, + "new_count": int, + "available_capacity": int, + "error": str (if error) + } + """ + try: + url = f"{self.base_url}/api/cf-accounts/internal/{account_id}/increment" + response = requests.post(url, headers=self.headers, timeout=10) + + if response.status_code == 200: + return response.json() + else: + return { + 'status': 'error', + 'error': f'Admin API returned {response.status_code}: {response.text}' + } + + except requests.exceptions.Timeout: + return { + 'status': 'error', + 'error': 'Admin API request timeout' + } + except requests.exceptions.ConnectionError: + return { + 'status': 'error', + 'error': 'Cannot connect to Admin API' + } + except Exception as e: + return { + 'status': 'error', + 'error': f'Admin API error: {str(e)}' + } + + def decrement_domain_count(self, account_id: int) -> Dict: + """ + Decrement domain count for CF account in Admin Panel + Called after successfully deleting a domain + + Args: + account_id: CF account ID + + Returns: + { + "status": "success" | "error", + "message": str, + "new_count": int, + "available_capacity": int, + "error": str (if error) + } + """ + try: + url = f"{self.base_url}/api/cf-accounts/internal/{account_id}/decrement" + response = requests.post(url, headers=self.headers, timeout=10) + + if response.status_code == 200: + return response.json() + else: + return { + 'status': 'error', + 'error': f'Admin API returned {response.status_code}: {response.text}' + } + + except requests.exceptions.Timeout: + return { + 'status': 'error', + 'error': 'Admin API request timeout' + } + except requests.exceptions.ConnectionError: + return { + 'status': 'error', + 'error': 'Cannot connect to Admin API' + } + except Exception as e: + return { + 'status': 'error', + 'error': f'Admin API error: {str(e)}' + } diff --git a/deploy.sh b/deploy.sh index a46b978..0bfd6ec 100755 --- a/deploy.sh +++ b/deploy.sh @@ -13,13 +13,37 @@ echo "║ 🚀 Hosting Platform Deployment Script 🚀 ║" echo "╚══════════════════════════════════════════════════════════════╝" echo "" -# 1. Git Pull -echo "📥 [1/6] Pulling latest code from Gitea..." +# 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 pull origin main +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" +echo "✅ Git pull complete and .env restored" echo "" # 2. Backend Dependencies