initial google drive setup

This commit is contained in:
Usman Nasir
2020-06-13 14:25:29 +05:00
parent aa4f39b2dd
commit ca6bd24efa
8 changed files with 204 additions and 130 deletions

View File

@@ -489,7 +489,6 @@ app.controller('listCloudLinuxPackages', function ($scope, $http) {
}); });
app.controller('websiteContainerLimitCL', function ($scope, $http, $timeout, $window) { app.controller('websiteContainerLimitCL', function ($scope, $http, $timeout, $window) {

View File

@@ -9,7 +9,7 @@ django.setup()
import json import json
from plogical.acl import ACLManager from plogical.acl import ACLManager
import plogical.CyberCPLogFileWriter as logging 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 from plogical.virtualHostUtilities import virtualHostUtilities
import subprocess import subprocess
import shlex import shlex
@@ -52,14 +52,49 @@ class BackupManager:
try: try:
currentACL = ACLManager.loadedACL(userID) currentACL = ACLManager.loadedACL(userID)
admin = Administrator.objects.get(pk=userID)
if ACLManager.currentContextPermission(currentACL, 'addDeleteDestinations') == 0: if ACLManager.currentContextPermission(currentACL, 'addDeleteDestinations') == 0:
return ACLManager.loadError() return ACLManager.loadError()
websitesName = ACLManager.findAllSites(currentACL, userID) gDriveAcctsList = []
return render(request, 'backup/googleDrive.html', {'websiteList': websitesName})
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: except BaseException as msg:
return HttpResponse(str(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): def restoreSite(self, request = None, userID = None, data = None):
try: try:
currentACL = ACLManager.loadedACL(userID) currentACL = ACLManager.loadedACL(userID)

View File

@@ -1534,6 +1534,7 @@ app.controller('remoteBackupControl', function ($scope, $http, $timeout) {
///** Backup site ends **/// ///** Backup site ends **///
//*** Remote Backup site ****// //*** Remote Backup site ****//
app.controller('backupLogsScheduled', function ($scope, $http, $timeout) { app.controller('backupLogsScheduled', function ($scope, $http, $timeout) {
$scope.cyberpanelLoading = true; $scope.cyberpanelLoading = true;
@@ -1595,3 +1596,56 @@ app.controller('backupLogsScheduled', function ($scope, $http, $timeout) {
}); });
///** Backup site ends **/// ///** 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'
});
}
};
});

View File

@@ -1,136 +1,99 @@
{% extends "baseTemplate/index.html" %} {% extends "baseTemplate/index.html" %}
{% load i18n %} {% load i18n %}
{% block title %}{% trans "Set up Back up Destinations" %}{% endblock %} {% block title %}{% trans "Google Drive Backups - CyberPanel" %}{% endblock %}
{% block content %} {% block content %}
{% load static %} {% load static %}
{% get_current_language as LANGUAGE_CODE %}
<!-- Current language: {{ LANGUAGE_CODE }} -->
<div class="container">
<div id="page-title">
<h2>{% trans "Set up Back up Destinations" %} - <a target="_blank" href="http://go.cyberpanel.net/remote-backup" style="height: 23px;line-height: 21px;" class="btn btn-border btn-alt border-red btn-link font-red" title=""><span>{% trans "Remote Backups" %}</span></a></h2>
<p>{% trans "On this page you can set up your Back up destinations. (SFTP)" %}</p>
</div>
<div ng-controller="backupDestinations" class="panel">
<div class="panel-body">
<h3 class="title-hero">
{% trans "Set up Back up Destinations (SSH port should be 22 on backup server)" %} <img ng-hide="destinationLoading" src="{% static 'images/loading.gif' %}">
</h3>
<div class="example-box-wrapper">
<form action="/" class="form-horizontal bordered-row">
<div class="form-group">
<label class="col-sm-3 control-label">{% trans "IP Address" %}</label>
<div class="col-sm-6">
<input type="text" class="form-control" ng-model="IPAddress" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">{% trans "User" %}</label>
<div class="col-sm-6">
<input placeholder="{% trans "Backup server SSH User, leave empty for root." %}" type="text" class="form-control" ng-model="user" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">{% trans "Password" %}</label>
<div class="col-sm-6">
<input placeholder="" type="password" class="form-control" ng-model="password" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">{% trans "Port" %}</label>
<div class="col-sm-6">
<input placeholder="{% trans "Backup server SSH Port, leave empty for 22." %}" type="text" class="form-control" ng-model="backupSSHPort" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label"></label>
<div class="col-sm-4">
<button type="button" ng-click="addDestination()" class="btn btn-primary btn-lg btn-block">{% trans "Add Destination" %}</button>
</div>
</div>
<!------ List of Destinations --------------->
<div class="form-group">
<div class="col-sm-12">
<div ng-hide="connectionFailed" class="alert alert-danger">
<p>{% trans "Connection to" %} {$ IPAddress $} {% trans "failed. Please delete and re-add. " %} {$ errorMessage $} </p>
</div>
<div ng-hide="connectionSuccess" class="alert alert-success">
<p>{% trans "Connection to" %} {$ IPAddress $} {% trans "successful." %}</p>
</div>
<div ng-hide="canNotAddDestination" class="alert alert-danger">
<p>{% trans "Cannot add destination. Error message:" %} {$ errorMessage $} </p>
</div>
<div ng-hide="destinationAdded" class="alert alert-success">
<p>{% trans "Destination Added." %}</p>
</div>
<div ng-hide="couldNotConnect" class="alert alert-danger">
<p>{% trans "Could not connect to server. Please refresh this page." %}</p>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<table class="table">
<thead>
<tr>
<th>{% trans "ID" %}</th>
<th>{% trans "IP" %}</th>
<th>{% trans "Check Connection" %}</th>
<th>{% trans "Delete" %}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="record in records track by $index">
<td ng-bind="record.id"></td>
<td ng-bind="record.ip"></td>
<td><button type="button" ng-click="checkConn(record.ip)" class="btn ra-100 btn-purple">{% trans "Check Connection" %}</button></td>
<td ng-click="delDest(record.ip)"><img src="{% static 'images/delete.png' %}"></td>
</tr>
</tbody>
</table>
</div>
</div>
<!------ List of records --------------->
</form>
{% get_current_language as LANGUAGE_CODE %}
<!-- Current language: {{ LANGUAGE_CODE }} -->
<div class="container">
<div id="page-title">
<h2>{% trans "Set up Google Drive Backups" %} - <a target="_blank"
href="http://go.cyberpanel.net/remote-backup"
style="height: 23px;line-height: 21px;"
class="btn btn-border btn-alt border-red btn-link font-red"
title=""><span>{% trans "Remote Backups" %}</span></a>
</h2>
<p>{% trans "On this page you can set up and manage Google Drive Backups." %}</p>
</div> </div>
<div ng-controller="googleDrive" class="panel">
<div class="panel-body">
<h3 class="title-hero">
{% trans "On this page you can set up and manage Google Drive Backups." %} <img
ng-hide="cyberPanelLoading" src="{% static 'images/loading.gif' %}"> <a
class="btn btn-border btn-alt border-blue-alt btn-link font-blue-alt"
href="#"
title="" data-toggle="modal"
data-target="#setupGdrive"><span>{% trans "Setup new Account" %}</span></a>
<div id="setupGdrive" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;
</button>
<h4 class="modal-title">{% trans "Repo Settings" %}</h4>
</div>
<div class="modal-body">
<form name="containerSettingsForm" action="/" class="form-horizontal">
<div ng-hide="installationDetailsForm" class="form-group">
<label class="col-sm-3 control-label">{% trans "Account Name" %}</label>
<div class="col-sm-6">
<input name="accountName" type="text" class="form-control"
ng-model="accountName">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary"
ng-click="setupAccount()">Save <img
ng-hide="cyberPanelLoading"
src="{% static 'images/loading.gif' %}">
</button>
<button type="button" ng-disabled="savingSettings"
class="btn btn-default" data-dismiss="modal">
Close
</button>
</div>
</div>
</div>
</div>
</h3>
<div class="example-box-wrapper">
<form action="/" class="form-horizontal bordered-row">
<div class="form-group">
<label class="col-sm-3 control-label">{% trans "Select Drive Account" %}</label>
<div class="col-sm-6">
<select ng-change="fetchWebsites()" ng-model="selectedAccount" class="form-control">
{% for items in accounts %}
<option>{{ items }}</option>
{% endfor %}
</select>
</div>
</div>
</form>
</div>
</div>
</div>
</div> </div>
</div>
</div>
{% endblock %} {% endblock %}

View File

@@ -6,7 +6,8 @@ urlpatterns = [
url(r'^getCurrentBackups', views.getCurrentBackups, name='getCurrentBackups'), url(r'^getCurrentBackups', views.getCurrentBackups, name='getCurrentBackups'),
url(r'^backupSite', views.backupSite, name='backupSite'), url(r'^backupSite', views.backupSite, name='backupSite'),
url(r'^restoreSite', views.restoreSite, name='restoreSite'), 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'), url(r'^submitBackupCreation', views.submitBackupCreation, name='submitBackupCreation'),

View File

@@ -38,6 +38,14 @@ def gDrive(request):
except KeyError: except KeyError:
return redirect(loadLoginPage) 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): def restoreSite(request):
try: try:
userID = request.session['userID'] userID = request.session['userID']

View File

@@ -581,7 +581,7 @@
title="{% trans 'Add Destination' %}"><span>{% trans "Add/Delete Destination" %}</span></a> title="{% trans 'Add Destination' %}"><span>{% trans "Add/Delete Destination" %}</span></a>
</li> </li>
<li class="addDeleteDestinations"><a href="{% url 'gDrive' %}" <li class="addDeleteDestinations"><a href="{% url 'gDrive' %}"
title="{% trans 'Setup Google Drive' %}"><span>{% trans "Add/Delete Destination" %}</span></a> title="{% trans 'Setup Google Drive' %}"><span>{% trans "Setup Google Drive" %}</span></a>
</li> </li>
<li class="scheDuleBackups"><a href="{% url 'scheduleBackup' %}" <li class="scheDuleBackups"><a href="{% url 'scheduleBackup' %}"
title="{% trans 'Schedule Back up' %}"><span>{% trans "Schedule Back up" %}</span></a> title="{% trans 'Schedule Back up' %}"><span>{% trans "Schedule Back up" %}</span></a>

View File

@@ -60,4 +60,18 @@ class BackupJob(models.Model):
class BackupJobLogs(models.Model): class BackupJobLogs(models.Model):
owner = models.ForeignKey(BackupJob, on_delete=models.CASCADE) owner = models.ForeignKey(BackupJob, on_delete=models.CASCADE)
status = models.IntegerField() 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() message = models.TextField()