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/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 585434d..5670de7 100755 --- a/shepherd +++ b/shepherd @@ -1,21 +1,27 @@ -#!/bin/ash -# shellcheck shell=dash +#!/bin/bash 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