system-helpers/usr/local/bin/check_updates

97 lines
2.1 KiB
Text
Raw Normal View History

2019-01-25 17:28:29 +00:00
#!/usr/bin/env bash
# usage
usage() {
USAGE=$(
cat <<EOF
2019-01-25 17:52:06 +00:00
Usage: check_updates [CONFIG_FILE (absolute path)]
2019-01-25 17:28:29 +00:00
Sends out update notifications. Please see man check_updates.
2019-01-25 17:28:29 +00:00
EOF
)
echo "$USAGE"
2019-01-25 17:28:29 +00:00
}
set -e
2019-01-25 17:28:29 +00:00
# check for config file
2022-07-10 16:29:44 +00:00
apply_config() {
local config=$1
2022-07-10 16:29:44 +00:00
if [[ ! -f "$config" ]]; then
echo "No config file specified"
echo ""
usage
exit 1
2022-07-10 16:29:44 +00:00
fi
set -a
# shellcheck disable=SC1090
source "$config"
set +a
2022-07-10 16:29:44 +00:00
}
2019-01-25 17:28:29 +00:00
source_config() {
local config=$1
local configFallback=$2
local configGlobalFallback=$3
2022-07-10 16:29:44 +00:00
if [[ -f "$config" ]]; then
apply_config "$config"
return
2022-07-10 16:29:44 +00:00
fi
if [[ -f "$configFallback" ]]; then
apply_config "$configFallback"
return
2022-07-10 16:29:44 +00:00
fi
if [[ -f "$configGlobalFallback" ]]; then
apply_config "$configGlobalFallback"
return
2022-07-10 16:29:44 +00:00
fi
2019-01-25 17:28:29 +00:00
}
apply_defaults() {
[[ -z ${CHECK_UPDATES_UPDATES_CMD} ]] && CHECK_UPDATES_UPDATES_CMD="/usr/bin/checkupdates"
[[ -z ${CHECK_UPDATES_UPDATES_AMOUNT_CMD} ]] && CHECK_UPDATES_UPDATES_AMOUNT_CMD="/usr/bin/checkupdates | wc -l"
}
check_required() {
if [[ -z ${CHECK_UPDATES_UPDATES_CMD} ]]; then
echo "CHECK_UPDATES_UPDATES_CMD is required"
exit 1
fi
if [[ -z ${CHECK_UPDATES_UPDATES_AMOUNT_CMD} ]]; then
echo "CHECK_UPDATES_UPDATES_AMOUNT_CMD is required"
exit 1
fi
if [[ -z ${CHECK_UPDATES_MAIL_ADDRESS} ]]; then
echo "CHECK_UPDATES_MAIL_ADDRESS is required"
exit 1
fi
type hostname &> /dev/null || { echo "Requiring 'hostname' but it's not installed"; exit 1; }
type mailx &> /dev/null || { echo "Requiring 'mailx' but it's not installed"; exit 1; }
}
2022-07-10 16:29:44 +00:00
source_config "$1" "$HOME/.check_updates.conf" "/etc/check_updates.conf"
apply_defaults
check_required
HOSTNAME=$(hostname)
CHECK_UPDATES_UPDATES=$(eval "${CHECK_UPDATES_UPDATES_CMD}")
CHECK_UPDATES_UPDATES_AMOUNT=$(eval "${CHECK_UPDATES_UPDATES_AMOUNT_CMD}")
2019-01-25 17:28:29 +00:00
if [[ "$CHECK_UPDATES_UPDATES_AMOUNT" -gt "0" ]]; then
SUBJECT="[updates ${HOSTNAME}]"
MESSAGE=$(
cat <<EOF
There are ${CHECK_UPDATES_UPDATES_AMOUNT} updates available on ${HOSTNAME}.
2019-01-25 17:28:29 +00:00
${CHECK_UPDATES_UPDATES};
2019-01-25 17:28:29 +00:00
EOF
)
echo "$MESSAGE" | mailx -Ssendwait -s "$SUBJECT" "$CHECK_UPDATES_MAIL_ADDRESS"
2019-01-25 17:28:29 +00:00
fi