From 4fda20d5215c4500d04c8d37fd0eb7d2b52d1406 Mon Sep 17 00:00:00 2001 From: Evann DREUMONT <53308142+LeGmask@users.noreply.github.com> Date: Wed, 8 Jan 2025 16:56:38 +0100 Subject: [PATCH] check_updates.sh: add support for gotify messages --- check_updates.sh | 60 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/check_updates.sh b/check_updates.sh index e2ad10c..3e7b526 100755 --- a/check_updates.sh +++ b/check_updates.sh @@ -1,9 +1,32 @@ #!/bin/sh +# +# check_updates.sh +# $1 - message transport agent, either sendmail or gotify, default to sendmail +# + PATH="$PATH:/usr/sbin:/sbin" TMP_FILE=$(mktemp) -MAIL=$(mktemp) +MESSAGE=$(mktemp) +MESSAGE_IS_MAIL=1 + +# message transport agent, either sendmail or gotify +# if you use gotify, you need to install gotify-cli +MTA=${1:-sendmail} +case "${MTA}" in + sendmail) + MTA='sendmail -t root@$(hostname -f)' + ;; + gotify) + MTA='gotify p -q -t $TITLE' + MESSAGE_IS_MAIL=0 + ;; + *) + echo "Error: unknown MTA" + exit 1 + ;; +esac # Possible race condition: if someone runs `eix blah` at the exact same time, # it will block the update. But that seems highly improbable. @@ -28,30 +51,37 @@ emerge -v --update --newuse --deep --with-bdeps=y --quiet-build=y @world \ &>> ${TMP_FILE} if [ "$(grep '\[ebuild U' ${TMP_FILE})" != '' ]; then - echo "From: $(whoami)@$(hostname -f)" > ${MAIL} - echo "To: root@$(hostname -f)" >> ${MAIL} - echo "Subject: $(hostname -f):$(grep Total: ${TMP_FILE} \ - | cut -d ':' -f 2 | cut -d ')' -f 1))" >> ${MAIL} - echo "Content-Type: text/plain; charset=UTF-8" >> ${MAIL} - echo "Date: $(date --rfc-email)" >> ${MAIL} - printf "\n" >> ${MAIL} - cat ${TMP_FILE} >> ${MAIL} + if [ ${MESSAGE_IS_MAIL} -eq 1 ]; then + # The message is a mail we add corresponding headers + echo "From: $(whoami)@$(hostname -f)" > ${MESSAGE} + echo "To: root@$(hostname -f)" >> ${MESSAGE} + echo "Subject: $(hostname -f):$(grep Total: ${TMP_FILE} \ + | cut -d ':' -f 2 | cut -d ')' -f 1))" >> ${MESSAGE} + echo "Content-Type: text/plain; charset=UTF-8" >> ${MESSAGE} + echo "Date: $(date --rfc-email)" >> ${MESSAGE} + printf "\n" >> ${MESSAGE} + else + TITLE = "$(hostname -f):$(grep Total: ${TMP_FILE} \ + | cut -d ':' -f 2 | cut -d ')' -f 1)" + fi + + cat ${TMP_FILE} >> ${MESSAGE} if [ "$(lsof -n | awk '{ if ($6 == "DEL") print $9 }' | sort -u)" ]; then - restart-services --no-color >> ${MAIL} + restart-services --no-color >> ${MESSAGE} fi if ! [ -z "$(grep -v '{}' /var/lib/portage/preserved_libs_registry)" ]; then - emerge -v --quiet-build=y @preserved-rebuild >> ${MAIL} + emerge -v --quiet-build=y @preserved-rebuild >> ${MESSAGE} fi - emerge -p --depclean --with-bdeps\=y >> ${MAIL} + emerge -p --depclean --with-bdeps\=y >> ${MESSAGE} eix-test-obsolete \ | grep -Pv '^No|^Skipping|[Aa]ll.*packages are in the database' \ - >> ${MAIL} + >> ${MESSAGE} - cat ${MAIL} | sendmail -t root@$(hostname -f) + eval "${MTA}" < "${MESSAGE}" fi -rm ${TMP_FILE} ${MAIL} +rm ${TMP_FILE} ${MESSAGE}