Skip to content

Commit

Permalink
Merge pull request #54 from shizunge/conditional
Browse files Browse the repository at this point in the history
Add GANTRY_NOTIFICATION_CONDITION
  • Loading branch information
shizunge authored Sep 20, 2024
2 parents 3b19f9c + bfefe3f commit 4d030a0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ You can configure the most behaviors of *Gantry* via environment variables.
| GANTRY_CLEANUP_IMAGES | true | Set to `true` to clean up the updated images. Set to `false` to disable the cleanup. Before cleaning up, *Gantry* will try to remove any *exited* and *dead* containers that are using the images. |
| GANTRY_CLEANUP_IMAGES_OPTIONS | | [Options](https://docs.docker.com/engine/reference/commandline/service_create/#options) added to the `docker service create` command to create a global job for images removal. You can use this to add a label to the service or the containers. |
| GANTRY_NOTIFICATION_APPRISE_URL | | Enable notifications on service update with [apprise](https://github.com/caronc/apprise-api). This must point to the notification endpoint (e.g. `http://apprise:8000/notify`) |
| GANTRY_NOTIFICATION_CONDITION | all | Valid values are `all` and `on-change`. Specifies the conditions under which notifications are sent. Set to `all` to send notifications every run. Set to `on-change` to send notifications only when there are updates or errors. |
| GANTRY_NOTIFICATION_TITLE | | Add an additional message to the notification title. |

## Authentication
Expand Down
1 change: 1 addition & 0 deletions docs/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ The label on the services to select config to enable authentication is renamed t
| GANTRY_LOG_LEVEL |
| GANTRY_MANIFEST_CMD |
| GANTRY_MANIFEST_OPTIONS |
| GANTRY_NOTIFICATION_CONDITION |
| GANTRY_NOTIFICATION_TITLE |
| GANTRY_POST_RUN_CMD |
| GANTRY_PRE_RUN_CMD |
Expand Down
17 changes: 15 additions & 2 deletions src/lib-gantry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ _get_number_of_elements_in_static_variable() {
}

_report_services() {
local CONDITION="${GANTRY_NOTIFICATION_CONDITION:-"all"}"
local STACK="${1:-gantry}"
# ACCUMULATED_ERRORS is the number of errors that are not caused by updating.
local ACCUMULATED_ERRORS="${2:-0}"
Expand Down Expand Up @@ -397,10 +398,22 @@ _report_services() {
[ "${NUM_TOTAL_ERRORS}" != "0" ] && TYPE="failure"
local ERROR_STRING=
[ "${NUM_ERRORS}" != "0" ] && ERROR_STRING=" ${NUM_TOTAL_ERRORS} error(s)"
local TITLE BODY
local TITLE BODY SEND_NOTIFICATION
TITLE="[${STACK}] ${NUM_UPDATED} services updated ${NUM_FAILED} failed${ERROR_STRING}"
BODY=$(echo -e "${UPDATED_MSG}\n${FAILED_MSG}\n${ERROR_MSG}")
_send_notification "${TYPE}" "${TITLE}" "${BODY}"
SEND_NOTIFICATION="true"
case "${CONDITION}" in
"on-change")
if [ "${NUM_UPDATED}" = "0" ] && [ "${NUM_TOTAL_ERRORS}" = "0" ]; then
SEND_NOTIFICATION="false"
fi
;;
"all"|*)
;;
esac
if is_true "${SEND_NOTIFICATION}"; then
_send_notification "${TYPE}" "${TITLE}" "${BODY}"
fi
}

_in_list() {
Expand Down

0 comments on commit 4d030a0

Please sign in to comment.