From 817506c5bf6ed8125437730c3aa992fa006dcbe7 Mon Sep 17 00:00:00 2001 From: usmannasir Date: Fri, 4 Jul 2025 17:26:26 +0500 Subject: [PATCH] refactor install.py --- install/install.py | 238 ++++++++++++++++++--------------------------- 1 file changed, 93 insertions(+), 145 deletions(-) diff --git a/install/install.py b/install/install.py index 90bf8fa65..13dc660ec 100644 --- a/install/install.py +++ b/install/install.py @@ -128,6 +128,49 @@ class preFlightsChecks: cyberPanelMirror = "mirror.cyberpanel.net/pip" cdn = 'cyberpanel.sh' SnappyVersion = '2.38.2' + + def install_package(self, package_name, options="", silent=False): + """Unified package installation across distributions""" + if self.distro == ubuntu: + command = f"DEBIAN_FRONTEND=noninteractive apt-get -y install {package_name} {options}" + shell = True + elif self.distro == centos: + command = f"yum install -y {package_name} {options}" + shell = False + else: # cent8, openeuler + command = f"dnf install -y {package_name} {options}" + shell = False + + if not silent: + return preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR, shell) + else: + return preFlightsChecks.call(command, self.distro, command, command, 0, 0, os.EX_OSERR, shell) + + def is_centos_family(self): + """Check if distro is CentOS, CentOS 8, or OpenEuler""" + return self.distro in [centos, cent8, openeuler] + + def manage_service(self, service_name, action="start"): + """Unified service management""" + command = f'systemctl {action} {service_name}' + return preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + + def remove_package(self, package_name, silent=False): + """Unified package removal across distributions""" + if self.distro == ubuntu: + command = f"DEBIAN_FRONTEND=noninteractive apt-get -y remove {package_name}" + shell = True + elif self.distro == centos: + command = f"yum remove -y {package_name}" + shell = False + else: # cent8, openeuler + command = f"dnf remove -y {package_name}" + shell = False + + if not silent: + return preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR, shell) + else: + return preFlightsChecks.call(command, self.distro, command, command, 0, 0, os.EX_OSERR, shell) def __init__(self, rootPath, ip, path, cwd, cyberPanelPath, distro, remotemysql=None, mysqlhost=None, mysqldb=None, mysqluser=None, mysqlpassword=None, mysqlport=None): @@ -148,11 +191,8 @@ class preFlightsChecks: def installQuota(self,): try: - if self.distro == centos or self.distro == cent8 or self.distro == openeuler: - command = "yum install quota -y" - preFlightsChecks.call(command, self.distro, command, - command, - 1, 0, os.EX_OSERR) + if self.is_centos_family(): + self.install_package("quota", silent=True) if self.edit_fstab('/', '/') == 0: preFlightsChecks.stdOut("Quotas will not be abled as we failed to modify fstab file.") @@ -187,15 +227,13 @@ class preFlightsChecks: command = 'apt update -y' preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) - command = 'DEBIAN_FRONTEND=noninteractive apt install quota -y' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR, True) + self.install_package("quota", silent=True) command = "find /lib/modules/ -type f -name '*quota_v*.ko*'" if subprocess.check_output(command,shell=True).decode("utf-8").find("quota/") == -1: - command = "DEBIAN_FRONTEND=noninteractive apt install linux-image-extra-virtual -y" - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR, True) + self.install_package("linux-image-extra-virtual", silent=True) if self.edit_fstab('/', '/') == 0: preFlightsChecks.stdOut("Quotas will not be abled as we are are failed to modify fstab file.") @@ -357,10 +395,7 @@ class preFlightsChecks: if result.stdout.find('openvz') > -1: if self.distro == ubuntu: - command = 'DEBIAN_FRONTEND=noninteractive apt install inetutils-inetd -y' - preFlightsChecks.call(command, self.distro, command, - command, - 1, 0, os.EX_OSERR) + self.install_package("inetutils-inetd") # ## On OpenVZ there is an issue using .tempdisk for /tmp as it breaks network on container after reboot. # @@ -505,11 +540,8 @@ class preFlightsChecks: def setup_account_cyberpanel(self): try: - if self.distro == centos or self.distro == cent8 or self.distro == openeuler: - command = "yum install sudo -y" - preFlightsChecks.call(command, self.distro, command, - command, - 1, 0, os.EX_OSERR) + if self.is_centos_family(): + self.install_package("sudo", silent=True) ## @@ -595,13 +627,7 @@ class preFlightsChecks: def install_psmisc(self): self.stdOut("Install psmisc") - - if self.distro == centos or self.distro == cent8 or self.distro == openeuler: - command = "yum -y install psmisc" - else: - command = "DEBIAN_FRONTEND=noninteractive apt-get -y install psmisc" - - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR, True) + self.install_package("psmisc") def download_install_CyberPanel(self, mysqlPassword, mysql): ## @@ -879,7 +905,7 @@ password="%s" command = "find /usr/local/CyberCP/ -name '*.pyc' -delete" preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) - if self.distro == cent8 or self.distro == centos or self.distro == openeuler: + if self.is_centos_family(): command = 'chown root:pdns /etc/pdns/pdns.conf' preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) @@ -934,24 +960,14 @@ password="%s" def install_unzip(self): self.stdOut("Install unzip") try: - if self.distro == centos or self.distro == cent8 or self.distro == openeuler: - command = 'yum -y install unzip' - else: - command = 'DEBIAN_FRONTEND=noninteractive apt-get -y install unzip' - - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR, True) + self.install_package("unzip") except BaseException as msg: logging.InstallLog.writeToFile('[ERROR] ' + str(msg) + " [install_unzip]") def install_zip(self): self.stdOut("Install zip") try: - if self.distro == centos or self.distro == cent8 or self.distro == openeuler: - command = 'yum -y install zip' - else: - command = 'DEBIAN_FRONTEND=noninteractive apt-get -y install zip' - - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR, True) + self.install_package("zip") except BaseException as msg: logging.InstallLog.writeToFile('[ERROR] ' + str(msg) + " [install_zip]") @@ -1044,11 +1060,9 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout'; try: if self.distro == centos: - command = 'yum remove postfix -y' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + self.remove_package("postfix") elif self.distro == ubuntu: - command = 'DEBIAN_FRONTEND=noninteractive apt-get -y remove postfix' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR, True) + self.remove_package("postfix") self.stdOut("Install dovecot - do the install") @@ -1075,8 +1089,7 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout'; preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR) else: - command = 'DEBIAN_FRONTEND=noninteractive apt-get -y install debconf-utils' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR, True) + self.install_package("debconf-utils", silent=True) file_name = self.cwd + '/pf.unattend.text' pf = open(file_name, 'w') pf.write('postfix postfix/mailname string ' + str(socket.getfqdn() + '\n')) @@ -1413,13 +1426,8 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout'; ################################### Restart postix - command = 'systemctl enable postfix.service' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) - - ## - - command = 'systemctl start postfix.service' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + self.manage_service('postfix', 'enable') + self.manage_service('postfix', 'start') ######################################## Permissions @@ -1433,18 +1441,12 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout'; ################################### Restart dovecot - command = 'systemctl enable dovecot.service' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + self.manage_service('dovecot', 'enable') + self.manage_service('dovecot', 'start') ## - command = 'systemctl start dovecot.service' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) - - ## - - command = 'systemctl restart postfix.service' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + self.manage_service('postfix', 'restart') ## chaging permissions for main.cf @@ -1479,8 +1481,7 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout'; writeToFile.writelines(items) writeToFile.close() - command = "systemctl restart dovecot" - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + self.manage_service('dovecot', 'restart') logging.InstallLog.writeToFile("Postfix and Dovecot configured") except BaseException as msg: @@ -1695,29 +1696,17 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout'; try: preFlightsChecks.stdOut("Enabling Firewall!") - if self.distro == ubuntu: - command = 'DEBIAN_FRONTEND=noninteractive apt-get -y install firewalld' - else: - command = 'yum -y install firewalld' - - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR, True) + self.install_package("firewalld") ###### if self.distro == centos: # Not available in ubuntu - command = 'systemctl restart dbus' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + self.manage_service('dbus', 'restart') - command = 'systemctl restart systemd-logind' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + self.manage_service('systemd-logind', 'restart') - command = 'systemctl start firewalld' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) - - ########## - - command = 'systemctl enable firewalld' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + self.manage_service('firewalld', 'start') + self.manage_service('firewalld', 'enable') FirewallUtilities.addRule("tcp", "8090") FirewallUtilities.addRule("tcp", "7080") @@ -1768,19 +1757,14 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout'; os.chdir(self.cwd) if self.distro == ubuntu: - command = "DEBIAN_FRONTEND=noninteractive apt-get -y install gcc g++ make autoconf rcs" + self.install_package("gcc g++ make autoconf rcs") else: - command = 'yum -y install gcc gcc-c++ make autoconf glibc' - - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR, True) + self.install_package("gcc gcc-c++ make autoconf glibc") if self.distro == ubuntu: - command = "DEBIAN_FRONTEND=noninteractive apt-get -y install libpcre3 libpcre3-dev openssl libexpat1 libexpat1-dev libgeoip-dev" \ - " zlib1g zlib1g-dev libudns-dev whichman curl" + self.install_package("libpcre3 libpcre3-dev openssl libexpat1 libexpat1-dev libgeoip-dev zlib1g zlib1g-dev libudns-dev whichman curl") else: - command = 'yum -y install pcre-devel openssl-devel expat-devel geoip-devel zlib-devel udns-devel' - - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR, True) + self.install_package("pcre-devel openssl-devel expat-devel geoip-devel zlib-devel udns-devel") command = 'tar zxf lscp.tar.gz -C /usr/local/' preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR) @@ -1847,14 +1831,14 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout'; except: pass - if self.distro == centos or self.distro == cent8 or self.distro == openeuler: + if self.is_centos_family(): command = 'adduser lscpd -M -d /usr/local/lscp' else: command = 'useradd lscpd -M -d /usr/local/lscp' preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) - if self.distro == centos or self.distro == cent8 or self.distro == openeuler: + if self.is_centos_family(): command = 'groupadd lscpd' preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) # Added group in useradd for Ubuntu @@ -2044,8 +2028,7 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout'; preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) ## - command = 'systemctl enable lscpd.service' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + self.manage_service('lscpd', 'enable') ## count = 0 @@ -2075,26 +2058,17 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout'; try: ## first install crontab - if self.distro == centos or self.distro == cent8 or self.distro == openeuler: - command = 'yum install cronie -y' + if self.is_centos_family(): + self.install_package('cronie') else: - command = 'DEBIAN_FRONTEND=noninteractive apt-get -y install cron' + self.install_package('cron') - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR, True) - - if self.distro == centos or self.distro == cent8 or self.distro == openeuler: - command = 'systemctl enable crond' + if self.is_centos_family(): + self.manage_service('crond', 'enable') + self.manage_service('crond', 'start') else: - command = 'systemctl enable cron' - - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) - - if self.distro == centos or self.distro == cent8 or self.distro == openeuler: - command = 'systemctl start crond' - else: - command = 'systemctl start cron' - - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + self.manage_service('cron', 'enable') + self.manage_service('cron', 'start') ## @@ -2152,12 +2126,10 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout'; command = 'chmod 600 %s' % (cronPath) preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) - if self.distro == centos or self.distro == cent8 or self.distro == openeuler: - command = 'systemctl restart crond.service' + if self.is_centos_family(): + self.manage_service('crond', 'restart') else: - command = 'systemctl restart cron.service' - - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + self.manage_service('cron', 'restart') except BaseException as msg: logging.InstallLog.writeToFile('[ERROR] ' + str(msg) + " [setup_cron]") @@ -2179,12 +2151,7 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout'; def install_rsync(self): try: - if self.distro == centos or self.distro == cent8 or self.distro == openeuler: - command = 'yum -y install rsync' - else: - command = 'DEBIAN_FRONTEND=noninteractive apt-get -y install rsync' - - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR, True) + self.install_package('rsync') except BaseException as msg: logging.InstallLog.writeToFile('[ERROR] ' + str(msg) + " [install_rsync]") @@ -2235,22 +2202,10 @@ $cfg['Servers'][$i]['LogoutURL'] = 'phpmyadminsignin.php?logout'; def installOpenDKIM(self): try: - if self.distro == centos: - command = 'yum -y install opendkim' - elif self.distro == cent8 or self.distro == openeuler: - command = 'dnf install opendkim -y' - else: - command = 'DEBIAN_FRONTEND=noninteractive apt-get -y install opendkim' + self.install_package('opendkim') - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR, True) - - if self.distro == cent8 or self.distro == openeuler: - command = 'dnf install opendkim-tools -y' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) - - if self.distro == ubuntu: - command = 'apt install opendkim-tools -y' - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + if self.distro == cent8 or self.distro == openeuler or self.distro == ubuntu: + self.install_package('opendkim-tools') command = 'mkdir -p /etc/opendkim/keys/' preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) @@ -2307,16 +2262,9 @@ milter_default_action = accept #### Restarting Postfix and OpenDKIM - command = "systemctl start opendkim" - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) - - command = "systemctl enable opendkim" - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) - - ## - - command = "systemctl start postfix" - preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + self.manage_service('opendkim', 'start') + self.manage_service('opendkim', 'enable') + self.manage_service('postfix', 'start') except BaseException as msg: logging.InstallLog.writeToFile('[ERROR] ' + str(msg) + " [configureOpenDKIM]")