From 591857e2ba12aa9b9db8fd14ebea96bf3ac10ba0 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sat, 1 Feb 2020 18:34:59 +0100 Subject: [PATCH] add simple wait-for script --- .gitignore | 1 + Dockerfile.amd64 | 4 +- overlay/usr/local/bin/entrypoint | 11 +++ overlay/usr/local/bin/entrypoint.sh | 5 -- .../local/bin/{healthcheck.sh => healthcheck} | 2 +- overlay/usr/local/bin/wait-for | 79 +++++++++++++++++++ 6 files changed, 94 insertions(+), 8 deletions(-) create mode 100755 overlay/usr/local/bin/entrypoint delete mode 100755 overlay/usr/local/bin/entrypoint.sh rename overlay/usr/local/bin/{healthcheck.sh => healthcheck} (100%) create mode 100755 overlay/usr/local/bin/wait-for diff --git a/.gitignore b/.gitignore index 7635a93..49f67ba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ env* test/ +source/ diff --git a/Dockerfile.amd64 b/Dockerfile.amd64 index 4b4c0d3..4562046 100644 --- a/Dockerfile.amd64 +++ b/Dockerfile.amd64 @@ -35,7 +35,7 @@ USER app STOPSIGNAL SIGTERM -ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] -HEALTHCHECK --interval=10s --timeout=3s --retries=3 CMD /usr/local/bin/healthcheck.sh +ENTRYPOINT ["/usr/local/bin/entrypoint"] +HEALTHCHECK --interval=10s --timeout=3s --retries=3 CMD /usr/local/bin/healthcheck WORKDIR /app CMD [] diff --git a/overlay/usr/local/bin/entrypoint b/overlay/usr/local/bin/entrypoint new file mode 100755 index 0000000..62a2e28 --- /dev/null +++ b/overlay/usr/local/bin/entrypoint @@ -0,0 +1,11 @@ +#!/usr/bin/env sh +set -eo pipefail + +/usr/local/bin/gomplate -V -o /app/.env -f /etc/templates/env.tmpl + +if [ -z "$BITWARDENRS_WAIT_FOR_HOST" ] && [ -z "$BITWARDENRS_WAIT_FOR_PORT" ] +then + exec /app/bitwarden_rs +else + exec ${BITWARDENRS_WAIT_FOR_HOST}:${BITWARDENRS_WAIT_FOR_PORT} -- /app/bitwarden_rs +fi diff --git a/overlay/usr/local/bin/entrypoint.sh b/overlay/usr/local/bin/entrypoint.sh deleted file mode 100755 index f323c3d..0000000 --- a/overlay/usr/local/bin/entrypoint.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env sh - -/usr/local/bin/gomplate -V -o /app/.env -f /etc/templates/env.tmpl - -exec /app/bitwarden_rs diff --git a/overlay/usr/local/bin/healthcheck.sh b/overlay/usr/local/bin/healthcheck similarity index 100% rename from overlay/usr/local/bin/healthcheck.sh rename to overlay/usr/local/bin/healthcheck index d580dbf..c2778b1 100755 --- a/overlay/usr/local/bin/healthcheck.sh +++ b/overlay/usr/local/bin/healthcheck @@ -1,6 +1,6 @@ #!/usr/bin/env sh - set -eo pipefail + URL=http://127.0.0.1:8080/alive wget --quiet --tries=1 --spider ${URL} diff --git a/overlay/usr/local/bin/wait-for b/overlay/usr/local/bin/wait-for new file mode 100755 index 0000000..ddfc39e --- /dev/null +++ b/overlay/usr/local/bin/wait-for @@ -0,0 +1,79 @@ +#!/bin/sh + +TIMEOUT=15 +QUIET=0 + +echoerr() { + if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi +} + +usage() { + exitcode="$1" + cat << USAGE >&2 +Usage: + $cmdname host:port [-t timeout] [-- command args] + -q | --quiet Do not output any status messages + -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout + -- COMMAND ARGS Execute command with args after the test finishes +USAGE + exit "$exitcode" +} + +wait_for() { + for i in `seq $TIMEOUT` ; do + nc -z "$HOST" "$PORT" > /dev/null 2>&1 + + result=$? + if [ $result -eq 0 ] ; then + if [ $# -gt 0 ] ; then + exec "$@" + fi + exit 0 + fi + sleep 1 + done + echo "Operation timed out" >&2 + exit 1 +} + +while [ $# -gt 0 ] +do + case "$1" in + *:* ) + HOST=$(printf "%s\n" "$1"| cut -d : -f 1) + PORT=$(printf "%s\n" "$1"| cut -d : -f 2) + shift 1 + ;; + -q | --quiet) + QUIET=1 + shift 1 + ;; + -t) + TIMEOUT="$2" + if [ "$TIMEOUT" = "" ]; then break; fi + shift 2 + ;; + --timeout=*) + TIMEOUT="${1#*=}" + shift 1 + ;; + --) + shift + break + ;; + --help) + usage 0 + ;; + *) + echoerr "Unknown argument: $1" + usage 1 + ;; + esac +done + +if [ "$HOST" = "" -o "$PORT" = "" ]; then + echoerr "Error: you need to provide a host and port to test." + usage 2 +fi + +wait_for "$@"