forked from helderco/docker-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-label-sighup
executable file
·47 lines (42 loc) · 1.23 KB
/
docker-label-sighup
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
#!/bin/bash
function docker_api {
local host
local path=${1:-/}
local method=${2:-GET}
local data=${3:-}
local curl_opts=(-s)
if [ "$method" = 'POST' ]; then
curl_opts+=(-d "$data")
if [ -n "$data" ]; then
curl_opts+=(-H 'Content-Type: application/json')
fi
elif [ -n "$data" ]; then
curl_opts+=(--get)
curl_opts+=(--data-urlencode "$data")
fi
if [ -z "$DOCKER_HOST" ];then
echo "Error DOCKER_HOST variable not set" >&2
return 1
fi
if [[ "$DOCKER_HOST" == unix://* ]]; then
curl_opts+=(--unix-socket ${DOCKER_HOST#unix://})
host='http://localhost'
else
host="http://${DOCKER_HOST#*://}"
fi
curl "${curl_opts[@]}" ${host}$path
}
function docker_kill {
local id="${1?missing id}"
local signal="${2?missing signal}"
docker_api "/containers/$id/kill?signal=$signal" "POST"
}
containers=$(docker_api "/containers/json" "GET" 'filters={"label": ["'$1'"]}' | jq -r '[.[] | .Id] | join(" ")')
if [ -z "${containers:-}" ]; then
echo "Error: can't get containers for label '$1'" >&2
exit 1
fi
for cid in ${containers}; do
echo "Sending SIGHUP to $cid..."
docker_kill "$cid" SIGHUP
done