From ff84c80f018c013a29868d129b7097657f1c7c8e Mon Sep 17 00:00:00 2001 From: usmannasir Date: Mon, 14 Oct 2024 15:28:59 +0500 Subject: [PATCH] add backward compatability to subprocess.run --- install/install.py | 41 ++++++++++++++++++---- install/installCyberPanel.py | 5 ++- plogical/rebuildQuotas.py | 25 +++++++++++--- plogical/sslUtilities.py | 30 +++++++++++----- plogical/upgrade.py | 66 ++++++++++++++++++++++++++++++------ 5 files changed, 136 insertions(+), 31 deletions(-) diff --git a/install/install.py b/install/install.py index 4c21bdce1..ee479b281 100755 --- a/install/install.py +++ b/install/install.py @@ -162,7 +162,11 @@ 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,universal_newlines=True, shell=True) + try: + mResult = subprocess.run(command, capture_output=True,universal_newlines=True, shell=True) + except: + mResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=True) + if mResult.returncode != 0: fstab_path = '/etc/fstab' backup_path = fstab_path + '.bak' @@ -197,7 +201,12 @@ class preFlightsChecks: return 0 command = 'mount -o remount /' - mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + try: + mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + mResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, shell=True) + if mResult.returncode != 0: fstab_path = '/etc/fstab' backup_path = fstab_path + '.bak' @@ -214,19 +223,31 @@ class preFlightsChecks: #### command = "find /lib/modules/ -type f -name '*quota_v*.ko*'" - iResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + try: + iResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + iResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + 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, universal_newlines=True, shell=True) + try: + result = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, 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, universal_newlines=True, shell=True) + try: + ffResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + ffResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=True) + ffResult = ffResult.stdout.rstrip('\n') command = f"DEBIAN_FRONTEND=noninteractive apt-get install linux-modules-extra-{ffResult}" @@ -328,7 +349,10 @@ class preFlightsChecks: def mountTemp(self): try: - result = subprocess.run('systemd-detect-virt', capture_output=True, universal_newlines=True, shell=True) + try: + result = subprocess.run('systemd-detect-virt', capture_output=True, universal_newlines=True, shell=True) + except: + result = subprocess.run('systemd-detect-virt', stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=True) if result.stdout.find('openvz') > -1: if self.distro == ubuntu: @@ -1775,7 +1799,10 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout'; # lscpdSelection = 'lscpd.aarch64' try: - result = subprocess.run('uname -a', capture_output=True, universal_newlines=True, shell=True) + try: + result = subprocess.run('uname -a', capture_output=True, universal_newlines=True, shell=True) + except: + result = subprocess.run('uname -a', stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=True) if result.stdout.find('aarch64') == -1: lscpdSelection = 'lscpd-0.3.1' diff --git a/install/installCyberPanel.py b/install/installCyberPanel.py index fcae20dd6..c9cd42af7 100755 --- a/install/installCyberPanel.py +++ b/install/installCyberPanel.py @@ -65,7 +65,10 @@ class InstallCyberPanel: try: command = 'uname -a' - result = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + try: + result = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=True) if 'aarch64' in result.stdout: return True diff --git a/plogical/rebuildQuotas.py b/plogical/rebuildQuotas.py index e7d0c8a85..d66f16777 100644 --- a/plogical/rebuildQuotas.py +++ b/plogical/rebuildQuotas.py @@ -27,7 +27,10 @@ class rebuildQuotas: if rData.find('xfs') > -1: command = "mount | grep ' / '" - qResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + try: + qResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + qResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=True) if qResult.stdout.find('usrquota') > -1: print("Looks like Quotas are enabled in filesystem, moving on..") @@ -37,7 +40,12 @@ class rebuildQuotas: exit(1) else: command = "mount | grep quota" - qResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + try: + qResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + qResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, shell=True) + if qResult.stdout.find('usrquota') > -1: print("Looks like Quotas are enabled in filesystem, moving on..") else: @@ -49,13 +57,22 @@ 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, universal_newlines=True, shell=True) + try: + qResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + qResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + 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, universal_newlines=True, shell=True) + try: + qResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + qResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, shell=True) + else: print(f"Ignored {website.domain} because the selected package does not enforce disk limits.") except: diff --git a/plogical/sslUtilities.py b/plogical/sslUtilities.py index 0845866d0..fb8188294 100755 --- a/plogical/sslUtilities.py +++ b/plogical/sslUtilities.py @@ -585,8 +585,10 @@ context /.well-known/acme-challenge { #output = subprocess.check_output(shlex.split(command)).decode("utf-8") - result = subprocess.run(command, capture_output=True, universal_newlines=True, - shell=True) + try: + result = subprocess.run(command, capture_output=True, universal_newlines=True,shell=True) + except: + result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=True) stdout = result.stdout @@ -605,8 +607,12 @@ context /.well-known/acme-challenge { logging.CyberCPLogFileWriter.writeToFile(command, 0) - result = subprocess.run(command, capture_output=True, universal_newlines=True, - shell=True) + try: + result = subprocess.run(command, capture_output=True, universal_newlines=True, + shell=True) + except: + result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, shell=True) stdout = result.stdout stderr = result.stderr @@ -653,8 +659,12 @@ 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, universal_newlines=True, - shell=True) + try: + result = subprocess.run(command, capture_output=True, universal_newlines=True, + shell=True) + except: + result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, shell=True) stdout = result.stdout stderr = result.stderr @@ -668,8 +678,12 @@ 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, universal_newlines=True, - shell=True) + try: + result = subprocess.run(command, capture_output=True, universal_newlines=True, + shell=True) + except: + result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, shell=True) stdout = result.stdout stderr = result.stderr diff --git a/plogical/upgrade.py b/plogical/upgrade.py index f8aec2f6a..4e4d86fbd 100755 --- a/plogical/upgrade.py +++ b/plogical/upgrade.py @@ -2291,7 +2291,10 @@ CREATE TABLE `websiteFunctions_backupsv2` (`id` integer AUTO_INCREMENT NOT NULL os.remove(lscpdPath) try: - result = subprocess.run('uname -a', capture_output=True, universal_newlines=True, shell=True) + try: + result = subprocess.run('uname -a', capture_output=True, universal_newlines=True, shell=True) + except: + result = subprocess.run('uname -a', stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=True) if result.stdout.find('aarch64') == -1: lscpdSelection = 'lscpd-0.3.1' @@ -3625,7 +3628,12 @@ pm.max_spare_servers = 3 command = 'mount -o remount /' - mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + try: + mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + mResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, shell=True) + if mResult.returncode != 0: fstab_path = '/etc/fstab' backup_path = fstab_path + '.bak' @@ -3660,7 +3668,11 @@ pm.max_spare_servers = 3 return 0 command = 'mount -o remount /' - mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + try: + mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + mResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, shell=True) if mResult.returncode != 0: fstab_path = '/etc/fstab' backup_path = fstab_path + '.bak' @@ -3672,7 +3684,11 @@ pm.max_spare_servers = 3 return 0 command = 'quotacheck -ugm /' - mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + try: + mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + mResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, shell=True) if mResult.returncode != 0: fstab_path = '/etc/fstab' backup_path = fstab_path + '.bak' @@ -3686,19 +3702,31 @@ pm.max_spare_servers = 3 #### command = "find /lib/modules/ -type f -name '*quota_v*.ko*'" - iResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + try: + iResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + iResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + 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, universal_newlines=True, shell=True) + try: + result = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + 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, universal_newlines=True, shell=True) + try: + ffResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + ffResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, shell=True) ffResult = ffResult.stdout.rstrip('\n') command = f"apt-get install linux-modules-extra-{ffResult}" @@ -3707,7 +3735,11 @@ pm.max_spare_servers = 3 ### command = f'modprobe quota_v1 -S {ffResult}' - mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + try: + mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + mResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, shell=True) if mResult.returncode != 0: fstab_path = '/etc/fstab' backup_path = fstab_path + '.bak' @@ -3719,7 +3751,11 @@ pm.max_spare_servers = 3 return 0 command = f'modprobe quota_v2 -S {ffResult}' - mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + try: + mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + mResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, shell=True) if mResult.returncode != 0: fstab_path = '/etc/fstab' backup_path = fstab_path + '.bak' @@ -3731,7 +3767,11 @@ pm.max_spare_servers = 3 return 0 command = f'quotacheck -ugm /' - mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + try: + mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + mResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, shell=True) if mResult.returncode != 0: fstab_path = '/etc/fstab' backup_path = fstab_path + '.bak' @@ -3743,7 +3783,11 @@ pm.max_spare_servers = 3 return 0 command = f'quotaon -v /' - mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + try: + mResult = subprocess.run(command, capture_output=True, universal_newlines=True, shell=True) + except: + mResult = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, shell=True) if mResult.returncode != 0: fstab_path = '/etc/fstab' backup_path = fstab_path + '.bak'