1
1
#! /bin/bash
2
2
set -euo pipefail
3
3
4
+ server_version () {
5
+ docker version -f " {{.Server.Version}}"
6
+ }
7
+
4
8
update_services () {
5
9
local blacklist=" $1 "
10
+ local supports_detach_option=$2
11
+ local detach_option=" "
12
+ [ $supports_detach_option = true ] && detach_option=" --detach=false"
6
13
7
14
for service in $( IFS=" \n" docker service ls --quiet) ; do
8
15
local name image_with_digest image
@@ -11,19 +18,30 @@ update_services() {
11
18
image_with_digest=" $( docker service inspect " $service " -f ' {{.Spec.TaskTemplate.ContainerSpec.Image}}' ) "
12
19
image=$( echo " $image_with_digest " | cut -d@ -f1)
13
20
echo " Updating service $name with image $image "
14
- docker service update " $service " --detach=false --image=" $image " > /dev/null
21
+ docker service update " $service " $detach_option --image=" $image " > /dev/null
15
22
fi
16
23
done
17
24
}
18
25
19
26
main () {
20
- local blacklist=" ${BLACKLIST_SERVICES:- } "
27
+ local blacklist sleep_time supports_detach_option
28
+ blacklist=" ${BLACKLIST_SERVICES:- } "
29
+ sleep_time=" ${SLEEP_TIME:- 5m} "
30
+
31
+ supports_detach_option=false
32
+ if [[ " $( server_version) " > " 17.05" ]]; then
33
+ supports_detach_option=true
34
+ echo " Enabling synchronous service updates"
35
+ else
36
+ supports_detach_option=false
37
+ fi
38
+
21
39
[[ " $blacklist " != " " ]] && echo " Excluding services: $blacklist "
22
40
23
41
while true ; do
24
- update_services " ${ blacklist} "
25
- echo " Sleeping ${SLEEP_TIME} before next update"
26
- sleep " ${SLEEP_TIME} "
42
+ update_services " $blacklist " " $supports_detach_option "
43
+ echo " Sleeping $sleep_time before next update"
44
+ sleep " $sleep_time "
27
45
done
28
46
}
29
47
0 commit comments