|
3 | 3 | # Courtesy of @fxdgear
|
4 | 4 | # https://github.com/elastic/elasticsearch-py/issues/778#issuecomment-384389668
|
5 | 5 |
|
6 |
| -set -e |
| 6 | +set -ux |
7 | 7 |
|
8 | 8 | 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 |
17 | 9 |
|
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 |
20 | 15 |
|
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 |
24 | 17 | sleep 1
|
25 | 18 | done
|
26 | 19 |
|
| 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 |
27 | 23 |
|
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. |
35 | 27 | 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 |
38 | 33 | sleep 1
|
39 | 34 | done
|
40 | 35 |
|
41 |
| ->&2 echo "Elastic Search is up" |
42 |
| -exec $cmd |
| 36 | +echo "Elastic Search is up" >&2 |
| 37 | +shift |
| 38 | +exec "$@" |
0 commit comments