add git manager for child domains

This commit is contained in:
Usman Nasir
2020-03-28 19:23:57 +05:00
parent 360dd42f14
commit 40f1de4215
4 changed files with 142 additions and 93 deletions

View File

@@ -109,8 +109,18 @@ class IncScheduler():
finalPath = '/home/vmail/%s' % (website) finalPath = '/home/vmail/%s' % (website)
finalPathConf = '%s/%s' % (finalPathInside, file) finalPathConf = '%s/%s' % (finalPathInside, file)
else: else:
finalPath = '/var/lib/mysql/%s' % (file)
finalPathConf = '%s/%s' % (finalPathInside, file) sqlCheck = 1
for childs in web.childdomains_set.all():
if childs.path.find(file) > -1:
finalPath = childs.path
finalPathConf = '%s/%s' % (finalPathInside, file)
sqlCheck = 0
if sqlCheck:
finalPath = '/var/lib/mysql/%s' % (file)
finalPathConf = '%s/%s' % (finalPathInside, file)
gitConf = json.loads(open(finalPathConf, 'r').read()) gitConf = json.loads(open(finalPathConf, 'r').read())
data = {} data = {}

View File

@@ -6546,12 +6546,18 @@ app.controller('manageGIT', function ($scope, $http, $timeout, $window) {
var currentComit; var currentComit;
var fetchFileCheck = 0; var fetchFileCheck = 0;
var initial = 1;
$scope.fetchFiles = function (commit) { $scope.fetchFiles = function (commit) {
currentComit = commit; currentComit = commit;
$scope.cyberpanelLoading = false; $scope.cyberpanelLoading = false;
fetchFileCheck = 1;
if (initial === 1){
initial = 0;
}else {
fetchFileCheck = 1;
}
url = "/websites/fetchFiles"; url = "/websites/fetchFiles";
@@ -6607,6 +6613,7 @@ app.controller('manageGIT', function ($scope, $http, $timeout, $window) {
$scope.fetchChangesInFile = function () { $scope.fetchChangesInFile = function () {
$scope.fileStatus = true; $scope.fileStatus = true;
if(fetchFileCheck === 1){ if(fetchFileCheck === 1){
fetchFileCheck = 0; fetchFileCheck = 0;
return 0; return 0;

View File

@@ -699,6 +699,17 @@
</a> </a>
</div>--> </div>-->
<div class="col-md-3 panel-body">
<a href="/websites/{{ childDomain }}/manageGIT" target="_blank"
title="{% trans 'Attach Git with this website!' %}">
<img src="{% static 'images/icons/git-logo.png' %}" width="65" class="mr-10">
</a>
<a href="/websites/{{ childDomain }}/manageGIT" target="_blank"
title="{% trans 'Attach Git with this website!' %}">
<span class="h4">{% trans "Git" %}</span>
</a>
</div>
<div class="col-md-3 panel-body"> <div class="col-md-3 panel-body">

View File

@@ -2893,9 +2893,10 @@ StrictHostKeyChecking no
except: except:
self.childWebsite = ChildDomains.objects.get(domain=self.domain) self.childWebsite = ChildDomains.objects.get(domain=self.domain)
folders = ['/home/%s/public_html' % (self.childWebsite.master.domain), '/home/%s' % (self.childWebsite.master.domain),
'/home/vmail/%s' % (self.childWebsite.master.domain), self.childWebsite.path] folders = [self.childWebsite.path]
databases = self.childWebsite.master.databases_set.all() databases = self.childWebsite.master.databases_set.all()
@@ -2905,49 +2906,97 @@ StrictHostKeyChecking no
return render(request, 'websiteFunctions/manageGIT.html', return render(request, 'websiteFunctions/manageGIT.html',
{'domainName': self.domain, 'folders': folders}) {'domainName': self.domain, 'folders': folders})
except BaseException as msg: except BaseException as msg:
return HttpResponse(str(msg)) return HttpResponse(str(msg))
def folderCheck(self): def folderCheck(self):
domainPath = '/home/%s/public_html' % (self.domain) domainPath = '/home/%s/public_html' % (self.domain)
vhRoot = '/home/%s' % (self.domain) vhRoot = '/home/%s' % (self.domain)
vmailPath = '/home/vmail/%s' % (self.domain) vmailPath = '/home/vmail/%s' % (self.domain)
##
try: try:
website = Websites.objects.get(domain=self.domain) website = Websites.objects.get(domain=self.domain)
self.masterWebsite = website
self.masterDomain = website.domain
externalApp = website.externalApp externalApp = website.externalApp
self.externalAppLocal = website.externalApp
self.adminEmail = website.adminEmail
self.firstName = website.admin.firstName
self.lastName = website.admin.lastName
self.home = 0
if self.folder == '/home/%s/public_html' % (self.domain):
self.home = 1
except: except:
website = ChildDomains.objects.get(domain=self.domain) website = ChildDomains.objects.get(domain=self.domain)
self.masterWebsite = website.master
self.masterDomain = website.master.domain
externalApp = website.master.externalApp externalApp = website.master.externalApp
self.externalAppLocal = website.master.externalApp
self.adminEmail = website.master.adminEmail
self.firstName = website.master.admin.firstName
self.lastName = website.master.admin.lastName
self.home = 0
if self.folder == website.path:
self.home = 1
##
if self.folder == domainPath: if self.folder == domainPath:
self.externalApp = externalApp self.externalApp = externalApp
return 1 return 1
##
if self.folder == vhRoot: if self.folder == vhRoot:
self.externalApp = externalApp self.externalApp = externalApp
return 1 return 1
##
try: try:
childDomain = ChildDomains.objects.get(domain=self.domain) childDomain = ChildDomains.objects.get(domain=self.domain)
if self.folder == childDomain.path: if self.folder == childDomain.path:
self.externalApp = externalApp
return 1 return 1
except: except:
pass pass
##
if self.folder == vmailPath: if self.folder == vmailPath:
self.externalApp = 'vmail' self.externalApp = 'vmail'
return 1 return 1
for database in website.databases_set.all(): try:
self.externalApp = 'mysql'
basePath = '/var/lib/mysql/' for database in website.databases_set.all():
dbPath = '%s%s' % (basePath, database.dbName) self.externalApp = 'mysql'
basePath = '/var/lib/mysql/'
dbPath = '%s%s' % (basePath, database.dbName)
if self.folder == dbPath:
return 1
except:
for database in website.master.databases_set.all():
self.externalApp = 'mysql'
basePath = '/var/lib/mysql/'
dbPath = '%s%s' % (basePath, database.dbName)
if self.folder == dbPath:
return 1
if self.folder == dbPath:
return 1
return 0 return 0
@@ -2970,43 +3019,23 @@ StrictHostKeyChecking no
else: else:
return ACLManager.loadErrorJson() return ACLManager.loadErrorJson()
try:
website = Websites.objects.get(domain=self.domain)
self.externalAppLocal = website.externalApp
## Check if home
home = 0
if self.folder == '/home/%s/public_html' % (self.domain):
home = 1
except:
website = ChildDomains.objects.get(domain=self.domain)
self.externalAppLocal = website.master.externalApp
## Check if home
home = 0
if self.folder == website.path:
home = 1
gitPath = '%s/.git' % (self.folder) gitPath = '%s/.git' % (self.folder)
command = 'ls -la %s' % (gitPath) command = 'ls -la %s' % (gitPath)
if ProcessUtilities.outputExecutioner(command).find('No such file or directory') > -1: if ProcessUtilities.outputExecutioner(command).find('No such file or directory') > -1:
command = 'cat /home/%s/.ssh/%s.pub' % (self.domain, self.externalAppLocal) command = 'cat /home/%s/.ssh/%s.pub' % (self.masterDomain, self.externalAppLocal)
deploymentKey = ProcessUtilities.outputExecutioner(command) deploymentKey = ProcessUtilities.outputExecutioner(command)
if deploymentKey.find('No such file or directory') > -1: if deploymentKey.find('No such file or directory') > -1:
command = "ssh-keygen -f /home/%s/.ssh/%s -t rsa -N ''" % (self.domain, self.externalAppLocal) command = "ssh-keygen -f /home/%s/.ssh/%s -t rsa -N ''" % (self.masterDomain, self.externalAppLocal)
ProcessUtilities.executioner(command, self.externalAppLocal) ProcessUtilities.executioner(command, self.externalAppLocal)
command = 'cat /home/%s/.ssh/%s.pub' % (self.domain, self.externalAppLocal) command = 'cat /home/%s/.ssh/%s.pub' % (self.masterDomain, self.externalAppLocal)
deploymentKey = ProcessUtilities.outputExecutioner(command) deploymentKey = ProcessUtilities.outputExecutioner(command)
data_ret = {'status': 1, 'repo': 0, 'deploymentKey': deploymentKey, 'home': home} data_ret = {'status': 1, 'repo': 0, 'deploymentKey': deploymentKey, 'home': self.home}
json_data = json.dumps(data_ret) json_data = json.dumps(data_ret)
return HttpResponse(json_data) return HttpResponse(json_data)
else: else:
@@ -3022,10 +3051,10 @@ StrictHostKeyChecking no
deploymentKey = ProcessUtilities.outputExecutioner(command) deploymentKey = ProcessUtilities.outputExecutioner(command)
if deploymentKey.find('No such file or directory') > -1: if deploymentKey.find('No such file or directory') > -1:
command = "ssh-keygen -f /home/%s/.ssh/%s -t rsa -N ''" % (self.domain, self.externalAppLocal) command = "ssh-keygen -f /home/%s/.ssh/%s -t rsa -N ''" % (self.masterDomain, self.externalAppLocal)
ProcessUtilities.executioner(command, self.externalAppLocal) ProcessUtilities.executioner(command, self.externalAppLocal)
command = 'cat /home/%s/.ssh/%s.pub' % (self.domain, self.externalAppLocal) command = 'cat /home/%s/.ssh/%s.pub' % (self.masterDomain, self.externalAppLocal)
deploymentKey = ProcessUtilities.outputExecutioner(command) deploymentKey = ProcessUtilities.outputExecutioner(command)
## Find Remote if any ## Find Remote if any
@@ -3049,7 +3078,7 @@ StrictHostKeyChecking no
## Fetch Configurations ## Fetch Configurations
gitConfFolder = '/home/cyberpanel/git' gitConfFolder = '/home/cyberpanel/git'
gitConFile = '%s/%s' % (gitConfFolder, self.domain) gitConFile = '%s/%s' % (gitConfFolder, self.masterDomain)
if not os.path.exists(gitConfFolder): if not os.path.exists(gitConfFolder):
os.mkdir(gitConfFolder) os.mkdir(gitConfFolder)
@@ -3086,7 +3115,7 @@ StrictHostKeyChecking no
webHookURL = 'https://%s:8090/websites/%s/webhook' % (ACLManager.fetchIP(), self.domain) webHookURL = 'https://%s:8090/websites/%s/webhook' % (ACLManager.fetchIP(), self.domain)
data_ret = {'status': 1, 'repo': 1, 'finalBranches': branches, 'deploymentKey': deploymentKey, data_ret = {'status': 1, 'repo': 1, 'finalBranches': branches, 'deploymentKey': deploymentKey,
'remote': remote, 'remoteResult': remoteResult, 'totalCommits': totalCommits, 'home': home, 'remote': remote, 'remoteResult': remoteResult, 'totalCommits': totalCommits, 'home': self.home,
'webHookURL': webHookURL, 'autoCommitCurrent': autoCommitCurrent, 'webHookURL': webHookURL, 'autoCommitCurrent': autoCommitCurrent,
'autoPushCurrent':autoPushCurrent, 'emailLogsCurrent': emailLogsCurrent, 'commands': commands, "webhookCommandCurrent": webhookCommandCurrent} 'autoPushCurrent':autoPushCurrent, 'emailLogsCurrent': emailLogsCurrent, 'commands': commands, "webhookCommandCurrent": webhookCommandCurrent}
@@ -3117,16 +3146,6 @@ StrictHostKeyChecking no
else: else:
return ACLManager.loadErrorJson() return ACLManager.loadErrorJson()
try:
website = Websites.objects.get(domain=self.domain)
self.adminEmail = website.adminEmail
self.firstName = website.admin.firstName
self.lastName = website.admin.lastName
except:
website = ChildDomains.objects.get(domain=self.domain)
self.adminEmail = website.master.adminEmail
self.firstName = website.master.admin.firstName
self.lastName = website.master.admin.lastName
command = 'git -C %s init' % (self.folder) command = 'git -C %s init' % (self.folder)
result = ProcessUtilities.outputExecutioner(command) result = ProcessUtilities.outputExecutioner(command)
@@ -3184,14 +3203,7 @@ StrictHostKeyChecking no
### set default ssh key ### set default ssh key
try: command = 'git -C %s config --local core.sshCommand "ssh -i /home/%s/.ssh/%s -o "StrictHostKeyChecking=no""' % (self.folder, self.masterDomain, self.externalAppLocal)
website = Websites.objects.get(domain=self.domain)
self.externalAppLocal = website.adminEmail
except:
website = ChildDomains.objects.get(domain=self.domain)
self.externalAppLocal = website.master.adminEmail
command = 'git -C %s config --local core.sshCommand "ssh -i /home/%s/.ssh/%s -o "StrictHostKeyChecking=no""' % (self.folder, self.domain, self.externalAppLocal)
ProcessUtilities.executioner(command) ProcessUtilities.executioner(command)
## Check if remote exists ## Check if remote exists
@@ -3307,7 +3319,7 @@ StrictHostKeyChecking no
## ##
command = 'git -C %s checkout -b "%s"' % (self.folder, self.newBranchName) command = 'git -C %s checkout -b "%s"' % (self.folder, self.newBranchName)
commandStatus = ProcessUtilities.outputExecutioner(command ) commandStatus = ProcessUtilities.outputExecutioner(command)
if commandStatus.find(self.newBranchName) > -1: if commandStatus.find(self.newBranchName) > -1:
data_ret = {'status': 1, 'commandStatus': commandStatus} data_ret = {'status': 1, 'commandStatus': commandStatus}
@@ -3359,37 +3371,36 @@ StrictHostKeyChecking no
commandStatus = ProcessUtilities.outputExecutioner(command) commandStatus = ProcessUtilities.outputExecutioner(command)
if commandStatus.find('nothing to commit') == -1: if commandStatus.find('nothing to commit') == -1:
gitConfFolder = '/home/cyberpanel/git/%s' % (self.domain) gitConfFolder = '/home/cyberpanel/git/%s' % (self.masterDomain)
finalFile = '%s/%s' % (gitConfFolder, self.folder.split('/')[-1]) finalFile = '%s/%s' % (gitConfFolder, self.folder.split('/')[-1])
gitConf = json.loads(open(finalFile, 'r').read()) gitConf = json.loads(open(finalFile, 'r').read())
web = Websites.objects.get(domain=self.domain)
try: try:
if gitConf['commands'] != 'NONE': if gitConf['commands'] != 'NONE':
GitLogs(owner=web, type='INFO', message='Running commands after successful git commit..').save() GitLogs(owner=self.masterWebsite, type='INFO', message='Running commands after successful git commit..').save()
if gitConf['commands'].find(',') > -1: if gitConf['commands'].find(',') > -1:
commands = gitConf['commands'].split(',') commands = gitConf['commands'].split(',')
for command in commands: for command in commands:
GitLogs(owner=web, type='INFO', GitLogs(owner=self.masterWebsite, type='INFO',
message='Running: %s' % (command)).save() message='Running: %s' % (command)).save()
result = ProcessUtilities.outputExecutioner(command, web.externalApp) result = ProcessUtilities.outputExecutioner(command, self.externalAppLocal)
GitLogs(owner=web, type='INFO', GitLogs(owner=self.masterWebsite, type='INFO',
message='Result: %s' % (result)).save() message='Result: %s' % (result)).save()
else: else:
GitLogs(owner=web, type='INFO', GitLogs(owner=self.masterWebsite, type='INFO',
message='Running: %s' % (gitConf['commands'])).save() message='Running: %s' % (gitConf['commands'])).save()
result = ProcessUtilities.outputExecutioner(gitConf['commands'], web.externalApp) result = ProcessUtilities.outputExecutioner(gitConf['commands'], self.externalAppLocal)
GitLogs(owner=web, type='INFO', GitLogs(owner=self.masterWebsite, type='INFO',
message='Result: %s' % (result)).save() message='Result: %s' % (result)).save()
GitLogs(owner=web, type='INFO', GitLogs(owner=self.masterWebsite, type='INFO',
message='Finished running commands.').save() message='Finished running commands.').save()
except: except:
pass pass
@@ -3429,10 +3440,9 @@ StrictHostKeyChecking no
### set default ssh key ### set default ssh key
externalApp = Websites.objects.get(domain=self.domain).externalApp
command = 'git -C %s config --local core.sshCommand "ssh -i /home/%s/.ssh/%s -o "StrictHostKeyChecking=no""' % ( command = 'git -C %s config --local core.sshCommand "ssh -i /home/%s/.ssh/%s -o "StrictHostKeyChecking=no""' % (
self.folder, self.domain, externalApp) self.folder, self.masterDomain, self.externalAppLocal)
ProcessUtilities.executioner(command ) ProcessUtilities.executioner(command )
## Check if remote exists ## Check if remote exists
@@ -3476,10 +3486,8 @@ StrictHostKeyChecking no
### set default ssh key ### set default ssh key
externalApp = Websites.objects.get(domain=self.domain).externalApp
command = 'git -C %s config --local core.sshCommand "ssh -i /home/%s/.ssh/%s -o "StrictHostKeyChecking=no""' % ( command = 'git -C %s config --local core.sshCommand "ssh -i /home/%s/.ssh/%s -o "StrictHostKeyChecking=no""' % (
self.folder, self.domain, externalApp) self.folder, self.masterDomain, self.externalAppLocal)
ProcessUtilities.executioner(command) ProcessUtilities.executioner(command)
## ##
@@ -3541,6 +3549,8 @@ StrictHostKeyChecking no
else: else:
return ACLManager.loadErrorJson() return ACLManager.loadErrorJson()
logging.CyberCPLogFileWriter.writeToFile('hello world 2')
## Security check ## Security check
if validators.domain(self.gitHost) and ACLManager.validateInput(self.gitUsername) and ACLManager.validateInput(self.gitReponame): if validators.domain(self.gitHost) and ACLManager.validateInput(self.gitUsername) and ACLManager.validateInput(self.gitReponame):
@@ -3557,9 +3567,7 @@ StrictHostKeyChecking no
## Set defauly key ## Set defauly key
website = Websites.objects.get(domain=self.domain) command = 'git config --global core.sshCommand "ssh -i /home/%s/.ssh/%s -o "StrictHostKeyChecking=no""' % (self.masterDomain, self.externalAppLocal)
command = 'git config --global core.sshCommand "ssh -i /home/%s/.ssh/%s -o "StrictHostKeyChecking=no""' % (self.domain, website.externalApp)
ProcessUtilities.executioner(command) ProcessUtilities.executioner(command)
## ##
@@ -3572,12 +3580,12 @@ StrictHostKeyChecking no
from filemanager.filemanager import FileManager from filemanager.filemanager import FileManager
fm = FileManager(None, None) fm = FileManager(None, None)
fm.fixPermissions(self.domain) fm.fixPermissions(self.masterDomain)
command = 'git -C %s config --local user.email %s' % (self.folder, website.adminEmail) command = 'git -C %s config --local user.email %s' % (self.folder, self.adminEmail)
ProcessUtilities.executioner(command) ProcessUtilities.executioner(command)
command = 'git -C %s config --local user.name "%s %s"' % (self.folder, website.admin.firstName, website.admin.lastName) command = 'git -C %s config --local user.name "%s %s"' % (self.folder, self.firstName, self.lastName)
ProcessUtilities.executioner(command) ProcessUtilities.executioner(command)
data_ret = {'status': 1, 'commandStatus': commandStatus} data_ret = {'status': 1, 'commandStatus': commandStatus}
@@ -3589,7 +3597,7 @@ StrictHostKeyChecking no
from filemanager.filemanager import FileManager from filemanager.filemanager import FileManager
fm = FileManager(None, None) fm = FileManager(None, None)
fm.fixPermissions(self.domain) fm.fixPermissions(self.masterDomain)
data_ret = {'status': 0, 'error_message': 'Failed to clone.', 'commandStatus': commandStatus} data_ret = {'status': 0, 'error_message': 'Failed to clone.', 'commandStatus': commandStatus}
json_data = json.dumps(data_ret) json_data = json.dumps(data_ret)
@@ -3621,7 +3629,7 @@ StrictHostKeyChecking no
return ACLManager.loadErrorJson() return ACLManager.loadErrorJson()
command = 'rm -rf %s/.git' % (self.folder) command = 'rm -rf %s/.git' % (self.folder)
ProcessUtilities.executioner(command ) ProcessUtilities.executioner(command)
data_ret = {'status': 1} data_ret = {'status': 1}
json_data = json.dumps(data_ret) json_data = json.dumps(data_ret)
@@ -3653,7 +3661,7 @@ StrictHostKeyChecking no
return ACLManager.loadErrorJson() return ACLManager.loadErrorJson()
command = 'cat %s/.gitignore' % (self.folder) command = 'cat %s/.gitignore' % (self.folder)
gitIgnoreContent = ProcessUtilities.outputExecutioner(command, self.externalApp) gitIgnoreContent = ProcessUtilities.outputExecutioner(command, self.externalAppLocal)
if gitIgnoreContent.find('No such file or directory') > -1: if gitIgnoreContent.find('No such file or directory') > -1:
gitIgnoreContent = 'File is currently empty.' gitIgnoreContent = 'File is currently empty.'
@@ -3706,7 +3714,7 @@ StrictHostKeyChecking no
from filemanager.filemanager import FileManager from filemanager.filemanager import FileManager
fm = FileManager(None, None) fm = FileManager(None, None)
fm.fixPermissions(self.domain) fm.fixPermissions(self.masterDomain)
data_ret = {'status': 1} data_ret = {'status': 1}
json_data = json.dumps(data_ret) json_data = json.dumps(data_ret)
@@ -3939,7 +3947,7 @@ StrictHostKeyChecking no
## ##
gitConfFolder = '/home/cyberpanel/git' gitConfFolder = '/home/cyberpanel/git'
gitConFile = '%s/%s' % (gitConfFolder, self.domain) gitConFile = '%s/%s' % (gitConfFolder, self.masterDomain)
if not os.path.exists(gitConfFolder): if not os.path.exists(gitConfFolder):
os.mkdir(gitConfFolder) os.mkdir(gitConfFolder)
@@ -4001,8 +4009,7 @@ StrictHostKeyChecking no
else: else:
return ACLManager.loadErrorJson() return ACLManager.loadErrorJson()
website = Websites.objects.get(domain=self.domain) logs = self.masterWebsite.gitlogs_set.all().order_by('-id')
logs = website.gitlogs_set.all().order_by('-id')
from s3Backups.s3Backups import S3Backups from s3Backups.s3Backups import S3Backups
@@ -4026,12 +4033,17 @@ StrictHostKeyChecking no
try: try:
self.domain = domain self.domain = domain
self.folder = '/home/%s/public_html' % (domain)
### set default ssh key ### set default ssh key
web = Websites.objects.get(domain=self.domain) try:
externalApp = web.externalApp web = Websites.objects.get(domain=self.domain)
externalApp = web.externalApp
self.folder = '/home/%s/public_html' % (domain)
except:
web = ChildDomains.objects.get(domain=self.domain)
externalApp = web.master.externalApp
self.folder = web.path
## Check if remote exists ## Check if remote exists
@@ -4042,11 +4054,20 @@ StrictHostKeyChecking no
message = '[Webhook Fired] Status: %s.' % (commandStatus) message = '[Webhook Fired] Status: %s.' % (commandStatus)
GitLogs(owner=web, type='INFO', message=message).save() GitLogs(owner=web, type='INFO', message=message).save()
gitConfFolder = '/home/cyberpanel/git/%s' % (self.domain) try:
finalFile = '%s/%s' % (gitConfFolder, self.folder.split('/')[-1]) web = Websites.objects.get(domain=self.domain)
gitConf = json.loads(open(finalFile, 'r').read()) gitConfFolder = '/home/cyberpanel/git/%s' % (web.domain)
web = Websites.objects.get(domain=self.domain) finalFile = '%s/%s' % (gitConfFolder, self.folder.split('/')[-1])
gitConf = json.loads(open(finalFile, 'r').read())
except:
child = ChildDomains.objects.get(domain=self.domain)
web = child.master
gitConfFolder = '/home/cyberpanel/git/%s' % (web.domain)
finalFile = '%s/%s' % (gitConfFolder, self.folder.split('/')[-1])
gitConf = json.loads(open(finalFile, 'r').read())
try: try:
if gitConf['webhookCommand']: if gitConf['webhookCommand']: