# Manage a fail of the script # # [internal] # # usage: # ynh_exit_properly is used only by the helper ynh_abort_if_errors. # You should not use it directly. # Instead, add to your script: # ynh_clean_setup () { # instructions... # } # # This function provide a way to clean some residual of installation that not managed by remove script. # # It prints a warning to inform that the script was failed, and execute the ynh_clean_setup function if used in the app script # ynh_exit_properly () { local exit_code=$? if [ "$exit_code" -eq 0 ]; then exit 0 # Exit without error if the script ended correctly fi trap '' EXIT # Ignore new exit signals set +eu # Do not exit anymore if a command fail or if a variable is empty echo -e "!!\n $app's script has encountered an error. Its execution was cancelled.\n!!" >&2 if type -t ynh_clean_setup > /dev/null; then # Check if the function exist in the app script. ynh_clean_setup # Call the function to do specific cleaning for the app. fi ynh_die # Exit with error status } # Exits if an error occurs during the execution of the script. # # usage: ynh_abort_if_errors # # This configure the rest of the script execution such that, if an error occurs # or if an empty variable is used, the execution of the script stops # immediately and a call to `ynh_clean_setup` is triggered if it has been # defined by your script. # ynh_abort_if_errors () { set -eu # Exit if a command fail, and if a variable is used unset. trap ynh_exit_properly EXIT # Capturing exit signals on shell script } # Fetch the Debian release codename # # usage: ynh_get_debian_release # | ret: The Debian release codename (i.e. jessie, stretch, ...) ynh_get_debian_release () { echo $(lsb_release --codename --short) } # 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 --unit=$service_name --follow --since=-0 --quiet > "$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 }