forked from containrrr/shepherd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shepherd
executable file
·56 lines (46 loc) · 1.68 KB
/
shepherd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
set -euo pipefail
server_version() {
docker version -f "{{.Server.Version}}"
}
update_services() {
local blacklist="$1"
local supports_detach_option=$2
local supports_registry_auth=$3
local detach_option=""
local registry_auth=""
[ $supports_detach_option = true ] && detach_option="--detach=false"
[ $supports_registry_auth = true ] && registry_auth="--with-registry-auth"
for service in $(IFS="\n" docker service ls --quiet); do
local name image_with_digest image
name="$(docker service inspect "$service" -f '{{.Spec.Name}}')"
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" $detach_option $registry_auth --image="$image" > /dev/null
fi
done
}
main() {
local blacklist sleep_time supports_detach_option supports_registry_auth
blacklist="${BLACKLIST_SERVICES:-}"
sleep_time="${SLEEP_TIME:-5m}"
supports_detach_option=false
if [[ "$(server_version)" > "17.05" ]]; then
supports_detach_option=true
echo "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"
fi
[[ "$blacklist" != "" ]] && echo "Excluding services: $blacklist"
while true; do
update_services "$blacklist" "$supports_detach_option" "$supports_registry_auth"
echo "Sleeping $sleep_time before next update"
sleep "$sleep_time"
done
}
main "$@"