2021/05/25
1. Use pip to install ansi2html. You may have to use the --break-system-packages
flag
pip3 install ansi2html
2. Install colordiff, msmtp, msmtp-mta
apt install colordiff msmtp msmtp-mta
3. Create new script in oxidized working directory.
cat <<'EOF' > /home/oxidized/oxidized-report-git-commits
#!/bin/sh
echo "Node name: ${OX_NODE_NAME}"
echo "Group name: ${OX_NODE_GROUP}"
echo "Job status: ${OX_JOB_STATUS}"
echo "Job time: ${OX_JOB_TIME}"
if [ "${OX_EVENT}" = "node_fail" ]; then
echo 'Job failed'
exit 64
fi
if [ -z "${OX_REPO_COMMITREF}" ]; then
echo '$OX_REPO_COMMITREF not set'
exit 64
fi
if [ -z "${OX_REPO_NAME}" ]; then
echo '$OX_REPO_NAME not set'
exit 64
fi
echo "Git repo: ${OX_REPO_NAME}"
echo "Git commit ID: ${OX_REPO_COMMITREF}"
echo ""
git --bare --git-dir="${OX_REPO_NAME}" show --pretty='' --no-color "${OX_REPO_COMMITREF}"
EOF
4. Change the ownership of the new file and make it executable
chown oxidized:oxidized /home/oxidized/oxidized-report-git-commits
chmod +x /home/oxidized/oxidized-report-git-commits
5. Create new config file for msmtp. Make
cat <<EOF > /etc/msmtprc
# Set default values for all following accounts.
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log
# Gmail
account gmail #<-- set a descriptive name
host smtp.gmail.com #<-- use the correct email server hostname
port 587
from sender.email.example@gmail.com #<-- set this,
user sender.email.example@gmail.com #<-- and this to the correct "from" email account
password [sender email password] #<-- set the password for the "from" account with no quotes
# Set a default account
account default : gmail #<-- set this to the profile defined above
EOF
6. Edit the Oxidized config file. Place this hook at the bottom. Change receiver.address@gmail.com
and sender.email.example@gmail.com
to match the config you built in the last step.
hooks:
email_output:
type: exec
events: [post_store]
cmd: 'echo "From:Oxidized\nTo:receiver.address@gmail.com\nSubject: Production Network - Config diff ${OX_NODE_NAME}\nMIME-Version: 1.0\nContent-Type: text/html; charset=utf-8\n\n" > /tmp/${OX_NODE_NAME}; bash /home/oxidized/oxidized-report-git-commits | tr -d \\r | colordiff | ansi2html >> /tmp/${OX_NODE_NAME}; msmtp sender.email.example@gmail.com< /tmp/${OX_NODE_NAME}; rm /tmp/${OX_NODE_NAME}'
async: true
timeout: 120