From 9408f01f3d5979922df63708d948e7f02517036f Mon Sep 17 00:00:00 2001 From: John Kyrus Date: Thu, 30 Apr 2020 12:53:03 -0400 Subject: [PATCH 01/12] Added time stamp to logs --- shepherd | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/shepherd b/shepherd index 8cb75a8..cbaba30 100755 --- a/shepherd +++ b/shepherd @@ -24,20 +24,20 @@ update_services() { image=$(echo "$image_with_digest" | cut -d@ -f1) if ! DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect $image > /dev/null; then - echo "Error updating service $name! Image $image does not exist or it is not available" + echo "$(date) Error updating service $name! Image $image does not exist or it is not available" else - echo "Trying to update service $name with image $image" + echo "$(date) Trying to update service $name with image $image" docker service update "$name" $detach_option $registry_auth --image="$image" > /dev/null previousImage=$(docker service inspect "$name" -f '{{.PreviousSpec.TaskTemplate.ContainerSpec.Image}}') currentImage=$(docker service inspect "$name" -f '{{.Spec.TaskTemplate.ContainerSpec.Image}}') if [ "$previousImage" == "$currentImage" ]; then - echo "No updates to service $name!" + echo "$(date) No updates to service $name!" else - echo "Service $name was updated!" + echo "$(date) Service $name was updated!" if [[ "$apprise_sidecar_url" != "" ]]; then title="[Shepherd] Service $name updated" - body="Service $name was updated from $previousImage to $currentImage" + body="$(date) Service $name was updated from $previousImage to $currentImage" curl -X POST -H "Content-Type: application/json" --data "{\"title\": \"$title\", \"body\": \"$body\"}" "$apprise_sidecar_url" fi fi @@ -54,20 +54,20 @@ main() { supports_detach_option=false if [[ "$(server_version)" > "17.05" ]]; then supports_detach_option=true - echo "Enabling synchronous service updates" + echo "$(date) Enabling synchronous service updates" fi supports_registry_auth=false if [[ ${WITH_REGISTRY_AUTH+x} ]]; then supports_registry_auth=true - echo "Send registry authentication details to swarm agents" + echo "$(date) Send registry authentication details to swarm agents" fi - [[ "$blacklist" != "" ]] && echo "Excluding services: $blacklist" + [[ "$blacklist" != "" ]] && echo "$(date) Excluding services: $blacklist" while true; do update_services "$blacklist" "$supports_detach_option" "$supports_registry_auth" - echo "Sleeping $sleep_time before next update" + echo "$(date) Sleeping $sleep_time before next update" sleep "$sleep_time" done } From 47233071547583e0cc9e423c5f586bc5d2ad8666 Mon Sep 17 00:00:00 2001 From: John Kyrus Date: Thu, 14 May 2020 14:16:05 -0400 Subject: [PATCH 02/12] Created a logging function and added a verbose mode Log function which takes the log and whether it is considered a verbose mode log. If it is a verbose log and verbose mode is false it will NOT print the log. If verbose mode is true it will print all logs. Also using a timezone environment variable to set the timezone file at /etc/timezone. --- shepherd | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/shepherd b/shepherd index cbaba30..085d3c5 100755 --- a/shepherd +++ b/shepherd @@ -5,6 +5,15 @@ server_version() { docker version -f "{{.Server.Version}}" } +logger() { + local log="$1" + local is_verbose_log="${2:-false}" + + if [ "$is_verbose_log" == "true" -a "$verbose" == "true" -o "$is_verbose_log" == "false" ]; then + echo "$(date) $log" + fi +} + update_services() { local blacklist="$1" local supports_detach_option=$2 @@ -24,17 +33,17 @@ update_services() { image=$(echo "$image_with_digest" | cut -d@ -f1) if ! DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect $image > /dev/null; then - echo "$(date) Error updating service $name! Image $image does not exist or it is not available" + logger "Error updating service $name! Image $image does not exist or it is not available" else - echo "$(date) Trying to update service $name with image $image" + logger "Trying to update service $name with image $image" "true" docker service update "$name" $detach_option $registry_auth --image="$image" > /dev/null previousImage=$(docker service inspect "$name" -f '{{.PreviousSpec.TaskTemplate.ContainerSpec.Image}}') currentImage=$(docker service inspect "$name" -f '{{.Spec.TaskTemplate.ContainerSpec.Image}}') if [ "$previousImage" == "$currentImage" ]; then - echo "$(date) No updates to service $name!" + logger "No updates to service $name!" "true" else - echo "$(date) Service $name was updated!" + logger "Service $name was updated!" if [[ "$apprise_sidecar_url" != "" ]]; then title="[Shepherd] Service $name updated" body="$(date) Service $name was updated from $previousImage to $currentImage" @@ -47,27 +56,32 @@ update_services() { } main() { - local blacklist sleep_time supports_detach_option supports_registry_auth + local blacklist sleep_time supports_detach_option supports_registry_auth tz verbose blacklist="${BLACKLIST_SERVICES:-}" sleep_time="${SLEEP_TIME:-5m}" + tz="${TZ:-EST}" + verbose="${VERBOSE:-true}" + + echo $tz > /etc/timezone + logger "Timezone set to $tz" supports_detach_option=false if [[ "$(server_version)" > "17.05" ]]; then supports_detach_option=true - echo "$(date) Enabling synchronous service updates" + logger "Enabling synchronous service updates" fi supports_registry_auth=false if [[ ${WITH_REGISTRY_AUTH+x} ]]; then supports_registry_auth=true - echo "$(date) Send registry authentication details to swarm agents" + logger "Send registry authentication details to swarm agents" fi - [[ "$blacklist" != "" ]] && echo "$(date) Excluding services: $blacklist" + [[ "$blacklist" != "" ]] && logger "Excluding services: $blacklist" while true; do update_services "$blacklist" "$supports_detach_option" "$supports_registry_auth" - echo "$(date) Sleeping $sleep_time before next update" + logger "Sleeping $sleep_time before next update" "true" sleep "$sleep_time" done } From 94ab55b479f1ddf1d98ec2b4d34f02a358d1c96d Mon Sep 17 00:00:00 2001 From: John Kyrus Date: Thu, 14 May 2020 14:17:16 -0400 Subject: [PATCH 03/12] Added tzdata package for setting the timezone --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 1036c34..651d8de 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ FROM docker ENV SLEEP_TIME='5m' ENV FILTER_SERVICES='' -RUN apk add --update --no-cache bash curl +RUN apk add --update --no-cache bash curl tzdata COPY shepherd /usr/local/bin/shepherd From 5626525f3338c5034798ca89b4b344bbfcdf22f8 Mon Sep 17 00:00:00 2001 From: John Kyrus Date: Thu, 14 May 2020 14:18:51 -0400 Subject: [PATCH 04/12] Removed env variables from dockerfile and moving to docker-compose --- Dockerfile | 3 --- 1 file changed, 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 651d8de..484f797 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,5 @@ FROM docker -ENV SLEEP_TIME='5m' -ENV FILTER_SERVICES='' - RUN apk add --update --no-cache bash curl tzdata COPY shepherd /usr/local/bin/shepherd From a69d3bab5e8e189604edd834058c30dc27cca876 Mon Sep 17 00:00:00 2001 From: John Kyrus Date: Thu, 14 May 2020 14:20:10 -0400 Subject: [PATCH 05/12] Moved env variables from dockerfile and added a timezone and verbose logging variables --- docker-compose.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 0fd026a..53d8fcf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,3 +10,8 @@ services: placement: constraints: - node.role == manager + environment: + TZ: 'US/Eastern' + SLEEP_TIME: '5m' + FILTER_SERVICES: '' + VERBOSE: 'true' From 641079421d95cb467fc603043689a218fc0fecd5 Mon Sep 17 00:00:00 2001 From: John Kyrus Date: Thu, 14 May 2020 14:22:02 -0400 Subject: [PATCH 06/12] Moved env variables from dockerfile and added a timezone and verbose logging variables --- docker-compose.apprise.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docker-compose.apprise.yml b/docker-compose.apprise.yml index f9e9666..ecf116d 100644 --- a/docker-compose.apprise.yml +++ b/docker-compose.apprise.yml @@ -14,6 +14,11 @@ services: placement: constraints: - node.role == manager + environment: + TZ: 'US/Eastern' + SLEEP_TIME: '5m' + FILTER_SERVICES: '' + VERBOSE: 'true' notify: image: mazzolino/apprise-microservice:0.1 From f8283539a5bbe8dc1aedd832d0496d6776b58977 Mon Sep 17 00:00:00 2001 From: John Kyrus Date: Thu, 14 May 2020 14:29:29 -0400 Subject: [PATCH 07/12] Update shepherd --- shepherd | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/shepherd b/shepherd index 085d3c5..f583056 100755 --- a/shepherd +++ b/shepherd @@ -18,13 +18,19 @@ update_services() { local blacklist="$1" local supports_detach_option=$2 local supports_registry_auth=$3 + local supports_insecure_registry=$4 + local supports_no_resolve_image=$5 local detach_option="" local registry_auth="" + local insecure_registry_flag="" + local no_resolve_image_flag="" local name local apprise_sidecar_url="${APPRISE_SIDECAR_URL:-}" [ $supports_detach_option = true ] && detach_option="--detach=false" [ $supports_registry_auth = true ] && registry_auth="--with-registry-auth" + [ $supports_insecure_registry = true ] && insecure_registry_flag="--insecure" + [ $supports_no_resolve_image = true ] && no_resolve_image_flag="--no-resolve-image" for name in $(IFS=$'\n' docker service ls --quiet --filter "${FILTER_SERVICES}" --format '{{.Name}}'); do local image_with_digest image @@ -32,11 +38,11 @@ update_services() { image_with_digest="$(docker service inspect "$name" -f '{{.Spec.TaskTemplate.ContainerSpec.Image}}')" image=$(echo "$image_with_digest" | cut -d@ -f1) - if ! DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect $image > /dev/null; then + if ! DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect $insecure_registry_flag $image > /dev/null; then logger "Error updating service $name! Image $image does not exist or it is not available" else logger "Trying to update service $name with image $image" "true" - docker service update "$name" $detach_option $registry_auth --image="$image" > /dev/null + docker service update "$name" $detach_option $registry_auth $no_resolve_image_flag --image="$image" > /dev/null previousImage=$(docker service inspect "$name" -f '{{.PreviousSpec.TaskTemplate.ContainerSpec.Image}}') currentImage=$(docker service inspect "$name" -f '{{.Spec.TaskTemplate.ContainerSpec.Image}}') @@ -77,10 +83,22 @@ main() { logger "Send registry authentication details to swarm agents" fi + supports_insecure_registry=false + if [[ ${WITH_INSECURE_REGISTRY+x} ]]; then + supports_insecure_registry=true + logger "Connection to insecure registry available" + fi + + supports_no_resolve_image=false + if [[ ${WITH_NO_RESOLVE_IMAGE+x} ]]; then + supports_no_resolve_image=true + logger "Deployment without resolving image" + fi + [[ "$blacklist" != "" ]] && logger "Excluding services: $blacklist" while true; do - update_services "$blacklist" "$supports_detach_option" "$supports_registry_auth" + update_services "$blacklist" "$supports_detach_option" "$supports_registry_auth" "$supports_insecure_registry" "$supports_no_resolve_image" logger "Sleeping $sleep_time before next update" "true" sleep "$sleep_time" done From 72d8fe10c9f7f2b2fcf4d2821bdfa59563fe3d45 Mon Sep 17 00:00:00 2001 From: John Kyrus Date: Mon, 1 Jun 2020 14:38:47 -0400 Subject: [PATCH 08/12] Added ENV variables back in --- Dockerfile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Dockerfile b/Dockerfile index 484f797..6316c25 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,10 @@ FROM docker +ENV SLEEP_TIME='5m' +ENV FILTER_SERVICES='' +ENV TZ='US/Eastern' +ENV VERBOSE='true' + RUN apk add --update --no-cache bash curl tzdata COPY shepherd /usr/local/bin/shepherd From 529272e0d50d72138ff00532196371724a3f3cef Mon Sep 17 00:00:00 2001 From: John Kyrus Date: Tue, 2 Jun 2020 10:06:43 -0400 Subject: [PATCH 09/12] Combined the 2 environment sections Didn't notice the original environment section when adding the new variables --- docker-compose.apprise.yml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/docker-compose.apprise.yml b/docker-compose.apprise.yml index ecf116d..6ceaacd 100644 --- a/docker-compose.apprise.yml +++ b/docker-compose.apprise.yml @@ -6,6 +6,10 @@ services: image: mazzolino/shepherd environment: APPRISE_SIDECAR_URL: notify:5000 + TZ: 'US/Eastern' + SLEEP_TIME: '5m' + FILTER_SERVICES: '' + VERBOSE: 'true' volumes: - /var/run/docker.sock:/var/run/docker.sock networks: @@ -13,12 +17,7 @@ services: deploy: placement: constraints: - - node.role == manager - environment: - TZ: 'US/Eastern' - SLEEP_TIME: '5m' - FILTER_SERVICES: '' - VERBOSE: 'true' + - node.role == manager notify: image: mazzolino/apprise-microservice:0.1 From fa7c9496c000816c4612866b5ce79382fd912ed7 Mon Sep 17 00:00:00 2001 From: John Kyrus Date: Thu, 4 Jun 2020 22:36:48 -0400 Subject: [PATCH 10/12] Updated if statement operators --- shepherd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shepherd b/shepherd index 334c877..20997d3 100755 --- a/shepherd +++ b/shepherd @@ -9,7 +9,7 @@ logger() { local log="$1" local is_verbose_log="${2:-false}" - if [ "$is_verbose_log" == "true" -a "$verbose" == "true" -o "$is_verbose_log" == "false" ]; then + if [[ "$is_verbose_log" == "true" && "$verbose" == "true" || "$is_verbose_log" == "false" ]]; then echo "$(date) $log" fi } From 36afdf66219c067530e65cee25cac0911bd88522 Mon Sep 17 00:00:00 2001 From: John Kyrus Date: Sun, 7 Jun 2020 18:54:32 -0400 Subject: [PATCH 11/12] Removed setting of timezone file since it is not needed --- shepherd | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/shepherd b/shepherd index 20997d3..4f45df2 100755 --- a/shepherd +++ b/shepherd @@ -66,11 +66,9 @@ main() { local blacklist sleep_time supports_detach_option supports_registry_auth tz verbose blacklist="${BLACKLIST_SERVICES:-}" sleep_time="${SLEEP_TIME:-5m}" - tz="${TZ:-EST}" verbose="${VERBOSE:-true}" - - echo $tz > /etc/timezone - logger "Timezone set to $tz" + + logger "Timezone set to $TZ" supports_detach_option=false if [[ "$(server_version)" > "17.05" ]]; then From 76a884a2e2f5c136ff773e85015dade3b527909f Mon Sep 17 00:00:00 2001 From: Martin Honermeyer Date: Mon, 8 Jun 2020 23:29:02 +0200 Subject: [PATCH 12/12] Cleanup compose files --- docker-compose.apprise.yml | 2 +- docker-compose.yml | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docker-compose.apprise.yml b/docker-compose.apprise.yml index 6ceaacd..75df9d6 100644 --- a/docker-compose.apprise.yml +++ b/docker-compose.apprise.yml @@ -17,7 +17,7 @@ services: deploy: placement: constraints: - - node.role == manager + - node.role == manager notify: image: mazzolino/apprise-microservice:0.1 diff --git a/docker-compose.yml b/docker-compose.yml index 53d8fcf..decc3b3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,14 +4,14 @@ services: app: build: . image: mazzolino/shepherd + environment: + TZ: 'US/Eastern' + SLEEP_TIME: '5m' + FILTER_SERVICES: '' + VERBOSE: 'true' volumes: - /var/run/docker.sock:/var/run/docker.sock deploy: placement: constraints: - node.role == manager - environment: - TZ: 'US/Eastern' - SLEEP_TIME: '5m' - FILTER_SERVICES: '' - VERBOSE: 'true'