From d41d6549eb459e05f2b9d22392a0df8f8e10c0ce Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 14 Jan 2024 19:41:54 +0100 Subject: [PATCH] legacy: drop autopatching of PHP versions, nowadays Sury is here by default and offers more flexibility over which PHP versions may be installed e.g. even 5.6 can still be installed on Bookworm, but anyway only a ~handful of apps are still using 7.2 or lower --- src/app.py | 16 +--- src/backup.py | 42 ---------- src/migrations/0022_php73_to_php74_pools.py | 93 --------------------- src/utils/legacy.py | 92 -------------------- 4 files changed, 3 insertions(+), 240 deletions(-) delete mode 100644 src/migrations/0022_php73_to_php74_pools.py diff --git a/src/app.py b/src/app.py index d08d9ab76..9f84bb2fd 100644 --- a/src/app.py +++ b/src/app.py @@ -561,7 +561,7 @@ def app_upgrade( ) from yunohost.permission import permission_sync_to_user from yunohost.regenconf import manually_modified_files - from yunohost.utils.legacy import _patch_legacy_php_versions, _patch_legacy_helpers + from yunohost.utils.legacy import _patch_legacy_helpers from yunohost.backup import ( backup_list, backup_create, @@ -729,9 +729,6 @@ def app_upgrade( # Attempt to patch legacy helpers ... _patch_legacy_helpers(extracted_app_folder) - # Apply dirty patch to make php5 apps compatible with php7 - _patch_legacy_php_versions(extracted_app_folder) - # Prepare env. var. to pass to script env_dict = _make_environment_for_app_script( app_instance_name, workdir=extracted_app_folder, action="upgrade" @@ -1055,7 +1052,7 @@ def app_install( permission_sync_to_user, ) from yunohost.regenconf import manually_modified_files - from yunohost.utils.legacy import _patch_legacy_php_versions, _patch_legacy_helpers + from yunohost.utils.legacy import _patch_legacy_helpers from yunohost.utils.form import ask_questions_and_parse_answers # Check if disk space available @@ -1118,9 +1115,6 @@ def app_install( # Attempt to patch legacy helpers ... _patch_legacy_helpers(extracted_app_folder) - # Apply dirty patch to make php5 apps compatible with php7 - _patch_legacy_php_versions(extracted_app_folder) - # We'll check that the app didn't brutally edit some system configuration manually_modified_files_before_install = manually_modified_files() @@ -1383,7 +1377,7 @@ def app_remove(operation_logger, app, purge=False, force_workdir=None): purge -- Remove with all app data force_workdir -- Special var to force the working directoy to use, in context such as remove-after-failed-upgrade or remove-after-failed-restore """ - from yunohost.utils.legacy import _patch_legacy_php_versions, _patch_legacy_helpers + from yunohost.utils.legacy import _patch_legacy_helpers from yunohost.hook import hook_exec, hook_remove, hook_callback from yunohost.permission import ( user_permission_list, @@ -1405,10 +1399,6 @@ def app_remove(operation_logger, app, purge=False, force_workdir=None): # Attempt to patch legacy helpers ... _patch_legacy_helpers(app_setting_path) - # Apply dirty patch to make php5 apps compatible with php7 (e.g. the remove - # script might date back from jessie install) - _patch_legacy_php_versions(app_setting_path) - if force_workdir: # This is when e.g. calling app_remove() from the upgrade-failed case # where we want to remove using the *new* remove script and not the old one diff --git a/src/backup.py b/src/backup.py index 54d8efbfa..45fa36df5 100644 --- a/src/backup.py +++ b/src/backup.py @@ -1188,9 +1188,6 @@ class RestoreManager: try: self._postinstall_if_needed() - # Apply dirty patch to redirect php5 file on php7 - self._patch_legacy_php_versions_in_csv_file() - self._restore_system() self._restore_apps() except Exception as e: @@ -1201,39 +1198,6 @@ class RestoreManager: finally: self.clean() - def _patch_legacy_php_versions_in_csv_file(self): - """ - Apply dirty patch to redirect php5 and php7.x files to php8.2 - """ - from yunohost.utils.legacy import LEGACY_PHP_VERSION_REPLACEMENTS - - backup_csv = os.path.join(self.work_dir, "backup.csv") - - if not os.path.isfile(backup_csv): - return - - replaced_something = False - with open(backup_csv) as csvfile: - reader = csv.DictReader(csvfile, fieldnames=["source", "dest"]) - newlines = [] - for row in reader: - for pattern, replace in LEGACY_PHP_VERSION_REPLACEMENTS: - if pattern in row["source"]: - replaced_something = True - row["source"] = row["source"].replace(pattern, replace) - - newlines.append(row) - - if not replaced_something: - return - - with open(backup_csv, "w") as csvfile: - writer = csv.DictWriter( - csvfile, fieldnames=["source", "dest"], quoting=csv.QUOTE_ALL - ) - for row in newlines: - writer.writerow(row) - def _restore_system(self): """Restore user and system parts""" @@ -1368,8 +1332,6 @@ class RestoreManager: name should be already install) """ from yunohost.utils.legacy import ( - _patch_legacy_php_versions, - _patch_legacy_php_versions_in_settings, _patch_legacy_helpers, ) from yunohost.user import user_group_list @@ -1408,10 +1370,6 @@ class RestoreManager: # Attempt to patch legacy helpers... _patch_legacy_helpers(app_settings_in_archive) - # Apply dirty patch to make php5 apps compatible with php7 - _patch_legacy_php_versions(app_settings_in_archive) - _patch_legacy_php_versions_in_settings(app_settings_in_archive) - # Delete _common.sh file in backup common_file = os.path.join(app_backup_in_archive, "_common.sh") rm(common_file, force=True) diff --git a/src/migrations/0022_php73_to_php74_pools.py b/src/migrations/0022_php73_to_php74_pools.py deleted file mode 100644 index 23ecc5b89..000000000 --- a/src/migrations/0022_php73_to_php74_pools.py +++ /dev/null @@ -1,93 +0,0 @@ -import os -import glob -from shutil import copy2 -from logging import getLogger - -from yunohost.app import _is_installed -from yunohost.utils.legacy import _patch_legacy_php_versions_in_settings -from yunohost.tools import Migration -from yunohost.service import _run_service_command - -logger = getLogger("yunohost.migration") - -OLDPHP_POOLS = "/etc/php/7.3/fpm/pool.d" -NEWPHP_POOLS = "/etc/php/7.4/fpm/pool.d" - -OLDPHP_SOCKETS_PREFIX = "/run/php/php7.3-fpm" -NEWPHP_SOCKETS_PREFIX = "/run/php/php7.4-fpm" - -# Because of synapse é_è -OLDPHP_SOCKETS_PREFIX2 = "/run/php7.3-fpm" -NEWPHP_SOCKETS_PREFIX2 = "/run/php7.4-fpm" - -MIGRATION_COMMENT = ( - "; YunoHost note : this file was automatically moved from {}".format(OLDPHP_POOLS) -) - - -class MyMigration(Migration): - "Migrate php7.3-fpm 'pool' conf files to php7.4" - - dependencies = ["migrate_to_bullseye"] - - def run(self): - # Get list of php7.3 pool files - oldphp_pool_files = glob.glob("{}/*.conf".format(OLDPHP_POOLS)) - - # Keep only basenames - oldphp_pool_files = [os.path.basename(f) for f in oldphp_pool_files] - - # Ignore the "www.conf" (default stuff, probably don't want to touch it ?) - oldphp_pool_files = [f for f in oldphp_pool_files if f != "www.conf"] - - for pf in oldphp_pool_files: - # Copy the files to the php7.3 pool - src = "{}/{}".format(OLDPHP_POOLS, pf) - dest = "{}/{}".format(NEWPHP_POOLS, pf) - copy2(src, dest) - - # Replace the socket prefix if it's found - c = "sed -i -e 's@{}@{}@g' {}".format( - OLDPHP_SOCKETS_PREFIX, NEWPHP_SOCKETS_PREFIX, dest - ) - os.system(c) - c = "sed -i -e 's@{}@{}@g' {}".format( - OLDPHP_SOCKETS_PREFIX2, NEWPHP_SOCKETS_PREFIX2, dest - ) - os.system(c) - - # Also add a comment that it was automatically moved from php7.3 - # (for human traceability and backward migration) - c = "sed -i '1i {}' {}".format(MIGRATION_COMMENT, dest) - os.system(c) - - app_id = os.path.basename(pf)[: -len(".conf")] - if _is_installed(app_id): - _patch_legacy_php_versions_in_settings( - "/etc/yunohost/apps/%s/" % app_id - ) - - nginx_conf_files = glob.glob("/etc/nginx/conf.d/*.d/%s.conf" % app_id) - for nf in nginx_conf_files: - # Replace the socket prefix if it's found - c = "sed -i -e 's@{}@{}@g' {}".format( - OLDPHP_SOCKETS_PREFIX, NEWPHP_SOCKETS_PREFIX, nf - ) - os.system(c) - c = "sed -i -e 's@{}@{}@g' {}".format( - OLDPHP_SOCKETS_PREFIX2, NEWPHP_SOCKETS_PREFIX2, nf - ) - os.system(c) - - os.system( - "rm /etc/logrotate.d/php7.3-fpm" - ) # We remove this otherwise the logrotate cron will be unhappy - - # Reload/restart the php pools - os.system("systemctl stop php7.3-fpm") - os.system("systemctl disable php7.3-fpm") - _run_service_command("restart", "php7.4-fpm") - _run_service_command("enable", "php7.4-fpm") - - # Reload nginx - _run_service_command("reload", "nginx") diff --git a/src/utils/legacy.py b/src/utils/legacy.py index 9b83dceca..f9e6b8661 100644 --- a/src/utils/legacy.py +++ b/src/utils/legacy.py @@ -25,8 +25,6 @@ from moulinette.core import MoulinetteError from moulinette.utils.filesystem import ( read_file, write_to_file, - write_to_yaml, - read_yaml, ) from yunohost.utils.error import YunohostValidationError @@ -34,96 +32,6 @@ from yunohost.utils.error import YunohostValidationError logger = getLogger("yunohost.utils.legacy") -LEGACY_PHP_VERSION_REPLACEMENTS = [ - ("/etc/php5", "/etc/php/8.2"), - ("/etc/php/7.0", "/etc/php/8.2"), - ("/etc/php/7.3", "/etc/php/8.2"), - ("/etc/php/7.4", "/etc/php/8.2"), - ("/var/run/php5-fpm", "/var/run/php/php8.2-fpm"), - ("/var/run/php/php7.0-fpm", "/var/run/php/php8.2-fpm"), - ("/var/run/php/php7.3-fpm", "/var/run/php/php8.2-fpm"), - ("/var/run/php/php7.4-fpm", "/var/run/php/php8.2-fpm"), - ("php5", "php8.2"), - ("php7.0", "php8.2"), - ("php7.3", "php8.2"), - ("php7.4", "php8.2"), - ('YNH_PHP_VERSION="7.3"', 'YNH_PHP_VERSION="8.2"'), - ('YNH_PHP_VERSION="7.4"', 'YNH_PHP_VERSION="8.2"'), - ( - 'phpversion="${phpversion:-7.0}"', - 'phpversion="${phpversion:-8.2}"', - ), # Many helpers like the composer ones use 7.0 by default ... - ( - 'phpversion="${phpversion:-7.3}"', - 'phpversion="${phpversion:-8.2}"', - ), # Many helpers like the composer ones use 7.0 by default ... - ( - 'phpversion="${phpversion:-7.4}"', - 'phpversion="${phpversion:-8.2}"', - ), # Many helpers like the composer ones use 7.0 by default ... - ( - '"$phpversion" == "7.0"', - '$(bc <<< "$phpversion >= 8.2") -eq 1', - ), # patch ynh_install_php to refuse installing/removing php <= 7.3 - ( - '"$phpversion" == "7.3"', - '$(bc <<< "$phpversion >= 8.2") -eq 1', - ), # patch ynh_install_php to refuse installing/removing php <= 7.3 - ( - '"$phpversion" == "7.4"', - '$(bc <<< "$phpversion >= 8.2") -eq 1', - ), # patch ynh_install_php to refuse installing/removing php <= 7.3 - -] - - -def _patch_legacy_php_versions(app_folder): - files_to_patch = [] - files_to_patch.extend(glob.glob("%s/conf/*" % app_folder)) - files_to_patch.extend(glob.glob("%s/scripts/*" % app_folder)) - files_to_patch.extend(glob.glob("%s/scripts/*/*" % app_folder)) - files_to_patch.extend(glob.glob("%s/scripts/.*" % app_folder)) - files_to_patch.append("%s/manifest.json" % app_folder) - files_to_patch.append("%s/manifest.toml" % app_folder) - - for filename in files_to_patch: - # Ignore non-regular files - if not os.path.isfile(filename): - continue - - c = ( - "sed -i " - + "".join(f"-e 's@{p}@{r}@g' " for p, r in LEGACY_PHP_VERSION_REPLACEMENTS) - + "%s" % filename - ) - os.system(c) - - -def _patch_legacy_php_versions_in_settings(app_folder): - settings = read_yaml(os.path.join(app_folder, "settings.yml")) - - if settings.get("fpm_config_dir") in ["/etc/php/7.0/fpm", "/etc/php/7.3/fpm", "/etc/php/7.4/fpm"]: - settings["fpm_config_dir"] = "/etc/php/8.2/fpm" - if settings.get("fpm_service") in ["php7.0-fpm", "php7.3-fpm", "php7.4-fpm"]: - settings["fpm_service"] = "php8.2-fpm" - if settings.get("phpversion") in ["7.0", "7.3", "7.4"]: - settings["phpversion"] = "8.2" - - # We delete these checksums otherwise the file will appear as manually modified - list_to_remove = [ - "checksum__etc_php_7.4_fpm_pool", - "checksum__etc_php_7.3_fpm_pool", - "checksum__etc_php_7.0_fpm_pool", - "checksum__etc_nginx_conf.d", - ] - settings = { - k: v - for k, v in settings.items() - if not any(k.startswith(to_remove) for to_remove in list_to_remove) - } - - write_to_yaml(app_folder + "/settings.yml", settings) - def _patch_legacy_helpers(app_folder): files_to_patch = []