Files
CyberPanel/websiteFunctions/StagingSetup.py

278 lines
12 KiB
Python
Raw Normal View History

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)
2021-08-31 13:16:17 +05:00
from managePHP.phpManager import PHPManager
php = PHPManager.getPHPString(website.phpSelection)
FinalPHPPath = '/usr/local/lsws/lsphp%s/bin/php' % (php)
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
2021-01-28 14:43:55 +05:00
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
##
2020-05-16 04:15:28 +05:00
command = 'chmod 755 %s' % (masterPath)
ProcessUtilities.executioner(command)
2019-08-03 14:53:31 +05:00
## Creating Child Domain
2021-01-28 14:43:55 +05:00
path = "/home/" + masterDomain + "/" + domain
2019-08-03 14:53:31 +05:00
logging.statusWriter(tempStatusPath, 'Creating domain for staging environment..,5')
2021-01-28 14:43:55 +05:00
phpSelection = website.phpSelection
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 + \
2021-01-12 17:56:36 +05:00
" --phpVersion '" + phpSelection + "' --ssl 1 --dkimCheck 0 --openBasedir 0 --path " + path + ' --websiteOwner ' \
2019-08-03 14:53:31 +05:00
+ 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')
2021-01-28 14:43:55 +05:00
## Creating WP Site and setting Database
2020-05-16 04:15:28 +05:00
2021-08-31 13:26:48 +05:00
command = '%s -d error_reporting=0 /usr/bin/wp core download --path=%s' % (FinalPHPPath, path)
2021-01-28 14:43:55 +05:00
ProcessUtilities.executioner(command, website.externalApp)
2019-08-03 14:53:31 +05:00
logging.statusWriter(tempStatusPath, 'Creating and copying database..,50')
dbNameRestore, dbUser, dbPassword = ApplicationInstaller(None, None).dbCreation(tempStatusPath, website)
2021-08-31 13:26:48 +05:00
command = '%s -d error_reporting=0 /usr/bin/wp core config --dbname=%s --dbuser=%s --dbpass=%s --dbhost=%s:%s --path=%s' % (FinalPHPPath, dbNameRestore, dbUser, dbPassword, ApplicationInstaller.LOCALHOST, ApplicationInstaller.PORT, path)
2021-01-28 14:43:55 +05:00
ProcessUtilities.executioner(command, website.externalApp)
2019-08-03 14:53:31 +05:00
2021-01-28 14:43:55 +05:00
## Exporting and importing database
2019-08-03 14:53:31 +05:00
2021-08-31 13:26:48 +05:00
command = '%s -d error_reporting=0 /usr/bin/wp --allow-root --skip-plugins --skip-themes --path=%s db export %s/dbexport-stage.sql' % (FinalPHPPath, masterPath, path)
2021-01-28 14:43:55 +05:00
ProcessUtilities.executioner(command)
2020-05-16 04:15:28 +05:00
2021-01-28 14:43:55 +05:00
## Import
2019-08-03 14:53:31 +05:00
2021-08-31 13:26:48 +05:00
command = '%s -d error_reporting=0 /usr/bin/wp --allow-root --skip-plugins --skip-themes --path=%s --quiet db import %s/dbexport-stage.sql' % (FinalPHPPath, path, path)
2021-01-28 14:43:55 +05:00
ProcessUtilities.executioner(command)
2019-08-03 14:53:31 +05:00
try:
2021-01-28 14:43:55 +05:00
command = 'rm -f %s/dbexport-stage.sql' % (path)
ProcessUtilities.executioner(command)
2019-08-03 14:53:31 +05:00
except:
pass
2021-01-28 14:43:55 +05:00
## Sync WP-Content Folder
2021-08-31 13:26:48 +05:00
command = '%s -d error_reporting=0 /usr/bin/wp theme path --skip-plugins --skip-themes --allow-root --path=%s' % (FinalPHPPath, masterPath)
WpContentPath = ProcessUtilities.outputExecutioner(command).splitlines()[-1].replace('themes', '')
2021-03-27 16:35:12 +05:00
command = 'cp -R %s %s/' % (WpContentPath, path)
ProcessUtilities.executioner(command)
## Copy htaccess
command = 'cp -f %s/.htaccess %s/' % (WpContentPath.replace('/wp-content/', ''), path)
2019-12-07 18:06:00 +05:00
ProcessUtilities.executioner(command)
2021-01-28 14:43:55 +05:00
## Search and replace url
2021-08-31 13:26:48 +05:00
command = '%s -d error_reporting=0 /usr/bin/wp search-replace --skip-plugins --skip-themes --allow-root --path=%s "%s" "%s"' % (FinalPHPPath, path, replaceDomain, domain)
2021-08-31 13:16:17 +05:00
ProcessUtilities.executioner(command)
2021-08-31 13:26:48 +05:00
command = '%s -d error_reporting=0 /usr/bin/wp search-replace --skip-plugins --skip-themes --allow-root --path=%s "www.%s" "%s"' % (FinalPHPPath, path, replaceDomain, domain)
2019-12-07 18:06:00 +05:00
ProcessUtilities.executioner(command)
2019-08-03 14:53:31 +05:00
2021-08-31 13:26:48 +05:00
command = '%s -d error_reporting=0 /usr/bin/wp search-replace --skip-plugins --skip-themes --allow-root --path=%s "https://%s" "http://%s"' % (
2021-08-31 13:16:17 +05:00
FinalPHPPath, path, domain, domain)
2021-01-28 14:43:55 +05:00
ProcessUtilities.executioner(command)
2019-08-03 14:53:31 +05:00
2021-01-28 14:43:55 +05:00
logging.statusWriter(tempStatusPath, 'Fixing permissions..,90')
2019-08-03 14:53:31 +05:00
2020-05-16 04:15:28 +05:00
from filemanager.filemanager import FileManager
fm = FileManager(None, None)
fm.fixPermissions(masterDomain)
2020-05-16 04:43:02 +05:00
from plogical.installUtilities import installUtilities
installUtilities.reStartLiteSpeed()
2021-01-28 14:43:55 +05:00
logging.statusWriter(tempStatusPath, 'Completed,[200]')
2019-08-03 14:53:31 +05:00
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']
2021-01-28 14:43:55 +05:00
#eraseCheck = self.extraArgs['eraseCheck']
2019-08-03 14:53:31 +05:00
dbCheck = self.extraArgs['dbCheck']
2021-01-28 14:43:55 +05:00
#copyChanged = self.extraArgs['copyChanged']
2019-08-03 14:53:31 +05:00
child = ChildDomains.objects.get(domain=childDomain)
2021-08-31 15:21:23 +05:00
from managePHP.phpManager import PHPManager
php = PHPManager.getPHPString(child.master.phpSelection)
FinalPHPPath = '/usr/local/lsws/lsphp%s/bin/php' % (php)
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
2019-08-03 14:53:31 +05:00
2020-05-16 04:43:02 +05:00
command = 'chmod 755 /home/%s/public_html' % (child.master.domain)
ProcessUtilities.executioner(command)
2019-08-03 14:53:31 +05:00
configPath = '%s/wp-config.php' % (child.path)
2020-05-16 04:43:02 +05:00
2019-08-03 14:53:31 +05:00
if not os.path.exists(configPath):
logging.statusWriter(tempStatusPath, 'WordPress is not detected. [404]')
return 0
2021-01-28 14:43:55 +05:00
## Restore db
2019-08-03 14:53:31 +05:00
2021-01-28 14:43:55 +05:00
logging.statusWriter(tempStatusPath, 'Syncing databases..,10')
2019-08-03 14:53:31 +05:00
2021-08-31 15:21:23 +05:00
command = '%s -d error_reporting=0 /usr/bin/wp --allow-root --skip-plugins --skip-themes --path=%s db export %s/dbexport-stage.sql' % (FinalPHPPath, child.path, masterPath)
2021-04-30 23:41:05 +05:00
result = ProcessUtilities.outputExecutioner(command)
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(result)
2019-08-03 14:53:31 +05:00
2021-01-28 14:43:55 +05:00
## Restore to master domain
2019-08-03 14:53:31 +05:00
2021-08-31 15:21:23 +05:00
command = '%s -d error_reporting=0 /usr/bin/wp --allow-root --skip-plugins --skip-themes --path=%s --quiet db import %s/dbexport-stage.sql' % (FinalPHPPath, masterPath, masterPath)
2021-04-30 23:41:05 +05:00
result = ProcessUtilities.outputExecutioner(command)
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(result)
2019-08-03 14:53:31 +05:00
2021-01-28 14:43:55 +05:00
try:
command = 'rm -f %s/dbexport-stage.sql' % (masterPath)
ProcessUtilities.executioner(command)
except:
pass
2019-08-03 14:53:31 +05:00
2021-01-28 14:43:55 +05:00
## Sync WP-Content Folder
2019-08-03 14:53:31 +05:00
2021-01-28 14:43:55 +05:00
logging.statusWriter(tempStatusPath, 'Syncing data..,50')
2019-08-03 14:53:31 +05:00
2021-08-31 15:21:23 +05:00
command = '%s -d error_reporting=0 /usr/bin/wp theme path --allow-root --skip-plugins --skip-themes --path=%s' % (FinalPHPPath, masterPath)
2021-03-27 16:35:12 +05:00
WpContentPath = ProcessUtilities.outputExecutioner(command).splitlines()[-1].replace('wp-content/themes', '')
2021-04-30 23:41:05 +05:00
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(WpContentPath)
2021-03-27 16:35:12 +05:00
command = 'cp -R %s/wp-content/ %s' % (child.path, WpContentPath)
ProcessUtilities.executioner(command)
## COPY Htaccess
2021-03-27 16:35:12 +05:00
command = 'cp -f %s/.htaccess %s' % (child.path, WpContentPath)
2021-01-28 14:43:55 +05:00
ProcessUtilities.executioner(command)
2019-08-03 14:53:31 +05:00
2021-01-28 14:43:55 +05:00
## Search and replace url
2019-08-03 14:53:31 +05:00
2021-08-31 15:21:23 +05:00
command = '%s -d error_reporting=0 /usr/bin/wp search-replace --allow-root --skip-plugins --skip-themes --path=%s "%s" "%s"' % (FinalPHPPath, masterPath, child.domain, replaceDomain)
result = ProcessUtilities.outputExecutioner(command)
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(result)
command = '%s -d error_reporting=0 /usr/bin/wp search-replace --allow-root --skip-plugins --skip-themes --path=%s "www.%s" "%s"' % (FinalPHPPath,masterPath, child.domain, replaceDomain)
2021-04-30 23:41:05 +05:00
result = ProcessUtilities.outputExecutioner(command)
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(result)
2019-08-03 14:53:31 +05:00
2021-08-31 15:21:23 +05:00
command = '%s -d error_reporting=0 /usr/bin/wp search-replace --allow-root --skip-plugins --skip-themes --path=%s "https://%s" "http://%s"' % (FinalPHPPath,
masterPath, replaceDomain, replaceDomain)
2021-04-30 23:41:05 +05:00
result = ProcessUtilities.outputExecutioner(command)
if os.path.exists(ProcessUtilities.debugPath):
logging.writeToFile(result)
2019-08-03 14:53:31 +05:00
2020-05-16 04:43:02 +05:00
from filemanager.filemanager import FileManager
fm = FileManager(None, None)
fm.fixPermissions(child.master.domain)
from plogical.installUtilities import installUtilities
installUtilities.reStartLiteSpeed()
2021-01-28 14:43:55 +05:00
logging.statusWriter(tempStatusPath, 'Completed,[200]')
2020-05-16 04:43:02 +05:00
2019-08-03 14:53:31 +05:00
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)