Skip to content

Commit 025c1b4

Browse files
committed
Simplify wait-for-es.sh script
1 parent 57ac38a commit 025c1b4

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

wait-for-es.sh

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,36 @@
33
# Courtesy of @fxdgear
44
# https://github.com/elastic/elasticsearch-py/issues/778#issuecomment-384389668
55

6-
set -e
6+
set -ux
77

88
host="$1"
9-
shift
10-
cmd="$@"
11-
12-
13-
until $(curl --output /dev/null --silent --head --fail "$host"); do
14-
printf '.'
15-
sleep 1
16-
done
179

18-
# First wait for ES to start...
19-
response=$(curl $host)
10+
while true; do
11+
response=$(curl --write-out '%{http_code}' --silent --fail --output /dev/null "$host")
12+
if [[ "$response" -eq "200" ]]; then
13+
break
14+
fi
2015

21-
until [ "$response" = "200" ]; do
22-
response=$(curl --write-out %{http_code} --silent --output /dev/null "$host")
23-
>&2 echo "Elastic Search is unavailable - sleeping"
16+
echo "Elastic Search is unavailable - sleeping" >&2
2417
sleep 1
2518
done
2619

20+
# set -e now because it was causing the curl command above to exit the script
21+
# if the server was not available
22+
set -e
2723

28-
# Wait for ES status to turn to yellow.
29-
# TODO: Ideally we'd be waiting for green, but we need multiple nodes for that.
30-
31-
health="$(curl -fsSL "$host/_cat/health?h=status")"
32-
health="$(echo "$health" | sed -r 's/^[[:space:]]+|[[:space:]]+$//g')" # trim whitespace (otherwise we'll have "green ")
33-
34-
until [ "$health" = 'yellow' ]; do
24+
while true; do
25+
## Wait for ES status to turn to yellow.
26+
## TODO: Ideally we'd be waiting for green, but we need multiple nodes for that.
3527
health="$(curl -fsSL "$host/_cat/health?h=status")"
36-
health="$(echo "$health" | sed -r 's/^[[:space:]]+|[[:space:]]+$//g')" # trim whitespace (otherwise we'll have "green ")
37-
>&2 echo "Elastic Search is unavailable ($health) - sleeping"
28+
health="$(echo "$health" | xargs)" # trim whitespace (otherwise we'll have "green ")
29+
if [[ $health == 'yellow' || $health == 'green' ]]; then
30+
break
31+
fi
32+
echo "Elastic Search is unavailable ($health) - sleeping" >&2
3833
sleep 1
3934
done
4035

41-
>&2 echo "Elastic Search is up"
42-
exec $cmd
36+
echo "Elastic Search is up" >&2
37+
shift
38+
exec "$@"

0 commit comments

Comments
 (0)