diff --git a/scripts/_common.sh b/scripts/_common.sh index a6b1ce9..8078143 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -1,7 +1,90 @@ #!/bin/bash -CHECK_VAR () { # Verifies that the variable is not empty. - # $1 = Variable to be checked - # $2 = Display text on error - test -n "$1" || (echo "$2" >&2 && false) +# Start (or other actions) a service, print a log in case of failure and optionnaly wait until the service is completely started +# +# usage: ynh_systemd_action [-n service_name] [-a action] [ [-l "line to match"] [-p log_path] [-t timeout] [-e length] ] +# | arg: -n, --service_name= - Name of the service to reload. Default : $app +# | arg: -a, --action= - Action to perform with systemctl. Default: start +# | arg: -l, --line_match= - Line to match - The line to find in the log to attest the service have finished to boot. +# If not defined it don't wait until the service is completely started. +# | arg: -p, --log_path= - Log file - Path to the log file. Default : /var/log/$app/$app.log +# | arg: -t, --timeout= - Timeout - The maximum time to wait before ending the watching. Default : 300 seconds. +# | arg: -e, --length= - Length of the error log : Default : 20 +ynh_systemd_action() { + # Declare an array to define the options of this helper. + declare -Ar args_array=( [n]=service_name= [a]=action= [l]=line_match= [p]=log_path= [t]=timeout= [e]=length= ) + local service_name + local action + local line_match + local length + local log_path + local timeout + + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + local service_name="${service_name:-$app}" + local action=${action:-start} + local log_path="${log_path:-/var/log/$service_name/$service_name.log}" + local length=${length:-20} + local timeout=${timeout:-300} + + # Start to read the log + if [[ -n "${line_match:-}" ]] + then + local templog="$(mktemp)" + # Following the starting of the app in its log + if [ "$log_path" == "systemd" ] ; then + # Read the systemd journal + journalctl -u $service_name -f --since=-45 > "$templog" & + else + # Read the specified log file + tail -F -n0 "$log_path" > "$templog" & + fi + # Get the PID of the tail command + local pid_tail=$! + fi + + echo "${action^} the service $service_name" >&2 + systemctl $action $service_name \ + || ( journalctl --lines=$length -u $service_name >&2 \ + ; test -n "$log_path" && echo "--" && tail --lines=$length "$log_path" >&2 \ + ; false ) + + # Start the timeout and try to find line_match + if [[ -n "${line_match:-}" ]] + then + local i=0 + for i in $(seq 1 $timeout) + do + # Read the log until the sentence is found, that means the app finished to start. Or run until the timeout + if grep --quiet "$line_match" "$templog" + then + echo "The service $service_name has correctly started." >&2 + break + fi + echo -n "." >&2 + sleep 1 + done + if [ $i -eq $timeout ] + then + echo "The service $service_name didn't fully started before the timeout." >&2 + echo "Please find here an extract of the end of the log of the service $service_name:" + journalctl --lines=$length -u $service_name >&2 + test -n "$log_path" && echo "--" && tail --lines=$length "$log_path" >&2 + fi + + echo "" + ynh_clean_check_starting + fi } + +# Clean temporary process and file used by ynh_check_starting +# (usually used in ynh_clean_setup scripts) +# +# usage: ynh_clean_check_starting +ynh_clean_check_starting () { + # Stop the execution of tail. + kill -s 15 $pid_tail 2>&1 + ynh_secure_remove "$templog" 2>&1 +} \ No newline at end of file diff --git a/scripts/install b/scripts/install index 23bedcb..9a54996 100644 --- a/scripts/install +++ b/scripts/install @@ -183,11 +183,11 @@ sudo chown -R www-data $final_path #================================================= systemctl enable $app.service -systemctl start $app +ynh_systemd_action -n $app -a start -l "Server available at" -p "systemd" #================================================= # RELOAD NGINX #================================================= # Reload Nginx -sudo service nginx reload +systemctl reload nginx diff --git a/scripts/restore b/scripts/restore index 02f5892..17bb378 100644 --- a/scripts/restore +++ b/scripts/restore @@ -77,7 +77,7 @@ ynh_install_app_dependencies build-essential libssl-dev zlib1g-dev libpng-dev li ynh_restore_file "/etc/systemd/system/$app.service" systemctl enable $app.service -systemctl start $app +ynh_systemd_action -n $app -a start -l "Server available at" -p "systemd" #================================================= # ADVERTISE SERVICE IN ADMIN PANEL diff --git a/scripts/upgrade b/scripts/upgrade index 36ec1ea..04c96c8 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -139,7 +139,7 @@ yunohost service add $app --log "/var/log/$app.log" # RESTART LSTU #================================================= -systemctl reload $app +ynh_systemd_action -n $app -a reload -l "Starting hot deployment for Hypnotoad server" -p "systemd" #================================================= # SETUP SSOWAT