From 83f4ee4a1dee60a631f0616fd922df83e7e130fe Mon Sep 17 00:00:00 2001 From: ericgaspar Date: Wed, 9 Dec 2020 17:49:11 +0100 Subject: [PATCH] Use ynh_exec_as instead of sudo --- scripts/_common.sh | 15 +++++++++++++++ scripts/install | 16 +++++++++------- scripts/upgrade | 8 ++++---- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index bf76472..522e19c 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -71,4 +71,19 @@ ynh_redis_get_free_db() { ynh_redis_remove_db() { local db=$1 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 } \ No newline at end of file diff --git a/scripts/install b/scripts/install index 39ebc2e..40f4a86 100755 --- a/scripts/install +++ b/scripts/install @@ -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 "$final_path" -sudo mkdir -p "$log_path" -sudo touch "${log_file}" +mkdir -p "$log_path" +touch "${log_file}" #================================================= # 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 virtualenv --python=python3 "${final_path}/venv" -sudo chown -R "$app" "$final_path" +chown -R "$app" "$final_path" #run source in a 'sub shell' ( set +o nounset source "${final_path}/venv/bin/activate" set -o nounset - sudo -u $app $final_path/venv/bin/pip install --upgrade pip - sudo -u $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 pip + ynh_exec_as $app $final_path/venv/bin/pip install --upgrade setuptools wheel psycopg2-binary + 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" - +#================================================= +# END OF SCRIPT +#================================================= ynh_script_progression --message="Installation of $app completed" --last diff --git a/scripts/upgrade b/scripts/upgrade index d244b34..2ab2f72 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -95,9 +95,9 @@ sudo chown -R "$app" "$final_path" set +o nounset source "${final_path}/venv/bin/activate" set -o nounset - sudo -u $app $final_path/venv/bin/pip install --upgrade pip - sudo -u $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 pip + ynh_exec_as $app $final_path/venv/bin/pip install --upgrade setuptools wheel psycopg2-binary + 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 #================================================= -ynh_script_progression --message="Upgrade of $app completed" --last \ No newline at end of file +ynh_script_progression --message="Upgrade of $app completed" --last