diff --git a/manifest.json b/manifest.json index c26e888..45faad0 100644 --- a/manifest.json +++ b/manifest.json @@ -47,8 +47,7 @@ "ask": { "en": "Choose the version of PHP to use." }, - "choices": ["7.0", "7.1", "7.2", "7.3"], - "default": "7.3" + "default": "8.0.3" } ] } diff --git a/scripts/_common.sh b/scripts/_common.sh index 944a65e..67ea80b 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -4,6 +4,9 @@ # COMMON VARIABLES #================================================= +# dependencies used by the app +pkg_dependencies="curl" + #================================================= # PERSONAL HELPERS #================================================= diff --git a/scripts/backup b/scripts/backup index 0853e2c..27a81a3 100755 --- a/scripts/backup +++ b/scripts/backup @@ -9,6 +9,7 @@ #Keep this path for calling _common.sh inside the execution's context of backup and restore scripts source ../settings/scripts/_common.sh source /usr/share/yunohost/helpers +source ../settings/scripts/ynh_install_php #================================================= # MANAGE SCRIPT FAILURE diff --git a/scripts/install b/scripts/install index 73df6fc..2838e07 100755 --- a/scripts/install +++ b/scripts/install @@ -8,6 +8,7 @@ source _common.sh source /usr/share/yunohost/helpers +source ynh_install_php #================================================= # MANAGE SCRIPT FAILURE @@ -22,7 +23,7 @@ ynh_abort_if_errors domain=$YNH_APP_ARG_DOMAIN path_url=$YNH_APP_ARG_PATH -YNH_PHP_VERSION=$YNH_APP_ARG_PHPVERSION +php_version=$YNH_APP_ARG_PHPVERSION app=$YNH_APP_INSTANCE_NAME @@ -47,6 +48,14 @@ ynh_app_setting_set --app=$app --key=path --value=$path_url #================================================= # STANDARD MODIFICATIONS +#================================================= +# INSTALL DEPENDENCIES +#================================================= +ynh_script_progression --message="Installing dependencies..." + +ynh_install_app_dependencies $pkg_dependencies +ynh_install_php --php_version=$php_version + #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= @@ -60,9 +69,9 @@ cp ../sources/index.php /$final_path/. #================================================= # NGINX CONFIGURATION #================================================= -ynh_script_progression --message="Configuring nginx web server..." +ynh_script_progression --message="Configuring NGINX web server..." -# Create a dedicated nginx config +# Create a dedicated NGINX config ynh_add_nginx_config #================================================= @@ -76,11 +85,10 @@ ynh_system_user_create --username=$app #================================================= # PHP-FPM CONFIGURATION #================================================= -ynh_script_progression --message="Configuring php-fpm..." +ynh_script_progression --message="Configuring PHP-FPM..." -# Create a dedicated php-fpm config -ynh_add_fpm_config --usage=low --footprint=low -phpversion=$(ynh_app_setting_get --app=$app --key=phpversion) +# Create a dedicated PHP-FPM config +ynh_add_fpm_config #================================================= # GENERIC FINALIZATION @@ -95,7 +103,7 @@ chown -R root: $final_path #================================================= # RELOAD NGINX #================================================= -ynh_script_progression --message="Reloading nginx web server..." +ynh_script_progression --message="Reloading NGINX web server..." ynh_systemd_action --service_name=nginx --action=reload diff --git a/scripts/remove b/scripts/remove index 059bf77..b5d573f 100755 --- a/scripts/remove +++ b/scripts/remove @@ -8,6 +8,7 @@ source _common.sh source /usr/share/yunohost/helpers +source ynh_install_php #================================================= # LOAD SETTINGS @@ -21,6 +22,15 @@ final_path=$(ynh_app_setting_get --app=$app --key=final_path) #================================================= # STANDARD REMOVE +#================================================= +# REMOVE DEPENDENCIES +#================================================= +ynh_script_progression --message="Removing dependencies..." + +# Remove metapackage and its dependencies +ynh_remove_php +ynh_remove_app_dependencies + #================================================= # REMOVE APP MAIN DIR #================================================= @@ -32,17 +42,17 @@ ynh_secure_remove --file="$final_path" #================================================= # REMOVE NGINX CONFIGURATION #================================================= -ynh_script_progression --message="Removing nginx web server configuration..." +ynh_script_progression --message="Removing NGINX web server configuration..." -# Remove the dedicated nginx config +# Remove the dedicated NGINX config ynh_remove_nginx_config #================================================= # REMOVE PHP-FPM CONFIGURATION #================================================= -ynh_script_progression --message="Removing php-fpm configuration..." +ynh_script_progression --message="Removing PHP-FPM configuration..." -# Remove the dedicated php-fpm config +# Remove the dedicated PHP-FPM config ynh_remove_fpm_config #================================================= diff --git a/scripts/restore b/scripts/restore index c5fcbbf..4db340c 100755 --- a/scripts/restore +++ b/scripts/restore @@ -6,7 +6,7 @@ # IMPORT GENERIC HELPERS #================================================= -#Keep this path for calling _common.sh inside the execution's context of backup and restore scripts +# Keep this path for calling _common.sh inside the execution's context of backup and restore scripts source ../settings/scripts/_common.sh source /usr/share/yunohost/helpers @@ -44,7 +44,7 @@ test ! -d $final_path \ #================================================= # RESTORE THE NGINX CONFIGURATION #================================================= -ynh_script_progression --message="Restoring the nginx configuration..." +ynh_script_progression --message="Restoring the NGINX web server configuration..." ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf" @@ -71,21 +71,30 @@ ynh_script_progression --message="Restoring user rights..." # Restore permissions on app files chown -R root: $final_path +#================================================= +# SPECIFIC RESTORATION +#================================================= +# REINSTALL DEPENDENCIES +#================================================= +ynh_script_progression --message="Reinstalling dependencies..." + +# Define and install dependencies +ynh_install_app_dependencies $pkg_dependencies +ynh_install_php --php_version=$php_version + #================================================= # RESTORE THE PHP-FPM CONFIGURATION #================================================= ynh_script_progression --message="Restoring PHP-FPM configuration..." -# Recreate a dedicated php-fpm config -ynh_add_fpm_config --usage=low --footprint=low -phpversion=$(ynh_app_setting_get --app=$app --key=phpversion) +ynh_restore_file --origin_path="/etc/php/$phpversion/fpm/pool.d/$app.conf" #================================================= # GENERIC FINALIZATION #================================================= # RELOAD NGINX AND PHP-FPM #================================================= -ynh_script_progression --message="Reloading nginx web server and php-fpm..." +ynh_script_progression --message="Reloading NGINX web server and PHP-FPM..." ynh_systemd_action --service_name=php$phpversion-fpm --action=reload ynh_systemd_action --service_name=nginx --action=reload diff --git a/scripts/upgrade b/scripts/upgrade index 4574761..03269e9 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -39,7 +39,7 @@ ynh_script_progression --message="Backing up the app before upgrading (may take # Backup the current version of the app ynh_backup_before_upgrade ynh_clean_setup () { - # restore it if the upgrade fails + # Restore it if the upgrade fails ynh_restore_upgradebackup } # Exit if an error occurs during the execution of the script @@ -62,11 +62,19 @@ fi #================================================= # NGINX CONFIGURATION #================================================= -ynh_script_progression --message="Upgrading nginx web server configuration..." +ynh_script_progression --message="Upgrading NGINX web server configuration..." -# Create a dedicated nginx config +# Create a dedicated NGINX config ynh_add_nginx_config +#================================================= +# UPGRADE DEPENDENCIES +#================================================= +ynh_script_progression --message="Upgrading dependencies..." + +ynh_install_app_dependencies $pkg_dependencies +ynh_install_php --php_version=$php_version + #================================================= # CREATE DEDICATED USER #================================================= @@ -78,11 +86,10 @@ ynh_system_user_create --username=$app #================================================= # PHP-FPM CONFIGURATION #================================================= -ynh_script_progression --message="Upgrading php-fpm configuration..." +ynh_script_progression --message="Upgrading PHP-FPM configuration..." -# Create a dedicated php-fpm config -ynh_add_fpm_config --usage=low --footprint=low -phpversion=$(ynh_app_setting_get --app=$app --key=phpversion) +# Create a dedicated PHP-FPM config +ynh_add_fpm_config #================================================= # GENERIC FINALIZATION @@ -97,7 +104,7 @@ chown -R root: $final_path #================================================= # RELOAD NGINX #================================================= -ynh_script_progression --message="Reloading nginx web server..." +ynh_script_progression --message="Reloading NGINX web server..." ynh_systemd_action --service_name=nginx --action=reload diff --git a/scripts/ynh_install_php b/scripts/ynh_install_php new file mode 100644 index 0000000..380dce4 --- /dev/null +++ b/scripts/ynh_install_php @@ -0,0 +1,303 @@ +#!/bin/bash + +phpenv_version=20190623 +php_build_version=20210307 +phpenv_aliases_version=20180628 +phpenv_composer_version=20160325 +phpenv_install_dir="/opt/phpenv" +php_version_path="$phpenv_install_dir/versions" +# PHPENV_ROOT is the directory of phpenv, it needs to be loaded as a environment variable. +export PHPENV_ROOT="$phpenv_install_dir" + +# Required dependencies +phpenv_dependencies="autoconf2.13 autoconf2.64 autoconf bash bison build-essential ca-certificates curl findutils git libbz2-dev libcurl4-gnutls-dev libicu-dev libjpeg-dev libmcrypt-dev libonig-dev libpng-dev libreadline-dev libsqlite3-dev libssl-dev libtidy-dev libxml2-dev libxslt1-dev libzip-dev pkg-config re2c zlib1g-dev" + +# Install PHP Version Management +# +# [internal] +# +# usage: ynh_install_phpenv +# +# Requires YunoHost version 2.7.12 or higher. +ynh_install_phpenv () { + ynh_print_info --message="Installation of phpenv - PHP Version Management - phpenv-${phpenv_version}/php-build-${php_build_version}" + + # Build an app.src for phpenv + mkdir -p "../conf" + echo "SOURCE_URL=https://github.com/phpenv/phpenv/archive/9b7e4e1c0083c46be69f4c6d063f78c18654aad1.tar.gz +SOURCE_SUM=96d8edddb2ab3f953f9ee64af8b45e7e983713e99507bba75c6b8cbaa466089a" > "../conf/phpenv.src" + # Download and extract phpenv + ynh_setup_source --dest_dir="$phpenv_install_dir" --source_id=phpenv + + # Build an app.src for php-build + mkdir -p "../conf" + echo "SOURCE_URL=https://github.com/php-build/php-build/archive/6530e7501ccc758928d5510813dc3f5fbdc87419.tar.gz +SOURCE_SUM=e2de1071f956033aa670e08f0fcf3317a26249b69eea7df42fa95187afa9a17f" > "../conf/php-build.src" + # Download and extract php-build + ynh_setup_source --dest_dir="$phpenv_install_dir/plugins/php-build" --source_id=php-build + + # Build an app.src for phpenv-aliases + mkdir -p "../conf" + echo "SOURCE_URL=https://github.com/madumlao/phpenv-aliases/archive/89cbd4d4aa808f06299d903046c674139a8978fe.tar.gz +SOURCE_SUM=ffaad964ff11a9cad8935e7e972c57cd08da203028a1573792dbbe5f15e0696a" > "../conf/phpenv-aliases.src" + # Download and extract phpenv-aliases + ynh_setup_source --dest_dir="$phpenv_install_dir/plugins/phpenv-aliases" --source_id=phpenv-aliases + + # Build an app.src for phpenv-composer + mkdir -p "../conf" + echo "SOURCE_URL=https://github.com/ngyuki/phpenv-composer/archive/45af2a346747192aa46413f1d764fc25e177a46a.tar.gz +SOURCE_SUM=1643fe1d49cf424c3590af6a23cef09c6050fc5b71212205b6d459debc6d063f" > "../conf/phpenv-composer.src" + # Download and extract phpenv-composer + ynh_setup_source --dest_dir="$phpenv_install_dir/plugins/phpenv-composer" --source_id=phpenv-composer + + # Create shims directory if needed + if [ ! -d $phpenv_install_dir/shims ] ; then + mkdir $phpenv_install_dir/shims + fi +} + +# Load the version of PHP for an app, and set variables. +# +# ynh_use_php has to be used in any app scripts before using PHP for the first time. +# This helper will provide alias and variables to use in your scripts. +# +# To use gem or PHP, use the alias `ynh_gem` and `ynh_php` +# Those alias will use the correct version installed for the app +# For example: use `ynh_gem install` instead of `gem install` +# +# With `sudo` or `ynh_exec_as`, use instead the fallback variables `$ynh_gem` and `$ynh_php` +# And propagate $PATH to sudo with $ynh_php_load_path +# Exemple: `ynh_exec_as $app $ynh_php_load_path $ynh_gem install` +# +# $PATH contains the path of the requested version of PHP. +# However, $PATH is duplicated into $php_path to outlast any manipulation of $PATH +# You can use the variable `$ynh_php_load_path` to quickly load your PHP version +# in $PATH for an usage into a separate script. +# Exemple: $ynh_php_load_path $final_path/script_that_use_gem.sh` +# +# +# Finally, to start a PHP service with the correct version, 2 solutions +# Either the app is dependent of PHP or gem, but does not called it directly. +# In such situation, you need to load PATH +# `Environment="__YNH_PHP_LOAD_ENV_PATH__"` +# `ExecStart=__FINALPATH__/my_app` +# You will replace __YNH_PHP_LOAD_ENV_PATH__ with $ynh_php_load_path +# +# Or PHP start the app directly, then you don't need to load the PATH variable +# `ExecStart=__YNH_PHP__ my_app run` +# You will replace __YNH_PHP__ with $ynh_php +# +# +# one other variable is also available +# - $php_path: The absolute path to PHP binaries for the chosen version. +# +# usage: ynh_use_php +# +# Requires YunoHost version 2.7.12 or higher. +ynh_use_php () { + php_version=$(ynh_app_setting_get --app=$app --key=php_version) + + # Get the absolute path of this version of PHP + php_path="$php_version_path/$YNH_APP_INSTANCE_NAME/bin" + + # Allow alias to be used into bash script + shopt -s expand_aliases + + # Create an alias for the specific version of PHP and a variable as fallback + ynh_php="$php_path/php" + alias ynh_php="$ynh_php" + # And gem + ynh_gem="$php_path/gem" + alias ynh_gem="$ynh_gem" + + ynh_fpm_conf="$php_version_path/$YNH_APP_INSTANCE_NAME/etc/php-fpm.d/$YNH_APP_INSTANCE_NAME.conf" + + ynh_fpm_service="$php_version_path/$YNH_APP_INSTANCE_NAME/etc/systemd/system/php-fpm.service" + + rm -f "$php_version_path/$YNH_APP_INSTANCE_NAME/etc/php-fpm.d/www.conf" + + # Load the path of this version of PHP in $PATH + if [[ :$PATH: != *":$php_path"* ]]; then + PATH="$php_path:$PATH" + fi + # Create an alias to easily load the PATH + ynh_php_load_path="PATH=$PATH" + + # Sets the local application-specific PHP version + (cd $final_path + phpenv local $php_version) +} + +# Install a specific version of PHP +# +# ynh_install_php will install the version of PHP provided as argument by using phpenv. +# +# phpenv (PHP Version Management) stores the target PHP version in a .php_version file created in the target folder (using phpenv local ) +# It then uses that information for every PHP user that uses phpenv provided PHP command +# +# This helper creates a /etc/profile.d/phpenv.sh that configures PATH environment for phpenv +# for every LOGIN user, hence your user must have a defined shell (as opposed to /usr/sbin/nologin) +# +# Don't forget to execute PHP-dependent command in a login environment +# (e.g. sudo --login option) +# When not possible (e.g. in systemd service definition), please use direct path +# to phpenv shims (e.g. $PHPENV_ROOT/shims/bundle) +# +# usage: ynh_install_php --php_version=php_version +# | arg: -v, --php_version= - Version of PHP to install. +# +# Requires YunoHost version 2.7.12 or higher. +ynh_install_php () { + # Declare an array to define the options of this helper. + local legacy_args=v + local -A args_array=( [v]=php_version= ) + local php_version + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # Store php_version into the config of this app + ynh_app_setting_set --app=$YNH_APP_INSTANCE_NAME --key=php_version --value=$php_version + + # Create $phpenv_install_dir if doesn't exist already + mkdir -p "$phpenv_install_dir/plugins/php-build" + + # Install required dependencies + ynh_add_app_dependencies --package="$phpenv_dependencies" + + # Load phpenv path in PATH + local CLEAR_PATH="$phpenv_install_dir/bin:$PATH" + + # Remove /usr/local/bin in PATH in case of PHP prior installation + PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') + + # Move an existing PHP binary, to avoid to block phpenv + test -x /usr/bin/PHP && mv /usr/bin/PHP /usr/bin/php_phpenv + + # If phpenv is not previously setup, install it + if ! type phpenv > /dev/null 2>&1 + then + ynh_install_phpenv + elif dpkg --compare-versions "$($phpenv_install_dir/bin/phpenv --version | cut -d" " -f2)" lt $phpenv_version + then + ynh_install_phpenv + elif dpkg --compare-versions "$($phpenv_install_dir/plugins/php-build/bin/php-build --version | cut -d" " -f2)" lt $php_build_version + then + ynh_install_phpenv + fi + + # Restore /usr/local/bin in PATH + PATH=$CLEAR_PATH + + # And replace the old PHP binary + test -x /usr/bin/php_phpenv && mv /usr/bin/php_phpenv /usr/bin/PHP + + # Install the requested version of PHP + ynh_print_info --message="Installation of PHP-"$php_version + phpenv install --skip-existing $php_version + + # Remove app virtualenv + if `phpenv alias --list | grep --quiet "$YNH_APP_INSTANCE_NAME" 1>/dev/null 2>&1` + then + phpenv alias $YNH_APP_INSTANCE_NAME --remove + fi + + # Create app virtualenv + phpenv alias $YNH_APP_INSTANCE_NAME $php_version + + # Cleanup PHP versions + ynh_cleanup_php + + # Create app virtualenv + phpenv alias $YNH_APP_INSTANCE_NAME $php_version + + # Set environment for PHP users + echo "#phpenv +export PHPENV_ROOT=$phpenv_install_dir +export PATH=\"$phpenv_install_dir/bin:$PATH\" +eval \"\$(phpenv init -)\" + #phpenv" > /etc/profile.d/phpenv.sh + + # Load the environment + eval "$(phpenv init -)" +} + +# Remove the version of PHP used by the app. +# +# This helper will also cleanup PHP versions +# +# usage: ynh_remove_php +ynh_remove_php () { + local php_version=$(ynh_app_setting_get --app=$YNH_APP_INSTANCE_NAME --key=php_version) + + # Load phpenv path in PATH + local CLEAR_PATH="$phpenv_install_dir/bin:$PATH" + + # Remove /usr/local/bin in PATH in case of PHP prior installation + PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') + + phpenv alias $YNH_APP_INSTANCE_NAME --remove + + # Remove the line for this app + ynh_app_setting_delete --app=$YNH_APP_INSTANCE_NAME --key=php_version + + # Cleanup PHP versions + ynh_cleanup_php +} + +# Remove no more needed versions of PHP used by the app. +# +# This helper will check what PHP version are no more required, +# and uninstall them +# If no app uses PHP, phpenv will be also removed. +# +# usage: ynh_cleanup_php +ynh_cleanup_php () { + + # List required PHP version + local installed_apps=$(yunohost app list | grep -oP 'id: \K.*$') + local required_php_versions="" + for installed_app in $installed_apps + do + local installed_app_php_version=$(yunohost app setting $installed_app php_version) + if [[ $installed_app_php_version ]] + then + required_php_versions="${installed_app_php_version}\n${required_php_versions}" + fi + done + + # Remove no more needed PHP version + local installed_php_versions=$(phpenv versions --bare --skip-aliases | grep -Ev '/') + for installed_php_version in $installed_php_versions + do + if ! `echo ${required_php_versions} | grep "${installed_php_version}" 1>/dev/null 2>&1` + then + ynh_print_info --message="Removing of PHP-"$installed_php_version + $phpenv_install_dir/bin/phpenv uninstall --force $installed_php_version + fi + done + + # If none PHP version is required + if [[ ! $required_php_versions ]] + then + # Remove phpenv environment configuration + ynh_print_info --message="Removing of phpenv-"$phpenv_version + ynh_secure_remove --file="$phpenv_install_dir" + rm /etc/profile.d/phpenv.sh + fi +} + + +ynh_add_fpm_config () { + ynh_use_php + + ynh_add_config --template="../conf/php-fpm.conf" --destination="$ynh_fpm_conf" + ln -s $ynh_fpm_service /etc/systemd/system/$app.service + service $app start +} + +ynh_remove_fpm_config () { + ynh_use_php + + ynh_remove_systemd_config + rm /etc/systemd/system/$app.service +}