You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

65 lines
1.6 KiB

#!/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] SERVICE SIGNAL"
service=
signal=
while [ "$1" ]; do
case "$1" in
"--help"|"-h")
echo "$usage" >&2
exit 0
;;
*)
[ -z "$service" ] && { service=$1 ; shift ; continue ; }
[ -z "$signal" ] && { signal=$1 ; shift ; continue ; }
echo "Unexpected argument '$1'." >&2
exit 1
;;
esac
shift
done
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 $service aargument." >&2
echo "$usage" >&2
exit 1
fi
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