5d1c971e8541f2dfd8b92215ae5d847a993a8f80
[ta/caas-logging.git] / docker-build / elasticsearch / healthcheck
1 #!/bin/bash
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License. 
15
16 set -eo pipefail
17
18 host="$(hostname --ip-address || echo '127.0.0.1')"
19
20 if health="$(curl -fsSL "http://$host:${ELASTICSEARCH_PORT}/_cat/health?h=status")"; then
21         health="$(echo "$health" | sed -r 's/^[[:space:]]+|[[:space:]]+$//g')" # trim whitespace (otherwise we'll have "green ")
22         if [ "$health" = 'green' ]; then
23                 exit 0
24         fi
25         echo >&2 "unexpected health status: $health"
26 fi
27
28 # If the probe returns 2 ("starting") when the container has already moved out of the "starting" state then it is treated as "unhealthy" instead.
29 # https://github.com/docker/docker/blob/dcc65376bac8e73bb5930fce4cddc2350bb7baa2/docs/reference/builder.md#healthcheck
30 exit 2