Файловый менеджер - Редактировать - /opt/cpmigrate/environments/nginx.py
Ðазад
"""NGINX Environment module""" import os import re import subprocess from environments.base import Environment class NGINX(Environment): """ Checks the origin for NGINX and 8080/8443 ports. If both are found, the IMH ultrastack package will be installed on this server. """ def __init__(self): Environment.__init__(self) self.nginx_package = 'imh-nginx' self.ultrastack_package = 'imh-ultrastack-ded' self.origin_nginx_installed = False self.origin_nginx_ports = False self.installed_nginx = False self.post_install_job = 0 def check(self, _): self.info("Checking for NGINX installation and ports.") self.check_origin_nginx() self.check_origin_ports() if self.origin_nginx_installed and self.origin_nginx_ports: self.actions.append(f"+ Install {self.ultrastack_package}.") self.actions.append("+ Rebuild and restart Apache.") self.actions.append("+ Rebuild ngxconf and restart NGINX.") def run(self, _): if self.origin_nginx_installed and self.origin_nginx_ports: self.installed_nginx = True self.install_nginx() self.check_postinstall() self.check_services() self.rebuild_ngxconf() self.purge_cache() elif self.origin_nginx_ports and not self.origin_nginx_installed: self.warning( "Origin has 8080/8443 ports but no IMH-NGINX installed. " "Possible custom NGINX installation detected.", note=True, ) def check_origin_nginx(self): """Checks if our NGINX is installed on the remote server.""" ret_code, out = self.xfer.origin_command( f"/bin/rpm -qa {self.nginx_package}", command_out=subprocess.PIPE, sleep=2, quiet=True, ) if ret_code == 0: if len(out) > 0: if self.nginx_package in out[0]: self.origin_nginx_installed = True self.info("NGINX is installed on the remote server.") if not self.origin_nginx_installed: self.info("NGINX is not installed on the remote server.") else: self.error("Failed to get RPM status for NGINX.") def check_origin_ports(self): """Checks the current Apache ports on the origin server.""" resp_nonssl = self.xfer.origin_whmapi_call( 'get_tweaksetting', {'key': 'apache_port'} ) resp_ssl = self.xfer.origin_whmapi_call( 'get_tweaksetting', {'key': 'apache_ssl_port'} ) try: port_nonssl = str(resp_nonssl.get('tweaksetting').get('value')) port_ssl = str(resp_ssl.get('tweaksetting').get('value')) port_nonssl = port_nonssl.replace('0.0.0.0:', '') port_ssl = port_ssl.replace('0.0.0.0:', '') if port_nonssl == "8080" and port_ssl == "8443": self.origin_nginx_ports = True self.info("Detected NGINX-Apache ports on origin server.") elif port_nonssl == "80" and port_ssl == "443": self.info("Detected standard Apache ports.") else: self.warning( "Detected non-standard Apache ports: " f"{port_nonssl} and {port_ssl}" ) self.actions.append( "!! Non-standard Apache ports and no IMH-NGINX package. " "This could mean a custom NGINX installation." ) except KeyError: self.error("Failed to obtain Apache ports on origin server.") def install_nginx(self): """Installs NGINX on this server.""" log_path = os.path.join(self.xfer.log_path, 'nginx_install.log') self.info(f"Installing {self.ultrastack_package}, log: {log_path}") with open(log_path, 'w', encoding="utf-8") as out: ret_code, _ = self.xfer.local_command( ['/usr/bin/yum', 'install', '-y', self.ultrastack_package], command_out=out, ) with open(log_path, encoding="utf-8") as read: search = re.search(r"Use 'at -c (\d+)'", read.read()) if search: self.post_install_job = search[1] if ret_code == 0: self.info( "NGINX has been installed successfully. " "Waiting for post-install to complete." ) self.xfer.print_progress("Waiting.") else: self.error("NGINX has failed to install. Check logs.", note=True) def check_postinstall(self): """Checks to see if post-install job has completed.""" if self.post_install_job != 0: ret_code, _ = self.xfer.local_command( ['/usr/bin/at', '-c', self.post_install_job], sleep=1, quiet=True, ) if ret_code == 0: self.xfer.print_progress() self.check_postinstall() elif ret_code == 1: print() self.info("NGINX post-install has completed.") else: self.warning(f"Unexpected return for post-install: {ret_code}") def check_services(self): """Checks to ensure NGINX and Apache are running.""" self.info("Checking if Apache and NGINX are running.") httpd = self.xfer.service_status('httpd') nginx = self.xfer.service_status('nginx') if httpd == 0: self.info("Apache is currently running.") else: self.error("Apache is not running post-nginx install!", note=True) if nginx == 0: self.info("NGINX is currently running.") else: self.error("NGINX is not running post-nginx install!", note=True) def rebuild_apache(self): """Rebuilds the apache configuration""" self.info("Rebuilding httpd configuration.") ret_code, _ = self.xfer.local_command( ['/usr/local/cpanel/scripts/rebuildhttpdconf'], sleep=2 ) if ret_code == 0: self.info("Apache rebuilt successfully.") else: self.error("Apache rebuild failed. Please check manually.") self.xfer.notes.append( "Apache rebuild failed. Please check manually." ) def restart_apache(self): """Restarts Apache to ensure it still works""" self.info("Restarting Apache.") ret_code = self.xfer.restart_service('httpd') if ret_code == 0: self.info("Apache restarted successfully, no errors.") else: self.error("Apache failed to restart. Please check logs.") self.xfer.notes.append( "Apache failed to restart. Please check logs." ) def rebuild_ngxconf(self): """Rebuilds ngxconf to ensure VHosts are configured.""" self.info("Rebuilding ngxconf.") ret_code, _ = self.xfer.local_command(['/usr/bin/ngxconf', '-RrdF']) if ret_code == 0: self.info("Rebuilt ngxconf successfully.") else: self.error("Ngxconf rebuild has failed.") def restart_nginx(self): """Restarts NGINX to ensure it still works""" self.info("Restarting NGINX.") ret_code = self.xfer.restart_service('nginx') if ret_code == 0: self.info("NGINX restarted successfully, no errors.") else: self.error("NGINX failed to restart. Please check logs.") self.xfer.notes.append( "NGINX failed to restart. Please check logs." ) def purge_cache(self): """Purges NGINX cache for all users. This is to ensure any pre-rebuild issues are cleared from cache. Ran into issues while testing, which caused unnecessary failures when validating domains. """ self.info("Purging all NGINX cache.") ret_code, _ = self.xfer.local_command( ['/usr/bin/ngxutil', '--purgeall'] ) if ret_code == 0: self.info("Purged cache successfully.") else: self.error("Failed to purge cache.") def post_transfer(self): if self.installed_nginx: self.rebuild_apache() self.restart_apache() self.restart_nginx() self.rebuild_ngxconf() self.purge_cache() def capture_state(self): state = {'installed_nginx': self.installed_nginx} return super().capture_state(state) def load_state(self, loadstate): self.installed_nginx = loadstate.get('installed_nginx', False) super().load_state(loadstate)
| ver. 1.1 | |
.
| PHP 8.3.30 | Ð“ÐµÐ½ÐµÑ€Ð°Ñ†Ð¸Ñ Ñтраницы: 0 |
proxy
|
phpinfo
|
ÐаÑтройка