diff --git a/conf/systemd.service b/conf/systemd.service index b5a4e27..baf1489 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -7,7 +7,7 @@ Type=simple User=ztncui Group=ztncui Environment="PATH=__PATH__" -WorkingDirectory=__FINAL_PATH__/src/ +WorkingDirectory=__FINALPATH__/src/ ExecStart=__NODEJS_PATH__/npm start [Install] diff --git a/scripts/_common.sh b/scripts/_common.sh index f55d066..da093a9 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -32,6 +32,67 @@ exec_as() { fi } +# Create a dedicated systemd config +# +# usage: ynh_add_systemd_config [--service=service] [--template=template] [--others_var="list of others variables to replace"] +# | arg: -s, --service - Service name (optional, $app by default) +# | arg: -t, --template - Name of template file (optional, this is 'systemd' by default, meaning ./conf/systemd.service will be used as template) +# | arg: -v, --others_var - List of others variables to replace separated by a space. For example: 'var_1 var_2 ...' +# +# 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 +# +# And dynamic variables (from the last example) : +# __VAR_1__ by $var_1 +# __VAR_2__ by $var_2 +# +# Requires YunoHost version 2.7.2 or higher. +ynh_add_systemd_config_vars () { + # Declare an array to define the options of this helper. + local legacy_args=stv + declare -Ar args_array=( [s]=service= [t]=template= [v]=others_var= ) + local service + local template + local others_var + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + service="${service:-$app}" + template="${template:-systemd.service}" + others_var="${others_var:-}" + + finalsystemdconf="/etc/systemd/system/$service.service" + ynh_backup_if_checksum_is_different --file="$finalsystemdconf" + cp ../conf/$template "$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 --match_string="__FINALPATH__" --replace_string="$final_path" --target_file="$finalsystemdconf" + fi + if test -n "${app:-}"; then + ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="$finalsystemdconf" + fi + + # Replace all other variables given as arguments + for var_to_replace in $others_var + do + # ${var_to_replace^^} make the content of the variable on upper-cases + # ${!var_to_replace} get the content of the variable named $var_to_replace + ynh_replace_string --match_string="__${var_to_replace^^}__" --replace_string="${!var_to_replace}" --target_file="$finalsystemdconf" + done + + ynh_store_file_checksum --file="$finalsystemdconf" + + chown root: "$finalsystemdconf" + systemctl enable $service + systemctl daemon-reload +} + #================================================= # FUTURE OFFICIAL HELPERS #================================================= diff --git a/scripts/install b/scripts/install index 21f9cbe..19c9851 100644 --- a/scripts/install +++ b/scripts/install @@ -211,13 +211,8 @@ ynh_script_progression --message="Configuring a systemd service..." --time --wei ### - As well as the section "RESTORE SYSTEMD" in the restore script ### - And the section "SETUP SYSTEMD" in the upgrade script -# Set the systemd service settings -ynh_replace_string "__PATH__" "$PATH" "../conf/systemd.service" -ynh_replace_string "__NODEJS_PATH__" "$nodejs_path" "../conf/systemd.service" -ynh_replace_string "__FINAL_PATH__" "$final_path" "../conf/systemd.service" - # Create a dedicated systemd config -ynh_add_systemd_config +ynh_add_systemd_config_vars --others_var="path nodejs_path" #================================================= # GENERIC FINALIZATION diff --git a/scripts/upgrade b/scripts/upgrade index f4c271a..e2a18a1 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -183,13 +183,8 @@ ynh_use_logrotate --non-append #================================================= ynh_script_progression --message="Upgrading systemd configuration..." --time --weight=1 -# Set the systemd service settings -ynh_replace_string "__PATH__" "$PATH" "../conf/systemd.service" -ynh_replace_string "__NODEJS_PATH__" "$nodejs_path" "../conf/systemd.service" -ynh_replace_string "__FINAL_PATH__" "$final_path" "../conf/systemd.service" - # Create a dedicated systemd config -ynh_add_systemd_config +ynh_add_systemd_config_vars --others_var="path nodejs_path" #================================================= # GENERIC FINALIZATION