Created
March 12, 2018 20:42
-
-
Save arleighdickerson/785f98afdad94c478e92b92a9736f465 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment