wait-for.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/bin/sh
  2. # The MIT License (MIT)
  3. #
  4. # Copyright (c) 2017 Eficode Oy
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in all
  14. # copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. VERSION="2.2.3"
  24. set -- "$@" -- "$TIMEOUT" "$QUIET" "$PROTOCOL" "$HOST" "$PORT" "$result"
  25. TIMEOUT=15
  26. QUIET=0
  27. # The protocol to make the request with, either "tcp" or "http"
  28. PROTOCOL="tcp"
  29. echoerr() {
  30. if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
  31. }
  32. usage() {
  33. exitcode="$1"
  34. cat << USAGE >&2
  35. Usage:
  36. $0 host:port|url [-t timeout] [-- command args]
  37. -q | --quiet Do not output any status messages
  38. -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
  39. -v | --version Show the version of this tool
  40. -- COMMAND ARGS Execute command with args after the test finishes
  41. USAGE
  42. exit "$exitcode"
  43. }
  44. wait_for() {
  45. case "$PROTOCOL" in
  46. tcp)
  47. if ! command -v nc >/dev/null; then
  48. echoerr 'nc command is missing!'
  49. exit 1
  50. fi
  51. ;;
  52. http)
  53. if ! command -v wget >/dev/null; then
  54. echoerr 'wget command is missing!'
  55. exit 1
  56. fi
  57. ;;
  58. esac
  59. TIMEOUT_END=$(($(date +%s) + TIMEOUT))
  60. while :; do
  61. case "$PROTOCOL" in
  62. tcp)
  63. nc -w 1 -z "$HOST" "$PORT" > /dev/null 2>&1
  64. ;;
  65. http)
  66. wget --timeout=1 -q "$HOST" -O /dev/null > /dev/null 2>&1
  67. ;;
  68. *)
  69. echoerr "Unknown protocol '$PROTOCOL'"
  70. exit 1
  71. ;;
  72. esac
  73. result=$?
  74. if [ $result -eq 0 ] ; then
  75. if [ $# -gt 7 ] ; then
  76. for result in $(seq $(($# - 7))); do
  77. result=$1
  78. shift
  79. set -- "$@" "$result"
  80. done
  81. TIMEOUT=$2 QUIET=$3 PROTOCOL=$4 HOST=$5 PORT=$6 result=$7
  82. shift 7
  83. exec "$@"
  84. fi
  85. exit 0
  86. fi
  87. if [ $TIMEOUT -ne 0 -a $(date +%s) -ge $TIMEOUT_END ]; then
  88. echo "Operation timed out" >&2
  89. exit 1
  90. fi
  91. sleep 1
  92. done
  93. }
  94. while :; do
  95. case "$1" in
  96. http://*|https://*)
  97. HOST="$1"
  98. PROTOCOL="http"
  99. shift 1
  100. ;;
  101. *:* )
  102. HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
  103. PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
  104. shift 1
  105. ;;
  106. -v | --version)
  107. echo $VERSION
  108. exit
  109. ;;
  110. -q | --quiet)
  111. QUIET=1
  112. shift 1
  113. ;;
  114. -q-*)
  115. QUIET=0
  116. echoerr "Unknown option: $1"
  117. usage 1
  118. ;;
  119. -q*)
  120. QUIET=1
  121. result=$1
  122. shift 1
  123. set -- -"${result#-q}" "$@"
  124. ;;
  125. -t | --timeout)
  126. TIMEOUT="$2"
  127. shift 2
  128. ;;
  129. -t*)
  130. TIMEOUT="${1#-t}"
  131. shift 1
  132. ;;
  133. --timeout=*)
  134. TIMEOUT="${1#*=}"
  135. shift 1
  136. ;;
  137. --)
  138. shift
  139. break
  140. ;;
  141. --help)
  142. usage 0
  143. ;;
  144. -*)
  145. QUIET=0
  146. echoerr "Unknown option: $1"
  147. usage 1
  148. ;;
  149. *)
  150. QUIET=0
  151. echoerr "Unknown argument: $1"
  152. usage 1
  153. ;;
  154. esac
  155. done
  156. if ! [ "$TIMEOUT" -ge 0 ] 2>/dev/null; then
  157. echoerr "Error: invalid timeout '$TIMEOUT'"
  158. usage 3
  159. fi
  160. case "$PROTOCOL" in
  161. tcp)
  162. if [ "$HOST" = "" ] || [ "$PORT" = "" ]; then
  163. echoerr "Error: you need to provide a host and port to test."
  164. usage 2
  165. fi
  166. ;;
  167. http)
  168. if [ "$HOST" = "" ]; then
  169. echoerr "Error: you need to provide a host to test."
  170. usage 2
  171. fi
  172. ;;
  173. esac
  174. wait_for "$@"