2019-12-10 23:04:24 +05:00
|
|
|
#!/usr/local/CyberCP/bin/python
|
2019-08-03 14:53:31 +05:00
|
|
|
import threading as multi
|
|
|
|
|
from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging
|
|
|
|
|
from plogical.virtualHostUtilities import virtualHostUtilities
|
|
|
|
|
from plogical.processUtilities import ProcessUtilities
|
|
|
|
|
from .models import Websites, ChildDomains
|
|
|
|
|
from plogical.applicationInstaller import ApplicationInstaller
|
|
|
|
|
from plogical.mysqlUtilities import mysqlUtilities
|
|
|
|
|
from random import randint
|
|
|
|
|
import os
|
|
|
|
|
|
2019-08-25 21:14:04 +05:00
|
|
|
|
2019-08-03 14:53:31 +05:00
|
|
|
class StagingSetup(multi.Thread):
|
|
|
|
|
|
|
|
|
|
def __init__(self, function, extraArgs):
|
|
|
|
|
multi.Thread.__init__(self)
|
|
|
|
|
self.function = function
|
|
|
|
|
self.extraArgs = extraArgs
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
|
try:
|
|
|
|
|
if self.function == 'startCloning':
|
|
|
|
|
self.startCloning()
|
|
|
|
|
elif self.function == 'startSyncing':
|
|
|
|
|
self.startSyncing()
|
2019-12-10 15:09:10 +05:00
|
|
|
except BaseException as msg:
|
2019-08-25 21:14:04 +05:00
|
|
|
logging.writeToFile(str(msg) + ' [StagingSetup.run]')
|
2019-08-03 14:53:31 +05:00
|
|
|
|
|
|
|
|
def startCloning(self):
|
|
|
|
|
try:
|
|
|
|
|
tempStatusPath = self.extraArgs['tempStatusPath']
|
2019-12-07 18:06:00 +05:00
|
|
|
self.tempStatusPath = tempStatusPath
|
2019-08-03 14:53:31 +05:00
|
|
|
masterDomain = self.extraArgs['masterDomain']
|
|
|
|
|
domain = self.extraArgs['domain']
|
|
|
|
|
admin = self.extraArgs['admin']
|
|
|
|
|
|
|
|
|
|
website = Websites.objects.get(domain=masterDomain)
|
|
|
|
|
|
2020-05-16 04:15:28 +05:00
|
|
|
masterPath = '/home/%s/public_html' % (masterDomain)
|
|
|
|
|
|
|
|
|
|
command = 'chmod 755 %s' % (masterPath)
|
|
|
|
|
ProcessUtilities.executioner(command)
|
|
|
|
|
|
2019-08-03 14:53:31 +05:00
|
|
|
## Creating Child Domain
|
|
|
|
|
|
|
|
|
|
path = "/home/" + masterDomain + "/public_html/" + domain
|
|
|
|
|
|
|
|
|
|
logging.statusWriter(tempStatusPath, 'Creating domain for staging environment..,5')
|
|
|
|
|
phpSelection = 'PHP 7.1'
|
2019-12-10 23:04:24 +05:00
|
|
|
execPath = "/usr/local/CyberCP/bin/python " + virtualHostUtilities.cyberPanel + "/plogical/virtualHostUtilities.py"
|
2019-08-03 14:53:31 +05:00
|
|
|
|
|
|
|
|
execPath = execPath + " createDomain --masterDomain " + masterDomain + " --virtualHostName " + domain + \
|
|
|
|
|
" --phpVersion '" + phpSelection + "' --ssl 0 --dkimCheck 0 --openBasedir 0 --path " + path + ' --websiteOwner ' \
|
|
|
|
|
+ admin.userName + ' --tempStatusPath %s' % (tempStatusPath + '1') + " --apache 0"
|
|
|
|
|
|
|
|
|
|
ProcessUtilities.executioner(execPath)
|
|
|
|
|
|
|
|
|
|
domainCreationStatusPath = tempStatusPath + '1'
|
|
|
|
|
|
|
|
|
|
data = open(domainCreationStatusPath, 'r').read()
|
|
|
|
|
|
|
|
|
|
if data.find('[200]') > -1:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
2020-05-16 04:15:28 +05:00
|
|
|
logging.statusWriter(tempStatusPath, 'Failed to create child-domain for staging environment. [404]')
|
2019-08-03 14:53:31 +05:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
logging.statusWriter(tempStatusPath, 'Domain successfully created..,15')
|
|
|
|
|
|
|
|
|
|
## Copying Data
|
|
|
|
|
|
2020-05-16 04:15:28 +05:00
|
|
|
## Fetch child domain path
|
|
|
|
|
|
|
|
|
|
childDomainPaths = []
|
|
|
|
|
|
|
|
|
|
for childs in website.childdomains_set.all():
|
|
|
|
|
childDomainPaths.append(childs.path)
|
|
|
|
|
|
|
|
|
|
filesAndFolder = os.listdir(masterPath)
|
|
|
|
|
|
|
|
|
|
for items in filesAndFolder:
|
|
|
|
|
completePath = '%s/%s' % (masterPath, items)
|
|
|
|
|
|
|
|
|
|
if completePath in childDomainPaths:
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
command = 'cp -r %s %s/' % (completePath, path)
|
|
|
|
|
ProcessUtilities.executioner(command, website.externalApp)
|
|
|
|
|
|
|
|
|
|
foldersToBeRemoved = ['%s/.git' % (path), '%s/wp-content/backups' % (path), '%s/wp-content/updraft' % (path), '%s/wp-content/cache' % (path), '%s/wp-content/plugins/litespeed-cache' % (path)]
|
|
|
|
|
|
|
|
|
|
for rmv in foldersToBeRemoved:
|
|
|
|
|
command = 'rm -rf %s' % (rmv)
|
|
|
|
|
ProcessUtilities.executioner(command, website.externalApp)
|
2019-08-03 14:53:31 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
logging.statusWriter(tempStatusPath, 'Data copied..,50')
|
|
|
|
|
|
|
|
|
|
## Creating Database
|
|
|
|
|
|
|
|
|
|
logging.statusWriter(tempStatusPath, 'Creating and copying database..,50')
|
|
|
|
|
|
|
|
|
|
dbNameRestore, dbUser, dbPassword = ApplicationInstaller(None, None).dbCreation(tempStatusPath, website)
|
|
|
|
|
|
|
|
|
|
# Create dump of existing database
|
|
|
|
|
|
|
|
|
|
configPath = '%s/wp-config.php' % (masterPath)
|
|
|
|
|
|
2020-05-16 04:15:28 +05:00
|
|
|
command = 'ls -la %s' % (configPath)
|
|
|
|
|
output = ProcessUtilities.outputExecutioner(command)
|
|
|
|
|
|
|
|
|
|
if output.find('No such file or') > -1:
|
2019-08-03 14:53:31 +05:00
|
|
|
logging.statusWriter(tempStatusPath, 'WordPress is not detected. [404]')
|
|
|
|
|
return 0
|
|
|
|
|
|
2020-05-16 04:15:28 +05:00
|
|
|
command = 'cat %s' % (configPath)
|
|
|
|
|
data = ProcessUtilities.outputExecutioner(command).split('\n')
|
2019-08-03 14:53:31 +05:00
|
|
|
|
|
|
|
|
for items in data:
|
|
|
|
|
if items.find('DB_NAME') > -1:
|
2019-09-29 12:55:58 +05:00
|
|
|
try:
|
|
|
|
|
dbName = items.split("'")[3]
|
|
|
|
|
if mysqlUtilities.createDatabaseBackup(dbName, '/home/cyberpanel'):
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
raise BaseException('Failed to create database backup.')
|
|
|
|
|
except:
|
|
|
|
|
dbName = items.split('"')[1]
|
|
|
|
|
if mysqlUtilities.createDatabaseBackup(dbName, '/home/cyberpanel'):
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
raise BaseException('Failed to create database backup.')
|
|
|
|
|
|
2019-08-03 14:53:31 +05:00
|
|
|
databasePath = '%s/%s.sql' % ('/home/cyberpanel', dbName)
|
|
|
|
|
|
|
|
|
|
if not mysqlUtilities.restoreDatabaseBackup(dbNameRestore, '/home/cyberpanel', None, 1, dbName):
|
|
|
|
|
try:
|
|
|
|
|
os.remove(databasePath)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
raise BaseException('Failed to restore database backup.')
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
os.remove(databasePath)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
## Update final config file
|
|
|
|
|
|
|
|
|
|
pathFinalConfig = '%s/wp-config.php' % (path)
|
2020-05-16 04:15:28 +05:00
|
|
|
|
|
|
|
|
command = 'cat %s' % (configPath)
|
|
|
|
|
data = ProcessUtilities.outputExecutioner(command).split('\n')
|
2019-08-03 14:53:31 +05:00
|
|
|
|
|
|
|
|
tmp = "/tmp/" + str(randint(1000, 9999))
|
|
|
|
|
writeToFile = open(tmp, 'w')
|
|
|
|
|
|
|
|
|
|
for items in data:
|
|
|
|
|
if items.find('DB_NAME') > -1:
|
2020-05-16 04:15:28 +05:00
|
|
|
writeToFile.write("\ndefine( 'DB_NAME', '%s' );\n" % (dbNameRestore))
|
2019-08-03 14:53:31 +05:00
|
|
|
elif items.find('DB_USER') > -1:
|
2020-05-16 04:15:28 +05:00
|
|
|
writeToFile.write("\ndefine( 'DB_USER', '%s' );\n" % (dbUser))
|
2019-08-03 14:53:31 +05:00
|
|
|
elif items.find('DB_PASSWORD') > -1:
|
2020-05-16 04:15:28 +05:00
|
|
|
writeToFile.write("\ndefine( 'DB_PASSWORD', '%s' );\n" % (dbPassword))
|
2019-08-26 12:43:16 +05:00
|
|
|
elif items.find('WP_SITEURL') > -1:
|
|
|
|
|
continue
|
2020-05-16 04:15:28 +05:00
|
|
|
elif items.find("That's all, stop editing! Happy publishing.") > -1:
|
|
|
|
|
content = """
|
|
|
|
|
define('WP_HOME','http://%s');
|
|
|
|
|
define('WP_SITEURL','http://%s');
|
|
|
|
|
""" % (domain, domain)
|
|
|
|
|
writeToFile.write(content)
|
|
|
|
|
writeToFile.writelines(items)
|
2019-08-03 14:53:31 +05:00
|
|
|
else:
|
2020-05-16 04:15:28 +05:00
|
|
|
writeToFile.write(items + '\n')
|
2019-08-03 14:53:31 +05:00
|
|
|
|
|
|
|
|
writeToFile.close()
|
|
|
|
|
|
|
|
|
|
command = 'mv %s %s' % (tmp, pathFinalConfig)
|
2019-12-07 18:06:00 +05:00
|
|
|
ProcessUtilities.executioner(command)
|
|
|
|
|
|
|
|
|
|
command = 'chown %s:%s %s' % (website.externalApp, website.externalApp, pathFinalConfig)
|
|
|
|
|
ProcessUtilities.executioner(command)
|
2019-08-03 14:53:31 +05:00
|
|
|
|
|
|
|
|
logging.statusWriter(tempStatusPath, 'Database synced..,100')
|
|
|
|
|
|
|
|
|
|
try:
|
2019-12-07 18:06:00 +05:00
|
|
|
os.remove(databasePath)
|
2019-08-03 14:53:31 +05:00
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
2020-05-16 04:15:28 +05:00
|
|
|
from filemanager.filemanager import FileManager
|
|
|
|
|
|
|
|
|
|
fm = FileManager(None, None)
|
|
|
|
|
fm.fixPermissions(masterDomain)
|
|
|
|
|
|
2019-08-03 14:53:31 +05:00
|
|
|
logging.statusWriter(tempStatusPath, 'Data copied..,[200]')
|
|
|
|
|
|
|
|
|
|
return 0
|
2019-12-10 15:09:10 +05:00
|
|
|
except BaseException as msg:
|
2020-01-07 12:41:32 +05:00
|
|
|
mesg = '%s. [168][404]' % (str(msg))
|
2019-12-07 18:06:00 +05:00
|
|
|
logging.statusWriter(self.tempStatusPath, mesg)
|
2019-08-03 14:53:31 +05:00
|
|
|
|
|
|
|
|
def startSyncing(self):
|
|
|
|
|
try:
|
|
|
|
|
tempStatusPath = self.extraArgs['tempStatusPath']
|
|
|
|
|
childDomain = self.extraArgs['childDomain']
|
|
|
|
|
eraseCheck = self.extraArgs['eraseCheck']
|
|
|
|
|
dbCheck = self.extraArgs['dbCheck']
|
|
|
|
|
copyChanged = self.extraArgs['copyChanged']
|
|
|
|
|
|
|
|
|
|
child = ChildDomains.objects.get(domain=childDomain)
|
|
|
|
|
|
|
|
|
|
configPath = '%s/wp-config.php' % (child.path)
|
|
|
|
|
if not os.path.exists(configPath):
|
|
|
|
|
logging.statusWriter(tempStatusPath, 'WordPress is not detected. [404]')
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
if dbCheck:
|
|
|
|
|
logging.statusWriter(tempStatusPath, 'Syncing databases..,10')
|
|
|
|
|
|
|
|
|
|
## Create backup of child-domain database
|
|
|
|
|
|
|
|
|
|
configPath = '%s/wp-config.php' % (child.path)
|
|
|
|
|
|
|
|
|
|
data = open(configPath, 'r').readlines()
|
|
|
|
|
|
|
|
|
|
for items in data:
|
|
|
|
|
if items.find('DB_NAME') > -1:
|
|
|
|
|
dbName = items.split("'")[3]
|
|
|
|
|
if mysqlUtilities.createDatabaseBackup(dbName, '/home/cyberpanel'):
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
raise BaseException('Failed to create database backup.')
|
|
|
|
|
|
|
|
|
|
databasePath = '%s/%s.sql' % ('/home/cyberpanel', dbName)
|
|
|
|
|
command = "sed -i 's/%s/%s/g' %s" % (child.domain, child.master.domain, databasePath)
|
|
|
|
|
ProcessUtilities.executioner(command, 'cyberpanel')
|
|
|
|
|
|
|
|
|
|
## Restore to master domain
|
|
|
|
|
|
|
|
|
|
masterPath = '/home/%s/public_html' % (child.master.domain)
|
|
|
|
|
|
|
|
|
|
configPath = '%s/wp-config.php' % (masterPath)
|
|
|
|
|
|
|
|
|
|
data = open(configPath, 'r').readlines()
|
|
|
|
|
|
|
|
|
|
for items in data:
|
|
|
|
|
if items.find('DB_NAME') > -1:
|
|
|
|
|
dbNameRestore = items.split("'")[3]
|
|
|
|
|
if not mysqlUtilities.restoreDatabaseBackup(dbNameRestore, '/home/cyberpanel', None, 1, dbName):
|
|
|
|
|
try:
|
|
|
|
|
os.remove(databasePath)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
raise BaseException('Failed to restore database backup.')
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
os.remove(databasePath)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
if eraseCheck:
|
|
|
|
|
sourcePath = child.path
|
|
|
|
|
destinationPath = '/home/%s/public_html' % (child.master.domain)
|
|
|
|
|
|
|
|
|
|
command = 'rsync -avzh --exclude "wp-config.php" %s/ %s' % (sourcePath, destinationPath)
|
|
|
|
|
ProcessUtilities.executioner(command, child.master.externalApp)
|
|
|
|
|
elif copyChanged:
|
|
|
|
|
sourcePath = child.path
|
|
|
|
|
destinationPath = '/home/%s/public_html' % (child.master.domain)
|
|
|
|
|
|
|
|
|
|
command = 'rsync -avzh --exclude "wp-config.php" %s/ %s' % (sourcePath, destinationPath)
|
|
|
|
|
ProcessUtilities.executioner(command, child.master.externalApp)
|
|
|
|
|
|
|
|
|
|
logging.statusWriter(tempStatusPath, 'Data copied..,[200]')
|
|
|
|
|
|
|
|
|
|
return 0
|
2019-12-10 15:09:10 +05:00
|
|
|
except BaseException as msg:
|
2019-08-03 14:53:31 +05:00
|
|
|
mesg = '%s. [404]' % (str(msg))
|
2019-08-25 21:14:04 +05:00
|
|
|
logging.statusWriter(tempStatusPath, mesg)
|