-
Notifications
You must be signed in to change notification settings - Fork 195
/
wait-for-es.sh
executable file
·55 lines (45 loc) · 1.31 KB
/
wait-for-es.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
# Courtesy of @fxdgear
# https://github.com/elastic/elasticsearch-py/issues/778#issuecomment-384389668
set -ux
HOST="$1"
CONTAINER=${2:-""}
PREAMBLE=""
echo "container |$CONTAINER|"
if [[ $CONTAINER != "" ]]; then
PREAMBLE="docker-compose exec $CONTAINER"
fi
while true; do
response=$($PREAMBLE curl --write-out '%{http_code}' --silent --fail --output /dev/null "$HOST")
if [[ "$response" -eq "200" ]]; then
break
fi
echo "Elastic Search is unavailable - sleeping" >&2
sleep 1
done
# set -e now because it was causing the curl command above to exit the script
# if the server was not available
set -e
COUNTER=0
MAX_LOOPS=60
while true; do
## Wait for ES status to turn to yellow.
## TODO: Ideally we'd be waiting for green, but we need multiple nodes for that.
health=$($PREAMBLE curl -fsSL "$HOST/_cat/health?format=JSON" | jq '.[0].status == "yellow" or .[0].status == "green"')
if [[ $health == 'true' ]]; then
echo "Elasticsearch is up" >&2
break
fi
echo "Elastic Search is unavailable ($health) - sleeping" >&2
COUNTER=$((COUNTER + 1))
if [[ $COUNTER -gt $MAX_LOOPS ]]; then
echo "Giving up after $COUNTER attempts"
exit 1
break
fi
sleep 1
done
# Allow commands to be chained
shift
shift
exec "$@"