Skip to content

Instantly share code, notes, and snippets.

@carestad
Last active Jul 17, 2019
Embed
What would you like to do?
Script to notify on Slack when updates are available in APT. Logs already reported packages on a $0.log file.
#!/bin/bash
#
# Author: Alexander Karlstad <carestad@github>
# Updated: 2019-07-17
# Configurable variables
slack_channel="@alexander"
slack_icon=":information_source:"
slack_user="apt@$(hostname -f)"
slack_webhook=""
# Internal variables
pkglog="$0.log"
notify=()
upgradeable="$(apt-get upgrade -s | grep ^Inst | awk '{ print $2,$3; }' | tr -d '[]')"
# No pkglog file? Create it
if [ ! -f "$pkglog" ]; then
touch "$pkglog"
fi
# If nothing to upgrade, we have done it already. Make sure pkglog file is empty
if [ -z "$upgradeable" ]; then
echo "No packages to update"
echo "" > "$pkglog"
exit 0
fi
while read line; do
if ! grep "$line" "$pkglog"; then
notify+=("$line")
fi
done <<< "$upgradeable"
# Packages can be updated, but have been reported already
if [[ -z "${notify[*]}" ]]; then
echo "Package to upgrade available, but already reported"
exit 0
fi
markdown=""
for line in "${notify[@]}"; do
pkg="$(echo "$line" | cut -d" " -f1)"
ver="$(echo "$line" | cut -d" " -f2)"
markdown="$markdown\n• *${pkg}* (_${ver}_)"
done
curl -s -X POST -H 'Content-type: application/json' -d "{'channel': '$slack_channel', 'icon_emoji': '$slack_icon', 'username': '$slack_user', 'text': 'The following packages can be updated:${markdown}', 'type': 'mrkdwn'}" $slack_webhook > /dev/null
# Add to log
for line in "${notify[@]}"; do
echo "$line" >> "$pkglog"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment