From 05a4aac76efe289e8b3736d1193fa0746b8031a8 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 10 Dec 2024 21:33:50 +0100 Subject: [PATCH] Simplify ynh_app_spawn_shell code for helpers 2.1 now that the app's settings are loaded in the context --- helpers/helpers.v2.1.d/utils | 58 +++++++++++++++++------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/helpers/helpers.v2.1.d/utils b/helpers/helpers.v2.1.d/utils index c090d91ee6..05d3bf1853 100644 --- a/helpers/helpers.v2.1.d/utils +++ b/helpers/helpers.v2.1.d/utils @@ -397,17 +397,16 @@ ynh_user_list() { # Spawn a Bash shell with the app environment loaded # -# usage: ynh_spawn_app_shell "appname" +# usage: ynh_spawn_app_shell # # examples: -# ynh_spawn_app_shell "foobar" <<< 'echo "$USER"' -# ynh_spawn_app_shell "foobar" < /tmp/some_script.bash +# ynh_spawn_app_shell <<< 'echo "$USER"' +# ynh_spawn_app_shell < /tmp/some_script.bash # # The spawned shell will have environment variables loaded and environment files sourced # from the app's service configuration file (defaults to $app.service, overridable by the packager with `service` setting). # If the app relies on a specific PHP version, then `php` will be aliased that version. The PHP command will also be appended with the `phpflags` settings. ynh_spawn_app_shell() { - local app=$1 # Force Bash to be used to run this helper [[ $0 =~ \/?bash$ ]] || ynh_die "Please use Bash as shell" @@ -419,52 +418,49 @@ ynh_spawn_app_shell() { id -u "$app" &> /dev/null || ynh_die "There is no \"$app\" system user" # Make sure the app has an install_dir setting - local install_dir=$(ynh_app_setting_get --app=$app --key=install_dir) - [ -n "$install_dir" ] || ynh_die "$app has no install_dir setting (does it use packaging format >=2?)" - - # Load the app's service name, or default to $app - local service=$(ynh_app_setting_get --app=$app --key=service) - [ -z "$service" ] && service=$app + [ -n "${install_dir:-}" ] || ynh_die "$app has no install_dir setting!" # Export HOME variable export HOME=$install_dir - # Load the Environment variables from the app's service - local env_var=$(systemctl show $service.service -p "Environment" --value) - [ -n "$env_var" ] && export $env_var - # Force `php` to its intended version # We use `eval`+`export` since `alias` is not propagated to subshells, even with `export` - local phpversion=$(ynh_app_setting_get --app=$app --key=php_version) - local phpflags=$(ynh_app_setting_get --app=$app --key=phpflags) - if [ -n "$phpversion" ]; then - eval "php() { php${phpversion} ${phpflags} \"\$@\"; }" + if [ -n "${php_version:-}" ]; then + eval "php() { php${php_version} ${phpflags:-} \"\$@\"; }" export -f php fi - # Source the EnvironmentFiles from the app's service - local env_files=($(systemctl show $service.service -p "EnvironmentFiles" --value)) - if [ ${#env_files[*]} -gt 0 ]; then - # set -/+a enables and disables new variables being automatically exported. Needed when using `source`. - set -a - for file in ${env_files[*]}; do - [[ $file = /* ]] && source $file - done - set +a + # Load the app's service name, or default to $app + service=${service:-$app} + if systemctl list-units | grep -q "$service.service" + then + # Load the Environment variables from the app's service + local env_var=$(systemctl show $service.service -p "Environment" --value) + [ -n "${env_var:-}" ] && export $env_var + + # Source the EnvironmentFiles from the app's service + local env_files=($(systemctl show $service.service -p "EnvironmentFiles" --value)) + if [ ${#env_files[*]} -gt 0 ]; then + # set -/+a enables and disables new variables being automatically exported. Needed when using `source`. + set -a + for file in ${env_files[*]}; do + [[ $file = /* ]] && source $file + done + set +a + fi fi # Activate the Python environment, if it exists - if [ -f $install_dir/venv/bin/activate ]; then + if [ -f "$install_dir/venv/bin/activate" ]; then # set -/+a enables and disables new variables being automatically exported. Needed when using `source`. set -a - source $install_dir/venv/bin/activate + source "$install_dir/venv/bin/activate" set +a fi # cd into the WorkingDirectory set in the service, or default to the install_dir local env_dir=$(systemctl show $service.service -p "WorkingDirectory" --value) - [ -z $env_dir ] && env_dir=$install_dir - cd $env_dir + cd "${env_dir:-$install_dir}" # Spawn the app shell su -s /bin/bash $app