From ca6bd24efa1e9d3f6fdf271707069eb01ba719c5 Mon Sep 17 00:00:00 2001 From: Usman Nasir Date: Sat, 13 Jun 2020 14:25:29 +0500 Subject: [PATCH] initial google drive setup --- CLManager/static/CLManager/CLManager.js | 1 - backup/backupManager.py | 41 +++- backup/static/backup/backup.js | 54 +++++ backup/templates/backup/googleDrive.html | 211 ++++++++---------- backup/urls.py | 3 +- backup/views.py | 8 + .../templates/baseTemplate/index.html | 2 +- websiteFunctions/models.py | 14 ++ 8 files changed, 204 insertions(+), 130 deletions(-) diff --git a/CLManager/static/CLManager/CLManager.js b/CLManager/static/CLManager/CLManager.js index ae17f696c..d2c52ef22 100644 --- a/CLManager/static/CLManager/CLManager.js +++ b/CLManager/static/CLManager/CLManager.js @@ -489,7 +489,6 @@ app.controller('listCloudLinuxPackages', function ($scope, $http) { }); - app.controller('websiteContainerLimitCL', function ($scope, $http, $timeout, $window) { diff --git a/backup/backupManager.py b/backup/backupManager.py index e75e275e5..52d731ee7 100755 --- a/backup/backupManager.py +++ b/backup/backupManager.py @@ -9,7 +9,7 @@ django.setup() import json from plogical.acl import ACLManager import plogical.CyberCPLogFileWriter as logging -from websiteFunctions.models import Websites, Backups, dest, backupSchedules, BackupJob, BackupJobLogs +from websiteFunctions.models import Websites, Backups, dest, backupSchedules, BackupJob, BackupJobLogs, GDrive, GDriveSites, GDriveJobLogs from plogical.virtualHostUtilities import virtualHostUtilities import subprocess import shlex @@ -52,14 +52,49 @@ class BackupManager: try: currentACL = ACLManager.loadedACL(userID) + admin = Administrator.objects.get(pk=userID) + if ACLManager.currentContextPermission(currentACL, 'addDeleteDestinations') == 0: return ACLManager.loadError() - websitesName = ACLManager.findAllSites(currentACL, userID) - return render(request, 'backup/googleDrive.html', {'websiteList': websitesName}) + gDriveAcctsList = [] + + gDriveAccts = admin.gdrive_set.all() + + for items in gDriveAccts: + gDriveAcctsList.append(items.name) + + return render(request, 'backup/googleDrive.html', {'accounts': gDriveAcctsList}) except BaseException as msg: return HttpResponse(str(msg)) + def gDriveSetup(self, userID = None, request = None): + try: + currentACL = ACLManager.loadedACL(userID) + admin = Administrator.objects.get(pk=userID) + + if ACLManager.currentContextPermission(currentACL, 'addDeleteDestinations') == 0: + return ACLManager.loadError() + + gDriveData = {} + gDriveData['name'] = request.GET.get('n') + gDriveData['token'] = request.GET.get('t') + gDriveData['refresh_token'] = request.GET.get('r') + gDriveData['token_uri'] = request.GET.get('to') + gDriveData['client_id'] = request.GET.get('c') + gDriveData['client_secret'] = request.GET.get('cl') + gDriveData['scopes'] = request.GET.get('s') + + gD = GDrive(owner=admin, name=gDriveData['name'],auth=json.dumps(gDriveData)) + gD.save() + + final_json = json.dumps({'status': 1, 'message': 'Successfully saved.'}) + return HttpResponse(final_json) + except BaseException as msg: + final_dic = {'status': 0, 'fetchStatus': 0, 'error_message': str(msg)} + final_json = json.dumps(final_dic) + return HttpResponse(final_json) + def restoreSite(self, request = None, userID = None, data = None): try: currentACL = ACLManager.loadedACL(userID) diff --git a/backup/static/backup/backup.js b/backup/static/backup/backup.js index e233a8636..b8336dd87 100755 --- a/backup/static/backup/backup.js +++ b/backup/static/backup/backup.js @@ -1534,6 +1534,7 @@ app.controller('remoteBackupControl', function ($scope, $http, $timeout) { ///** Backup site ends **/// //*** Remote Backup site ****// + app.controller('backupLogsScheduled', function ($scope, $http, $timeout) { $scope.cyberpanelLoading = true; @@ -1595,3 +1596,56 @@ app.controller('backupLogsScheduled', function ($scope, $http, $timeout) { }); ///** Backup site ends **/// + +app.controller('googleDrive', function ($scope, $http) { + + $scope.cyberPanelLoading = true; + + $scope.setupAccount = function(){ + window.open("https://platform.cyberpanel.net/gDrive?name=" + $scope.accountName + '&server=' + window.location.href + 'Setup'); + }; + + $scope.fetchPackageas = function () { + $scope.cyberPanelLoading = false; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + + var data = {}; + + + dataurl = "/CloudLinux/fetchPackages"; + + $http.post(dataurl, data, config).then(ListInitialData, cantLoadInitialData); + + + function ListInitialData(response) { + $scope.cyberPanelLoading = true; + if (response.data.status === 1) { + $scope.packages = JSON.parse(response.data.data); + } else { + new PNotify({ + title: 'Operation Failed!', + text: response.data.error_message, + type: 'error' + }); + } + } + + function cantLoadInitialData(response) { + $scope.cyberPanelLoading = true; + new PNotify({ + title: 'Operation Failed!', + text: 'Could not connect to server, please refresh this page', + type: 'error' + }); + } + + + }; + + +}); diff --git a/backup/templates/backup/googleDrive.html b/backup/templates/backup/googleDrive.html index 5ab7e5ad9..fb3577434 100755 --- a/backup/templates/backup/googleDrive.html +++ b/backup/templates/backup/googleDrive.html @@ -1,136 +1,99 @@ {% extends "baseTemplate/index.html" %} {% load i18n %} -{% block title %}{% trans "Set up Back up Destinations" %}{% endblock %} +{% block title %}{% trans "Google Drive Backups - CyberPanel" %}{% endblock %} {% block content %} -{% load static %} - - -{% get_current_language as LANGUAGE_CODE %} - - -
-
-

{% trans "Set up Back up Destinations" %} - {% trans "Remote Backups" %}

-

{% trans "On this page you can set up your Back up destinations. (SFTP)" %}

-
- -
-
-

- {% trans "Set up Back up Destinations (SSH port should be 22 on backup server)" %} -

-
- - -
- - -
- -
- -
-
- -
- -
- -
-
- - -
- -
- -
-
- -
- -
- -
-
- -
- -
- - -
-
- - - - - -
- -
-
-

{% trans "Connection to" %} {$ IPAddress $} {% trans "failed. Please delete and re-add. " %} {$ errorMessage $}

-
- -
-

{% trans "Connection to" %} {$ IPAddress $} {% trans "successful." %}

-
- -
-

{% trans "Cannot add destination. Error message:" %} {$ errorMessage $}

-
- -
-

{% trans "Destination Added." %}

-
- -
-

{% trans "Could not connect to server. Please refresh this page." %}

-
-
-
- -
- -
- - - - - - - - - - - - - - - - - - - -
{% trans "ID" %}{% trans "IP" %}{% trans "Check Connection" %}{% trans "Delete" %}
-
-
- - - - -
+ {% load static %} + {% get_current_language as LANGUAGE_CODE %} + +
+
+

{% trans "Set up Google Drive Backups" %} - {% trans "Remote Backups" %} +

+

{% trans "On this page you can set up and manage Google Drive Backups." %}

+ +
+
+

+ {% trans "On this page you can set up and manage Google Drive Backups." %} {% trans "Setup new Account" %} + +

+
+ + +
+ +
+ +
+ +
+
+ +
+ + +
+
+
+ +
-
- - -
{% endblock %} \ No newline at end of file diff --git a/backup/urls.py b/backup/urls.py index 07711116e..866f780fb 100755 --- a/backup/urls.py +++ b/backup/urls.py @@ -6,7 +6,8 @@ urlpatterns = [ url(r'^getCurrentBackups', views.getCurrentBackups, name='getCurrentBackups'), url(r'^backupSite', views.backupSite, name='backupSite'), url(r'^restoreSite', views.restoreSite, name='restoreSite'), - url(r'^gDrive', views.gDrive, name='gDrive'), + url(r'^gDrive$', views.gDrive, name='gDrive'), + url(r'^gDriveSetup$', views.gDriveSetup, name='gDriveSetup'), url(r'^submitBackupCreation', views.submitBackupCreation, name='submitBackupCreation'), diff --git a/backup/views.py b/backup/views.py index 0d6d8ca25..d176e25df 100755 --- a/backup/views.py +++ b/backup/views.py @@ -38,6 +38,14 @@ def gDrive(request): except KeyError: return redirect(loadLoginPage) +def gDriveSetup(request): + try: + userID = request.session['userID'] + wm = BackupManager() + return wm.gDriveSetup(userID, request) + except KeyError: + return redirect(loadLoginPage) + def restoreSite(request): try: userID = request.session['userID'] diff --git a/baseTemplate/templates/baseTemplate/index.html b/baseTemplate/templates/baseTemplate/index.html index c445e2f65..8f04e097f 100755 --- a/baseTemplate/templates/baseTemplate/index.html +++ b/baseTemplate/templates/baseTemplate/index.html @@ -581,7 +581,7 @@ title="{% trans 'Add Destination' %}">{% trans "Add/Delete Destination" %}
  • {% trans "Add/Delete Destination" %} + title="{% trans 'Setup Google Drive' %}">{% trans "Setup Google Drive" %}
  • {% trans "Schedule Back up" %} diff --git a/websiteFunctions/models.py b/websiteFunctions/models.py index ff4a5aea2..80ed1dc4a 100755 --- a/websiteFunctions/models.py +++ b/websiteFunctions/models.py @@ -60,4 +60,18 @@ class BackupJob(models.Model): class BackupJobLogs(models.Model): owner = models.ForeignKey(BackupJob, on_delete=models.CASCADE) status = models.IntegerField() + message = models.TextField() + +class GDrive(models.Model): + owner = models.ForeignKey(Administrator, on_delete=models.CASCADE) + name = models.CharField(max_length=50) + auth = models.TextField(max_length=65532, default='Inactive') + +class GDriveSites(models.Model): + owner = models.ForeignKey(GDrive, on_delete=models.CASCADE) + domain = models.CharField(max_length=200) + +class GDriveJobLogs(models.Model): + owner = models.ForeignKey(GDrive, on_delete=models.CASCADE) + status = models.IntegerField() message = models.TextField() \ No newline at end of file