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
850862
send_email() {
850862
    local EMAIL="$1"
850862
    local MESSAGE="$2"
850862
850862
    echo "$MESSAGE"
850862
850862
    if [ ! -z "$EMAIL" ]; then
850862
        mutt -s "$EMAIL_SUBJECT - $MESSAGE" -a "$LOG_FILE" -- "$EMAIL" << EOF
850862
$EMAIL_BODY
850862
850862
Command:
850862
    $COMMAND
850862
850862
$MESSAGE
850862
EOF
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"