Skip to content

Commit

Permalink
Improve notifications handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Steeven9 committed Apr 7, 2024
1 parent e74ea80 commit fa45cbe
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions shepherd
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ authenticate_to_registries() {
fi
}

send_notification() {
if [[ "$apprise_sidecar_url" != "" ]]; then
status=$(curl -s -X POST -H "Content-Type: application/json" --data "{\"title\": \"$1\", \"body\": \"$2\", \"notify_type\": \"$3\"}" "$apprise_sidecar_url")
if [[ $status = 0 ]]; then
logger "Sent notification"
else
logger "Failed sending notification!"
fi
fi
}

update_services() {
local ignorelist="$1"
local supports_detach_option=$2
Expand Down Expand Up @@ -94,11 +105,11 @@ update_services() {

if ! DOCKER_CLI_EXPERIMENTAL=enabled docker "${config_flag[@]}" manifest inspect $insecure_registry_flag "$image" > /dev/null; then
logger "Error updating service $name! Image $image does not exist or it is not available"
if [[ "$apprise_sidecar_url" != "" ]]; then
title="[Shepherd] Error updating service $name"
body="$(date) Service $name Image $image does not exist or it is not available"
curl -X POST -H "Content-Type: application/json" --data "{\"title\": \"$title\", \"body\": \"$body\", \"type\": \"failure\"}" "$apprise_sidecar_url"
fi

title="[Shepherd] Error updating service $name"
body="$(date) Service $name Image $image does not exist or it is not available"
send_notification $title $body "failure"

else
logger "Trying to update service $name with image $image" "true"

Expand All @@ -115,11 +126,11 @@ update_services() {
# shellcheck disable=SC2086
docker "${config_flag[@]}" service update "$name" $detach_option ${ROLLBACK_OPTIONS} --rollback > /dev/null
fi
if [[ "$apprise_sidecar_url" != "" ]]; then
title="[Shepherd] Service $name update failed on $hostname"
body="$(date) Service $name failed to update to $(docker service inspect "$name" -f '{{.Spec.TaskTemplate.ContainerSpec.Image}}')"
curl -X POST -H "Content-Type: application/json" --data "{\"title\": \"$title\", \"body\": \"$body\", \"type\": \"failure\"}" "$apprise_sidecar_url"
fi

title="[Shepherd] Service $name update failed on $hostname"
body="$(date) Service $name failed to update to $(docker service inspect "$name" -f '{{.Spec.TaskTemplate.ContainerSpec.Image}}')"
send_notification $title $body "failure"

continue # continue with next service
fi

Expand All @@ -130,11 +141,10 @@ update_services() {
logger "No updates to service $name!" "true"
else
logger "Service $name was updated!"
if [[ "$apprise_sidecar_url" != "" ]]; then
title="[Shepherd] Service $name updated on $hostname"
body="$(date) Service $name was updated from $previous_image to $current_image"
curl -X POST -H "Content-Type: application/json" --data "{\"title\": \"$title\", \"body\": \"$body\", \"type\": \"success\"}" "$apprise_sidecar_url"
fi

title="[Shepherd] Service $name updated on $hostname"
body="$(date) Service $name was updated from $previous_image to $current_image"
send_notification $title $body "success"

if [[ "$image_autoclean_limit" != "" ]]; then
logger "Cleaning up old docker images, leaving last $image_autoclean_limit"
Expand Down

0 comments on commit fa45cbe

Please sign in to comment.