Files
CyberPanel/websiteFunctions/StagingSetup.py

234 lines
9.1 KiB
Python
Raw Normal View History

2019-08-03 14:53:31 +05:00
#!/usr/local/CyberCP/bin/python2
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()
except BaseException, 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']
masterDomain = self.extraArgs['masterDomain']
domain = self.extraArgs['domain']
admin = self.extraArgs['admin']
website = Websites.objects.get(domain=masterDomain)
## Creating Child Domain
path = "/home/" + masterDomain + "/public_html/" + domain
logging.statusWriter(tempStatusPath, 'Creating domain for staging environment..,5')
phpSelection = 'PHP 7.1'
2019-08-27 14:40:02 +05:00
execPath = "/usr/local/CyberCP/bin/python2 " + 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:
logging.statusWriter(tempStatusPath, 'Failed to create child-domain for staging enviroment. [404]')
return 0
logging.statusWriter(tempStatusPath, 'Domain successfully created..,15')
## Copying Data
masterPath = '/home/%s/public_html' % (masterDomain)
2019-08-25 21:14:04 +05:00
command = 'rsync -avzh --exclude "%s" --exclude "wp-content/backups" --exclude "wp-content/updraft" --exclude "wp-content/cache" --exclude "wp-content/plugins/litespeed-cache" %s/ %s' % (
domain, masterPath, path)
2019-08-03 14:53:31 +05:00
ProcessUtilities.executioner(command, website.externalApp)
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)
if not os.path.exists(configPath):
logging.statusWriter(tempStatusPath, 'WordPress is not detected. [404]')
return 0
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" % (masterDomain, domain, databasePath)
ProcessUtilities.executioner(command, 'cyberpanel')
2019-08-16 15:18:11 +05:00
command = "sed -i 's/%s/%s/g' %s" % ('https', 'http', databasePath)
ProcessUtilities.executioner(command, 'cyberpanel')
2019-08-03 14:53:31 +05:00
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)
data = open(pathFinalConfig, 'r').readlines()
tmp = "/tmp/" + str(randint(1000, 9999))
writeToFile = open(tmp, 'w')
for items in data:
if items.find('DB_NAME') > -1:
writeToFile.write("define( 'DB_NAME', '%s' );\n" % (dbNameRestore))
elif items.find('DB_USER') > -1:
writeToFile.write("define( 'DB_USER', '%s' );\n" % (dbUser))
elif items.find('DB_PASSWORD') > -1:
writeToFile.write("define( 'DB_PASSWORD', '%s' );\n" % (dbPassword))
elif items.find('WP_SITEURL') > -1:
continue
2019-08-03 14:53:31 +05:00
else:
writeToFile.write(items)
writeToFile.close()
command = 'mv %s %s' % (tmp, pathFinalConfig)
ProcessUtilities.executioner(command, website.externalApp)
logging.statusWriter(tempStatusPath, 'Database synced..,100')
try:
os.path.remove(databasePath)
except:
pass
logging.statusWriter(tempStatusPath, 'Data copied..,[200]')
return 0
except BaseException, msg:
mesg = '%s. [404]' % (str(msg))
logging.statusWriter(tempStatusPath, mesg)
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
except BaseException, msg:
mesg = '%s. [404]' % (str(msg))
2019-08-25 21:14:04 +05:00
logging.statusWriter(tempStatusPath, mesg)