From 2fbaa6014f4ca1f879bf7abfe103965bc43f957b Mon Sep 17 00:00:00 2001 From: usmannasir Date: Sun, 13 Apr 2025 16:35:41 +0500 Subject: [PATCH] add code to fetch existing backups --- backup/backupManager.py | 72 ++++++++++++++++++++ backup/static/backup/backup.js | 44 ++++++++++++ backup/templates/backup/oneClickBackups.html | 8 +-- backup/urls.py | 1 + 4 files changed, 121 insertions(+), 4 deletions(-) diff --git a/backup/backupManager.py b/backup/backupManager.py index 37b52ec83..f4ebe7f5d 100755 --- a/backup/backupManager.py +++ b/backup/backupManager.py @@ -34,6 +34,7 @@ import googleapiclient.discovery from googleapiclient.discovery import build from websiteFunctions.models import NormalBackupDests, NormalBackupJobs, NormalBackupSites from plogical.IncScheduler import IncScheduler +from django.http import JsonResponse class BackupManager: localBackupPath = '/home/cyberpanel/localBackupPath' @@ -2338,4 +2339,75 @@ class BackupManager: json_data = json.dumps(data_ret) return HttpResponse(json_data) + def ReconfigureSubscription(self, request=None, userID=None, data=None): + try: + userID = request.session['userID'] + admin = Administrator.objects.get(pk=userID) + + data = json.loads(request.body) + subscription_id = data['subscription_id'] + customer_id = data['customer_id'] + plan_name = data['plan_name'] + amount = data['amount'] + interval = data['interval'] + + # Call platform API to update SFTP key + import requests + import json + + url = 'http://platform.cyberpersons.com/Billing/ReconfigureSubscription' + + payload = { + 'subscription_id': subscription_id, + 'key': ProcessUtilities.outputExecutioner(f'cat /root/.ssh/cyberpanel.pub'), + 'serverIP': ACLManager.fetchIP() + } + + headers = {'Content-Type': 'application/json'} + response = requests.post(url, headers=headers, data=json.dumps(payload)) + + if response.status_code == 200: + response_data = response.json() + if response_data.get('status') == 1: + # Create OneClickBackups record + from IncBackups.models import OneClickBackups + backup_plan = OneClickBackups( + owner=admin, + planName=plan_name, + months='1' if interval == 'month' else '12', + price=amount, + customer=customer_id, + subscription=subscription_id, + sftpUser=response_data.get('sftpUser'), # Get username from platform response + state=1 # Set as active since SFTP is already configured + ) + backup_plan.save() + + # Create SFTP destination in CyberPanel + finalDic = { + 'IPAddress': response_data.get('ipAddress'), + 'password': 'NOT-NEEDED', + 'backupSSHPort': '22', + 'userName': response_data.get('sftpUser'), # Use username from platform + 'type': 'SFTP', + 'path': 'cpbackups', + 'name': response_data.get('sftpUser') # Use username from platform + } + + wm = BackupManager() + response_inner = wm.submitDestinationCreation(userID, finalDic) + response_data_inner = json.loads(response_inner.content.decode('utf-8')) + + if response_data_inner.get('status') == 0: + return JsonResponse({'status': 0, 'error_message': response_data_inner.get('error_message')}) + + return JsonResponse({'status': 1}) + else: + return JsonResponse({'status': 0, 'error_message': response_data.get('error_message')}) + else: + return JsonResponse({'status': 0, 'error_message': f'Platform API error: {response.text}'}) + + except BaseException as msg: + return JsonResponse({'status': 0, 'error_message': str(msg)}) + diff --git a/backup/static/backup/backup.js b/backup/static/backup/backup.js index 2aebf2bd3..d7617492a 100755 --- a/backup/static/backup/backup.js +++ b/backup/static/backup/backup.js @@ -256,6 +256,50 @@ app.controller('backupPlanNowOneClick', function($scope, $http) { }); } }; + + $scope.ReconfigureSubscription = function(subscription) { + $scope.cyberpanelLoading = false; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + + var data = { + subscription_id: subscription.subscription_id, + customer_id: subscription.customer, + plan_name: subscription.plan_name, + amount: subscription.amount, + interval: subscription.interval + }; + + $http.post('/backup/ReconfigureSubscription', data, config).then(function(response) { + $scope.cyberpanelLoading = true; + if (response.data.status === 1) { + new PNotify({ + title: 'Success', + text: 'Subscription configured successfully for this server.', + type: 'success' + }); + // Refresh the page to show new backup plan in the list + window.location.reload(); + } else { + new PNotify({ + title: 'Error', + text: response.data.error_message, + type: 'error' + }); + } + }, function(error) { + $scope.cyberpanelLoading = true; + new PNotify({ + title: 'Error', + text: 'Could not configure subscription. Please try again.', + type: 'error' + }); + }); + }; }); //*** Backup site ****// diff --git a/backup/templates/backup/oneClickBackups.html b/backup/templates/backup/oneClickBackups.html index 1ec0c61e5..c1c1d631e 100755 --- a/backup/templates/backup/oneClickBackups.html +++ b/backup/templates/backup/oneClickBackups.html @@ -124,7 +124,7 @@ - + @@ -134,7 +134,7 @@ - + diff --git a/backup/urls.py b/backup/urls.py index 310a7c95d..22b0a2953 100755 --- a/backup/urls.py +++ b/backup/urls.py @@ -10,6 +10,7 @@ urlpatterns = [ re_path(r'^fetchOCSites$', views.fetchOCSites, name='fetchOCSites'), re_path(r'^StartOCRestore$', views.StartOCRestore, name='StartOCRestore'), re_path(r'^DeployAccount$', views.DeployAccount, name='DeployAccount'), + re_path(r'^ReconfigureSubscription$', views.ReconfigureSubscription, name='ReconfigureSubscription'), re_path(r'^backupSite$', views.backupSite, name='backupSite'), re_path(r'^restoreSite$', views.restoreSite, name='restoreSite'),
Plan NameSubscription ID Status Amount Billing Interval
{$ sub.plan_name $}{$ sub.subscription_id $} {$ sub.status $} @@ -144,8 +144,8 @@ {$ sub.interval $} {$ sub.current_period_end | date:'medium' $} -