From 4b71b4e679a645c9295eb8ce33f0166753394ba8 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Sat, 5 Aug 2023 12:07:10 +0200 Subject: [PATCH 1/9] Refactor site detection --- wo/cli/plugins/site_functions.py | 273 +++---------------------------- 1 file changed, 22 insertions(+), 251 deletions(-) diff --git a/wo/cli/plugins/site_functions.py b/wo/cli/plugins/site_functions.py index f8809c0..a3914a5 100644 --- a/wo/cli/plugins/site_functions.py +++ b/wo/cli/plugins/site_functions.py @@ -1137,268 +1137,39 @@ def detSitePar(opts): 2. raises RuntimeError when wrong combination is used like "--wp --wpsubdir" or "--html --wp" """ + # Initialize sitetype and cachetype sitetype, cachetype = '', '' + + # Initialize type and cache lists typelist = list() cachelist = list() + + # Populate type and cache lists based on opts for key, val in opts.items(): if val and key in ['html', 'php', 'mysql', 'wp', - 'wpsubdir', 'wpsubdomain', 'php72', - 'php73', 'php74', 'php80', 'php81', 'php82', ]: + 'wpsubdir', 'wpsubdomain', + 'php72', 'php73', 'php74', + 'php80', 'php81', 'php82']: typelist.append(key) elif val and key in ['wpfc', 'wpsc', 'wpredis', 'wprocket', 'wpce']: cachelist.append(key) + # Determine sitetype and cachetype if len(typelist) > 1 or len(cachelist) > 1: - if len(cachelist) > 1: - raise RuntimeError( - "Could not determine cache type." - "Multiple cache parameter entered") - elif False not in [x in ('php', 'mysql', 'html') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('php72', 'mysql', 'html') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('php73', 'mysql', 'html') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('php74', 'mysql', 'html') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('php80', 'mysql', 'html') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('php81', 'mysql', 'html') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('php82', 'mysql', 'html') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('php', 'mysql') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('php72', 'mysql') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('php73', 'mysql') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('php74', 'mysql') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('php80', 'mysql') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('php81', 'mysql') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('php82', 'mysql') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('html', 'mysql') for x in typelist]: - sitetype = 'mysql' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('php', 'html') for x in typelist]: - sitetype = 'php' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wp', 'wpsubdir') for x in typelist]: - sitetype = 'wpsubdir' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wp', 'wpsubdomain') for x in typelist]: - sitetype = 'wpsubdomain' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wp', 'php72') for x in typelist]: - sitetype = 'wp' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wp', 'php73') for x in typelist]: - sitetype = 'wp' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wp', 'php74') for x in typelist]: - sitetype = 'wp' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wp', 'php80') for x in typelist]: - sitetype = 'wp' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wp', 'php81') for x in typelist]: - sitetype = 'wp' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wp', 'php82') for x in typelist]: - sitetype = 'wp' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wpsubdir', 'php72') for x in typelist]: - sitetype = 'wpsubdir' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wpsubdir', 'php73') for x in typelist]: - sitetype = 'wpsubdir' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wpsubdir', 'php74') for x in typelist]: - sitetype = 'wpsubdir' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wpsubdir', 'php80') for x in typelist]: - sitetype = 'wpsubdir' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wpsubdir', 'php81') for x in typelist]: - sitetype = 'wpsubdir' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wpsubdir', 'php82') for x in typelist]: - sitetype = 'wpsubdir' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wpsubdomain', 'php72') for x in typelist]: - sitetype = 'wpsubdomain' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wpsubdomain', 'php73') for x in typelist]: - sitetype = 'wpsubdomain' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wpsubdomain', 'php74') for x in typelist]: - sitetype = 'wpsubdomain' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wpsubdomain', 'php80') for x in typelist]: - sitetype = 'wpsubdomain' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wpsubdomain', 'php81') for x in typelist]: - sitetype = 'wpsubdomain' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - elif False not in [x in ('wpsubdomain', 'php82') for x in typelist]: - sitetype = 'wpsubdomain' - if not cachelist: - cachetype = 'basic' - else: - cachetype = cachelist[0] - else: - raise RuntimeError("could not determine site and cache type") + raise RuntimeError( + "Could not determine site or cache type. " + "Multiple types or caches entered.") + + # If no type or cache specified, set to None + if not typelist and not cachelist: + sitetype = None + cachetype = None else: - if not typelist and not cachelist: - sitetype = None - cachetype = None - elif (not typelist or "php72" in typelist) and cachelist: - sitetype = 'wp' - cachetype = cachelist[0] - elif (not typelist or "php73" in typelist) and cachelist: - sitetype = 'wp' - cachetype = cachelist[0] - elif (not typelist or "php74" in typelist) and cachelist: - sitetype = 'wp' - cachetype = cachelist[0] - elif (not typelist or "php80" in typelist) and cachelist: - sitetype = 'wp' - cachetype = cachelist[0] - elif (not typelist or "php81" in typelist) and cachelist: - sitetype = 'wp' - cachetype = cachelist[0] - elif (not typelist or "php82" in typelist) and cachelist: - sitetype = 'wp' - cachetype = cachelist[0] - elif typelist and (not cachelist): - sitetype = typelist[0] - cachetype = 'basic' - else: - sitetype = typelist[0] - cachetype = cachelist[0] + # Use the first specified type or 'wp' if not specified + sitetype = typelist[0] if typelist else 'wp' + + # Use the first specified cache or 'basic' if not specified + cachetype = cachelist[0] if cachelist else 'basic' return (sitetype, cachetype) From a93ec45ce48df3c21bad905e3a554baba4cd8ade Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Sat, 5 Aug 2023 14:57:04 +0200 Subject: [PATCH 2/9] Refactorise package detection --- wo/cli/plugins/site_functions.py | 110 ++++++++----------------------- 1 file changed, 29 insertions(+), 81 deletions(-) diff --git a/wo/cli/plugins/site_functions.py b/wo/cli/plugins/site_functions.py index a3914a5..a0a55ec 100644 --- a/wo/cli/plugins/site_functions.py +++ b/wo/cli/plugins/site_functions.py @@ -872,95 +872,43 @@ def site_package_check(self, stype): wo_nginx.write('fastcgi_param \tSCRIPT_FILENAME ' '\t$request_filename;\n') - if ((pargs.php and pargs.php73) or (pargs.php and pargs.php74) or - (pargs.php and pargs.php72) or (pargs.php and pargs.php80) or - (pargs.php and pargs.php81) or (pargs.php and pargs.php82) or - (pargs.php72 and pargs.php73) or (pargs.php72 and pargs.php74) or - (pargs.php72 and pargs.php80) or (pargs.php72 and pargs.php81) or - (pargs.php72 and pargs.php82) or - (pargs.php73 and pargs.php74) or (pargs.php73 and pargs.php80) or - (pargs.php73 and pargs.php81) or (pargs.php73 and pargs.php82) or - (pargs.php74 and pargs.php80) or (pargs.php74 and pargs.php81) or - (pargs.php74 and pargs.php82) or - (pargs.php80 and pargs.php81) or (pargs.php80 and pargs.php82) or - (pargs.php81 and pargs.php82)): - Log.error( - self, "Error: two different PHP versions cannot be " - "combined within the same WordOps site") + php_versions = ['php72', 'php73', 'php74', 'php80', 'php81', 'php82'] + + selected_versions = [version for version in php_versions if getattr(pargs, version)] + if len(selected_versions) > 1: + Log.error(self, "Error: two different PHP versions cannot be " + "combined within the same WordOps site") + + php_versions = { + 'php72': '7.2', + 'php73': '7.3', + 'php74': '7.4', + 'php80': '8.0', + 'php81': '8.1', + 'php82': '8.2', + } if ((not pargs.php72) and (not pargs.php73) and (not pargs.php74) and (not pargs.php80) and (not pargs.php81) and (not pargs.php82) and stype in ['php', 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']): Log.debug(self, "Setting apt_packages variable for PHP") - php_check = 'php8.0-fpm' - php_to_setup = WOVar.wo_php80 - if self.app.config.has_section('php'): - config_php_ver = self.app.config.get( - 'php', 'version') - if config_php_ver == '7.2': - php_check = 'php7.2-fpm' - php_to_setup = WOVar.wo_php72 - elif config_php_ver == '7.3': - php_check = 'php7.3-fpm' - php_to_setup = WOVar.wo_php73 - elif config_php_ver == '7.4': - php_check = 'php7.4-fpm' - php_to_setup = WOVar.wo_php74 - elif config_php_ver == '8.0': - php_check = 'php8.0-fpm' - php_to_setup = WOVar.wo_php80 - elif config_php_ver == '8.1': - php_check = 'php8.1-fpm' - php_to_setup = WOVar.wo_php81 - elif config_php_ver == '8.2': - php_check = 'php8.2-fpm' - php_to_setup = WOVar.wo_php82 - else: - php_check = 'php8.1-fpm' - php_to_setup = WOVar.wo_php81 - else: - php_check = 'php8.1-fpm' - php_to_setup = WOVar.wo_php81 - if not (WOAptGet.is_installed(self, php_check)): - apt_packages = apt_packages + php_to_setup + WOVar.wo_php_extra + for version_key, version_number in php_versions.items(): + if (self.app.config.has_section('php') and + self.app.config.get('php', 'version') == version_number): + Log.debug( + self, + f"Setting apt_packages variable for PHP {version_number}") + if not WOAptGet.is_installed(self, f'php{version_number}-fpm'): + apt_packages += getattr( + WOVar, f'wo_{version_key}') + WOVar.wo_php_extra - if pargs.php72 and stype in ['php72', 'mysql', 'wp', - 'wpsubdir', 'wpsubdomain']: - Log.debug(self, "Setting apt_packages variable for PHP 7.2") - if not WOAptGet.is_installed(self, 'php7.2-fpm'): - apt_packages = apt_packages + WOVar.wo_php72 + WOVar.wo_php_extra - - if pargs.php73 and stype in ['php73', 'mysql', 'wp', - 'wpsubdir', 'wpsubdomain']: - Log.debug(self, "Setting apt_packages variable for PHP 7.3") - if not WOAptGet.is_installed(self, 'php7.3-fpm'): - apt_packages = apt_packages + WOVar.wo_php73 + WOVar.wo_php_extra - - if pargs.php74 and stype in ['php74', 'mysql', 'wp', - 'wpsubdir', 'wpsubdomain']: - Log.debug(self, "Setting apt_packages variable for PHP 7.4") - if not WOAptGet.is_installed(self, 'php7.4-fpm'): - apt_packages = apt_packages + WOVar.wo_php74 + WOVar.wo_php_extra - - if pargs.php80 and stype in ['php80', 'mysql', 'wp', - 'wpsubdir', 'wpsubdomain']: - Log.debug(self, "Setting apt_packages variable for PHP 8.0") - if not WOAptGet.is_installed(self, 'php8.0-fpm'): - apt_packages = apt_packages + WOVar.wo_php80 + WOVar.wo_php_extra - - if pargs.php81 and stype in ['php81', 'mysql', 'wp', - 'wpsubdir', 'wpsubdomain']: - Log.debug(self, "Setting apt_packages variable for PHP 8.1") - if not WOAptGet.is_installed(self, 'php8.1-fpm'): - apt_packages = apt_packages + WOVar.wo_php81 + WOVar.wo_php_extra - - if pargs.php82 and stype in ['php82', 'mysql', 'wp', - 'wpsubdir', 'wpsubdomain']: - Log.debug(self, "Setting apt_packages variable for PHP 8.2") - if not WOAptGet.is_installed(self, 'php8.2-fpm'): - apt_packages = apt_packages + WOVar.wo_php82 + WOVar.wo_php_extra + for version_key, version_number in php_versions.items(): + if getattr(self.pargs, version_key) and stype in [version_key, 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']: + Log.debug(self, f"Setting apt_packages variable for PHP {version_number}") + if not WOAptGet.is_installed(self, f'php{version_number}-fpm'): + apt_packages += getattr(WOVar, f'wo_{version_key}') + WOVar.wo_php_extra if stype in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']: Log.debug(self, "Setting apt_packages variable for MySQL") From c46e5d3b111ed43f374be5984526e99876618f89 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Sat, 5 Aug 2023 15:09:51 +0200 Subject: [PATCH 3/9] Fix argument --- wo/cli/plugins/site_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wo/cli/plugins/site_functions.py b/wo/cli/plugins/site_functions.py index a0a55ec..888e190 100644 --- a/wo/cli/plugins/site_functions.py +++ b/wo/cli/plugins/site_functions.py @@ -905,7 +905,7 @@ def site_package_check(self, stype): WOVar, f'wo_{version_key}') + WOVar.wo_php_extra for version_key, version_number in php_versions.items(): - if getattr(self.pargs, version_key) and stype in [version_key, 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']: + if getattr(pargs, version_key) and stype in [version_key, 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']: Log.debug(self, f"Setting apt_packages variable for PHP {version_number}") if not WOAptGet.is_installed(self, f'php{version_number}-fpm'): apt_packages += getattr(WOVar, f'wo_{version_key}') + WOVar.wo_php_extra From f9b3f96e148748cbbb8e1add98b51b8777b570f5 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Sat, 5 Aug 2023 15:30:45 +0200 Subject: [PATCH 4/9] Cleanup php stack removal --- wo/cli/plugins/stack.py | 117 +++++++++++----------------------------- 1 file changed, 31 insertions(+), 86 deletions(-) diff --git a/wo/cli/plugins/stack.py b/wo/cli/plugins/stack.py index 4821e22..92c00d0 100644 --- a/wo/cli/plugins/stack.py +++ b/wo/cli/plugins/stack.py @@ -669,97 +669,42 @@ class WOStackController(CementBaseController): Log.debug(self, "Removing apt_packages variable of Nginx") apt_packages = apt_packages + WOVar.wo_nginx - # PHP 7.2 - if pargs.php72: - Log.debug(self, "Setting apt_packages variable for PHP 7.2") - if (WOAptGet.is_installed(self, 'php7.2-fpm')): - apt_packages = apt_packages + WOVar.wo_php72 - if not (WOAptGet.is_installed(self, 'php7.3-fpm') or - WOAptGet.is_installed(self, 'php7.4-fpm') or - WOAptGet.is_installed(self, 'php8.0-fpm') or - WOAptGet.is_installed(self, 'php8.1-fpm') or - WOAptGet.is_installed(self, 'php8.2-fpm')): - apt_packages = apt_packages + WOVar.wo_php_extra - else: - Log.debug(self, "PHP 7.2 is not installed") - Log.info(self, "PHP 7.2 is not installed") + # Create a dictionary that maps PHP versions to corresponding variables. + php_versions = { + 'php72': '7.2', + 'php73': '7.3', + 'php74': '7.4', + 'php80': '8.0', + 'php81': '8.1', + 'php82': '8.2', + } - # PHP 7.3 - if pargs.php73: - Log.debug(self, "Setting apt_packages variable for PHP 7.3") - if WOAptGet.is_installed(self, 'php7.3-fpm'): - apt_packages = apt_packages + WOVar.wo_php73 - if not (WOAptGet.is_installed(self, 'php7.2-fpm') or - WOAptGet.is_installed(self, 'php7.4-fpm') or - WOAptGet.is_installed(self, 'php8.0-fpm') or - WOAptGet.is_installed(self, 'php8.1-fpm') or - WOAptGet.is_installed(self, 'php8.2-fpm')): - apt_packages = apt_packages + WOVar.wo_php_extra - else: - Log.debug(self, "PHP 7.3 is not installed") - Log.info(self, "PHP 7.3 is not installed") + wo_vars = { + 'php72': WOVar.wo_php72, + 'php73': WOVar.wo_php73, + 'php74': WOVar.wo_php74, + 'php80': WOVar.wo_php80, + 'php81': WOVar.wo_php81, + 'php82': WOVar.wo_php82, + } - # PHP 7.4 - if pargs.php74: - Log.debug(self, "Setting apt_packages variable for PHP 7.4") - if WOAptGet.is_installed(self, 'php7.4-fpm'): - apt_packages = apt_packages + WOVar.wo_php74 - if not (WOAptGet.is_installed(self, 'php7.3-fpm') or - WOAptGet.is_installed(self, 'php7.2-fpm') or - WOAptGet.is_installed(self, 'php8.0-fpm') or - WOAptGet.is_installed(self, 'php8.1-fpm') or - WOAptGet.is_installed(self, 'php8.2-fpm')): - apt_packages = apt_packages + WOVar.wo_php_extra - else: - Log.debug(self, "PHP 7.4 is not installed") - Log.info(self, "PHP 7.4 is not installed") + # Loop through all versions. + for parg_version, version in php_versions.items(): + # Check if this version is present in pargs. + if getattr(pargs, parg_version): + Log.debug(self, f"Setting apt_packages variable for PHP {version}") - # PHP 8.0 - if pargs.php80: - Log.debug(self, "Setting apt_packages variable for PHP 8.0") - if WOAptGet.is_installed(self, 'php8.0-fpm'): - apt_packages = apt_packages + WOVar.wo_php80 - if not (WOAptGet.is_installed(self, 'php7.3-fpm') or - WOAptGet.is_installed(self, 'php7.2-fpm') or - WOAptGet.is_installed(self, 'php7.4-fpm') or - WOAptGet.is_installed(self, 'php8.1-fpm') or - WOAptGet.is_installed(self, 'php8.2-fpm')): - apt_packages = apt_packages + WOVar.wo_php_extra - else: - Log.debug(self, "PHP 8.0 is not installed") - Log.info(self, "PHP 8.0 is not installed") + if WOAptGet.is_installed(self, f'php{version}-fpm'): + apt_packages += wo_vars[parg_version] - # PHP 8.1 - if pargs.php81: - Log.debug(self, "Setting apt_packages variable for PHP 8.1") - if WOAptGet.is_installed(self, 'php8.1-fpm'): - apt_packages = apt_packages + WOVar.wo_php81 - if not (WOAptGet.is_installed(self, 'php7.3-fpm') or - WOAptGet.is_installed(self, 'php7.2-fpm') or - WOAptGet.is_installed(self, 'php7.4-fpm') or - WOAptGet.is_installed(self, 'php8.0-fpm') or - WOAptGet.is_installed(self, 'php8.1-fpm') or - WOAptGet.is_installed(self, 'php8.2-fpm')): - apt_packages = apt_packages + WOVar.wo_php_extra - else: - Log.debug(self, "PHP 8.1 is not installed") - Log.info(self, "PHP 8.1 is not installed") + # Check if other versions are installed. + if not any(WOAptGet.is_installed(self, f'php{other_version}-fpm') for + other_version in php_versions.values() if other_version != version): + apt_packages += WOVar.wo_php_extra - # PHP 8.2 - if pargs.php82: - Log.debug(self, "Setting apt_packages variable for PHP 8.2") - if WOAptGet.is_installed(self, 'php8.2-fpm'): - apt_packages = apt_packages + WOVar.wo_php82 - if not (WOAptGet.is_installed(self, 'php7.3-fpm') or - WOAptGet.is_installed(self, 'php7.2-fpm') or - WOAptGet.is_installed(self, 'php7.4-fpm') or - WOAptGet.is_installed(self, 'php8.0-fpm') or - WOAptGet.is_installed(self, 'php8.1-fpm') or - WOAptGet.is_installed(self, 'php8.2-fpm')): - apt_packages = apt_packages + WOVar.wo_php_extra - else: - Log.debug(self, "PHP 8.2 is not installed") - Log.info(self, "PHP 8.2 is not installed") + else: + Log.debug(self, f"PHP {version} is not installed") + Log.info(self, f"PHP {version} is not installed") # REDIS if pargs.redis: From ce961e90a8fc4260f1190fdb0e397bb59742da17 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Sat, 5 Aug 2023 15:47:01 +0200 Subject: [PATCH 5/9] Refactor php versions management --- setup.cfg | 1 + wo/cli/plugins/site_functions.py | 13 ++-------- wo/cli/plugins/stack.py | 13 ++-------- wo/core/stackconf.py | 2 +- wo/core/variables.py | 43 ++++++++++++++++---------------- 5 files changed, 28 insertions(+), 44 deletions(-) diff --git a/setup.cfg b/setup.cfg index 242fed0..7d616e1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -16,6 +16,7 @@ license-file = LICENSE [flake8] ignore = F405,W504,S322,S404,S603,s607,s602,C901 +max-line-length = 120 exclude = # No need to traverse our git directory .git, diff --git a/wo/cli/plugins/site_functions.py b/wo/cli/plugins/site_functions.py index 888e190..5ef6b48 100644 --- a/wo/cli/plugins/site_functions.py +++ b/wo/cli/plugins/site_functions.py @@ -879,22 +879,13 @@ def site_package_check(self, stype): Log.error(self, "Error: two different PHP versions cannot be " "combined within the same WordOps site") - php_versions = { - 'php72': '7.2', - 'php73': '7.3', - 'php74': '7.4', - 'php80': '8.0', - 'php81': '8.1', - 'php82': '8.2', - } - if ((not pargs.php72) and (not pargs.php73) and (not pargs.php74) and (not pargs.php80) and (not pargs.php81) and (not pargs.php82) and stype in ['php', 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']): Log.debug(self, "Setting apt_packages variable for PHP") - for version_key, version_number in php_versions.items(): + for version_key, version_number in WOVar.wo_php_versions.items(): if (self.app.config.has_section('php') and self.app.config.get('php', 'version') == version_number): Log.debug( @@ -904,7 +895,7 @@ def site_package_check(self, stype): apt_packages += getattr( WOVar, f'wo_{version_key}') + WOVar.wo_php_extra - for version_key, version_number in php_versions.items(): + for version_key, version_number in WOVar.wo_php_versions.items(): if getattr(pargs, version_key) and stype in [version_key, 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']: Log.debug(self, f"Setting apt_packages variable for PHP {version_number}") if not WOAptGet.is_installed(self, f'php{version_number}-fpm'): diff --git a/wo/cli/plugins/stack.py b/wo/cli/plugins/stack.py index 92c00d0..a509b8c 100644 --- a/wo/cli/plugins/stack.py +++ b/wo/cli/plugins/stack.py @@ -670,15 +670,6 @@ class WOStackController(CementBaseController): apt_packages = apt_packages + WOVar.wo_nginx # Create a dictionary that maps PHP versions to corresponding variables. - php_versions = { - 'php72': '7.2', - 'php73': '7.3', - 'php74': '7.4', - 'php80': '8.0', - 'php81': '8.1', - 'php82': '8.2', - } - wo_vars = { 'php72': WOVar.wo_php72, 'php73': WOVar.wo_php73, @@ -689,7 +680,7 @@ class WOStackController(CementBaseController): } # Loop through all versions. - for parg_version, version in php_versions.items(): + for parg_version, version in WOVar.wo_php_versions.items(): # Check if this version is present in pargs. if getattr(pargs, parg_version): Log.debug(self, f"Setting apt_packages variable for PHP {version}") @@ -699,7 +690,7 @@ class WOStackController(CementBaseController): # Check if other versions are installed. if not any(WOAptGet.is_installed(self, f'php{other_version}-fpm') for - other_version in php_versions.values() if other_version != version): + other_version in WOVar.wo_php_versions.values() if other_version != version): apt_packages += WOVar.wo_php_extra else: diff --git a/wo/core/stackconf.py b/wo/core/stackconf.py index d065bf9..e66fea9 100644 --- a/wo/core/stackconf.py +++ b/wo/core/stackconf.py @@ -12,7 +12,7 @@ class WOConf(): def nginxcommon(self): """nginx common configuration deployment""" - wo_php_version = ["php72", "php73", "php74", "php80", "php81", "php82"] + wo_php_version = list(WOVar.wo_php_versions.keys()) ngxcom = '/etc/nginx/common' if not os.path.exists(ngxcom): os.mkdir(ngxcom) diff --git a/wo/core/variables.py b/wo/core/variables.py index 6659de9..d6eda65 100644 --- a/wo/core/variables.py +++ b/wo/core/variables.py @@ -150,31 +150,32 @@ class WOVar(): wo_nginx = ["nginx-custom", "nginx-wo"] wo_nginx_key = 'FB898660' + wo_php_versions = { + 'php72': '7.2', + 'php73': '7.3', + 'php74': '7.4', + 'php80': '8.0', + 'php81': '8.1', + 'php82': '8.2', + } + wo_module = ["bcmath", "cli", "common", "curl", "fpm", "gd", "igbinary", "imagick", "imap", "intl", "mbstring", "memcached", "msgpack", "mysql", "opcache", "readline", "redis", "soap", "xdebug", "xml", "zip"] - wo_php72 = [] - for module in wo_module: - wo_php72 = wo_php72 + ["php7.2-{0}".format(module)] - wo_php72 = wo_php72 + ["php7.2-recode"] - wo_php73 = [] - for module in wo_module: - wo_php73 = wo_php73 + ["php7.3-{0}".format(module)] - wo_php73 = wo_php73 + ["php7.3-recode"] - wo_php74 = [] - for module in wo_module: - wo_php74 = wo_php74 + ["php7.4-{0}".format(module)] - wo_php74 = wo_php74 + ["php7.4-geoip", "php7.4-json"] - wo_php80 = [] - for module in wo_module: - wo_php80 = wo_php80 + ["php8.0-{0}".format(module)] - wo_php81 = [] - for module in wo_module: - wo_php81 = wo_php81 + ["php8.1-{0}".format(module)] - wo_php82 = [] - for module in wo_module: - wo_php82 = wo_php82 + ["php8.2-{0}".format(module)] + + wo_php_modules = {} + + for version_key, version_number in wo_php_versions.items(): + wo_php_modules[version_key] = [] + for module in wo_module: + wo_php_modules[version_key].append("{0}-{1}".format(version_key, module)) + + # Add version-specific modules + if version_key == 'php72' or version_key == 'php73': + wo_php_modules[version_key].append("{0}-recode".format(version_key)) + elif version_key == 'php74': + wo_php_modules[version_key].extend(["{0}-geoip".format(version_key), "{0}-json".format(version_key)]) wo_php_extra = ["graphviz"] From c4a6356cff3df8b0b91421d7b62075afb8f683b9 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Sat, 5 Aug 2023 17:31:10 +0200 Subject: [PATCH 6/9] Fix php variable --- wo/core/variables.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/wo/core/variables.py b/wo/core/variables.py index d6eda65..2576334 100644 --- a/wo/core/variables.py +++ b/wo/core/variables.py @@ -159,23 +159,26 @@ class WOVar(): 'php82': '8.2', } - wo_module = ["bcmath", "cli", "common", "curl", "fpm", "gd", "igbinary", - "imagick", "imap", "intl", "mbstring", "memcached", "msgpack", - "mysql", "opcache", "readline", "redis", "soap", "xdebug", - "xml", "zip"] + def generate_php_modules(version_prefix, version_number): + wo_module = ["bcmath", "cli", "common", "curl", "fpm", "gd", "igbinary", + "imagick", "imap", "intl", "mbstring", "memcached", "msgpack", + "mysql", "opcache", "readline", "redis", "soap", "xdebug", + "xml", "zip"] + php_modules = ["{0}-{1}".format(version_prefix, module) for module in wo_module] - wo_php_modules = {} + if version_prefix == 'php72' or version_prefix == 'php73': + php_modules.append("{0}-recode".format(version_prefix)) + elif version_prefix == 'php74': + php_modules.extend(["{0}-geoip".format(version_prefix), "{0}-json".format(version_prefix)]) - for version_key, version_number in wo_php_versions.items(): - wo_php_modules[version_key] = [] - for module in wo_module: - wo_php_modules[version_key].append("{0}-{1}".format(version_key, module)) + return php_modules - # Add version-specific modules - if version_key == 'php72' or version_key == 'php73': - wo_php_modules[version_key].append("{0}-recode".format(version_key)) - elif version_key == 'php74': - wo_php_modules[version_key].extend(["{0}-geoip".format(version_key), "{0}-json".format(version_key)]) + wo_php72 = generate_php_modules('php72', '7.2') + wo_php73 = generate_php_modules('php73', '7.3') + wo_php74 = generate_php_modules('php74', '7.4') + wo_php80 = generate_php_modules('php80', '8.0') + wo_php81 = generate_php_modules('php81', '8.1') + wo_php82 = generate_php_modules('php82', '8.2') wo_php_extra = ["graphviz"] @@ -183,10 +186,7 @@ class WOVar(): "mariadb-server", "percona-toolkit", "mariadb-common", "python3-mysqldb"] if wo_distro == 'raspbian': - if wo_platform_codename == 'stretch': - mariadb_ver = '10.1' - else: - mariadb_ver = '10.3' + mariadb_ver = '10.3' else: mariadb_ver = '10.11' wo_mysql = wo_mysql + ["mariadb-backup"] From 9f5dacb4ec061b876b8276138280b4e5e2790b04 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Sat, 5 Aug 2023 17:31:40 +0200 Subject: [PATCH 7/9] Improve purge opcache --- wo/cli/plugins/clean.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/wo/cli/plugins/clean.py b/wo/cli/plugins/clean.py index e4a05e3..1dbd763 100644 --- a/wo/cli/plugins/clean.py +++ b/wo/cli/plugins/clean.py @@ -9,6 +9,7 @@ from wo.core.aptget import WOAptGet from wo.core.logging import Log from wo.core.services import WOService from wo.core.shellexec import WOShellExec +from wo.core.variables import WOVar def wo_clean_hook(app): @@ -54,7 +55,7 @@ class WOCleanController(CementBaseController): @expose(hide=True) def clean_redis(self): """This function clears Redis cache""" - if(WOAptGet.is_installed(self, "redis-server")): + if (WOAptGet.is_installed(self, "redis-server")): Log.info(self, "Cleaning Redis cache") WOShellExec.cmd_exec(self, "redis-cli flushall") else: @@ -62,7 +63,7 @@ class WOCleanController(CementBaseController): @expose(hide=True) def clean_fastcgi(self): - if(os.path.isdir("/var/run/nginx-cache") and + if (os.path.isdir("/var/run/nginx-cache") and os.path.exists('/usr/sbin/nginx')): Log.info(self, "Cleaning NGINX FastCGI cache") WOShellExec.cmd_exec(self, "rm -rf /var/run/nginx-cache/*") @@ -78,15 +79,12 @@ class WOCleanController(CementBaseController): '/var/www/22222/htdocs/cache/opcache')): try: Log.info(self, "Cleaning opcache") - if os.path.exists('{0}php72.php'.format(opcache_dir)): - requests.get( - "http://127.0.0.1/cache/opcache/php72.php") - if os.path.exists('{0}php73.php'.format(opcache_dir)): - requests.get( - "http://127.0.0.1/cache/opcache/php73.php") - if os.path.exists('{0}php74.php'.format(opcache_dir)): - requests.get( - "http://127.0.0.1/cache/opcache/php74.php") + wo_php_version = list(WOVar.wo_php_versions.keys()) + for wo_php in wo_php_version: + if os.path.exists('{0}{1}.php'.format(opcache_dir, wo_php)): + requests.get( + "http://127.0.0.1/cache/opcache/{0}.php".format(wo_php)) + except requests.HTTPError as e: Log.debug(self, "{0}".format(e)) Log.debug(self, "Unable hit url, " From 0a9c23b5967c81e604bca25f8a04346ee447e55f Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Sat, 5 Aug 2023 17:53:40 +0200 Subject: [PATCH 8/9] Fix php module --- wo/core/variables.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wo/core/variables.py b/wo/core/variables.py index 2576334..c2e79b5 100644 --- a/wo/core/variables.py +++ b/wo/core/variables.py @@ -164,12 +164,12 @@ class WOVar(): "imagick", "imap", "intl", "mbstring", "memcached", "msgpack", "mysql", "opcache", "readline", "redis", "soap", "xdebug", "xml", "zip"] - php_modules = ["{0}-{1}".format(version_prefix, module) for module in wo_module] + php_modules = ["php{0}-{1}".format(version_number, module) for module in wo_module] if version_prefix == 'php72' or version_prefix == 'php73': - php_modules.append("{0}-recode".format(version_prefix)) + php_modules.append("php{0}-recode".format(version_number)) elif version_prefix == 'php74': - php_modules.extend(["{0}-geoip".format(version_prefix), "{0}-json".format(version_prefix)]) + php_modules.extend(["php{0}-geoip".format(version_number), "php{0}-json".format(version_number)]) return php_modules From 41d8299f2d65d3c100986ae5a274691312316c4c Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Sat, 5 Aug 2023 18:02:49 +0200 Subject: [PATCH 9/9] Refactor php version install --- wo/cli/plugins/stack.py | 74 +++++++++-------------------------------- 1 file changed, 16 insertions(+), 58 deletions(-) diff --git a/wo/cli/plugins/stack.py b/wo/cli/plugins/stack.py index a509b8c..15ba2f2 100644 --- a/wo/cli/plugins/stack.py +++ b/wo/cli/plugins/stack.py @@ -200,65 +200,23 @@ class WOStackController(CementBaseController): else: Log.debug(self, "Redis already installed") - # PHP 7.2 - if pargs.php72: - Log.debug(self, "Setting apt_packages variable for PHP 7.2") - if not (WOAptGet.is_installed(self, 'php7.2-fpm')): - apt_packages = (apt_packages + WOVar.wo_php72 + - WOVar.wo_php_extra) - else: - Log.debug(self, "PHP 7.2 already installed") - Log.info(self, "PHP 7.2 already installed") + wo_vars = { + 'php72': WOVar.wo_php72, + 'php73': WOVar.wo_php73, + 'php74': WOVar.wo_php74, + 'php80': WOVar.wo_php80, + 'php81': WOVar.wo_php81, + 'php82': WOVar.wo_php82, + } - # PHP 7.3 - if pargs.php73: - Log.debug(self, "Setting apt_packages variable for PHP 7.3") - if not WOAptGet.is_installed(self, 'php7.3-fpm'): - apt_packages = (apt_packages + WOVar.wo_php73 + - WOVar.wo_php_extra) - else: - Log.debug(self, "PHP 7.3 already installed") - Log.info(self, "PHP 7.3 already installed") - - # PHP 7.4 - if pargs.php74: - Log.debug(self, "Setting apt_packages variable for PHP 7.4") - if not WOAptGet.is_installed(self, 'php7.4-fpm'): - apt_packages = (apt_packages + WOVar.wo_php74 + - WOVar.wo_php_extra) - else: - Log.debug(self, "PHP 7.4 already installed") - Log.info(self, "PHP 7.4 already installed") - - # PHP 8.0 - if pargs.php80: - Log.debug(self, "Setting apt_packages variable for PHP 8.0") - if not WOAptGet.is_installed(self, 'php8.0-fpm'): - apt_packages = (apt_packages + WOVar.wo_php80 + - WOVar.wo_php_extra) - else: - Log.debug(self, "PHP 8.0 already installed") - Log.info(self, "PHP 8.0 already installed") - - # PHP 8.1 - if pargs.php81: - Log.debug(self, "Setting apt_packages variable for PHP 8.1") - if not WOAptGet.is_installed(self, 'php8.1-fpm'): - apt_packages = (apt_packages + WOVar.wo_php81 + - WOVar.wo_php_extra) - else: - Log.debug(self, "PHP 8.1 already installed") - Log.info(self, "PHP 8.1 already installed") - - # PHP 8.2 - if pargs.php82: - Log.debug(self, "Setting apt_packages variable for PHP 8.2") - if not WOAptGet.is_installed(self, 'php8.2-fpm'): - apt_packages = (apt_packages + WOVar.wo_php82 + - WOVar.wo_php_extra) - else: - Log.debug(self, "PHP 8.2 already installed") - Log.info(self, "PHP 8.2 already installed") + for parg_version, version in WOVar.wo_php_versions.items(): + if getattr(pargs, parg_version, False): + Log.debug(self, f"Setting apt_packages variable for PHP {version}") + if not WOAptGet.is_installed(self, f'php{version}-fpm'): + apt_packages = apt_packages + wo_vars[parg_version] + WOVar.wo_php_extra + else: + Log.debug(self, f"PHP {version} already installed") + Log.info(self, f"PHP {version} already installed") # MariaDB if pargs.mysql: