Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # ============================================================================
- # entrypoint.sh
- #
- # An entrypoint that blocks until one or more TCP hosts become availabile
- # ============================================================================
- set -e
- trap '{ echo "Aborting due to interrupt" ; exit 1; }' INT
- timeout=${WAIT_HOSTS_TIMEOUT:-30}
- waitAfterHosts=${WAIT_AFTER_HOSTS:-0}
- waitBeforeHosts=${WAIT_BEFORE_HOSTS:-0}
- function try() {
- if type gtimeout > /dev/null 2>&1; then
- gtimeout 1 bash -c "$1"
- elif type timeout > /dev/null 2>&1; then
- timeout 1 bash -c "$1"
- else
- echo >&2 "Couldn't find timeout or gtimeout executables in path";
- exit 1;
- fi
- }
- echo "Waiting for ${waitBeforeHosts} seconds."
- sleep $waitBeforeHosts
- # comma separated pairs of the form "[host]:[ip]"
- if [ -n "$WAIT_HOSTS" ]; then
- uris=$(echo $WAIT_HOSTS | sed -e 's/,/ /g' -e 's/\s+/\n/g' | uniq)
- fi
- # wait for each target
- if [ -z "$uris" ];
- then
- echo "No wait targets found." >&2;
- else
- for uri in $uris
- do
- host=$(echo $uri | cut -d: -f1)
- port=$(echo $uri | cut -d: -f2)
- [ -n "${host}" ]
- [ -n "${port}" ]
- echo "Waiting for ${uri}."
- seconds=0
- while [ "$seconds" -lt "$timeout" ] && ! try "> /dev/tcp/$host/$port 2> /dev/null"
- do
- echo -n .
- seconds=$((seconds + 1))
- sleep 1
- done
- if [ "$seconds" -lt "$timeout" ]; then
- echo "${uri} is up!"
- else
- echo " ERROR: unable to connect to ${uri}" >&2
- exit 1
- fi
- done
- echo "All hosts are up"
- fi
- echo "Waiting for ${waitAfterHosts} seconds."
- sleep $waitAfterHosts
- # Execute the Docker container's CMD
- exec "$@"
Add Comment
Please, Sign In to add comment