Improve mailing scripts and workflow

This commit is contained in:
Alice Gaudon 2020-02-02 06:47:47 +01:00
parent ea4e5d0a01
commit 37ab936f2e
3 changed files with 11 additions and 10 deletions

8
mail.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
MAILFROM=$(cat ~/.host_mail)
MAILTO=$(cat "$HOME/.admin_mail")
SUBJECT=$1
cat | mailx -s "$SUBJECT" -r "$MAILFROM" "$MAILTO"

View File

@ -1,24 +1,20 @@
#!/bin/bash
SCRIPT=$1
shift
FILE_ID=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
touch /tmp/$FILE_ID.log
chmod a-rwx,u+r /tmp/$FILE_ID.log
$SCRIPT "$@" 2>&1 | tee /tmp/$FILE_ID.log
ERR=${PIPESTATUS[0]}
OUTPUT=$(cat /tmp/$FILE_ID.log)
rm /tmp/$FILE_ID.log
if [ $ERR -ne 0 ]; then
MAILTO=$(cat "$HOME/.admin_mail")
echo "Sending error mail"
echo -e "An error occured during the execution of $SCRIPT ($ERR).\n\n$OUTPUT" | mailx -s "$(hostname) $SCRIPT failure" "$MAILTO"
echo -e "An error occured during the execution of $SCRIPT ($ERR).\n\n$OUTPUT" | $(dirname $0)/mail.sh "$(hostname) $SCRIPT failure"
echo "Sent"
exit 1
fi

View File

@ -7,14 +7,11 @@ then
UPDATES=$(echo -e "$UPDATES" | grep -v "testing repos\!")
fi
echo -e "$UPDATES"
echo -en "$UPDATES"
if [ ! -z "$UPDATES" ]; then
HOST=$(hostname)
FROM=$(cat ~/.host_mail)
DEST=$(cat ~/.admin_mail)
echo -e "The following security updates are available for $HOST:\n\n$UPDATES\n\nIt is highly recommended that you perform these updates now" | mailx -s "Security updates are available on $HOST" -r "$FROM" $DEST
echo -e "The following security updates are available for $HOST:\n\n$UPDATES\n\nIt is highly recommended that you perform these updates now" | $(dirname $0)/mail.sh "Security updates are available on $HOST"
echo "Mail sent"
fi