From bfefe3f13e04c828d91ede36ae328cafcf99c2f4 Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Thu, 19 Sep 2024 22:07:53 -0700 Subject: [PATCH] [lib-gantry] add GANTRY_NOTIFICATION_CONDITION Co-authored-by: tito --- README.md | 1 + docs/migration.md | 1 + src/lib-gantry.sh | 17 +++++++++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dd53de1..ae4fe55 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docs/migration.md b/docs/migration.md index 3ade0fd..ae6ffba 100644 --- a/docs/migration.md +++ b/docs/migration.md @@ -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 | diff --git a/src/lib-gantry.sh b/src/lib-gantry.sh index 5f35f5a..84f1edd 100755 --- a/src/lib-gantry.sh +++ b/src/lib-gantry.sh @@ -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}" @@ -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() {