|
|
@ -1,9 +1,18 @@ |
|
|
|
#!/bin/bash |
|
|
|
#!/bin/sh |
|
|
|
|
|
|
|
docker_api() { |
|
|
|
local url="$1" |
|
|
|
shift |
|
|
|
## Note: the 'localhost' part is ignored by curl, but it is not |
|
|
|
## we can't remove it (like: http:/$url). If it does work on ubuntu's |
|
|
|
## curl, it doesn't work on alpine's curl. |
|
|
|
curl -s --unix-socket /var/run/docker.sock "http://localhost/$url" "$@" |
|
|
|
} |
|
|
|
|
|
|
|
exname=$(basename "$0") |
|
|
|
usage="$exname [-h|--help] CONTAINER SIGNAL" |
|
|
|
usage="$exname [-h|--help] SERVICE SIGNAL" |
|
|
|
|
|
|
|
container= |
|
|
|
service= |
|
|
|
signal= |
|
|
|
while [ "$1" ]; do |
|
|
|
case "$1" in |
|
|
@ -12,7 +21,7 @@ while [ "$1" ]; do |
|
|
|
exit 0 |
|
|
|
;; |
|
|
|
*) |
|
|
|
[ -z "$container" ] && { container=$1 ; shift ; continue ; } |
|
|
|
[ -z "$service" ] && { service=$1 ; shift ; continue ; } |
|
|
|
[ -z "$signal" ] && { signal=$1 ; shift ; continue ; } |
|
|
|
echo "Unexpected argument '$1'." >&2 |
|
|
|
exit 1 |
|
|
@ -21,18 +30,36 @@ while [ "$1" ]; do |
|
|
|
shift |
|
|
|
done |
|
|
|
|
|
|
|
if [ -z "$container" ]; then |
|
|
|
echo "You must provide a container name/id as first argument." >&2 |
|
|
|
if [ -z "$service" ]; then |
|
|
|
echo "You must provide a service name as first argument." >&2 |
|
|
|
echo "$usage" >&2 |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
|
|
|
|
if [ -z "$signal" ]; then |
|
|
|
echo "You must provide a signal to send to $container aargument." >&2 |
|
|
|
echo "You must provide a signal to send to $service aargument." >&2 |
|
|
|
echo "$usage" >&2 |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
|
|
|
|
container_id="$(docker inspect --format="{{ .Id }}" "$container")" |
|
|
|
|
|
|
|
echo -e "POST /containers/$container_id/kill?signal=$signal HTTP/1.0\r\n" | nc -U /var/run/docker.sock |
|
|
|
if ! containers=$( |
|
|
|
docker_api containers/json \ |
|
|
|
-G --data-urlencode filters="{\"label\": [\"compose.service=$service\"]}"); then |
|
|
|
echo "Curl toward socket for list of containers failed." >&2 |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
if ! container_id=$(echo "$containers" | jq -r '.[0].Id'); then |
|
|
|
echo "Failed to query following JSON:" >&2 |
|
|
|
echo "$containers" >&2 |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
|
|
|
|
if [ "$container_id" -a "$container_id" != "null" ]; then |
|
|
|
echo "Sending $signal to $service" >&2 |
|
|
|
docker_api "containers/${container_id}/kill" --data "signal=$signal" |
|
|
|
else |
|
|
|
echo "No container found for service '$service'." >&2 |
|
|
|
fi |
|
|
|
|
|
|
|
true |