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