Blame withemail.sh

850862
#!/bin/bash
850862
850862
set -e
850862
set -o pipefail
850862
850862
# accepts environment vars
850862
# EMAIL_SUCCESS
850862
# EMAIL_FAILED
850862
# EMAIL_QUIET
850862
# EMAIL_SUBJECT
850862
# EMAIL_BODY
850862
850862
850862
if [ ! -z "$EMAIL_QUIET" ]; then
850862
    # report only errors
850862
    EMAIL_SUCCESS=
850862
fi
850862
850862
if [ -z "$EMAIL_SUBJECT" ]; then
850862
    EMAIL_SUBJECT="builder task finished"
850862
fi
850862
850862
COMMAND="$@"
850862
LOG_FILE="/tmp/withemail-`uuidgen`.log"
850862
touch "$LOG_FILE"
850862
850862
540c27
print_email() {
540c27
    local MESSAGE="$1"
540c27
    echo "$EMAIL_BODY"
540c27
    echo
540c27
    echo "Command:"
540c27
    echo "    $COMMAND"
540c27
    echo "$MESSAGE"
540c27
    echo
540c27
    echo "log:"
540c27
    echo "----------------------------------------"
9a73df
    tail -n 100 "$LOG_FILE"
b82eb4
    echo "----------------------------------------"
b82eb4
    echo
540c27
}
540c27
850862
send_email() {
850862
    local EMAIL="$1"
850862
    local MESSAGE="$2"
850862
    echo "$MESSAGE"
850862
    if [ ! -z "$EMAIL" ]; then
540c27
        print_email "$MESSAGE" | mutt -s "$EMAIL_SUBJECT - $MESSAGE" "$EMAIL"
540c27
        #print_email "$MESSAGE" | mutt -s "$EMAIL_SUBJECT - $MESSAGE" -a "$LOG_FILE" -- "$EMAIL"
850862
        echo "email sent to $EMAIL"
850862
    fi
850862
    rm "$LOG_FILE"
850862
}
850862
850862
850862
("$@" 2>&1 | tee "$LOG_FILE") || (send_email "$EMAIL_FAILED" "FAILED" && false)
850862
850862
send_email "$EMAIL_SUCCESS" "SUCCESS"