11#! /bin/bash
2- function validate_opensearch {
3- export ES_HOST=${ES_HOST:- localhost}
4- export ES_PORT=${ES_PORT:- 9202}
5- response=$( curl -s " http://${ES_HOST} :${ES_PORT} /_cluster/health" )
6- http_code=$( curl -s -o /dev/null -w ' %{http_code}' " http://${ES_HOST} :${ES_PORT} /_cluster/health" )
7- echo " HTTP Status Code: $http_code "
8- echo " Cluster Health Response: $response "
9- if [ " $http_code " -eq 200 ]; then
10- return 0
11- else
2+ set -e
3+
4+ RED=' \033[0;31m'
5+ GREEN=' \033[0;32m'
6+ YELLOW=' \033[1;33m'
7+ BLUE=' \033[0;34m'
8+ NC=' \033[0m'
9+
10+ function print_error() {
11+ echo -e " ${RED} ERROR: $1 ${NC} " >&2
12+ }
13+
14+ function print_warning() {
15+ echo -e " ${YELLOW} WARNING: $1 ${NC} "
16+ }
17+
18+ function print_success() {
19+ echo -e " ${GREEN} SUCCESS: $1 ${NC} "
20+ }
21+
22+ function print_info() {
23+ echo -e " ${BLUE} INFO: $1 ${NC} "
24+ }
25+
26+ function validate_opensearch() {
27+ local retry_count=0
28+ local max_retries=5
29+ local wait_time=5
30+
31+ while [ $retry_count -lt $max_retries ]; do
32+ print_info " Checking OpenSearch connection (Attempt $(( retry_count + 1 )) /$max_retries )..."
33+
34+ health=$( curl -k -s -o /dev/null -w ' %{http_code}' " http://${ES_HOST} :${ES_PORT} /_cluster/health" 2> /dev/null)
35+
36+ if [ " $health " -eq 200 ]; then
37+ print_success " Successfully connected to OpenSearch via HTTP"
38+ export ES_USE_SSL=false
39+ return 0
40+ fi
41+
42+ print_info " HTTP connection failed, trying HTTPS..."
43+ health=$( curl -s -o /dev/null -w ' %{http_code}' " https://${ES_HOST} :${ES_PORT} /_cluster/health" 2> /dev/null)
44+
45+ if [ " $health " -eq 200 ]; then
46+ print_success " Successfully connected to OpenSearch via HTTPS"
47+ export ES_USE_SSL=true
48+ return 0
49+ fi
50+
51+ retry_count=$(( retry_count + 1 ))
52+ if [ $retry_count -lt $max_retries ]; then
53+ print_warning " Connection attempt $retry_count failed. Waiting ${wait_time} seconds before retry..."
54+ sleep $wait_time
55+ wait_time=$(( wait_time * 2 ))
56+ fi
57+ done
58+
59+ print_error " Failed to connect to OpenSearch after $max_retries attempts:"
60+ print_error " - http://${ES_HOST} :${ES_PORT} "
61+ print_error " - https://${ES_HOST} :${ES_PORT} "
62+ print_error " Please ensure:"
63+ print_error " - OpenSearch is running"
64+ print_error " - ES_HOST and ES_PORT are correctly set"
65+ print_error " - Network connectivity is available"
66+ print_error " - SSL/TLS settings are correct if using HTTPS"
1267 return 1
13- fi
1468}
1569
70+ if [ " ${RUN_LOCAL_OS} " = " 0" ]; then
71+ if [ -z " ${ES_HOST} " ] || [ -z " ${ES_PORT} " ]; then
72+ print_error " When RUN_LOCAL_OS=0, you must specify both ES_HOST and ES_PORT"
73+ print_error " Current settings:"
74+ print_error " ES_HOST: ${ES_HOST:- not set} "
75+ print_error " ES_PORT: ${ES_PORT:- not set} "
76+ exit 1
77+ fi
78+ else
79+ export ES_HOST=${ES_HOST:- localhost}
80+ export ES_PORT=${ES_PORT:- 9202}
81+ fi
82+
1683if [ " ${RUN_LOCAL_OS} " = " 1" ]; then
17- echo " starting opensearch"
18- /usr/share/opensearch/bin/opensearch &
19-
20- echo " wait for os to be ready"
21- until validate_opensearch; do
22- echo -n " ."
23- sleep 5
24- done
25- echo " opensearch is up"
84+ print_info " Starting local OpenSearch instance"
85+ /usr/share/opensearch/bin/opensearch &
86+
87+ print_info " Waiting for OpenSearch to be ready"
88+ sleep 10 # Initial wait for OpenSearch to start
89+ until validate_opensearch; do
90+ print_info " OpenSearch not yet ready. Retrying..."
91+ sleep 5
92+ done
93+ print_success " OpenSearch is up and running"
94+ else
95+ print_info " Using external OpenSearch at ${ES_HOST} :${ES_PORT} "
96+ if ! validate_opensearch; then
97+ print_error " Cannot connect to external OpenSearch. Exiting..."
98+ exit 1
99+ fi
26100fi
27101
28- echo " start stac-fastapi-os "
102+ print_info " Starting STAC FastAPI OpenSearch "
29103exec uvicorn stac_fastapi.opensearch.app:app \
30- --host " ${APP_HOST} " \
31- --port " ${APP_PORT} " \
32- --workers " ${WEB_CONCURRENCY} " \
33- --reload
104+ --host " ${APP_HOST} " \
105+ --port " ${APP_PORT} " \
106+ --workers " ${WEB_CONCURRENCY} " \
107+ --reload
0 commit comments