Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump ynh log if an app script fails #687

Merged
merged 8 commits into from
May 22, 2019
36 changes: 35 additions & 1 deletion data/helpers.d/utils
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +27,41 @@ ynh_exit_properly () {

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' \
alexAubin marked this conversation as resolved.
Show resolved Hide resolved
| 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
Expand Down
4 changes: 3 additions & 1 deletion src/yunohost/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
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
Expand Down Expand Up @@ -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

Expand Down