From 0d0c93a5e1d57a6f38961d0cb58f981d757a78e2 Mon Sep 17 00:00:00 2001 From: Martin Honermeyer Date: Sat, 3 Jun 2017 02:50:11 +0200 Subject: [PATCH 1/2] Make shepherd a bash script We need this in order to conveniently do string operations. --- Dockerfile | 2 ++ shepherd | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1ff28cb..3bf14b5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,8 @@ FROM docker ENV SLEEP_TIME='5m' +RUN apk add --update --no-cache bash + COPY shepherd /usr/local/bin/shepherd ENTRYPOINT ["/usr/local/bin/shepherd"] diff --git a/shepherd b/shepherd index 585434d..f64842b 100755 --- a/shepherd +++ b/shepherd @@ -1,5 +1,4 @@ -#!/bin/ash -# shellcheck shell=dash +#!/bin/bash set -euo pipefail update_services() { From 97b78c550208acd1426f364701c2a3b47f237d2d Mon Sep 17 00:00:00 2001 From: Martin Honermeyer Date: Sat, 3 Jun 2017 02:50:49 +0200 Subject: [PATCH 2/2] Allow blacklisting services --- README.md | 1 + shepherd | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1c3ee45..16780fe 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ A Docker swarm service for automatically updating your services whenever their b --replicas 1 \ --constraint "node.role==manager" \ --env SLEEP_TIME="5m" \ + --env BLACKLIST_SERVICES="shepherd" \ --mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock,ro \ mazzolino/shepherd diff --git a/shepherd b/shepherd index f64842b..5670de7 100755 --- a/shepherd +++ b/shepherd @@ -2,19 +2,26 @@ set -euo pipefail update_services() { + local blacklist="$1" + for service in $(IFS="\n" docker service ls --quiet); do local name image_with_digest image name="$(docker service inspect "$service" -f '{{.Spec.Name}}')" - image_with_digest="$(docker service inspect "$service" -f '{{.Spec.TaskTemplate.ContainerSpec.Image}}')" - image=$(echo "$image_with_digest" | cut -d@ -f1) - echo "Updating service $name with image $image" - docker service update "$service" --image="$image" > /dev/null + if [[ " $blacklist " != *" $name "* ]]; then + image_with_digest="$(docker service inspect "$service" -f '{{.Spec.TaskTemplate.ContainerSpec.Image}}')" + image=$(echo "$image_with_digest" | cut -d@ -f1) + echo "Updating service $name with image $image" + docker service update "$service" --image="$image" > /dev/null + fi done } main() { + local blacklist="${BLACKLIST_SERVICES:-}" + [[ "$blacklist" != "" ]] && echo "Excluding services: $blacklist" + while true; do - update_services + update_services "${blacklist}" echo "Sleeping ${SLEEP_TIME} before next update" sleep "${SLEEP_TIME}" done