Skip to content

Commit

Permalink
Ensure Consistent Usage of Environment Variables for Elasticsearch Ho…
Browse files Browse the repository at this point in the history
…st and Port Configuration

### What was specifically improved?
- **Consistency in configuration**: Instead of passing the port as an argument or via a function, you now use an environment variable (`ELASTIC_PORT`), just like for the host. This brings consistency, allowing users to set both the host and port using environment variables, which fits better into automated environments like Docker.
- **Simplification of code**: Since the `get_elastic_port` function was removed, the code becomes less complex, and reusing environment variables reduces unnecessary argument passing and functionality.
  • Loading branch information
Be-Mann authored Oct 24, 2024
1 parent 74a8302 commit f174e53
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions cmd/elastic.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
#!/bin/bash
set -e;

# Function to determine the Elasticsearch port with a fallback to the default port
function get_elastic_port(){
if [[ -n "$1" ]]; then
echo "$1"
else
echo "9200"
fi
}

function elastic_schema_drop(){ compose_run 'schema' node scripts/drop_index "$@" || true; }
function elastic_schema_create(){ compose_run 'schema' ./bin/create_index; }
function elastic_start(){
Expand All @@ -28,12 +19,11 @@ register 'elastic' 'stop' 'stop elasticsearch server' elastic_stop

# Function to get HTTP status of Elasticsearch, with an optional port argument
function elastic_status(){
local port=$(get_elastic_port "$1")
curl \
--output /dev/null \
--silent \
--write-out "%{http_code}" \
"http://${ELASTIC_HOST:-localhost}:${port}/_cluster/health?wait_for_status=yellow&timeout=1s" \
"http://${ELASTIC_HOST:-localhost}:${ELASTIC_PORT:-9200}/_cluster/health?wait_for_status=yellow&timeout=1s" \
|| true;
}

Expand All @@ -46,14 +36,13 @@ register 'elastic' 'status' 'HTTP status code of the elasticsearch service' elas
function elastic_wait(){
echo 'waiting for elasticsearch service to come up';
retry_count=30
local port=$(get_elastic_port "$1")

i=1
while [[ "$i" -le "$retry_count" ]]; do
if [[ $(elastic_status "$port") -eq 200 ]]; then
echo "Elasticsearch up on port $port!"
if [[ $(elastic_status") -eq 200 ]]; then
echo "Elasticsearch up on port ${ELASTIC_PORT:-9200}!"
exit 0
elif [[ $(elastic_status "$port") -eq 408 ]]; then
elif [[ $(elastic_status") -eq 408 ]]; then
# 408 indicates the server is up but has not reached yellow status yet
printf ":"
else
Expand All @@ -72,15 +61,13 @@ register 'elastic' 'wait' 'wait for elasticsearch to start up' elastic_wait
# Function to get Elasticsearch version and build info with an optional port argument
function elastic_info(){
local port=$(get_elastic_port "$1")
curl -s "http://${ELASTIC_HOST:-localhost}:${port}/";
curl -s "http://${ELASTIC_HOST:-localhost}:${ELASTIC_PORT:-9200}/";
}
register 'elastic' 'info' 'display elasticsearch version and build info' elastic_info
# Function to display a summary of document counts per source/layer with optional port
function elastic_stats(){
local port=$(get_elastic_port "$1")
curl -s "http://${ELASTIC_HOST:-localhost}:${port}/pelias/_search?request_cache=true&timeout=10s&pretty=true" \
curl -s "http://${ELASTIC_HOST:-localhost}:${ELASTIC_PORT:-9200}/pelias/_search?request_cache=true&timeout=10s&pretty=true" \
-H 'Content-Type: application/json' \
-d '{
"aggs": {
Expand Down

0 comments on commit f174e53

Please sign in to comment.