gentoo-auto-update/check_updates.sh

88 lines
2.4 KiB
Bash
Raw Permalink Normal View History

2018-04-08 17:28:42 +02:00
#!/bin/sh
#
# check_updates.sh
# $1 - message transport agent, either sendmail or gotify, default to sendmail
#
2018-04-08 17:28:42 +02:00
PATH="$PATH:/usr/sbin:/sbin"
TMP_FILE=$(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
2018-04-08 17:28:42 +02:00
2024-04-22 10:23:03 +02:00
# Possible race condition: if someone runs `eix blah` at the exact same time,
# it will block the update. But that seems highly improbable.
# The mail goal is to avoid stacking updates or eix-sync/eix-updates
PORTAGE_RUNING=$(pgrep 'emerge|eix')
if [ "${PORTAGE_RUNING}" ]; then
echo "Error: another emerge is already running, exiting"
exit 1
fi
2018-04-08 17:28:42 +02:00
PORTAGE_QUIET='1'
2024-04-22 10:23:03 +02:00
eix-sync 1>/dev/null 2>${TMP_FILE} || eix-update 1>/dev/null 2>${TMP_FILE}
2018-04-08 17:28:42 +02:00
emerge -v --update --newuse --deep --with-bdeps=y --quiet-build=y @security \
&>> ${TMP_FILE}
emerge -v --update --newuse --deep --with-bdeps=y --quiet-build=y @system \
&>> ${TMP_FILE}
emerge -v --update --newuse --deep --with-bdeps=y --quiet-build=y @unavailable
&>> ${TMP_FILE}
2018-04-10 09:09:53 +02:00
emerge -v --update --newuse --deep --with-bdeps=y --quiet-build=y @world \
2018-04-08 17:28:42 +02:00
&>> ${TMP_FILE}
if [ "$(grep '\[ebuild U' ${TMP_FILE})" != '' ]; then
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}
2018-04-08 17:28:42 +02:00
if [ "$(lsof -n | awk '{ if ($6 == "DEL") print $9 }' | sort -u)" ]; then
restart-services --no-color >> ${MESSAGE}
2018-04-08 17:28:42 +02:00
fi
2018-05-15 10:07:32 +02:00
if ! [ -z "$(grep -v '{}' /var/lib/portage/preserved_libs_registry)" ]; then
emerge -v --quiet-build=y @preserved-rebuild >> ${MESSAGE}
2018-05-15 10:07:32 +02:00
fi
emerge -p --depclean --with-bdeps\=y >> ${MESSAGE}
2018-04-08 17:28:42 +02:00
2018-04-27 10:33:12 +02:00
eix-test-obsolete \
| grep -Pv '^No|^Skipping|[Aa]ll.*packages are in the database' \
>> ${MESSAGE}
2018-04-27 10:33:12 +02:00
eval "${MTA}" < "${MESSAGE}"
2018-04-08 17:28:42 +02:00
fi
rm ${TMP_FILE} ${MESSAGE}