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.

64 lines
1.6 KiB

  1. #!/bin/sh
  2. docker_api() {
  3. local url="$1"
  4. shift
  5. ## Note: the 'localhost' part is ignored by curl, but it is not
  6. ## we can't remove it (like: http:/$url). If it does work on ubuntu's
  7. ## curl, it doesn't work on alpine's curl.
  8. curl -s --unix-socket /var/run/docker.sock "http://localhost/$url" "$@"
  9. }
  10. exname=$(basename "$0")
  11. usage="$exname [-h|--help] SERVICE SIGNAL"
  12. service=
  13. signal=
  14. while [ "$1" ]; do
  15. case "$1" in
  16. "--help"|"-h")
  17. echo "$usage" >&2
  18. exit 0
  19. ;;
  20. *)
  21. [ -z "$service" ] && { service=$1 ; shift ; continue ; }
  22. [ -z "$signal" ] && { signal=$1 ; shift ; continue ; }
  23. echo "Unexpected argument '$1'." >&2
  24. exit 1
  25. ;;
  26. esac
  27. shift
  28. done
  29. if [ -z "$service" ]; then
  30. echo "You must provide a service name as first argument." >&2
  31. echo "$usage" >&2
  32. exit 1
  33. fi
  34. if [ -z "$signal" ]; then
  35. echo "You must provide a signal to send to $service aargument." >&2
  36. echo "$usage" >&2
  37. exit 1
  38. fi
  39. if ! containers=$(
  40. docker_api containers/json \
  41. -G --data-urlencode filters="{\"label\": [\"compose.service=$service\"]}"); then
  42. echo "Curl toward socket for list of containers failed." >&2
  43. exit 1
  44. fi
  45. if ! container_id=$(echo "$containers" | jq -r '.[0].Id'); then
  46. echo "Failed to query following JSON:" >&2
  47. echo "$containers" >&2
  48. exit 1
  49. fi
  50. if [ "$container_id" -a "$container_id" != "null" ]; then
  51. echo "Sending $signal to $service" >&2
  52. docker_api "containers/${container_id}/kill" --data "signal=$signal"
  53. else
  54. echo "No container found for service '$service'." >&2
  55. fi
  56. true