From e7ee66177f3508b5859fdbad16f1e03110db3a86 Mon Sep 17 00:00:00 2001 From: Usman Nasir Date: Mon, 30 Mar 2020 17:14:33 +0500 Subject: [PATCH] refactor code in git manager --- plogical/IncScheduler.py | 79 ++++++-------- websiteFunctions/website.py | 202 +++++++++++++++++++++--------------- 2 files changed, 148 insertions(+), 133 deletions(-) diff --git a/plogical/IncScheduler.py b/plogical/IncScheduler.py index b08f4c9ab..ba410c125 100644 --- a/plogical/IncScheduler.py +++ b/plogical/IncScheduler.py @@ -103,63 +103,48 @@ class IncScheduler(): for file in os.listdir(finalPathInside): - if file == website: - continue + try: - if file.find('public_html') > -1: - finalPath = '/home/%s/public_html' % (website) + ## finalPathConf = '%s/%s' % (finalPathInside, file) - elif file.find('vmail') > -1: - finalPath = '/home/vmail/%s' % (website) - finalPathConf = '%s/%s' % (finalPathInside, file) - else: - sqlCheck = 1 + gitConf = json.loads(open(finalPathConf, 'r').read()) - for childs in web.childdomains_set.all(): - if childs.path.find(file) > -1: - finalPath = childs.path - finalPathConf = '%s/%s' % (finalPathInside, file) - sqlCheck = 0 + data = {} + data['domain'] = gitConf['domain'] + data['folder'] = gitConf['folder'] + data['commitMessage'] = 'Auto commit by CyberPanel %s cron on %s' % (type, time.strftime('%m-%d-%Y_%H-%M-%S')) - if sqlCheck: - finalPath = '/var/lib/mysql/%s' % (file) - finalPathConf = '%s/%s' % (finalPathInside, file) + if gitConf['autoCommit'] == type: - gitConf = json.loads(open(finalPathConf, 'r').read()) - data = {} - data['domain'] = website - data['folder'] = finalPath - data['commitMessage'] = 'Auto commit by CyberPanel %s cron on %s' % (type, time.strftime('%m-%d-%Y_%H-%M-%S')) + wm = WebsiteManager() + resp = wm.commitChanges(1, data) + resp = json.loads(resp.content) - if gitConf['autoCommit'] == type: + if resp['status'] == 1: + message = 'Folder: %s, Status: %s' % (gitConf['folder'], resp['commandStatus']) + finalText = '%s\n%s' % (finalText, message) + GitLogs(owner=web, type='INFO', message=message).save() + else: + message = 'Folder: %s, Status: %s' % (gitConf['folder'], resp['commandStatus']) + finalText = '%s\n%s' % (finalText, message) + GitLogs(owner=web, type='ERROR', message=message).save() - wm = WebsiteManager() - resp = wm.commitChanges(1, data) - resp = json.loads(resp.content) + if gitConf['autoPush'] == type: + wm = WebsiteManager() + resp = wm.gitPush(1, data) + resp = json.loads(resp.content) - if resp['status'] == 1: - message = 'Folder: %s, Status: %s' % (finalPath, resp['commandStatus']) - finalText = '%s\n%s' % (finalText, message) - GitLogs(owner=web, type='INFO', message=message).save() - else: - message = 'Folder: %s, Status: %s' % (finalPath, resp['commandStatus']) - finalText = '%s\n%s' % (finalText, message) - GitLogs(owner=web, type='ERROR', message=message).save() - - if gitConf['autoPush'] == type: - - wm = WebsiteManager() - resp = wm.gitPush(1, data) - resp = json.loads(resp.content) - - if resp['status'] == 1: - GitLogs(owner=web, type='INFO', message=resp['commandStatus']).save() - finalText = '%s\n%s' % (finalText, resp['commandStatus']) - else: - GitLogs(owner=web, type='ERROR', message=resp['commandStatus']).save() - finalText = '%s\n%s' % (finalText, resp['commandStatus']) + if resp['status'] == 1: + GitLogs(owner=web, type='INFO', message=resp['commandStatus']).save() + finalText = '%s\n%s' % (finalText, resp['commandStatus']) + else: + GitLogs(owner=web, type='ERROR', message=resp['commandStatus']).save() + finalText = '%s\n%s' % (finalText, resp['commandStatus']) + except BaseException as msg: + message = 'File: %s, Status: %s' % (file, str(msg)) + finalText = '%s\n%s' % (finalText, message) message = '[%s Cron] Finished checking for %s on %s.' % (type, website, time.strftime("%m.%d.%Y_%H-%M-%S")) diff --git a/websiteFunctions/website.py b/websiteFunctions/website.py index db05af9c8..5084c9464 100755 --- a/websiteFunctions/website.py +++ b/websiteFunctions/website.py @@ -2914,6 +2914,8 @@ StrictHostKeyChecking no try: + ### + domainPath = '/home/%s/public_html' % (self.domain) vhRoot = '/home/%s' % (self.domain) vmailPath = '/home/vmail/%s' % (self.domain) @@ -2951,6 +2953,53 @@ StrictHostKeyChecking no if self.folder == website.path: self.home = 1 + ### Fetch git configurations + + self.confCheck = 1 + + gitConfFolder = '/home/cyberpanel/git' + gitConFile = '%s/%s' % (gitConfFolder, self.masterDomain) + + if not os.path.exists(gitConfFolder): + os.mkdir(gitConfFolder) + + if not os.path.exists(gitConFile): + os.mkdir(gitConFile) + + if os.path.exists(gitConFile): + files = os.listdir(gitConFile) + + if len(files) >= 1: + for file in files: + self.finalFile = '%s/%s' % (gitConFile, file) + + gitConf = json.loads(open(self.finalFile, 'r').read()) + + if gitConf['folder'] == self.folder: + + self.autoCommitCurrent = gitConf['autoCommit'] + self.autoPushCurrent = gitConf['autoPush'] + self.emailLogsCurrent = gitConf['emailLogs'] + try: + self.commands = gitConf['commands'] + except: + self.commands = "Add Commands to run after every commit, separate commands using comma." + + try: + self.webhookCommandCurrent = gitConf['webhookCommand'] + except: + self.webhookCommandCurrent = "False" + + self.confCheck = 0 + break + + if self.confCheck: + self.autoCommitCurrent = 'Never' + self.autoPushCurrent = 'Never' + self.emailLogsCurrent = 'False' + self.webhookCommandCurrent = 'False' + self.commands = "Add Commands to run after every commit, separate commands using comma." + ## if self.folder == domainPath: @@ -3083,49 +3132,14 @@ StrictHostKeyChecking no if totalCommits.find('fatal') > -1: totalCommits = '0' - ## Fetch Configurations - - gitConfFolder = '/home/cyberpanel/git' - gitConFile = '%s/%s' % (gitConfFolder, self.masterDomain) - - if not os.path.exists(gitConfFolder): - os.mkdir(gitConfFolder) - - if not os.path.exists(gitConFile): - os.mkdir(gitConFile) - - finalFile = '%s/%s' % (gitConFile, self.folder.split('/')[-1]) - - if os.path.exists(finalFile): - gitConf = json.loads(open(finalFile, 'r').read()) - - autoCommitCurrent = gitConf['autoCommit'] - autoPushCurrent = gitConf['autoPush'] - emailLogsCurrent = gitConf['emailLogs'] - try: - commands = gitConf['commands'] - except: - commands = "Add Commands to run after every commit, separate commands using comma." - - try: - webhookCommandCurrent = gitConf['webhookCommand'] - except: - webhookCommandCurrent = "False" - else: - autoCommitCurrent = 'Never' - autoPushCurrent = 'Never' - emailLogsCurrent = 'False' - webhookCommandCurrent = 'False' - commands = "Add Commands to run after every commit, separate commands using comma." - ## webHookURL = 'https://%s:8090/websites/%s/webhook' % (ACLManager.fetchIP(), self.domain) data_ret = {'status': 1, 'repo': 1, 'finalBranches': branches, 'deploymentKey': deploymentKey, 'remote': remote, 'remoteResult': remoteResult, 'totalCommits': totalCommits, 'home': self.home, - 'webHookURL': webHookURL, 'autoCommitCurrent': autoCommitCurrent, - 'autoPushCurrent':autoPushCurrent, 'emailLogsCurrent': emailLogsCurrent, 'commands': commands, "webhookCommandCurrent": webhookCommandCurrent} + 'webHookURL': webHookURL, 'autoCommitCurrent': self.autoCommitCurrent, + 'autoPushCurrent':self.autoPushCurrent, 'emailLogsCurrent': self.emailLogsCurrent, 'commands': self.commands, "webhookCommandCurrent": self.webhookCommandCurrent} json_data = json.dumps(data_ret) return HttpResponse(json_data) @@ -3384,17 +3398,12 @@ StrictHostKeyChecking no if commandStatus.find('nothing to commit') == -1: try: - gitConfFolder = '/home/cyberpanel/git/%s' % (self.masterDomain) - finalFile = '%s/%s' % (gitConfFolder, self.folder.split('/')[-1]) - - gitConf = json.loads(open(finalFile, 'r').read()) - - if gitConf['commands'] != 'NONE': + if self.commands != 'NONE': GitLogs(owner=self.masterWebsite, type='INFO', message='Running commands after successful git commit..').save() - if gitConf['commands'].find(',') > -1: - commands = gitConf['commands'].split(',') + if self.commands.find(',') > -1: + commands = self.commands.split(',') for command in commands: GitLogs(owner=self.masterWebsite, type='INFO', @@ -3405,9 +3414,9 @@ StrictHostKeyChecking no message='Result: %s' % (result)).save() else: GitLogs(owner=self.masterWebsite, type='INFO', - message='Running: %s' % (gitConf['commands'])).save() + message='Running: %s' % (self.commands)).save() - result = ProcessUtilities.outputExecutioner(gitConf['commands'], self.externalAppLocal) + result = ProcessUtilities.outputExecutioner(self.commands, self.externalAppLocal) GitLogs(owner=self.masterWebsite, type='INFO', message='Result: %s' % (result)).save() @@ -3642,6 +3651,13 @@ StrictHostKeyChecking no command = 'rm -rf %s/.git' % (self.folder) ProcessUtilities.executioner(command) + gitConfFolder = '/home/cyberpanel/git' + gitConFile = '%s/%s' % (gitConfFolder, self.masterDomain) + finalFile = '%s/%s' % (gitConFile, self.folder.split('/')[-1]) + + command = 'rm -rf %s' % (finalFile) + ProcessUtilities.outputExecutioner(command) + data_ret = {'status': 1} json_data = json.dumps(data_ret) return HttpResponse(json_data) @@ -3926,6 +3942,8 @@ StrictHostKeyChecking no dic = {} + dic['domain'] = self.domain + dic['autoCommit'] = data['autoCommit'] try: @@ -3948,6 +3966,7 @@ StrictHostKeyChecking no except: dic['webhookCommand'] = False + dic['folder'] = self.folder if ACLManager.checkOwnership(self.domain, admin, currentACL) == 1: pass @@ -3961,18 +3980,18 @@ StrictHostKeyChecking no ## - gitConfFolder = '/home/cyberpanel/git' - gitConFile = '%s/%s' % (gitConfFolder, self.masterDomain) + if self.confCheck == 1: + gitConfFolder = '/home/cyberpanel/git' + gitConFile = '%s/%s' % (gitConfFolder, self.masterDomain) + self.finalFile = '%s/%s' % (gitConFile, str(randint(1000, 9999))) - if not os.path.exists(gitConfFolder): - os.mkdir(gitConfFolder) + if not os.path.exists(gitConfFolder): + os.mkdir(gitConfFolder) - if not os.path.exists(gitConFile): - os.mkdir(gitConFile) + if not os.path.exists(gitConFile): + os.mkdir(gitConFile) - finalFile = '%s/%s' % (gitConFile, self.folder.split('/')[-1]) - - writeToFile = open(finalFile, 'w') + writeToFile = open(self.finalFile, 'w') writeToFile.write(json.dumps(dic)) writeToFile.close() @@ -4055,10 +4074,12 @@ StrictHostKeyChecking no web = Websites.objects.get(domain=self.domain) externalApp = web.externalApp self.folder = '/home/%s/public_html' % (domain) + self.masterDomain = domain except: web = ChildDomains.objects.get(domain=self.domain) externalApp = web.master.externalApp self.folder = web.path + self.masterDomain = web.master.domain ## Check if remote exists @@ -4069,49 +4090,58 @@ StrictHostKeyChecking no message = '[Webhook Fired] Status: %s.' % (commandStatus) GitLogs(owner=web, type='INFO', message=message).save() - try: - web = Websites.objects.get(domain=self.domain) + ### Fetch git configurations - gitConfFolder = '/home/cyberpanel/git/%s' % (web.domain) - finalFile = '%s/%s' % (gitConfFolder, self.folder.split('/')[-1]) - gitConf = json.loads(open(finalFile, 'r').read()) + found = 0 - except: - child = ChildDomains.objects.get(domain=self.domain) - web = child.master + gitConfFolder = '/home/cyberpanel/git' + gitConFile = '%s/%s' % (gitConfFolder, self.masterDomain) - gitConfFolder = '/home/cyberpanel/git/%s' % (web.domain) - finalFile = '%s/%s' % (gitConfFolder, self.folder.split('/')[-1]) - gitConf = json.loads(open(finalFile, 'r').read()) + if not os.path.exists(gitConfFolder): + os.mkdir(gitConfFolder) - try: - if gitConf['webhookCommand']: - if gitConf['commands'] != 'NONE': + if not os.path.exists(gitConFile): + os.mkdir(gitConFile) - GitLogs(owner=web, type='INFO', message='Running commands after successful git commit..').save() + if os.path.exists(gitConFile): + files = os.listdir(gitConFile) - if gitConf['commands'].find(',') > -1: - commands = gitConf['commands'].split(',') + if len(files) >= 1: + for file in files: + finalFile = '%s/%s' % (gitConFile, file) + gitConf = json.loads(open(finalFile, 'r').read()) + if gitConf['folder'] == self.folder: + found = 1 + break + if found: + try: + if gitConf['webhookCommand']: + if gitConf['commands'] != 'NONE': - for command in commands: + GitLogs(owner=web, type='INFO', message='Running commands after successful git commit..').save() + + if gitConf['commands'].find(',') > -1: + commands = gitConf['commands'].split(',') + + for command in commands: + GitLogs(owner=web, type='INFO', + message='Running: %s' % (command)).save() + + result = ProcessUtilities.outputExecutioner(command, web.externalApp) + GitLogs(owner=web, type='INFO', + message='Result: %s' % (result)).save() + else: GitLogs(owner=web, type='INFO', - message='Running: %s' % (command)).save() + message='Running: %s' % (gitConf['commands'])).save() - result = ProcessUtilities.outputExecutioner(command, web.externalApp) + result = ProcessUtilities.outputExecutioner(gitConf['commands'], web.externalApp) GitLogs(owner=web, type='INFO', message='Result: %s' % (result)).save() - else: - GitLogs(owner=web, type='INFO', - message='Running: %s' % (gitConf['commands'])).save() - result = ProcessUtilities.outputExecutioner(gitConf['commands'], web.externalApp) GitLogs(owner=web, type='INFO', - message='Result: %s' % (result)).save() - - GitLogs(owner=web, type='INFO', - message='Finished running commands.').save() - except: - pass + message='Finished running commands.').save() + except: + pass data_ret = {'status': 1, 'commandStatus': commandStatus} json_data = json.dumps(data_ret)