Skip to content

Commit feb24d2

Browse files
committed
added better logs, and considered all different scenarios
1 parent 0e70af2 commit feb24d2

File tree

2 files changed

+197
-47
lines changed

2 files changed

+197
-47
lines changed

dockerfiles/entrypoint-es.sh

Lines changed: 98 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,106 @@
11
#!/bin/bash
2-
function validate_elasticsearch {
3-
export ES_HOST=${ES_HOST:-localhost}
4-
export ES_PORT=${ES_PORT:-9200}
5-
health=$(curl -s -o /dev/null -w '%{http_code}' "http://${ES_HOST}:${ES_PORT}/_cluster/health")
6-
if [ "$health" -eq 200 ]; then
7-
return 0
8-
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_elasticsearch() {
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 Elasticsearch 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 Elasticsearch 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 Elasticsearch 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 Elasticsearch 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 " - Elasticsearch 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"
967
return 1
10-
fi
1168
}
1269

70+
if [ "${RUN_LOCAL_ES}" = "0" ]; then
71+
if [ -z "${ES_HOST}" ] || [ -z "${ES_PORT}" ]; then
72+
print_error "When RUN_LOCAL_ES=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:-9200}
81+
fi
82+
1383
if [ "${RUN_LOCAL_ES}" = "1" ]; then
14-
echo "starting elasticsearch"
15-
/usr/share/elasticsearch/bin/elasticsearch &
16-
17-
echo "wait for es to be ready"
18-
until validate_elasticsearch; do
19-
echo -n "."
20-
sleep 5
21-
done
22-
echo "Elasticsearch is up"
84+
print_info "Starting local Elasticsearch instance"
85+
/usr/share/elasticsearch/bin/elasticsearch &
86+
87+
print_info "Waiting for Elasticsearch to be ready"
88+
until validate_elasticsearch; do
89+
print_info "Elasticsearch not yet ready. Retrying..."
90+
sleep 5
91+
done
92+
print_success "Elasticsearch is up and running"
93+
else
94+
print_info "Using external Elasticsearch at ${ES_HOST}:${ES_PORT}"
95+
if ! validate_elasticsearch; then
96+
print_error "Cannot connect to external Elasticsearch. Exiting..."
97+
exit 1
98+
fi
2399
fi
24100

25-
echo "start stac-fastapi-es"
101+
print_info "Starting STAC FastAPI Elasticsearch"
26102
exec uvicorn stac_fastapi.elasticsearch.app:app \
27-
--host "${APP_HOST}" \
28-
--port "${APP_PORT}" \
29-
--workers "${WEB_CONCURRENCY}" \
30-
--reload
103+
--host "${APP_HOST}" \
104+
--port "${APP_PORT}" \
105+
--workers "${WEB_CONCURRENCY}" \
106+
--reload

dockerfiles/entrypoint-os.sh

Lines changed: 99 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,107 @@
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+
1683
if [ "${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
26100
fi
27101

28-
echo "start stac-fastapi-os"
102+
print_info "Starting STAC FastAPI OpenSearch"
29103
exec 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

Comments
 (0)