mirror of
https://github.com/mag37/dockcheck.git
synced 2026-03-05 08:30:35 +01:00
Phase 2: Add notification system and improve dependency management
- Added notify_v2.sh wrapper for advanced notification management - Updated Discord and Gotify notification templates for v2 compatibility - Added snooze functionality to prevent duplicate notifications - Added support for multiple notification channels and formats (JSON, CSV, text) - Implemented robust dependency management system with package manager and static binary fallback - Added helper functions: releasenotes(), list_options(), progress_bar() - Improved datecheck() function with proper error handling - Added distro_checker() and binary_downloader() functions - Replaced old dependency checks with modern dependency_check() system
This commit is contained in:
parent
5257343706
commit
a77abafc04
4 changed files with 459 additions and 117 deletions
|
|
@ -1,28 +1,49 @@
|
|||
### DISCLAIMER: This is a third party addition to dockcheck - best effort testing.
|
||||
#
|
||||
# Copy/rename this file to notify.sh to enable the notification snippet.
|
||||
# Required receiving services must already be set up.
|
||||
# Modify to fit your setup - set DiscordWebhookUrl
|
||||
#!/usr/bin/env bash
|
||||
|
||||
send_notification() {
|
||||
[ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@")
|
||||
UpdToString=$( printf '%s\\n' "${Updates[@]}" )
|
||||
# Discord notification template for podcheck v2
|
||||
# Requires: DISCORD_WEBHOOK_URL
|
||||
|
||||
echo "$UpdToString"
|
||||
FromHost=$(hostname)
|
||||
|
||||
# platform specific notification code would go here
|
||||
printf "\nSending Discord notification\n"
|
||||
|
||||
# Setting the MessageBody variable here.
|
||||
MessageBody="🐋 Containers on $FromHost with updates available: \n$UpdToString"
|
||||
|
||||
# Modify to fit your setup:
|
||||
DiscordWebhookUrl="PasteYourFullDiscordWebhookURL"
|
||||
|
||||
MsgBody="{\"username\":\"$FromHost\",\"content\":\"$MessageBody\"}"
|
||||
|
||||
curl -sS -o /dev/null --fail -X POST -H "Content-Type: application/json" -d "$MsgBody" "$DiscordWebhookUrl"
|
||||
if [[ -z "${DISCORD_WEBHOOK_URL:-}" ]]; then
|
||||
echo "Error: DISCORD_WEBHOOK_URL not configured"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Prepare the Discord message
|
||||
if [[ -n "${NOTIFICATION_MESSAGE:-}" ]]; then
|
||||
# Format message for Discord - escape quotes and newlines
|
||||
discord_content="${NOTIFICATION_MESSAGE}"
|
||||
discord_content="${discord_content//\"/\\\"}"
|
||||
discord_content="${discord_content//$'\n'/\\n}"
|
||||
|
||||
# Create Discord webhook payload
|
||||
discord_payload=$(cat <<EOF
|
||||
{
|
||||
"content": "${discord_content}",
|
||||
"username": "Podcheck",
|
||||
"embeds": [
|
||||
{
|
||||
"title": "${NOTIFICATION_TITLE:-Podcheck Notification}",
|
||||
"description": "${discord_content}",
|
||||
"color": 3447003,
|
||||
"timestamp": "$(date -u +%Y-%m-%dT%H:%M:%S.000Z)"
|
||||
}
|
||||
]
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# Send to Discord
|
||||
if curl -H "Content-Type: application/json" \
|
||||
-d "$discord_payload" \
|
||||
"${DISCORD_WEBHOOK_URL}" \
|
||||
${CurlArgs:-} &>/dev/null; then
|
||||
return 0
|
||||
else
|
||||
echo "Failed to send Discord notification"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
echo "No notification message provided"
|
||||
return 1
|
||||
fi
|
||||
|
||||
|
|
|
|||
|
|
@ -1,29 +1,30 @@
|
|||
### DISCLAIMER: This is a third party addition to dockcheck - best effort testing.
|
||||
#
|
||||
# Copy/rename this file to notify.sh to enable the notification snippet.
|
||||
# Required receiving services must already be set up.
|
||||
# Modify to fit your setup - set GotifyUrl and GotifyToken.
|
||||
#!/usr/bin/env bash
|
||||
|
||||
send_notification() {
|
||||
[ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@")
|
||||
UpdToString=$( printf '%s\\n' "${Updates[@]}" )
|
||||
FromHost=$(hostname)
|
||||
# Gotify notification template for podcheck v2
|
||||
# Requires: GOTIFY_DOMAIN, GOTIFY_TOKEN
|
||||
|
||||
# platform specific notification code would go here
|
||||
printf "\nSending Gotify notification\n"
|
||||
if [[ -z "${GOTIFY_DOMAIN:-}" ]] || [[ -z "${GOTIFY_TOKEN:-}" ]]; then
|
||||
echo "Error: GOTIFY_DOMAIN and GOTIFY_TOKEN must be configured"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Setting the MessageTitle and MessageBody variable here.
|
||||
MessageTitle="${FromHost} - updates available."
|
||||
printf -v MessageBody "Containers on $FromHost with updates available:\n$UpdToString"
|
||||
|
||||
# Modify to fit your setup:
|
||||
GotifyToken="Your Gotify token here"
|
||||
GotifyUrl="https://api.gotify/message?token=${GotifyToken}"
|
||||
|
||||
curl \
|
||||
-F "title=${MessageTitle}" \
|
||||
-F "message=${MessageBody}" \
|
||||
-F "priority=5" \
|
||||
-X POST "${GotifyUrl}" 1> /dev/null
|
||||
|
||||
}
|
||||
# Prepare the Gotify message
|
||||
if [[ -n "${NOTIFICATION_MESSAGE:-}" ]]; then
|
||||
# Build Gotify URL
|
||||
gotify_url="${GOTIFY_DOMAIN}/message?token=${GOTIFY_TOKEN}"
|
||||
|
||||
# Send to Gotify
|
||||
if curl -F "title=${NOTIFICATION_TITLE:-Podcheck Notification}" \
|
||||
-F "message=${NOTIFICATION_MESSAGE}" \
|
||||
-F "priority=5" \
|
||||
-X POST "${gotify_url}" \
|
||||
${CurlArgs:-} &>/dev/null; then
|
||||
return 0
|
||||
else
|
||||
echo "Failed to send Gotify notification"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
echo "No notification message provided"
|
||||
return 1
|
||||
fi
|
||||
|
|
|
|||
253
notify_templates/notify_v2.sh
Normal file
253
notify_templates/notify_v2.sh
Normal file
|
|
@ -0,0 +1,253 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# notify_v2.sh - Advanced notification wrapper for podcheck
|
||||
# This is the main notification dispatch system
|
||||
|
||||
# Get hostname for notifications
|
||||
# Try multiple methods to get hostname
|
||||
if [[ -s "/etc/hostname" ]]; then
|
||||
HOSTNAME=$(cat /etc/hostname)
|
||||
elif command -v hostname &>/dev/null; then
|
||||
HOSTNAME=$(hostname)
|
||||
else
|
||||
HOSTNAME="podcheck-host"
|
||||
fi
|
||||
|
||||
# Default notification channels if not configured
|
||||
NOTIFY_CHANNELS=${NOTIFY_CHANNELS:-""}
|
||||
|
||||
# Check if any channels are configured
|
||||
if [[ -z "${NOTIFY_CHANNELS}" ]]; then
|
||||
echo "No notification channels configured. Set NOTIFY_CHANNELS in podcheck.config"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Function to check if a notification should be sent based on snooze
|
||||
should_send_notification() {
|
||||
local notification_type="$1"
|
||||
local snooze_file="${ScriptWorkDir}/.snooze_${notification_type}"
|
||||
local current_time=$(date +%s)
|
||||
|
||||
# If snooze is not enabled, always send
|
||||
if [[ -z "${SNOOZE_SECONDS:-}" ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Check if snooze file exists and is recent
|
||||
if [[ -f "$snooze_file" ]]; then
|
||||
local last_notification=$(cat "$snooze_file" 2>/dev/null || echo "0")
|
||||
local time_diff=$((current_time - last_notification))
|
||||
|
||||
if [[ $time_diff -lt ${SNOOZE_SECONDS} ]]; then
|
||||
return 1 # Don't send notification
|
||||
fi
|
||||
fi
|
||||
|
||||
# Update snooze file
|
||||
echo "$current_time" > "$snooze_file"
|
||||
return 0 # Send notification
|
||||
}
|
||||
|
||||
# Function to format output based on type
|
||||
format_output() {
|
||||
local format="$1"
|
||||
local title="$2"
|
||||
shift 2
|
||||
local containers=("$@")
|
||||
|
||||
case "$format" in
|
||||
"json")
|
||||
local container_json=""
|
||||
for container in "${containers[@]}"; do
|
||||
if [[ -n "$container_json" ]]; then
|
||||
container_json="${container_json},"
|
||||
fi
|
||||
container_json="${container_json}\"${container}\""
|
||||
done
|
||||
echo "{\"title\":\"${title}\",\"hostname\":\"${HOSTNAME}\",\"containers\":[${container_json}]}"
|
||||
;;
|
||||
"csv")
|
||||
local container_csv=""
|
||||
for container in "${containers[@]}"; do
|
||||
if [[ -n "$container_csv" ]]; then
|
||||
container_csv="${container_csv},"
|
||||
fi
|
||||
container_csv="${container_csv}${container}"
|
||||
done
|
||||
echo "${title},${HOSTNAME},${container_csv}"
|
||||
;;
|
||||
"text"|*)
|
||||
echo "${title} on ${HOSTNAME}:"
|
||||
for container in "${containers[@]}"; do
|
||||
echo " - ${container}"
|
||||
done
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Main notification function for container updates
|
||||
send_notification() {
|
||||
local containers=("$@")
|
||||
|
||||
# If no containers provided, exit early unless ALLOWEMPTY is set
|
||||
if [[ ${#containers[@]} -eq 0 ]]; then
|
||||
# Check if any channel allows empty notifications
|
||||
local send_empty=false
|
||||
for channel in ${NOTIFY_CHANNELS}; do
|
||||
local channel_upper=$(echo "$channel" | tr '[:lower:]' '[:upper:]')
|
||||
local allow_empty_var="${channel_upper}_ALLOWEMPTY"
|
||||
if [[ "${!allow_empty_var:-false}" == "true" ]]; then
|
||||
send_empty=true
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ "$send_empty" == "false" ]]; then
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# Check snooze for container notifications
|
||||
if ! should_send_notification "containers"; then
|
||||
echo "Container update notification snoozed"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Send notifications to each configured channel
|
||||
for channel in ${NOTIFY_CHANNELS}; do
|
||||
local channel_upper=$(echo "$channel" | tr '[:lower:]' '[:upper:]')
|
||||
|
||||
# Check if this channel should skip snooze
|
||||
local skip_snooze_var="${channel_upper}_SKIPSNOOZE"
|
||||
if [[ "${!skip_snooze_var:-false}" == "true" ]]; then
|
||||
# Force send by updating snooze file
|
||||
echo "$(date +%s)" > "${ScriptWorkDir}/.snooze_containers"
|
||||
fi
|
||||
|
||||
# Check if this channel is containers only
|
||||
local containers_only_var="${channel_upper}_CONTAINERSONLY"
|
||||
if [[ "${!containers_only_var:-false}" == "true" && ${#containers[@]} -eq 0 ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
# Get the template to use (default to channel name)
|
||||
local template_var="${channel_upper}_TEMPLATE"
|
||||
local template="${!template_var:-$channel}"
|
||||
|
||||
# Get output format
|
||||
local output_var="${channel_upper}_OUTPUT"
|
||||
local output_format="${!output_var:-text}"
|
||||
|
||||
# Format the message
|
||||
local title="Containers with updates available"
|
||||
if [[ ${#containers[@]} -eq 0 ]]; then
|
||||
title="No container updates available"
|
||||
fi
|
||||
|
||||
local message=$(format_output "$output_format" "$title" "${containers[@]}")
|
||||
|
||||
# Source and execute the notification template
|
||||
local template_file="${ScriptWorkDir}/notify_templates/notify_${template}.sh"
|
||||
if [[ -f "$template_file" ]]; then
|
||||
# Export message for template to use
|
||||
export NOTIFICATION_MESSAGE="$message"
|
||||
export NOTIFICATION_TITLE="$title"
|
||||
export NOTIFICATION_CONTAINERS=("${containers[@]}")
|
||||
|
||||
if source "$template_file"; then
|
||||
echo "Notification sent via $channel ($template)"
|
||||
else
|
||||
echo "Failed to send notification via $channel ($template)"
|
||||
fi
|
||||
else
|
||||
echo "Notification template not found: $template_file"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Function for podcheck self-update notifications
|
||||
podcheck_notification() {
|
||||
local current_version="$1"
|
||||
local latest_version="$2"
|
||||
local changes="$3"
|
||||
|
||||
# Check if podcheck notifications are disabled
|
||||
if [[ "${DISABLE_PODCHECK_NOTIFICATION:-false}" == "true" ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Check snooze
|
||||
if ! should_send_notification "podcheck"; then
|
||||
echo "Podcheck update notification snoozed"
|
||||
return 0
|
||||
fi
|
||||
|
||||
local title="Podcheck update available"
|
||||
local message="$title: $current_version → $latest_version"
|
||||
if [[ -n "$changes" ]]; then
|
||||
message="$message\nChanges: $changes"
|
||||
fi
|
||||
|
||||
# Send to configured channels
|
||||
for channel in ${NOTIFY_CHANNELS}; do
|
||||
local channel_upper=$(echo "$channel" | tr '[:lower:]' '[:upper:]')
|
||||
|
||||
# Get the template to use
|
||||
local template_var="${channel_upper}_TEMPLATE"
|
||||
local template="${!template_var:-$channel}"
|
||||
|
||||
# Get output format
|
||||
local output_var="${channel_upper}_OUTPUT"
|
||||
local output_format="${!output_var:-text}"
|
||||
|
||||
local formatted_message=$(format_output "$output_format" "$title" "podcheck: $current_version → $latest_version")
|
||||
|
||||
# Source and execute the notification template
|
||||
local template_file="${ScriptWorkDir}/notify_templates/notify_${template}.sh"
|
||||
if [[ -f "$template_file" ]]; then
|
||||
export NOTIFICATION_MESSAGE="$formatted_message"
|
||||
export NOTIFICATION_TITLE="$title"
|
||||
|
||||
if source "$template_file"; then
|
||||
echo "Podcheck update notification sent via $channel"
|
||||
else
|
||||
echo "Failed to send podcheck notification via $channel"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Function for notify template update notifications
|
||||
notify_update_notification() {
|
||||
# Check if notify notifications are disabled
|
||||
if [[ "${DISABLE_NOTIFY_NOTIFICATION:-false}" == "true" ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Check snooze
|
||||
if ! should_send_notification "notify"; then
|
||||
echo "Notify template update notification snoozed"
|
||||
return 0
|
||||
fi
|
||||
|
||||
local title="Notification templates updated"
|
||||
local message="Notification templates have been updated"
|
||||
|
||||
# Send to configured channels
|
||||
for channel in ${NOTIFY_CHANNELS}; do
|
||||
local channel_upper=$(echo "$channel" | tr '[:lower:]' '[:upper:]')
|
||||
|
||||
local template_var="${channel_upper}_TEMPLATE"
|
||||
local template="${!template_var:-$channel}"
|
||||
|
||||
local template_file="${ScriptWorkDir}/notify_templates/notify_${template}.sh"
|
||||
if [[ -f "$template_file" ]]; then
|
||||
export NOTIFICATION_MESSAGE="$message"
|
||||
export NOTIFICATION_TITLE="$title"
|
||||
|
||||
if source "$template_file"; then
|
||||
echo "Notify update notification sent via $channel"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue