Integration of Github webhooks.

This commit is contained in:
usmannasir
2018-07-26 23:13:02 +05:00
parent 0af4297788
commit bdec3badf6
6 changed files with 128 additions and 23 deletions

View File

@@ -9,13 +9,13 @@ from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging
import subprocess import subprocess
import shlex import shlex
from vhost import vhost from vhost import vhost
from loginSystem.models import Administrator
from websiteFunctions.models import ChildDomains, Websites from websiteFunctions.models import ChildDomains, Websites
import randomPassword import randomPassword
from mysqlUtilities import mysqlUtilities from mysqlUtilities import mysqlUtilities
from databases.models import Databases from databases.models import Databases
from installUtilities import installUtilities from installUtilities import installUtilities
import shutil import shutil
from plogical.mailUtilities import mailUtilities
class ApplicationInstaller(multi.Thread): class ApplicationInstaller(multi.Thread):
@@ -33,11 +33,12 @@ class ApplicationInstaller(multi.Thread):
self.installJoomla() self.installJoomla()
elif self.installApp == 'git': elif self.installApp == 'git':
self.setupGit() self.setupGit()
elif self.installApp == 'pull':
self.gitPull()
except BaseException, msg: except BaseException, msg:
logging.writeToFile( str(msg) + ' [ApplicationInstaller.run]') logging.writeToFile( str(msg) + ' [ApplicationInstaller.run]')
def installWPCLI(self): def installWPCLI(self):
try: try:
command = 'sudo wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar' 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: except BaseException, msg:
logging.writeToFile( str(msg) + ' [ApplicationInstaller.installWPCLI]') logging.writeToFile( str(msg) + ' [ApplicationInstaller.installWPCLI]')
def installGit(self, tempStatusPath): def installGit(self):
try: try:
command = 'sudo yum -y install http://repo.iotti.biz/CentOS/7/noarch/lux-release-7-1.noarch.rpm' 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: except BaseException, msg:
logging.writeToFile( str(msg) + ' [ApplicationInstaller.installGit]') logging.writeToFile( str(msg) + ' [ApplicationInstaller.installGit]')
def installWordPress(self): def installWordPress(self):
try: try:
@@ -305,7 +305,6 @@ class ApplicationInstaller(multi.Thread):
statusFile.close() statusFile.close()
return 0 return 0
def setupGit(self): def setupGit(self):
try: try:
admin = self.extraArgs['admin'] admin = self.extraArgs['admin']
@@ -325,30 +324,30 @@ class ApplicationInstaller(multi.Thread):
### Check git ### Check git
try: try:
command = 'sudo /usr/local/bin/git --help' command = 'sudo git --help'
res = subprocess.call(shlex.split(command)) res = subprocess.call(shlex.split(command))
if res == 1: if res == 1:
statusFile = open(tempStatusPath, 'w') statusFile = open(tempStatusPath, 'w')
statusFile.writelines('Installing GIT..,0') statusFile.writelines('Installing GIT..,0')
statusFile.close() statusFile.close()
self.installGit(tempStatusPath) self.installGit()
statusFile = open(tempStatusPath, 'w') statusFile = open(tempStatusPath, 'w')
statusFile.writelines('GIT successfully installed,40') statusFile.writelines('GIT successfully installed,20')
statusFile.close() statusFile.close()
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
statusFile = open(tempStatusPath, 'w') statusFile = open(tempStatusPath, 'w')
statusFile.writelines('Installing GIT..,0') statusFile.writelines('Installing GIT..,0')
statusFile.close() statusFile.close()
self.installGit(tempStatusPath) self.installGit()
statusFile = open(tempStatusPath, 'w') statusFile = open(tempStatusPath, 'w')
statusFile.writelines('GIT successfully installed.,40') statusFile.writelines('GIT successfully installed.,20')
statusFile.close() statusFile.close()
## Open Status File ## Open Status File
statusFile = open(tempStatusPath, 'w') statusFile = open(tempStatusPath, 'w')
statusFile.writelines('Setting up directories..,40') statusFile.writelines('Setting up directories..,20')
statusFile.close() statusFile.close()
try: try:
@@ -396,7 +395,7 @@ class ApplicationInstaller(multi.Thread):
pass pass
else: else:
statusFile = open(tempStatusPath, 'w') 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() statusFile.close()
return 0 return 0
elif len(dirFiles) == 0: elif len(dirFiles) == 0:
@@ -404,7 +403,7 @@ class ApplicationInstaller(multi.Thread):
else: else:
statusFile = open(tempStatusPath, 'w') statusFile = open(tempStatusPath, 'w')
statusFile.writelines( 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() statusFile.close()
return 0 return 0
@@ -416,13 +415,13 @@ class ApplicationInstaller(multi.Thread):
try: 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 '--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: except subprocess.CalledProcessError, msg:
statusFile = open(tempStatusPath, 'w') 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() statusFile.close()
return 0 return 0
@@ -435,18 +434,50 @@ class ApplicationInstaller(multi.Thread):
vhost.addRewriteRules(domainName) vhost.addRewriteRules(domainName)
installUtilities.reStartLiteSpeed() installUtilities.reStartLiteSpeed()
mailUtilities.checkHome()
gitPath = '/home/cyberpanel/' + domainName + '.git'
writeToFile = open(gitPath, 'w')
writeToFile.write(username + ':' + reponame)
writeToFile.close()
statusFile = open(tempStatusPath, 'w') statusFile = open(tempStatusPath, 'w')
statusFile.writelines("Successfully Installed. [200]") statusFile.writelines("GIT Repository successfully attached. [200]")
statusFile.close() statusFile.close()
return 0 return 0
except BaseException, msg: except BaseException, msg:
os.remove('/home/cyberpanel/' + domainName + '.git')
statusFile = open(tempStatusPath, 'w') statusFile = open(tempStatusPath, 'w')
statusFile.writelines(str(msg) + " [404]") statusFile.writelines(str(msg) + " [404]")
statusFile.close() statusFile.close()
return 0 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): def installJoomla(self):

View File

@@ -4245,6 +4245,8 @@ app.controller('setupGit', function($scope, $http, $timeout) {
$scope.gitLoading = true; $scope.gitLoading = true;
$scope.githubBranch = 'master'; $scope.githubBranch = 'master';
$scope.installProg = true; $scope.installProg = true;
$scope.goBackDisable = true;
var statusFile; var statusFile;
var domain = $("#domainNamePage").text(); var domain = $("#domainNamePage").text();
@@ -4306,6 +4308,7 @@ app.controller('setupGit', function($scope, $http, $timeout) {
$("#installProgress").css("width", "0%"); $("#installProgress").css("width", "0%");
$scope.installPercentage = "0"; $scope.installPercentage = "0";
$scope.goBackDisable = false;
} }
@@ -4326,6 +4329,7 @@ app.controller('setupGit', function($scope, $http, $timeout) {
$scope.canNotFetch = true; $scope.canNotFetch = true;
$scope.couldNotConnect = false; $scope.couldNotConnect = false;
$scope.goBackDisable = false;
} }
@@ -4343,7 +4347,7 @@ app.controller('setupGit', function($scope, $http, $timeout) {
$scope.gitLoading = false; $scope.gitLoading = false;
$scope.installProg = false; $scope.installProg = false;
$scope.currentStatus = "Starting installation.."; $scope.currentStatus = "Attaching GIT..";
url = "/websites/setupGitRepo"; url = "/websites/setupGitRepo";
@@ -4380,6 +4384,7 @@ app.controller('setupGit', function($scope, $http, $timeout) {
$scope.gitLoading = true; $scope.gitLoading = true;
$scope.errorMessage = response.data.error_message; $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%");
};
}); });

View File

@@ -4245,6 +4245,8 @@ app.controller('setupGit', function($scope, $http, $timeout) {
$scope.gitLoading = true; $scope.gitLoading = true;
$scope.githubBranch = 'master'; $scope.githubBranch = 'master';
$scope.installProg = true; $scope.installProg = true;
$scope.goBackDisable = true;
var statusFile; var statusFile;
var domain = $("#domainNamePage").text(); var domain = $("#domainNamePage").text();
@@ -4282,7 +4284,7 @@ app.controller('setupGit', function($scope, $http, $timeout) {
$scope.installationSuccessfull = false; $scope.installationSuccessfull = false;
$scope.couldNotConnect = true; $scope.couldNotConnect = true;
$scope.gitLoading = true; $scope.gitLoading = true;
$scope.goBackDisable = false; $scope.goBackDisable = true;
$scope.installationURL = domain; $scope.installationURL = domain;
@@ -4306,6 +4308,7 @@ app.controller('setupGit', function($scope, $http, $timeout) {
$("#installProgress").css("width", "0%"); $("#installProgress").css("width", "0%");
$scope.installPercentage = "0"; $scope.installPercentage = "0";
$scope.goBackDisable = false;
} }
@@ -4326,6 +4329,7 @@ app.controller('setupGit', function($scope, $http, $timeout) {
$scope.canNotFetch = true; $scope.canNotFetch = true;
$scope.couldNotConnect = false; $scope.couldNotConnect = false;
$scope.goBackDisable = false;
} }
@@ -4343,7 +4347,7 @@ app.controller('setupGit', function($scope, $http, $timeout) {
$scope.gitLoading = false; $scope.gitLoading = false;
$scope.installProg = false; $scope.installProg = false;
$scope.currentStatus = "Starting installation.."; $scope.currentStatus = "Attaching GIT..";
url = "/websites/setupGitRepo"; url = "/websites/setupGitRepo";
@@ -4380,6 +4384,7 @@ app.controller('setupGit', function($scope, $http, $timeout) {
$scope.gitLoading = true; $scope.gitLoading = true;
$scope.errorMessage = response.data.error_message; $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%");
};
}); });

View File

@@ -97,11 +97,11 @@
</div> </div>
<div ng-hide="installationFailed" class="alert alert-danger"> <div ng-hide="installationFailed" class="alert alert-danger">
<p>{% trans "Installation failed. Error message:" %} {$ errorMessage $}</p> <p>{% trans "Error message:" %} {$ errorMessage $}</p>
</div> </div>
<div ng-hide="installationSuccessfull" class="alert alert-success"> <div ng-hide="installationSuccessfull" class="alert alert-success">
<p>{% trans "Installation successful. Visit:" %} {$ installationURL $}</p> <p>{% trans "GIT Successfully attached. Visit:" %} {$ installationURL $}</p>
</div> </div>
@@ -114,6 +114,14 @@
</div> </div>
<div ng-hide="installationProgress" class="form-group">
<label class="col-sm-3 control-label"></label>
<div class="col-sm-4">
<button type="button" ng-disabled="goBackDisable" ng-click="goBack()" class="btn btn-primary btn-lg btn-block">{% trans "Go Back" %}</button>
</div>
</div>
</form> </form>
</div> </div>

View File

@@ -93,5 +93,8 @@ urlpatterns = [
url(r'^(?P<domain>([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?)/setupGit$', views.setupGit, name='setupGit'), url(r'^(?P<domain>([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?)/setupGit$', views.setupGit, name='setupGit'),
url(r'^setupGitRepo$', views.setupGitRepo, name='setupGitRepo'), url(r'^setupGitRepo$', views.setupGitRepo, name='setupGitRepo'),
url(r'^(?P<domain>([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?)/gitNotify$', views.gitNotify, name='gitNotify'),
] ]

View File

@@ -2560,4 +2560,31 @@ def setupGitRepo(request):
except KeyError, msg: except KeyError, msg:
status = {"installStatus":0,"error":str(msg)} status = {"installStatus":0,"error":str(msg)}
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[installWordpress]") logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[installWordpress]")
return HttpResponse("Not Logged in as admin") 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)