Add ynh_load_app_environment helper

This commit is contained in:
tituspijean 2023-05-09 21:29:52 +00:00
parent 3bb32dc1e4
commit d27e9a9eea
No known key found for this signature in database
GPG key ID: EF3B0D7CC0A94720
2 changed files with 60 additions and 9 deletions

View file

@ -111,3 +111,61 @@ ynh_remove_apps() {
done
fi
}
# Load an app environment in the current Bash shell
#
# usage: ynh_install_apps --app="app"
# | arg: -a, --app= - the app ID
#
# Requires YunoHost version 11.0.* or higher.
ynh_load_app_environment() {
# Declare an array to define the options of this helper.
local legacy_args=a
local -A args_array=([a]=app=)
local app
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
# Retrieve the list of installed apps
local installed_apps_list=($(yunohost app list --output-as json --quiet | jq -r .apps[].id))
# Force Bash to be used to run this helper
if [ $0 != "bash" ]
then
ynh_print_err --message="Please use Bash as shell"
exit 1
fi
# Make sure the app is installed
if [[ " ${installed_apps_list[*]} " != *" ${app} "* ]]
then
ynh_print_err --message="$app is not in the apps list"
exit 1
fi
# Make sure the app has an install_dir setting
install_dir="$(yunohost app setting $app install_dir)"
if [ -z "$install_dir" ]
then
ynh_print_err --message="$app has no install_dir setting (does it use packaging format >=2?)"
exit 1
fi
# Load the Environment variables from the app's service
env_var=`systemctl show $app.service -p "Environment" --value`
[ -n "$env_var" ] && export $env_var;
export HOME=$install_dir;
# Source the EnvironmentFiles from the app's service
env_files=(`systemctl show $app.service -p "EnvironmentFiles" --value`)
if [ ${#env_files[*]} -gt 0 ]
then
for file in ${env_files[*]}
do
[[ $file = /* ]] && source $file
done
fi
# Open the app shell
su -s /bin/bash $app
}

View file

@ -1655,15 +1655,8 @@ def app_shell(app):
"""
app_settings = _get_app_settings(app) or {}
#TODO init a env_dict
#TODO load the app's environment, parsed from:
#TODO - its settings (phpversion, ...)
#TODO - its service configuration (PATH, NodeJS production mode...)
#TODO this one could be performed in Bash, directly after initiating the subprocess:
#TODO - "Environment" clause: `systemctl show $app.service -p "Environment" --value`
#TODO - Source "EnvironmentFile" clauses
#TODO
#TODO find out how to open an interactive Bash shell from Python
#TODO Find out how to open an interactive Bash shell from Python
#TODO run `ynh_load_app_environment --app=$app` helper in there
def app_register_url(app, domain, path):
"""