Files
CyberPanel/websiteFunctions/StagingSetup.py
2021-06-19 12:28:56 +05:00

259 lines
10 KiB
Python

#!/usr/local/CyberCP/bin/python
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
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 as msg:
logging.writeToFile(str(msg) + ' [StagingSetup.run]')
def startCloning(self):
try:
tempStatusPath = self.extraArgs['tempStatusPath']
self.tempStatusPath = tempStatusPath
masterDomain = self.extraArgs['masterDomain']
domain = self.extraArgs['domain']
admin = self.extraArgs['admin']
website = Websites.objects.get(domain=masterDomain)
try:
import json
from cloudAPI.models import WPDeployments
wpd = WPDeployments.objects.get(owner=website)
path = json.loads(wpd.config)['path']
masterPath = '/home/%s/public_html/%s' % (masterDomain, path)
replaceDomain = '%s/%s' % (masterDomain, path)
except:
masterPath = '/home/%s/public_html' % (masterDomain)
replaceDomain = masterDomain
configPath = '%s/wp-config.php' % (masterPath)
## Check if WP Detected on Main Site
command = 'ls -la %s' % (configPath)
output = ProcessUtilities.outputExecutioner(command)
if output.find('No such file or') > -1:
logging.statusWriter(tempStatusPath, 'WordPress is not detected. [404]')
return 0
##
command = 'chmod 755 %s' % (masterPath)
ProcessUtilities.executioner(command)
## Creating Child Domain
path = "/home/" + masterDomain + "/" + domain
logging.statusWriter(tempStatusPath, 'Creating domain for staging environment..,5')
phpSelection = website.phpSelection
execPath = "/usr/local/CyberCP/bin/python " + virtualHostUtilities.cyberPanel + "/plogical/virtualHostUtilities.py"
execPath = execPath + " createDomain --masterDomain " + masterDomain + " --virtualHostName " + domain + \
" --phpVersion '" + phpSelection + "' --ssl 1 --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 environment. [404]')
return 0
logging.statusWriter(tempStatusPath, 'Domain successfully created..,15')
## Creating WP Site and setting Database
command = 'wp core download --path=%s' % (path)
ProcessUtilities.executioner(command, website.externalApp)
logging.statusWriter(tempStatusPath, 'Creating and copying database..,50')
dbNameRestore, dbUser, dbPassword = ApplicationInstaller(None, None).dbCreation(tempStatusPath, website)
command = 'wp core config --dbname=%s --dbuser=%s --dbpass=%s --dbhost=%s:%s --path=%s' % (dbNameRestore, dbUser, dbPassword, ApplicationInstaller.LOCALHOST, ApplicationInstaller.PORT, path)
ProcessUtilities.executioner(command, website.externalApp)
## Exporting and importing database
command = 'wp --allow-root --skip-plugins --skip-themes --path=%s db export %s/dbexport-stage.sql' % (masterPath, path)
ProcessUtilities.executioner(command)
## Import
command = 'wp --allow-root --skip-plugins --skip-themes --path=%s --quiet db import %s/dbexport-stage.sql' % (path, path)
ProcessUtilities.executioner(command)
try:
command = 'rm -f %s/dbexport-stage.sql' % (path)
ProcessUtilities.executioner(command)
except:
pass
## Sync WP-Content Folder
command = 'wp theme path --skip-plugins --skip-themes --allow-root --path=%s' % (masterPath)
WpContentPath = ProcessUtilities.outputExecutioner(command).splitlines()[-1].replace('themes', '')
command = 'cp -R %s %s/' % (WpContentPath, path)
ProcessUtilities.executioner(command)
## Copy htaccess
command = 'cp -f %s/.htaccess %s/' % (WpContentPath.replace('/wp-content/', ''), path)
ProcessUtilities.executioner(command)
## Search and replace url
command = 'wp search-replace --skip-plugins --skip-themes --allow-root --path=%s "%s" "%s"' % (path, replaceDomain, domain)
ProcessUtilities.executioner(command)
command = 'wp search-replace --skip-plugins --skip-themes --allow-root --path=%s "www.%s" "%s"' % (path, replaceDomain, domain)
ProcessUtilities.executioner(command)
logging.statusWriter(tempStatusPath, 'Fixing permissions..,90')
from filemanager.filemanager import FileManager
fm = FileManager(None, None)
fm.fixPermissions(masterDomain)
from plogical.installUtilities import installUtilities
installUtilities.reStartLiteSpeed()
logging.statusWriter(tempStatusPath, 'Completed,[200]')
return 0
except BaseException as msg:
mesg = '%s. [168][404]' % (str(msg))
logging.statusWriter(self.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)
try:
import json
from cloudAPI.models import WPDeployments
wpd = WPDeployments.objects.get(owner=child.master)
path = json.loads(wpd.config)['path']
masterPath = '/home/%s/public_html/%s' % (child.master.domain, path)
replaceDomain = '%s/%s' % (child.master.domain, path)
except:
masterPath = '/home/%s/public_html' % (child.master.domain)
replaceDomain = child.master.domain
command = 'chmod 755 /home/%s/public_html' % (child.master.domain)
ProcessUtilities.executioner(command)
configPath = '%s/wp-config.php' % (child.path)
if not os.path.exists(configPath):
logging.statusWriter(tempStatusPath, 'WordPress is not detected. [404]')
return 0
## Restore db
logging.statusWriter(tempStatusPath, 'Syncing databases..,10')
command = 'wp --allow-root --skip-plugins --skip-themes --path=%s db export %s/dbexport-stage.sql' % (child.path, masterPath)
result = ProcessUtilities.outputExecutioner(command)
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(result)
## Restore to master domain
command = 'wp --allow-root --skip-plugins --skip-themes --path=%s --quiet db import %s/dbexport-stage.sql' % (masterPath, masterPath)
result = ProcessUtilities.outputExecutioner(command)
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(result)
try:
command = 'rm -f %s/dbexport-stage.sql' % (masterPath)
ProcessUtilities.executioner(command)
except:
pass
## Sync WP-Content Folder
logging.statusWriter(tempStatusPath, 'Syncing data..,50')
command = 'wp theme path --allow-root --skip-plugins --skip-themes --path=%s' % (masterPath)
WpContentPath = ProcessUtilities.outputExecutioner(command).splitlines()[-1].replace('wp-content/themes', '')
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(WpContentPath)
command = 'cp -R %s/wp-content/ %s' % (child.path, WpContentPath)
ProcessUtilities.executioner(command)
## COPY Htaccess
command = 'cp -f %s/.htaccess %s' % (child.path, WpContentPath)
ProcessUtilities.executioner(command)
## Search and replace url
command = 'wp search-replace --allow-root --skip-plugins --skip-themes --path=%s "%s" "%s"' % (masterPath, child.domain, replaceDomain)
result = ProcessUtilities.outputExecutioner(command)
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(result)
command = 'wp search-replace --allow-root --skip-plugins --skip-themes --path=%s "www.%s" "%s"' % (masterPath, child.domain, replaceDomain)
result = ProcessUtilities.outputExecutioner(command)
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(result)
from filemanager.filemanager import FileManager
fm = FileManager(None, None)
fm.fixPermissions(child.master.domain)
from plogical.installUtilities import installUtilities
installUtilities.reStartLiteSpeed()
logging.statusWriter(tempStatusPath, 'Completed,[200]')
return 0
except BaseException as msg:
mesg = '%s. [404]' % (str(msg))
logging.statusWriter(tempStatusPath, mesg)