Skip to content

Commit

Permalink
Merge pull request #2009 from orhtej2/fix_helpers2.1_php
Browse files Browse the repository at this point in the history
Fix PHP sourcing in helpers 2.1
  • Loading branch information
alexAubin authored Dec 10, 2024
2 parents 7963e5c + 05a4aac commit bad1c5e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
8 changes: 4 additions & 4 deletions helpers/helpers.v1.d/getopts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
#
# Requires YunoHost version 3.2.2 or higher.
ynh_handle_getopts_args() {
# Trick to only re-enable debugging if it was set before
local xtrace_enable=$(set +o | grep xtrace)

# Manage arguments only if there's some provided
set +o xtrace # set +x
if [ $# -ne 0 ]; then
Expand Down Expand Up @@ -197,9 +200,6 @@ ynh_handle_getopts_args() {
if [ "${arguments[0]:0:1}" != "-" ]; then
# If not, enter in legacy mode and manage the arguments as positionnal ones..
# Dot not echo, to prevent to go through a helper output. But print only in the log.
set -x
echo "! Helper used in legacy mode !" > /dev/null
set +x
local i
for i in $(seq 0 $((${#arguments[@]} - 1))); do
# Try to use legacy_args as a list of option_flag of the array args_array
Expand Down Expand Up @@ -229,5 +229,5 @@ ynh_handle_getopts_args() {
parse_arg "${arguments[@]}"
fi
fi
set -o xtrace # set -x
eval "$xtrace_enable"
}
4 changes: 3 additions & 1 deletion helpers/helpers.v1.d/setting
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ ynh_app_setting_delete() {
# [internal]
#
ynh_app_setting() {
# Trick to only re-enable debugging if it was set before
local xtrace_enable=$(set +o | grep xtrace)
set +o xtrace # set +x
ACTION="$1" APP="$2" KEY="$3" VALUE="${4:-}" python3 - << EOF
import os, yaml, sys
Expand All @@ -150,7 +152,7 @@ else:
with open(setting_file, "w") as f:
yaml.safe_dump(settings, f, default_flow_style=False)
EOF
set -o xtrace # set -x
eval "$xtrace_enable"
}

# Check availability of a web path
Expand Down
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=phpversion)
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
3 changes: 2 additions & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,8 @@ def app_shell(app):
"/bin/bash",
"-c",
"source /usr/share/yunohost/helpers && ynh_spawn_app_shell " + app,
]
],
env=_make_environment_for_app_script(app)
)


Expand Down

0 comments on commit bad1c5e

Please sign in to comment.