From 027e2d79fc2616790f4c26fdce0c3a0162ceec2b Mon Sep 17 00:00:00 2001 From: usmannasir <01-134132-158@student.bahria.edu.pk> Date: Thu, 26 Jul 2018 23:13:02 +0500 Subject: [PATCH] Integration of Github webhooks. --- plogical/applicationInstaller.py | 65 ++++++++++++++----- static/websiteFunctions/websiteFunctions.js | 20 +++++- .../websiteFunctions/websiteFunctions.js | 22 ++++++- .../templates/websiteFunctions/setupGit.html | 12 +++- websiteFunctions/urls.py | 3 + websiteFunctions/views.py | 29 ++++++++- 6 files changed, 128 insertions(+), 23 deletions(-) diff --git a/plogical/applicationInstaller.py b/plogical/applicationInstaller.py index 4e0ea33ff..a56136cc9 100644 --- a/plogical/applicationInstaller.py +++ b/plogical/applicationInstaller.py @@ -9,13 +9,13 @@ from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging import subprocess import shlex from vhost import vhost -from loginSystem.models import Administrator from websiteFunctions.models import ChildDomains, Websites import randomPassword from mysqlUtilities import mysqlUtilities from databases.models import Databases from installUtilities import installUtilities import shutil +from plogical.mailUtilities import mailUtilities class ApplicationInstaller(multi.Thread): @@ -33,11 +33,12 @@ class ApplicationInstaller(multi.Thread): self.installJoomla() elif self.installApp == 'git': self.setupGit() + elif self.installApp == 'pull': + self.gitPull() except BaseException, msg: logging.writeToFile( str(msg) + ' [ApplicationInstaller.run]') - def installWPCLI(self): try: command = 'sudo wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar' @@ -52,7 +53,7 @@ class ApplicationInstaller(multi.Thread): except BaseException, msg: logging.writeToFile( str(msg) + ' [ApplicationInstaller.installWPCLI]') - def installGit(self, tempStatusPath): + def installGit(self): try: command = 'sudo yum -y install http://repo.iotti.biz/CentOS/7/noarch/lux-release-7-1.noarch.rpm' @@ -64,7 +65,6 @@ class ApplicationInstaller(multi.Thread): except BaseException, msg: logging.writeToFile( str(msg) + ' [ApplicationInstaller.installGit]') - def installWordPress(self): try: @@ -305,7 +305,6 @@ class ApplicationInstaller(multi.Thread): statusFile.close() return 0 - def setupGit(self): try: admin = self.extraArgs['admin'] @@ -325,30 +324,30 @@ class ApplicationInstaller(multi.Thread): ### Check git try: - command = 'sudo /usr/local/bin/git --help' + command = 'sudo git --help' res = subprocess.call(shlex.split(command)) if res == 1: statusFile = open(tempStatusPath, 'w') statusFile.writelines('Installing GIT..,0') statusFile.close() - self.installGit(tempStatusPath) + self.installGit() statusFile = open(tempStatusPath, 'w') - statusFile.writelines('GIT successfully installed,40') + statusFile.writelines('GIT successfully installed,20') statusFile.close() except subprocess.CalledProcessError: statusFile = open(tempStatusPath, 'w') statusFile.writelines('Installing GIT..,0') statusFile.close() - self.installGit(tempStatusPath) + self.installGit() statusFile = open(tempStatusPath, 'w') - statusFile.writelines('GIT successfully installed.,40') + statusFile.writelines('GIT successfully installed.,20') statusFile.close() ## Open Status File statusFile = open(tempStatusPath, 'w') - statusFile.writelines('Setting up directories..,40') + statusFile.writelines('Setting up directories..,20') statusFile.close() try: @@ -396,7 +395,7 @@ class ApplicationInstaller(multi.Thread): pass else: statusFile = open(tempStatusPath, 'w') - statusFile.writelines("Target directory should be empty before installation, otherwise data loss could occur." + " [404]") + statusFile.writelines("Target directory should be empty before attaching GIT, otherwise data loss could occur." + " [404]") statusFile.close() return 0 elif len(dirFiles) == 0: @@ -404,7 +403,7 @@ class ApplicationInstaller(multi.Thread): else: statusFile = open(tempStatusPath, 'w') statusFile.writelines( - "Target directory should be empty before installation, otherwise data loss could occur." + " [404]") + "Target directory should be empty before attaching GIT, otherwise data loss could occur." + " [404]") statusFile.close() return 0 @@ -416,13 +415,13 @@ class ApplicationInstaller(multi.Thread): try: - command = 'sudo GIT_SSH_COMMAND="ssh -i /root/.ssh/cyberpanel -o StrictHostKeyChecking=no" /usr/local/bin/git clone ' \ + command = 'sudo GIT_SSH_COMMAND="ssh -i /root/.ssh/cyberpanel -o StrictHostKeyChecking=no" git clone ' \ '--depth 1 --no-single-branch git@github.com:' + username + '/' + reponame + '.git -b ' + branch + ' ' + finalPath - subprocess.call(shlex.split(command)) + result = subprocess.check_output(shlex.split(command)) except subprocess.CalledProcessError, msg: statusFile = open(tempStatusPath, 'w') - statusFile.writelines('Failed to clone repository. [404]') + statusFile.writelines('Failed to clone repository, make sure you deployed your key to repository. [404]') statusFile.close() return 0 @@ -435,18 +434,50 @@ class ApplicationInstaller(multi.Thread): vhost.addRewriteRules(domainName) installUtilities.reStartLiteSpeed() + mailUtilities.checkHome() + + gitPath = '/home/cyberpanel/' + domainName + '.git' + writeToFile = open(gitPath, 'w') + writeToFile.write(username + ':' + reponame) + writeToFile.close() + statusFile = open(tempStatusPath, 'w') - statusFile.writelines("Successfully Installed. [200]") + statusFile.writelines("GIT Repository successfully attached. [200]") statusFile.close() return 0 except BaseException, msg: + + os.remove('/home/cyberpanel/' + domainName + '.git') + statusFile = open(tempStatusPath, 'w') statusFile.writelines(str(msg) + " [404]") statusFile.close() return 0 + def gitPull(self): + try: + domain = self.extraArgs['domain'] + + command = 'sudo GIT_SSH_COMMAND="ssh -i /root/.ssh/cyberpanel -o StrictHostKeyChecking=no" git -C /home/' + domain + '/public_html/ pull' + subprocess.check_output(shlex.split(command)) + + website = Websites.objects.get(domain=domain) + externalApp = website.externalApp + + ## + + command = "sudo chown -R " + externalApp + ":" + externalApp + " " + '/home/' + domain + '/public_html/' + cmd = shlex.split(command) + subprocess.call(cmd) + + return 0 + + + except BaseException, msg: + logging.writeToFile(str(msg)+ " [ApplicationInstaller.gitPull]") + return 0 def installJoomla(self): diff --git a/static/websiteFunctions/websiteFunctions.js b/static/websiteFunctions/websiteFunctions.js index 44702490b..58eb5d8d7 100644 --- a/static/websiteFunctions/websiteFunctions.js +++ b/static/websiteFunctions/websiteFunctions.js @@ -4245,6 +4245,8 @@ app.controller('setupGit', function($scope, $http, $timeout) { $scope.gitLoading = true; $scope.githubBranch = 'master'; $scope.installProg = true; + $scope.goBackDisable = true; + var statusFile; var domain = $("#domainNamePage").text(); @@ -4306,6 +4308,7 @@ app.controller('setupGit', function($scope, $http, $timeout) { $("#installProgress").css("width", "0%"); $scope.installPercentage = "0"; + $scope.goBackDisable = false; } @@ -4326,6 +4329,7 @@ app.controller('setupGit', function($scope, $http, $timeout) { $scope.canNotFetch = true; $scope.couldNotConnect = false; + $scope.goBackDisable = false; } @@ -4343,7 +4347,7 @@ app.controller('setupGit', function($scope, $http, $timeout) { $scope.gitLoading = false; $scope.installProg = false; - $scope.currentStatus = "Starting installation.."; + $scope.currentStatus = "Attaching GIT.."; url = "/websites/setupGitRepo"; @@ -4380,6 +4384,7 @@ app.controller('setupGit', function($scope, $http, $timeout) { $scope.gitLoading = true; $scope.errorMessage = response.data.error_message; + $scope.goBackDisable = false; } @@ -4394,4 +4399,17 @@ app.controller('setupGit', function($scope, $http, $timeout) { }; + $scope.goBack = function () { + $scope.installationDetailsForm = false; + $scope.installationProgress = true; + $scope.installProg = true; + $scope.installationFailed = true; + $scope.installationSuccessfull = true; + $scope.couldNotConnect = true; + $scope.gitLoading = true; + $scope.goBackDisable = true; + $("#installProgress").css("width", "0%"); + }; + + }); \ No newline at end of file diff --git a/websiteFunctions/static/websiteFunctions/websiteFunctions.js b/websiteFunctions/static/websiteFunctions/websiteFunctions.js index 44702490b..f571456f0 100644 --- a/websiteFunctions/static/websiteFunctions/websiteFunctions.js +++ b/websiteFunctions/static/websiteFunctions/websiteFunctions.js @@ -4245,6 +4245,8 @@ app.controller('setupGit', function($scope, $http, $timeout) { $scope.gitLoading = true; $scope.githubBranch = 'master'; $scope.installProg = true; + $scope.goBackDisable = true; + var statusFile; var domain = $("#domainNamePage").text(); @@ -4282,7 +4284,7 @@ app.controller('setupGit', function($scope, $http, $timeout) { $scope.installationSuccessfull = false; $scope.couldNotConnect = true; $scope.gitLoading = true; - $scope.goBackDisable = false; + $scope.goBackDisable = true; $scope.installationURL = domain; @@ -4306,6 +4308,7 @@ app.controller('setupGit', function($scope, $http, $timeout) { $("#installProgress").css("width", "0%"); $scope.installPercentage = "0"; + $scope.goBackDisable = false; } @@ -4326,6 +4329,7 @@ app.controller('setupGit', function($scope, $http, $timeout) { $scope.canNotFetch = true; $scope.couldNotConnect = false; + $scope.goBackDisable = false; } @@ -4343,7 +4347,7 @@ app.controller('setupGit', function($scope, $http, $timeout) { $scope.gitLoading = false; $scope.installProg = false; - $scope.currentStatus = "Starting installation.."; + $scope.currentStatus = "Attaching GIT.."; url = "/websites/setupGitRepo"; @@ -4380,6 +4384,7 @@ app.controller('setupGit', function($scope, $http, $timeout) { $scope.gitLoading = true; $scope.errorMessage = response.data.error_message; + $scope.goBackDisable = false; } @@ -4394,4 +4399,17 @@ app.controller('setupGit', function($scope, $http, $timeout) { }; + $scope.goBack = function () { + $scope.installationDetailsForm = false; + $scope.installationProgress = true; + $scope.installProg = true; + $scope.installationFailed = true; + $scope.installationSuccessfull = true; + $scope.couldNotConnect = true; + $scope.gitLoading = true; + $scope.goBackDisable = true; + $("#installProgress").css("width", "0%"); + }; + + }); \ No newline at end of file diff --git a/websiteFunctions/templates/websiteFunctions/setupGit.html b/websiteFunctions/templates/websiteFunctions/setupGit.html index aeca2bddf..0b30fe638 100644 --- a/websiteFunctions/templates/websiteFunctions/setupGit.html +++ b/websiteFunctions/templates/websiteFunctions/setupGit.html @@ -97,11 +97,11 @@
-

{% trans "Installation failed. Error message:" %} {$ errorMessage $}

+

{% trans "Error message:" %} {$ errorMessage $}

-

{% trans "Installation successful. Visit:" %} {$ installationURL $}

+

{% trans "GIT Successfully attached. Visit:" %} {$ installationURL $}

@@ -114,6 +114,14 @@ +
+ +
+ +
+
+ + diff --git a/websiteFunctions/urls.py b/websiteFunctions/urls.py index 8224482ab..88b0bb1f8 100644 --- a/websiteFunctions/urls.py +++ b/websiteFunctions/urls.py @@ -93,5 +93,8 @@ urlpatterns = [ url(r'^(?P([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?)/setupGit$', views.setupGit, name='setupGit'), url(r'^setupGitRepo$', views.setupGitRepo, name='setupGitRepo'), + url(r'^(?P([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?)/gitNotify$', views.gitNotify, name='gitNotify'), + + ] \ No newline at end of file diff --git a/websiteFunctions/views.py b/websiteFunctions/views.py index 41af79784..0a43cfee5 100644 --- a/websiteFunctions/views.py +++ b/websiteFunctions/views.py @@ -2560,4 +2560,31 @@ def setupGitRepo(request): except KeyError, msg: status = {"installStatus":0,"error":str(msg)} logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[installWordpress]") - return HttpResponse("Not Logged in as admin") \ No newline at end of file + return HttpResponse("Not Logged in as admin") + + +def gitNotify(request, domain): + try: + if request.method == 'POST': + try: + + extraArgs = {} + extraArgs['domain'] = domain + + background = ApplicationInstaller('pull', extraArgs) + background.start() + + data_ret = {'pulled': 1, 'error_message': 'None'} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + + except BaseException, msg: + data_ret = {'pulled': 0, 'error_message': str(msg)} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) + + + except KeyError, msg: + data_ret = {"pulled":0,"error":str(msg)} + json_data = json.dumps(data_ret) + return HttpResponse(json_data) \ No newline at end of file