Refactor notifications and add helper functions (#182)

* Refactor notifications and add helper functions

* Add helper functions to simplify sourcing files and executing functions if they exist

* Create notify_v2.sh wrapper script

* Simplify and consolidate notification logic within notify_v2.sh

* Support notification management via environment variables

* Move secrets to dockcheck.config

* Fix NOTIFY_CHANNELS default value when not set

* Feedback changes

* Remove leading spaces from MessageBody

* Check for valid notify v2 variables

* Warn on missing configuration and bypass notifications

* Update readme

* Additional feedback fixes

* More comments in default.config with different # depth for comments and settings

* Rename NOTIFY_TOPIC_NAME variable to NTFY_TOPIC_NAME for consistency

* Add TELEGRAM_TOPIC_ID

* Fix AppriseURL variable

* Add an ending newline to all MessageBody statements for consistency

* Remove troubleshooting echo statement

* Prevent attempting to trigger notifications for notification templates if versions are the same

---------

Co-authored-by: Matthew Oleksowicz <matt@everyoneneeds.it>
This commit is contained in:
vorezal 2025-05-25 12:26:13 -04:00 committed by GitHub
parent e4b93d113c
commit c63e2441fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 366 additions and 564 deletions

View file

@ -1,6 +1,6 @@
#!/usr/bin/env bash
VERSION="v0.6.4"
### ChangeNotes: Restructured update process - first pulls all images, then recreates all containers. Added -F option.
VERSION="v0.6.5"
### ChangeNotes: Refactored notification logic. See README.md for upgrade steps.
Github="https://github.com/mag37/dockcheck"
RawUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/dockcheck.sh"
@ -17,12 +17,17 @@ ScriptWorkDir="$(dirname "$ScriptPath")"
LatestRelease="$(curl -s -r 0-50 "$RawUrl" | sed -n "/VERSION/s/VERSION=//p" | tr -d '"')"
LatestChanges="$(curl -s -r 0-200 "$RawUrl" | sed -n "/ChangeNotes/s/# ChangeNotes: //p")"
# Source helper functions
source_if_exists() {
if [[ -s "$1" ]]; then source "$1"; fi
}
source_if_exists_or_fail() {
[[ -s "$1" ]] && source "$1"
}
# User customizable defaults
if [[ -s "${HOME}/.config/dockcheck.config" ]]; then
source "${HOME}/.config/dockcheck.config"
elif [[ -s "${ScriptWorkDir}/dockcheck.config" ]]; then
source "${ScriptWorkDir}/dockcheck.config"
fi
source_if_exists_or_fail "${HOME}/.config/dockcheck.config" || source_if_exists "${ScriptWorkDir}/dockcheck.config"
# Help Function
Help() {
@ -120,14 +125,16 @@ shift "$((OPTIND-1))"
# Set $1 to a variable for name filtering later
SearchName="${1:-}"
# Basic notify configuration check
if [[ "${Notify}" == true ]] && [[ ! -s "${ScriptWorkDir}/notify.sh" ]] && [[ -z "${NOTIFY_CHANNELS:-}" ]]; then
printf "Using v2 notifications with -i flag passed but no notify channels configured in dockcheck.config. This will result in no notifications being sent.\n"
fi
# Setting up options and sourcing functions
if [[ "$DontUpdate" == true ]]; then AutoMode=true; fi
if [[ "$MonoMode" == true ]]; then declare c_{red,green,yellow,blue,teal,reset}=""; fi
if [[ "$Notify" == true ]]; then
if [[ -s "${ScriptWorkDir}/notify.sh" ]]; then
source "${ScriptWorkDir}/notify.sh"
else Notify=false
fi
source_if_exists_or_fail "${ScriptWorkDir}/notify.sh" || source_if_exists_or_fail "${ScriptWorkDir}/notify_templates/notify_v2.sh" || Notify=false
fi
if [[ -n "$Exclude" ]]; then
IFS=',' read -ra Excludes <<< "$Exclude"
@ -148,6 +155,14 @@ if [[ -n "$CollectorTextFileDirectory" ]]; then
fi
fi
exec_if_exists() {
if [[ $(type -t $1) == function ]]; then "$@"; fi
}
exec_if_exists_or_fail() {
[[ $(type -t $1) == function ]] && "$@"
}
self_update_curl() {
cp "$ScriptPath" "$ScriptPath".bak
if command -v curl &>/dev/null; then
@ -335,10 +350,13 @@ if [[ "$VERSION" != "$LatestRelease" ]]; then
[[ "$SelfUpdate" =~ [yY] ]] && self_update
elif [[ "$AutoMode" == true ]] && [[ "$AutoSelfUpdate" == true ]]; then self_update;
else
[[ "$Notify" == true ]] && { [[ $(type -t dockcheck_notification) == function ]] && dockcheck_notification "$VERSION" "$LatestRelease" "$LatestChanges" || printf "Could not source notification function.\n"; }
[[ "$Notify" == true ]] && { exec_if_exists_or_fail dockcheck_notification "$VERSION" "$LatestRelease" "$LatestChanges" || printf "Could not source notification function.\n"; }
fi
fi
# Version check for notify templates
[[ "$Notify" == true ]] && { exec_if_exists_or_fail notify_update_notification || printf "Could not source notify notification function.\n"; }
dependency_check "regctl" "regbin" "https://github.com/regclient/regclient/releases/latest/download/regctl-linux-TEMP"
dependency_check "jq" "jqbin" "https://github.com/jqlang/jq/releases/latest/download/jq-linux-TEMP"
@ -457,11 +475,7 @@ unset IFS
# Run the prometheus exporter function
if [[ -n "${CollectorTextFileDirectory:-}" ]]; then
if type -t prometheus_exporter &>/dev/null; then
prometheus_exporter ${#NoUpdates[@]} ${#GotUpdates[@]} ${#GotErrors[@]}
else
printf "%s\n" "Could not source prometheus exporter function."
fi
exec_if_exists_or_fail prometheus_exporter ${#NoUpdates[@]} ${#GotUpdates[@]} ${#GotErrors[@]} || printf "%s\n" "Could not source prometheus exporter function."
fi
# Define how many updates are available
@ -481,7 +495,7 @@ if [[ -n ${GotUpdates[*]:-} ]]; then
printf "\n%bContainers with updates available:%b\n" "$c_yellow" "$c_reset"
if [[ -s "$ScriptWorkDir/urls.list" ]] && [[ "$PrintReleaseURL" == true ]]; then releasenotes; else Updates=("${GotUpdates[@]}"); fi
[[ "$AutoMode" == false ]] && list_options || printf "%s\n" "${Updates[@]}"
[[ "$Notify" == true ]] && { type -t send_notification &>/dev/null && send_notification "${GotUpdates[@]}" || printf "\nCould not source notification function.\n"; }
[[ "$Notify" == true ]] && { exec_if_exists_or_fail send_notification "${GotUpdates[@]}" || printf "\nCould not source notification function.\n"; }
fi
# Optionally get updates if there's any