Skip to content

Commit

Permalink
Simplify ynh_app_spawn_shell code for helpers 2.1 now that the app's …
Browse files Browse the repository at this point in the history
…settings are loaded in the context
  • Loading branch information
alexAubin committed Dec 10, 2024
1 parent 082ca97 commit 05a4aac
Showing 1 changed file with 27 additions and 31 deletions.
58 changes: 27 additions & 31 deletions helpers/helpers.v2.1.d/utils
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 05a4aac

Please sign in to comment.