helpers 2.1: heavily simplify php-related helpers: remove --phpversion argument, --composerversion (define YNH_COMPOSER_VERSION prior to calling helper), --usage/--footprint (can still be customized by defining fpm_usage/footprint setting)

This commit is contained in:
Alexandre Aubin 2024-05-27 22:38:53 +02:00
parent 1b03058858
commit 94c12c6409

View file

@ -9,7 +9,7 @@ YNH_PHP_VERSION=${YNH_PHP_VERSION:-$YNH_DEFAULT_PHP_VERSION}
# #
# usage: ynh_add_fpm_config # usage: ynh_add_fpm_config
# #
# Case 1 (recommended) : your provided a snippet conf/extra_php-fpm.conf # This helper assumes the app has an conf/extra_php-fpm.conf snippet
# #
# The actual PHP configuration will be automatically generated, # The actual PHP configuration will be automatically generated,
# and your extra_php-fpm.conf will be appended (typically contains PHP upload limits) # and your extra_php-fpm.conf will be appended (typically contains PHP upload limits)
@ -26,25 +26,6 @@ YNH_PHP_VERSION=${YNH_PHP_VERSION:-$YNH_DEFAULT_PHP_VERSION}
# Otherwise, if you want the user to have control over these, we encourage to create a config panel # Otherwise, if you want the user to have control over these, we encourage to create a config panel
# (which should ultimately be standardized by the core ...) # (which should ultimately be standardized by the core ...)
# #
# Case 2 (deprecate) : you provided an entire conf/php-fpm.conf
#
# The configuration will be hydrated, replacing __FOOBAR__ placeholders with $foobar values, etc.
#
# The resulting configuration will be deployed to the appropriate place, /etc/php/$phpversion/fpm/pool.d/$app.conf
#
# ----------------------
#
# fpm_footprint: Memory footprint of the service (low/medium/high).
# low - Less than 20 MB of RAM by pool.
# medium - Between 20 MB and 40 MB of RAM by pool.
# high - More than 40 MB of RAM by pool.
# N - Or you can specify a quantitative footprint as MB by pool (use watch -n0.5 ps -o user,cmd,%cpu,rss -u APP)
#
# fpm_usage: Expected usage of the service (low/medium/high).
# low - Personal usage, behind the SSO.
# medium - Low usage, few people or/and publicly accessible.
# high - High usage, frequently visited website.
#
# The footprint of the service will be used to defined the maximum footprint we can allow, which is half the maximum RAM. # The footprint of the service will be used to defined the maximum footprint we can allow, which is half the maximum RAM.
# So it will be used to defined 'pm.max_children' # So it will be used to defined 'pm.max_children'
# A lower value for the footprint will allow more children for 'pm.max_children'. And so for # A lower value for the footprint will allow more children for 'pm.max_children'. And so for
@ -69,52 +50,15 @@ YNH_PHP_VERSION=${YNH_PHP_VERSION:-$YNH_DEFAULT_PHP_VERSION}
# Requires YunoHost version 4.1.0 or higher. # Requires YunoHost version 4.1.0 or higher.
ynh_add_fpm_config() { ynh_add_fpm_config() {
# ============ Argument parsing ============= # ============ Argument parsing =============
local _globalphpversion=${phpversion-:} local -A args_array=([g]=group=)
local -A args_array=([v]=phpversion= [u]=usage= [f]=footprint= [g]=group=)
local group local group
local phpversion
local usage
local footprint
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
group=${group:-} group=${group:-}
# =========================================== # ===========================================
# The default behaviour is to use the template.
local autogenconf=false
usage="${usage:-}"
footprint="${footprint:-}"
if [ -n "$usage" ] || [ -n "$footprint" ] || [[ -e $YNH_APP_BASEDIR/conf/extra_php-fpm.conf ]]; then
autogenconf=true
# If no usage provided, default to the value existing in setting ... or to low
local fpm_usage_in_setting=$(ynh_app_setting_get --key=fpm_usage)
if [ -z "$usage" ]
then
usage=${fpm_usage_in_setting:-low}
ynh_app_setting_set --key=fpm_usage --value=$usage
fi
# If no footprint provided, default to the value existing in setting ... or to low
local fpm_footprint_in_setting=$(ynh_app_setting_get --key=fpm_footprint)
if [ -z "$footprint" ]
then
footprint=${fpm_footprint_in_setting:-low}
ynh_app_setting_set --key=fpm_footprint --value=$footprint
fi
fi
# Set the default PHP-FPM version by default
if dpkg --compare-versions ${YNH_APP_PACKAGING_FORMAT:-0} lt 2; then
phpversion="${phpversion:-$YNH_PHP_VERSION}"
else
phpversion="${phpversion:-$_globalphpversion}"
fi
local old_phpversion=$(ynh_app_setting_get --key=phpversion)
# If the PHP version changed, remove the old fpm conf # If the PHP version changed, remove the old fpm conf
# (NB: This stuff is also handled by the apt helper, which is usually triggered before this helper) # (NB: This stuff is also handled by the apt helper, which is usually triggered before this helper)
local old_phpversion=$(ynh_app_setting_get --key=phpversion)
if [ -n "$old_phpversion" ] && [ "$old_phpversion" != "$phpversion" ]; then if [ -n "$old_phpversion" ] && [ "$old_phpversion" != "$phpversion" ]; then
local old_php_fpm_config_dir=$(ynh_app_setting_get --key=fpm_config_dir) local old_php_fpm_config_dir=$(ynh_app_setting_get --key=fpm_config_dir)
local old_php_finalphpconf="$old_php_fpm_config_dir/pool.d/$app.conf" local old_php_finalphpconf="$old_php_fpm_config_dir/pool.d/$app.conf"
@ -136,20 +80,12 @@ ynh_add_fpm_config() {
ynh_app_setting_set --key=fpm_service --value="$fpm_service" ynh_app_setting_set --key=fpm_service --value="$fpm_service"
ynh_app_setting_set --key=phpversion --value=$phpversion ynh_app_setting_set --key=phpversion --value=$phpversion
if [ $autogenconf == "false" ]; then # Define the values to use for the configuration of PHP.
# Usage 1, use the template in conf/php-fpm.conf ynh_get_scalable_phpfpm
local phpfpm_path="$YNH_APP_BASEDIR/conf/php-fpm.conf"
# Make sure now that the template indeed exists
[ -e "$phpfpm_path" ] || ynh_die --message="Unable to find template to configure PHP-FPM."
else
# Usage 2, generate a PHP-FPM config file with ynh_get_scalable_phpfpm
# Define the values to use for the configuration of PHP. local phpfpm_group=$([[ -n "$group" ]] && echo "$group" || echo "$app")
ynh_get_scalable_phpfpm --usage=$usage --footprint=$footprint local phpfpm_path="$YNH_APP_BASEDIR/conf/php-fpm.conf"
echo "
local phpfpm_group=$([[ -n "$group" ]] && echo "$group" || echo "$app")
local phpfpm_path="$YNH_APP_BASEDIR/conf/php-fpm.conf"
echo "
[__APP__] [__APP__]
user = __APP__ user = __APP__
@ -167,23 +103,22 @@ pm.max_requests = 500
request_terminate_timeout = 1d request_terminate_timeout = 1d
" >"$phpfpm_path" " >"$phpfpm_path"
if [ "$php_pm" = "dynamic" ]; then if [ "$php_pm" = "dynamic" ]; then
echo " echo "
pm.start_servers = __PHP_START_SERVERS__ pm.start_servers = __PHP_START_SERVERS__
pm.min_spare_servers = __PHP_MIN_SPARE_SERVERS__ pm.min_spare_servers = __PHP_MIN_SPARE_SERVERS__
pm.max_spare_servers = __PHP_MAX_SPARE_SERVERS__ pm.max_spare_servers = __PHP_MAX_SPARE_SERVERS__
" >>"$phpfpm_path" " >>"$phpfpm_path"
elif [ "$php_pm" = "ondemand" ]; then elif [ "$php_pm" = "ondemand" ]; then
echo " echo "
pm.process_idle_timeout = 10s pm.process_idle_timeout = 10s
" >>"$phpfpm_path" " >>"$phpfpm_path"
fi fi
# Concatene the extra config. # Concatene the extra config.
if [ -e $YNH_APP_BASEDIR/conf/extra_php-fpm.conf ]; then if [ -e $YNH_APP_BASEDIR/conf/extra_php-fpm.conf ]; then
cat $YNH_APP_BASEDIR/conf/extra_php-fpm.conf >>"$phpfpm_path" cat $YNH_APP_BASEDIR/conf/extra_php-fpm.conf >>"$phpfpm_path"
fi
fi fi
local finalphpconf="$fpm_config_dir/pool.d/$app.conf" local finalphpconf="$fpm_config_dir/pool.d/$app.conf"
@ -206,17 +141,6 @@ pm.process_idle_timeout = 10s
ynh_remove_fpm_config() { ynh_remove_fpm_config() {
local fpm_config_dir=$(ynh_app_setting_get --key=fpm_config_dir) local fpm_config_dir=$(ynh_app_setting_get --key=fpm_config_dir)
local fpm_service=$(ynh_app_setting_get --key=fpm_service) local fpm_service=$(ynh_app_setting_get --key=fpm_service)
# Get the version of PHP used by this app
local phpversion=$(ynh_app_setting_get --key=phpversion)
# Assume default PHP-FPM version by default
phpversion="${phpversion:-$YNH_DEFAULT_PHP_VERSION}"
# Assume default PHP files if not set
if [ -z "$fpm_config_dir" ]; then
fpm_config_dir="/etc/php/$YNH_DEFAULT_PHP_VERSION/fpm"
fpm_service="php$YNH_DEFAULT_PHP_VERSION-fpm"
fi
ynh_secure_remove --file="$fpm_config_dir/pool.d/$app.conf" ynh_secure_remove --file="$fpm_config_dir/pool.d/$app.conf"
ynh_systemd_action --service_name=$fpm_service --action=reload ynh_systemd_action --service_name=$fpm_service --action=reload
@ -226,8 +150,8 @@ ynh_remove_fpm_config() {
# #
# [internal] # [internal]
# #
# usage: ynh_get_scalable_phpfpm --usage=usage --footprint=footprint [--print] # usage: ynh_get_scalable_phpfpm
# | arg: -f, --footprint= - Memory footprint of the service (low/medium/high). # Footprint can be defined via the "fpm_footprint", to be set prior to calling this helper
# low - Less than 20 MB of RAM by pool. # low - Less than 20 MB of RAM by pool.
# medium - Between 20 MB and 40 MB of RAM by pool. # medium - Between 20 MB and 40 MB of RAM by pool.
# high - More than 40 MB of RAM by pool. # high - More than 40 MB of RAM by pool.
@ -235,24 +159,24 @@ ynh_remove_fpm_config() {
# To have this value, use the following command and stress the service. # To have this value, use the following command and stress the service.
# watch -n0.5 ps -o user,cmd,%cpu,rss -u APP # watch -n0.5 ps -o user,cmd,%cpu,rss -u APP
# #
# | arg: -u, --usage= - Expected usage of the service (low/medium/high). # Usage can be defined via the "fpm_usage", to be set prior to calling this helper
# low - Personal usage, behind the SSO. # low - Personal usage, behind the SSO.
# medium - Low usage, few people or/and publicly accessible. # medium - Low usage, few people or/and publicly accessible.
# high - High usage, frequently visited website. # high - High usage, frequently visited website.
# #
# | arg: -p, --print - Print the result (intended for debug purpose only when packaging the app)
ynh_get_scalable_phpfpm() { ynh_get_scalable_phpfpm() {
# ============ Argument parsing =============
local -A args_array=([u]=usage= [f]=footprint= [p]=print)
local usage
local footprint
local print
ynh_handle_getopts_args "$@"
# Set all characters as lowercase
footprint=${footprint,,}
usage=${usage,,}
print=${print:-0}
# If no usage provided, default to the value existing in setting ... or to low
local fpm_usage_in_setting=$(ynh_app_setting_get --key=fpm_usage)
local usage=${fpm_usage_in_setting:-low}
ynh_app_setting_set --key=fpm_usage --value=$usage
# If no footprint provided, default to the value existing in setting ... or to low
local fpm_footprint_in_setting=$(ynh_app_setting_get --key=fpm_footprint)
local footprint=${fpm_footprint_in_setting:-low}
ynh_app_setting_set --key=fpm_footprint --value=$footprint
# Set all characters as lowercase
if [ "$footprint" = "low" ]; then if [ "$footprint" = "low" ]; then
footprint=20 footprint=20
elif [ "$footprint" = "medium" ]; then elif [ "$footprint" = "medium" ]; then
@ -364,27 +288,19 @@ YNH_COMPOSER_VERSION=${YNH_COMPOSER_VERSION:-$YNH_DEFAULT_COMPOSER_VERSION}
# Execute a command with Composer # Execute a command with Composer
# #
# usage: ynh_composer_exec [--phpversion=phpversion] [--workdir=$install_dir] --commands="commands" # usage: ynh_composer_exec [--workdir=$install_dir] --commands="commands"
# | arg: -v, --phpversion - PHP version to use with composer
# | arg: -w, --workdir - The directory from where the command will be executed. Default $install_dir or $final_path # | arg: -w, --workdir - The directory from where the command will be executed. Default $install_dir or $final_path
# | arg: -c, --commands - Commands to execute. # | arg: -c, --commands - Commands to execute.
# #
# Requires YunoHost version 4.2 or higher. # Requires YunoHost version 4.2 or higher.
ynh_composer_exec() { ynh_composer_exec() {
# ============ Argument parsing ============= # ============ Argument parsing =============
local _globalphpversion=${phpversion-:} local -A args_array=([w]=workdir= [c]=commands=)
local -A args_array=([v]=phpversion= [w]=workdir= [c]=commands=)
local phpversion
local workdir local workdir
local commands local commands
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
workdir="${workdir:-${install_dir:-$final_path}}" workdir="${workdir:-${install_dir:-$final_path}}"
# ===========================================
if dpkg --compare-versions ${YNH_APP_PACKAGING_FORMAT:-0} lt 2; then
phpversion="${phpversion:-$YNH_PHP_VERSION}"
else
phpversion="${phpversion:-$_globalphpversion}"
fi
COMPOSER_HOME="$workdir/.composer" COMPOSER_MEMORY_LIMIT=-1 \ COMPOSER_HOME="$workdir/.composer" COMPOSER_MEMORY_LIMIT=-1 \
php${phpversion} "$workdir/composer.phar" $commands \ php${phpversion} "$workdir/composer.phar" $commands \
@ -393,42 +309,25 @@ ynh_composer_exec() {
# Install and initialize Composer in the given directory # Install and initialize Composer in the given directory
# #
# usage: ynh_install_composer [--phpversion=phpversion] [--workdir=$install_dir] [--install_args="--optimize-autoloader"] [--composerversion=composerversion] # usage: ynh_install_composer [--workdir=$install_dir] [--install_args="--optimize-autoloader"]
# | arg: -v, --phpversion - PHP version to use with composer
# | arg: -w, --workdir - The directory from where the command will be executed. Default $install_dir. # | arg: -w, --workdir - The directory from where the command will be executed. Default $install_dir.
# | arg: -a, --install_args - Additional arguments provided to the composer install. Argument --no-dev already include # | arg: -a, --install_args - Additional arguments provided to the composer install. Argument --no-dev already include
# | arg: -c, --composerversion - Composer version to install
# #
# Requires YunoHost version 4.2 or higher. # Requires YunoHost version 4.2 or higher.
ynh_install_composer() { ynh_install_composer() {
# ============ Argument parsing ============= # ============ Argument parsing =============
local _globalphpversion=${phpversion-:} local -A args_array=([w]=workdir= [a]=install_args=)
local -A args_array=([v]=phpversion= [w]=workdir= [a]=install_args= [c]=composerversion=)
local phpversion
local workdir local workdir
local install_args local install_args
local composerversion
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
# =========================================== # ===========================================
if dpkg --compare-versions ${YNH_APP_PACKAGING_FORMAT:-0} lt 2; then workdir="${workdir:-$install_dir}"
workdir="${workdir:-$final_path}"
else
workdir="${workdir:-$install_dir}"
fi
if dpkg --compare-versions ${YNH_APP_PACKAGING_FORMAT:-0} lt 2; then
phpversion="${phpversion:-$YNH_PHP_VERSION}"
else
phpversion="${phpversion:-$_globalphpversion}"
fi
install_args="${install_args:-}" install_args="${install_args:-}"
composerversion="${composerversion:-$YNH_COMPOSER_VERSION}"
curl -sS https://getcomposer.org/installer \ curl -sS https://getcomposer.org/installer \
| COMPOSER_HOME="$workdir/.composer" \ | COMPOSER_HOME="$workdir/.composer" \
php${phpversion} -- --quiet --install-dir="$workdir" --version=$composerversion \ php${phpversion} -- --quiet --install-dir="$workdir" --version=$YNH_COMPOSER_VERSION \
|| ynh_die --message="Unable to install Composer." || ynh_die --message="Unable to install Composer."
# install dependencies # install dependencies