From c6e6718da6cabcea38a64de1466aaad26ab21908 Mon Sep 17 00:00:00 2001 From: Jimmy Monin Date: Tue, 24 Apr 2018 17:45:19 +0200 Subject: [PATCH] Workaround to use latest systemd helpers in Jessie --- scripts/_common.sh | 77 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/install | 2 +- scripts/remove | 2 +- scripts/restore | 2 +- scripts/upgrade | 2 +- 5 files changed, 81 insertions(+), 4 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index 7f92068..bd9ea6b 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -170,6 +170,83 @@ ynh_psql_drop_user() { su --command="dropuser \"${user}\"" postgres } +# ============= MODIFIED EXISTING YUNOHOST HELPERS ============= + +# Create a dedicated systemd config +# +# usage: ynh_add_systemd_config [service] [template] +# | arg: service - Service name (optionnal, $app by default) +# | arg: template - Name of template file (optionnal, this is 'systemd' by default, meaning ./conf/systemd.service will be used as template) +# +# This will use the template ../conf/.service +# to generate a systemd config, by replacing the following keywords +# with global variables that should be defined before calling +# this helper : +# +# __APP__ by $app +# __FINALPATH__ by $final_path +# +ynh_add_systemd_config () { + local service_name="${1:-$app}" + + finalsystemdconf="/etc/systemd/system/$service_name.service" + ynh_backup_if_checksum_is_different "$finalsystemdconf" + sudo cp ../conf/${2:-systemd.service} "$finalsystemdconf" + + # To avoid a break by set -u, use a void substitution ${var:-}. If the variable is not set, it's simply set with an empty variable. + # Substitute in a nginx config file only if the variable is not empty + if test -n "${final_path:-}"; then + ynh_replace_string "__FINALPATH__" "$final_path" "$finalsystemdconf" + fi + if test -n "${app:-}"; then + ynh_replace_string "__APP__" "$app" "$finalsystemdconf" + fi + ynh_store_file_checksum "$finalsystemdconf" + + sudo chown root: "$finalsystemdconf" + sudo systemctl enable $service_name + sudo systemctl daemon-reload +} + +# Remove the dedicated systemd config +# +# usage: ynh_remove_systemd_config [service] +# | arg: service - Service name (optionnal, $app by default) +# +ynh_remove_systemd_config () { + local service_name="${1:-$app}" + + local finalsystemdconf="/etc/systemd/system/$service_name.service" + if [ -e "$finalsystemdconf" ]; then + sudo systemctl stop $service_name + sudo systemctl disable $service_name + ynh_secure_remove "$finalsystemdconf" + sudo systemctl daemon-reload + fi +} + +# Create a system user +# +# usage: ynh_system_user_create user_name [home_dir] [use_shell] +# | arg: user_name - Name of the system user that will be create +# | arg: home_dir - Path of the home dir for the user. Usually the final path of the app. If this argument is omitted, the user will be created without home +# | arg: use_shell - Create a user using the default shell if present. If this argument is omitted, the user will be created with /usr/sbin/nologin shell +ynh_system_user_create () { + if ! ynh_system_user_exists "$1" # Check if the user exists on the system + then # If the user doesn't exist + if [ $# -ge 2 ]; then # If a home dir is mentioned + local user_home_dir="-d $2" + else + local user_home_dir="--no-create-home" + fi + if [ $# -ge 3 ]; then # If we want a shell for the user + local shell="" # Use default shell + else + local shell="--shell /usr/sbin/nologin" + fi + useradd $user_home_dir --system --user-group $1 $shell || ynh_die "Unable to create $1 system account" + fi +} # ============= FUTURE YUNOHOST HELPERS ============= diff --git a/scripts/install b/scripts/install index d12a003..eb75374 100644 --- a/scripts/install +++ b/scripts/install @@ -7,8 +7,8 @@ shopt -s extglob # sets extended pattern matching options in the bash shell # IMPORT GENERIC HELPERS #================================================= -source _common.sh source /usr/share/yunohost/helpers +source _common.sh #================================================= # MANAGE SCRIPT FAILURE diff --git a/scripts/remove b/scripts/remove index 925e711..5e0530c 100644 --- a/scripts/remove +++ b/scripts/remove @@ -6,8 +6,8 @@ # IMPORT GENERIC HELPERS #================================================= -source _common.sh source /usr/share/yunohost/helpers +source _common.sh #================================================= # LOAD SETTINGS diff --git a/scripts/restore b/scripts/restore index 387fd6d..d3032e4 100644 --- a/scripts/restore +++ b/scripts/restore @@ -9,8 +9,8 @@ if [ ! -e _common.sh ]; then cp ../settings/scripts/_common.sh ./_common.sh chmod a+rx _common.sh fi -source _common.sh source /usr/share/yunohost/helpers +source _common.sh #================================================= # MANAGE SCRIPT FAILURE diff --git a/scripts/upgrade b/scripts/upgrade index ddbd6aa..8a3313d 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -11,8 +11,8 @@ if [ ! -e _common.sh ]; then cp ../settings/scripts/_common.sh ./_common.sh chmod a+rx _common.sh fi -source _common.sh source /usr/share/yunohost/helpers +source _common.sh #================================================= # MANAGE SCRIPT FAILURE