bug fix: cpanel importer

This commit is contained in:
usmannasir 2024-10-11 19:20:04 +05:00
parent 0804176c6d
commit c058fa6437
7 changed files with 122 additions and 43 deletions

View File

@ -73,7 +73,7 @@ def upgrade_cyberpanel(request):
try:
upgrade_command = 'sh <(curl https://raw.githubusercontent.com/usmannasir/cyberpanel/stable/preUpgrade.sh || wget -O - https://raw.githubusercontent.com/usmannasir/cyberpanel/stable/preUpgrade.sh)'
result = subprocess.run(upgrade_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True)
universal_newlines=True)
if result.returncode == 0:
response_data = {'success': True, 'message': 'CyberPanel upgrade completed successfully.'}

View File

@ -162,7 +162,7 @@ class preFlightsChecks:
preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR)
command = 'mount -o remount /'
mResult = subprocess.run(command, capture_output=True, text=True, shell=True)
mResult = subprocess.run(command, capture_output=True,universal_newlines=True, shell=True)
if mResult.returncode != 0:
fstab_path = '/etc/fstab'
backup_path = fstab_path + '.bak'
@ -197,7 +197,7 @@ class preFlightsChecks:
return 0
command = 'mount -o remount /'
mResult = subprocess.run(command, capture_output=True, text=True, shell=True)
mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
if mResult.returncode != 0:
fstab_path = '/etc/fstab'
backup_path = fstab_path + '.bak'
@ -214,19 +214,19 @@ class preFlightsChecks:
####
command = "find /lib/modules/ -type f -name '*quota_v*.ko*'"
iResult = subprocess.run(command, capture_output=True, text=True, shell=True)
iResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
print(repr(iResult.stdout))
# Only if the first command works, run the rest
if iResult.returncode == 0:
command = "echo '{}' | sed -n 's|/lib/modules/\\([^/]*\\)/.*|\\1|p' | sort -u".format(iResult.stdout)
result = subprocess.run(command, capture_output=True, text=True, shell=True)
result = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
fResult = result.stdout.rstrip('\n')
print(repr(result.stdout.rstrip('\n')))
command = 'uname -r'
ffResult = subprocess.run(command, capture_output=True, text=True, shell=True)
ffResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
ffResult = ffResult.stdout.rstrip('\n')
command = f"DEBIAN_FRONTEND=noninteractive apt-get install linux-modules-extra-{ffResult}"
@ -328,7 +328,7 @@ class preFlightsChecks:
def mountTemp(self):
try:
result = subprocess.run('systemd-detect-virt', capture_output=True, text=True, shell=True)
result = subprocess.run('systemd-detect-virt', capture_output=True, universal_newlines=True, shell=True)
if result.stdout.find('openvz') > -1:
if self.distro == ubuntu:
@ -1775,7 +1775,7 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout';
# lscpdSelection = 'lscpd.aarch64'
try:
result = subprocess.run('uname -a', capture_output=True, text=True, shell=True)
result = subprocess.run('uname -a', capture_output=True, universal_newlines=True, shell=True)
if result.stdout.find('aarch64') == -1:
lscpdSelection = 'lscpd-0.3.1'

View File

@ -65,7 +65,7 @@ class InstallCyberPanel:
try:
command = 'uname -a'
result = subprocess.run(command, capture_output=True, text=True, shell=True)
result = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
if 'aarch64' in result.stdout:
return True

View File

@ -304,6 +304,9 @@ class cPanelImporter:
movePath = '%s/homedir/%s' % (
CompletPathToExtractedArchive, self.homeDir)
if os.path.exists(ProcessUtilities.debugPath):
logging.statusWriter(self.logFile, f'Directory from where docRoot of main site data will be moved {movePath}')
shutil.copytree(movePath, nowPath, symlinks=True)
message = 'Main site %s created from archive file: %s' % (DomainName, self.backupFile)
@ -591,6 +594,41 @@ class cPanelImporter:
##
passFile = "/etc/cyberpanel/mysqlPassword"
try:
import json
jsonData = json.loads(open(passFile, 'r').read())
mysqluser = jsonData['mysqluser']
mysqlpassword = jsonData['mysqlpassword']
mysqlport = jsonData['mysqlport']
mysqlhost = jsonData['mysqlhost']
password = mysqlpassword
except:
passFile = "/etc/cyberpanel/mysqlPassword"
f = open(passFile)
data = f.read()
password = data.split('\n', 1)[0]
mysqlhost = 'localhost'
mysqlport = '3306'
mysqluser = 'root'
cnfPath = '/home/cyberpanel/.my.cnf'
if not os.path.exists(cnfPath):
cnfContent = """[mysqldump]
user=root
password=%s
max_allowed_packet=1024M
[mysql]
user=root
password=%s
""" % (password, password)
writeToFile = open(cnfPath, 'w')
writeToFile.write(cnfContent)
writeToFile.close()
os.chmod(cnfPath, 0o600)
f = open(passFile)
data = f.read()
password = data.split('\n', 1)[0]
@ -616,14 +654,40 @@ class cPanelImporter:
message = 'Failed while restoring database %s from backup file %s, error message: %s' % (items.replace('.sql', ''), self.backupFile, str(msg))
logging.statusWriter(self.logFile, message, 1)
command = 'sudo mysql -u root -p' + password + ' ' + items.replace('.sql', '')
command = f'mysql --defaults-file=/home/cyberpanel/.my.cnf -u {mysqluser} --host={mysqlhost} --port {mysqlport} ' + items.replace('.sql', '')
message = f'Full command to restore DB {command}'
logging.statusWriter(self.logFile, message, 1)
cmd = shlex.split(command)
DBPath = "%s/%s" % (DatabasesPath, items)
# with open(DBPath, 'r') as f:
# message = f'Full command to restore DB {cmd}'
# logging.statusWriter(self.logFile, message, 1)
#
# res = subprocess.call(cmd, stdin=f)
with open(DBPath, 'r') as f:
res = subprocess.call(cmd, stdin=f)
try:
# Run the command using subprocess.run, capturing stdout and stderr
result = subprocess.run(cmd, stdin=f, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
# Log stdout and stderr
logging.statusWriter(self.logFile, f'STDOUT: {result.stdout}', 1)
logging.statusWriter(self.logFile, f'STDERR: {result.stderr}', 1)
# Check if the command failed
if result.returncode != 0:
logging.statusWriter(self.logFile,
f'Command failed with return code {result.returncode}', 2)
except Exception as e:
# Log any exception that occurs
logging.statusWriter(self.logFile, f'Exception occurred: {str(e)}', 2)
website = Websites.objects.get(domain=self.mainDomain)
@ -633,21 +697,33 @@ class cPanelImporter:
data = open(CommandsPath, 'r').readlines()
for inItems in data:
if inItems.find('GRANT ALL PRIVILEGES') > -1 and inItems.find('localhost') > -1 and inItems.find('_test') == -1:
cDBName = inItems.split('`')[1].replace('\\', '')
logging.statusWriter(self.logFile, inItems, 1)
if cDBName == items.replace('.sql', ''):
cDBUser = inItems.replace("`","'").replace("\\","").split("'")[1]
message = 'Database user for %s is %s.' % (cDBName, cDBUser)
logging.statusWriter(self.logFile, message, 1)
if Databases.objects.filter(dbUser=cDBUser).count() > 0:
continue
break
### temp disable if user not added, need to remove this try,catch and to ensure user gets added
try:
db = Databases(website=website, dbName=items.replace('.sql', ''), dbUser=cDBUser)
db.save()
for inItems in data:
if (inItems.find('GRANT ALL PRIVILEGES') > -1 or inItems.find('GRANT USAGE') > -1) and inItems.find('localhost') > -1 and inItems.find('_test') == -1:
cDBName = inItems.split('`')[1].replace('\\', '')
logging.statusWriter(self.logFile, inItems, 1)
if cDBName == items.replace('.sql', ''):
cDBUser = inItems.replace("`","'").replace("\\","").split("'")[1]
message = 'Database user for %s is %s.' % (cDBName, cDBUser)
logging.statusWriter(self.logFile, message, 1)
if Databases.objects.filter(dbUser=cDBUser).count() > 0:
continue
break
except:
pass
### temp disable if user not added, need to remove this try,catch and to ensure user gets added
try:
db = Databases(website=website, dbName=items.replace('.sql', ''), dbUser=cDBUser)
db.save()
except:
db = Databases(website=website, dbName=items.replace('.sql', ''), dbUser='root')
db.save()
pass
message = 'MySQL dump successfully restored for %s.' % (items.replace('.sql', ''))
logging.statusWriter(self.logFile, message, 1)
@ -663,6 +739,9 @@ class cPanelImporter:
if items.find("--") > -1 or items.find("'cyberpanel'@") > -1:
continue
try:
if os.path.exists(ProcessUtilities.debugPath):
message = f'Currently executing MySQL command {items}'
logging.statusWriter(self.logFile, message, 1)
cursor.execute(items)
except BaseException as msg:
message = 'Failed while restoring database %s from backup file %s, error message: %s' % (

View File

@ -27,7 +27,7 @@ class rebuildQuotas:
if rData.find('xfs') > -1:
command = "mount | grep ' / '"
qResult = subprocess.run(command, capture_output=True, text=True, shell=True)
qResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
if qResult.stdout.find('usrquota') > -1:
print("Looks like Quotas are enabled in filesystem, moving on..")
@ -37,7 +37,7 @@ class rebuildQuotas:
exit(1)
else:
command = "mount | grep quota"
qResult = subprocess.run(command, capture_output=True, text=True, shell=True)
qResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
if qResult.stdout.find('usrquota') > -1:
print("Looks like Quotas are enabled in filesystem, moving on..")
else:
@ -49,13 +49,13 @@ class rebuildQuotas:
for website in Websites.objects.all():
print(f"Rebuilding quotas for {website.domain}...")
command = 'chattr -R -i /home/%s/' % (website.domain)
subprocess.run(command, capture_output=True, text=True, shell=True)
subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
if website.package.enforceDiskLimits:
spaceString = f'{website.package.diskSpace}M {website.package.diskSpace}M'
command = f'setquota -u {website.externalApp} {spaceString} 0 0 /'
print(command)
qResult = subprocess.run(command, capture_output=True, text=True, shell=True)
qResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
else:
print(f"Ignored {website.domain} because the selected package does not enforce disk limits.")
except:

View File

@ -585,7 +585,7 @@ context /.well-known/acme-challenge {
#output = subprocess.check_output(shlex.split(command)).decode("utf-8")
result = subprocess.run(command, capture_output=True, text=True,
result = subprocess.run(command, capture_output=True, universal_newlines=True,
shell=True)
@ -605,7 +605,7 @@ context /.well-known/acme-challenge {
logging.CyberCPLogFileWriter.writeToFile(command, 0)
result = subprocess.run(command, capture_output=True, text=True,
result = subprocess.run(command, capture_output=True, universal_newlines=True,
shell=True)
stdout = result.stdout
@ -653,7 +653,7 @@ context /.well-known/acme-challenge {
logging.CyberCPLogFileWriter.writeToFile(command)
#output = subprocess.check_output(shlex.split(command)).decode("utf-8")
result = subprocess.run(command, capture_output=True, text=True,
result = subprocess.run(command, capture_output=True, universal_newlines=True,
shell=True)
stdout = result.stdout
@ -668,7 +668,7 @@ context /.well-known/acme-challenge {
+ '/cert.pem' + ' --key-file ' + existingCertPath + '/privkey.pem' \
+ ' --fullchain-file ' + existingCertPath + '/fullchain.pem' + ' -w /usr/local/lsws/Example/html -k ec-256 --force --server letsencrypt'
result = subprocess.run(command, capture_output=True, text=True,
result = subprocess.run(command, capture_output=True, universal_newlines=True,
shell=True)
stdout = result.stdout

View File

@ -2291,7 +2291,7 @@ CREATE TABLE `websiteFunctions_backupsv2` (`id` integer AUTO_INCREMENT NOT NULL
os.remove(lscpdPath)
try:
result = subprocess.run('uname -a', capture_output=True, text=True, shell=True)
result = subprocess.run('uname -a', capture_output=True, universal_newlines=True, shell=True)
if result.stdout.find('aarch64') == -1:
lscpdSelection = 'lscpd-0.3.1'
@ -3625,7 +3625,7 @@ pm.max_spare_servers = 3
command = 'mount -o remount /'
mResult = subprocess.run(command, capture_output=True, text=True, shell=True)
mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
if mResult.returncode != 0:
fstab_path = '/etc/fstab'
backup_path = fstab_path + '.bak'
@ -3660,7 +3660,7 @@ pm.max_spare_servers = 3
return 0
command = 'mount -o remount /'
mResult = subprocess.run(command, capture_output=True, text=True, shell=True)
mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
if mResult.returncode != 0:
fstab_path = '/etc/fstab'
backup_path = fstab_path + '.bak'
@ -3672,7 +3672,7 @@ pm.max_spare_servers = 3
return 0
command = 'quotacheck -ugm /'
mResult = subprocess.run(command, capture_output=True, text=True, shell=True)
mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
if mResult.returncode != 0:
fstab_path = '/etc/fstab'
backup_path = fstab_path + '.bak'
@ -3686,19 +3686,19 @@ pm.max_spare_servers = 3
####
command = "find /lib/modules/ -type f -name '*quota_v*.ko*'"
iResult = subprocess.run(command, capture_output=True, text=True, shell=True)
iResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
print(repr(iResult.stdout))
# Only if the first command works, run the rest
if iResult.returncode == 0:
command = "echo '{}' | sed -n 's|/lib/modules/\\([^/]*\\)/.*|\\1|p' | sort -u".format(iResult.stdout)
result = subprocess.run(command, capture_output=True, text=True, shell=True)
result = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
fResult = result.stdout.rstrip('\n')
print(repr(result.stdout.rstrip('\n')))
command = 'uname -r'
ffResult = subprocess.run(command, capture_output=True, text=True, shell=True)
ffResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
ffResult = ffResult.stdout.rstrip('\n')
command = f"apt-get install linux-modules-extra-{ffResult}"
@ -3707,7 +3707,7 @@ pm.max_spare_servers = 3
###
command = f'modprobe quota_v1 -S {ffResult}'
mResult = subprocess.run(command, capture_output=True, text=True, shell=True)
mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
if mResult.returncode != 0:
fstab_path = '/etc/fstab'
backup_path = fstab_path + '.bak'
@ -3719,7 +3719,7 @@ pm.max_spare_servers = 3
return 0
command = f'modprobe quota_v2 -S {ffResult}'
mResult = subprocess.run(command, capture_output=True, text=True, shell=True)
mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
if mResult.returncode != 0:
fstab_path = '/etc/fstab'
backup_path = fstab_path + '.bak'
@ -3731,7 +3731,7 @@ pm.max_spare_servers = 3
return 0
command = f'quotacheck -ugm /'
mResult = subprocess.run(command, capture_output=True, text=True, shell=True)
mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
if mResult.returncode != 0:
fstab_path = '/etc/fstab'
backup_path = fstab_path + '.bak'
@ -3743,7 +3743,7 @@ pm.max_spare_servers = 3
return 0
command = f'quotaon -v /'
mResult = subprocess.run(command, capture_output=True, text=True, shell=True)
mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True)
if mResult.returncode != 0:
fstab_path = '/etc/fstab'
backup_path = fstab_path + '.bak'