From 24211a90bc6ea250c904454fbc6bf8ba1bc7287a Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Fri, 4 Jun 2021 17:17:27 +0200 Subject: [PATCH] Yolo php7.3 -> php7.4 migration --- locales/en.json | 1 + src/yunohost/app.py | 16 +++- .../0022_php73_to_php74_pools.py | 83 +++++++++++++++++++ 3 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 src/yunohost/data_migrations/0022_php73_to_php74_pools.py diff --git a/locales/en.json b/locales/en.json index 813688a87..922ece34a 100644 --- a/locales/en.json +++ b/locales/en.json @@ -426,6 +426,7 @@ "migration_description_0018_xtable_to_nftable": "Migrate old network traffic rules to the new nftable system", "migration_description_0019_extend_permissions_features": "Extend/rework the app permission management system", "migration_description_0020_ssh_sftp_permissions": "Add SSH and SFTP permissions support", + "migration_description_0022_php73_to_php74_pools": "Migrate php7.3-fpm 'pool' conf files to php7.4", "migration_ldap_backup_before_migration": "Creating a backup of LDAP database and apps settings prior to the actual migration.", "migration_ldap_can_not_backup_before_migration": "The backup of the system could not be completed before the migration failed. Error: {error:s}", "migration_ldap_migration_failed_trying_to_rollback": "Could not migrate... trying to roll back the system.", diff --git a/src/yunohost/app.py b/src/yunohost/app.py index eafff1db3..98a8053e7 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -3480,10 +3480,18 @@ LEGACY_PHP_VERSION_REPLACEMENTS = [ 'phpversion="${phpversion:-7.0}"', 'phpversion="${phpversion:-7.4}"', ), # Many helpers like the composer ones use 7.0 by default ... + ( + 'phpversion="${phpversion:-7.3}"', + 'phpversion="${phpversion:-7.4}"', + ), # Many helpers like the composer ones use 7.0 by default ... ( '"$phpversion" == "7.0"', '$(bc <<< "$phpversion >= 7.4") -eq 1', ), # patch ynh_install_php to refuse installing/removing php <= 7.3 + ( + '"$phpversion" == "7.3"', + '$(bc <<< "$phpversion >= 7.4") -eq 1', + ), # patch ynh_install_php to refuse installing/removing php <= 7.3 ] @@ -3518,15 +3526,15 @@ 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") == "/etc/php/7.0/fpm": + if settings.get("fpm_config_dir") in ["/etc/php/7.0/fpm", "/etc/php/7.3/fpm"]: settings["fpm_config_dir"] = "/etc/php/7.4/fpm" - if settings.get("fpm_service") == "php7.0-fpm": + if settings.get("fpm_service") in ["php7.0-fpm", "php7.3-fpm"]: settings["fpm_service"] = "php7.4-fpm" - if settings.get("phpversion") == "7.0": + if settings.get("phpversion") in ["7.0", "7.3"]: settings["phpversion"] = "7.4" # We delete these checksums otherwise the file will appear as manually modified - list_to_remove = ["checksum__etc_php_7.0_fpm_pool", "checksum__etc_nginx_conf.d"] + list_to_remove = ["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() diff --git a/src/yunohost/data_migrations/0022_php73_to_php74_pools.py b/src/yunohost/data_migrations/0022_php73_to_php74_pools.py new file mode 100644 index 000000000..0157fa275 --- /dev/null +++ b/src/yunohost/data_migrations/0022_php73_to_php74_pools.py @@ -0,0 +1,83 @@ +import os +import glob +from shutil import copy2 + +from moulinette.utils.log import getActionLogger + +from yunohost.app import _is_installed, _patch_legacy_php_versions_in_settings +from yunohost.tools import Migration +from yunohost.service import _run_service_command + +logger = getActionLogger("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" + +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 f in oldphp_pool_files: + + # Copy the files to the php7.4 pool + src = "{}/{}".format(OLDPHP_POOLS, f) + dest = "{}/{}".format(NEWPHP_POOLS, f) + 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) + + # 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(f)[: -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 f 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, f + ) + 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 + _run_service_command("restart", "php7.4-fpm") + _run_service_command("enable", "php7.4-fpm") + os.system("systemctl stop php7.3-fpm") + os.system("systemctl disable php7.3-fpm") + + # Reload nginx + _run_service_command("reload", "nginx")