mirror of
https://github.com/usmannasir/cyberpanel.git
synced 2025-11-07 13:56:01 +01:00
add code to fetch existing backups
This commit is contained in:
@@ -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)})
|
||||
|
||||
|
||||
|
||||
@@ -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 ****//
|
||||
|
||||
@@ -124,7 +124,7 @@
|
||||
<table class="table table-hover table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Plan Name</th>
|
||||
<th>Subscription ID</th>
|
||||
<th>Status</th>
|
||||
<th>Amount</th>
|
||||
<th>Billing Interval</th>
|
||||
@@ -134,7 +134,7 @@
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr ng-repeat="sub in subscriptions">
|
||||
<td>{$ sub.plan_name $}</td>
|
||||
<td>{$ sub.subscription_id $}</td>
|
||||
<td>
|
||||
<span class="badge" ng-class="{'badge-success': sub.status === 'active', 'badge-warning': sub.status !== 'active'}">
|
||||
{$ sub.status $}
|
||||
@@ -144,8 +144,8 @@
|
||||
<td>{$ sub.interval $}</td>
|
||||
<td>{$ sub.current_period_end | date:'medium' $}</td>
|
||||
<td>
|
||||
<button class="btn btn-sm btn-primary" ng-click="BuyNowBackupP(sub.plan_name, sub.amount, sub.amount * 12, sub.interval === 'month' ? 1 : 12)">
|
||||
Manage Subscription
|
||||
<button class="btn btn-sm btn-primary" ng-click="ReconfigureSubscription(sub)">
|
||||
Configure for This Server
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -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'),
|
||||
|
||||
Reference in New Issue
Block a user