From a73fdc3460b9dd8dfc54c3087bd046799956e011 Mon Sep 17 00:00:00 2001 From: alban Date: Wed, 27 May 2020 14:58:50 +0200 Subject: [PATCH] [fix] docker compose should wait for es --- system/docker-compose/docker-compose.yml | 5 +- system/wait-for | 80 ++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100755 system/wait-for diff --git a/system/docker-compose/docker-compose.yml b/system/docker-compose/docker-compose.yml index 59ab397..e4ac8f6 100644 --- a/system/docker-compose/docker-compose.yml +++ b/system/docker-compose/docker-compose.yml @@ -26,9 +26,12 @@ services: app: env_file: .env - build: ../ + build: ../../ image: albancrommer/changelog-server:latest ports: - "3000:3000" depends_on: - es + command: ["./system/wait-for", "-t", "5", "es:9200", "--", "node", "index.js"] + read_only: true + diff --git a/system/wait-for b/system/wait-for new file mode 100755 index 0000000..451c637 --- /dev/null +++ b/system/wait-for @@ -0,0 +1,80 @@ +#!/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 +} + +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 + +while true; do + wait_for "$@" +done