1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/pyinventory_ynh.git synced 2024-09-03 20:16:09 +02:00

Use ynh_exec_as instead of sudo

This commit is contained in:
ericgaspar 2020-12-09 17:49:11 +01:00
parent 0cfa0bd871
commit 83f4ee4a1d
No known key found for this signature in database
GPG key ID: 574F281483054D44
3 changed files with 28 additions and 11 deletions

View file

@ -71,4 +71,19 @@ ynh_redis_get_free_db() {
ynh_redis_remove_db() { ynh_redis_remove_db() {
local db=$1 local db=$1
redis-cli -n "$db" flushall redis-cli -n "$db" flushall
}
#=================================================
# Execute a command as another user
# usage: ynh_exec_as USER COMMAND [ARG ...]
ynh_exec_as() {
local USER=$1
shift 1
if [[ $USER = $(whoami) ]]; then
eval "$@"
else
sudo -u "$USER" "$@"
fi
} }

View file

@ -31,8 +31,8 @@ ynh_webpath_register --app="$app" --domain="$domain" --path_url="$path_url"
mkdir -p "$public_path/media" "$public_path/static" mkdir -p "$public_path/media" "$public_path/static"
mkdir -p "$final_path" mkdir -p "$final_path"
sudo mkdir -p "$log_path" mkdir -p "$log_path"
sudo touch "${log_file}" touch "${log_file}"
#================================================= #=================================================
# STORE SETTINGS FROM MANIFEST # STORE SETTINGS FROM MANIFEST
@ -103,16 +103,16 @@ ynh_system_user_create --username="$app" --home_dir="$final_path" --use_shell
ynh_script_progression --message="Install PyInventory using PIP..." --weight=80 ynh_script_progression --message="Install PyInventory using PIP..." --weight=80
virtualenv --python=python3 "${final_path}/venv" virtualenv --python=python3 "${final_path}/venv"
sudo chown -R "$app" "$final_path" chown -R "$app" "$final_path"
#run source in a 'sub shell' #run source in a 'sub shell'
( (
set +o nounset set +o nounset
source "${final_path}/venv/bin/activate" source "${final_path}/venv/bin/activate"
set -o nounset set -o nounset
sudo -u $app $final_path/venv/bin/pip install --upgrade pip ynh_exec_as $app $final_path/venv/bin/pip install --upgrade pip
sudo -u $app $final_path/venv/bin/pip install --upgrade setuptools wheel psycopg2-binary ynh_exec_as $app $final_path/venv/bin/pip install --upgrade setuptools wheel psycopg2-binary
sudo -u $app $final_path/venv/bin/pip install --upgrade pyinventory=="$pyinventory_version" ynh_exec_as $app $final_path/venv/bin/pip install --upgrade pyinventory=="$pyinventory_version"
) )
#================================================= #=================================================
@ -238,6 +238,8 @@ ynh_script_progression --message="Reloading nginx web server..."
ynh_systemd_action --service_name="nginx" --action="reload" ynh_systemd_action --service_name="nginx" --action="reload"
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed" --last ynh_script_progression --message="Installation of $app completed" --last

View file

@ -95,9 +95,9 @@ sudo chown -R "$app" "$final_path"
set +o nounset set +o nounset
source "${final_path}/venv/bin/activate" source "${final_path}/venv/bin/activate"
set -o nounset set -o nounset
sudo -u $app $final_path/venv/bin/pip install --upgrade pip ynh_exec_as $app $final_path/venv/bin/pip install --upgrade pip
sudo -u $app $final_path/venv/bin/pip install --upgrade setuptools wheel psycopg2-binary ynh_exec_as $app $final_path/venv/bin/pip install --upgrade setuptools wheel psycopg2-binary
sudo -u $app $final_path/venv/bin/pip install --upgrade pyinventory=="$pyinventory_version" ynh_exec_as $app $final_path/venv/bin/pip install --upgrade pyinventory=="$pyinventory_version"
) )
#================================================= #=================================================
@ -223,4 +223,4 @@ ynh_systemd_action --service_name=nginx --action=reload
# END OF SCRIPT # END OF SCRIPT
#================================================= #=================================================
ynh_script_progression --message="Upgrade of $app completed" --last ynh_script_progression --message="Upgrade of $app completed" --last