Guest User

Untitled

a guest
Mar 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # ============================================================================
  4. # entrypoint.sh
  5. #
  6. # An entrypoint that blocks until one or more TCP hosts become availabile
  7. # ============================================================================
  8.  
  9. set -e
  10.  
  11. trap '{ echo "Aborting due to interrupt" ; exit 1; }' INT
  12.  
  13. timeout=${WAIT_HOSTS_TIMEOUT:-30}
  14. waitAfterHosts=${WAIT_AFTER_HOSTS:-0}
  15. waitBeforeHosts=${WAIT_BEFORE_HOSTS:-0}
  16.  
  17. function try() {
  18. if type gtimeout > /dev/null 2>&1; then
  19. gtimeout 1 bash -c "$1"
  20. elif type timeout > /dev/null 2>&1; then
  21. timeout 1 bash -c "$1"
  22. else
  23. echo >&2 "Couldn't find timeout or gtimeout executables in path";
  24. exit 1;
  25. fi
  26. }
  27.  
  28. echo "Waiting for ${waitBeforeHosts} seconds."
  29. sleep $waitBeforeHosts
  30.  
  31. # comma separated pairs of the form "[host]:[ip]"
  32. if [ -n "$WAIT_HOSTS" ]; then
  33. uris=$(echo $WAIT_HOSTS | sed -e 's/,/ /g' -e 's/\s+/\n/g' | uniq)
  34. fi
  35.  
  36. # wait for each target
  37. if [ -z "$uris" ];
  38. then
  39. echo "No wait targets found." >&2;
  40.  
  41. else
  42. for uri in $uris
  43. do
  44. host=$(echo $uri | cut -d: -f1)
  45. port=$(echo $uri | cut -d: -f2)
  46. [ -n "${host}" ]
  47. [ -n "${port}" ]
  48. echo "Waiting for ${uri}."
  49. seconds=0
  50. while [ "$seconds" -lt "$timeout" ] && ! try "> /dev/tcp/$host/$port 2> /dev/null"
  51. do
  52. echo -n .
  53. seconds=$((seconds + 1))
  54. sleep 1
  55. done
  56.  
  57. if [ "$seconds" -lt "$timeout" ]; then
  58. echo "${uri} is up!"
  59. else
  60. echo " ERROR: unable to connect to ${uri}" >&2
  61. exit 1
  62. fi
  63. done
  64. echo "All hosts are up"
  65. fi
  66.  
  67. echo "Waiting for ${waitAfterHosts} seconds."
  68. sleep $waitAfterHosts
  69.  
  70. # Execute the Docker container's CMD
  71. exec "$@"
Add Comment
Please, Sign In to add comment