diff --git a/data/helpers.d/utils b/data/helpers.d/utils index 630ee539e..a93fbf227 100644 --- a/data/helpers.d/utils +++ b/data/helpers.d/utils @@ -16,7 +16,6 @@ # # It prints a warning to inform that the script was failed, and execute the ynh_clean_setup function if used in the app script # -# Requires YunoHost version 2.6.4 or higher. ynh_exit_properly () { local exit_code=$? if [ "$exit_code" -eq 0 ]; then @@ -26,8 +25,46 @@ ynh_exit_properly () { trap '' EXIT # Ignore new exit signals set +eu # Do not exit anymore if a command fail or if a variable is empty + # Small tempo to avoid the next message being mixed up with other DEBUG messages + sleep 0.5 + ynh_print_err --message="!!\n $app's script has encountered an error. Its execution was cancelled.\n!!" + # If the script is executed from the CLI, dump the end of the log that precedes the crash. + if [ "$YNH_INTERFACE" == "cli" ] + then + # Unset xtrace to not spoil the log + set +x + + local ynh_log="/var/log/yunohost/yunohost-cli.log" + + # Wait for the log to be fill with the data until the crash. + local timeout=0 + while ! tail --lines=20 "$ynh_log" | grep --quiet "+ ynh_exit_properly" + do + ((timeout++)) + if [ $timeout -eq 500 ]; then + break + fi + done + + echo -e "\e[34m\e[1mPlease find here an extract of the log before the crash:\e[0m" >&2 + # Tail the last 30 lines of log of YunoHost + # But remove all lines after "ynh_exit_properly" + # Remove the timestamp at the beginning of the line + # Remove "yunohost.hook..." + # Add DEBUG and color it at the beginning of each log line. + echo -e "$(tail --lines=30 "$ynh_log" \ + | sed '1,/+ ynh_exit_properly/!d' \ + | sed 's/^[[:digit:]: ,-]*//g' \ + | sed 's/ *yunohost.hook.*\]/ -/g' \ + | sed 's/^WARNING /&/g' \ + | sed 's/^DEBUG /& /g' \ + | sed 's/^INFO /& /g' \ + | sed 's/^/\\e[34m\\e[1m[DEBUG]\\e[0m: /g')" >&2 + set -x + fi + 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 diff --git a/src/yunohost/hook.py b/src/yunohost/hook.py index 2841dd425..42807fdf7 100644 --- a/src/yunohost/hook.py +++ b/src/yunohost/hook.py @@ -28,7 +28,7 @@ import re import tempfile from glob import iglob -from moulinette import m18n +from moulinette import m18n, msettings from yunohost.utils.error import YunohostError from moulinette.utils import log from moulinette.utils.filesystem import read_json @@ -337,6 +337,8 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False, env = {} env['YNH_CWD'] = chdir + env['YNH_INTERFACE'] = msettings.get('interface') + stdinfo = os.path.join(tempfile.mkdtemp(), "stdinfo") env['YNH_STDINFO'] = stdinfo