This is an automated email from the ASF dual-hosted git repository.

shuber pushed a commit to branch unomi-3-dev
in repository https://gitbox.apache.org/repos/asf/unomi.git


The following commit(s) were added to refs/heads/unomi-3-dev by this push:
     new 3a8528315 UNOMI-828 Refactor Elasticsearch and OpenSearch setup scripts
3a8528315 is described below

commit 3a8528315ebec24280b63dba8ac722c59f389338
Author: Serge Huber <[email protected]>
AuthorDate: Thu Jan 15 18:23:51 2026 +0100

    UNOMI-828 Refactor Elasticsearch and OpenSearch setup scripts
    
    - Added a new function to load specific password variables from the 
.env.local file, ensuring only the relevant password is loaded for 
Elasticsearch and OpenSearch.
    - Updated the setup scripts to utilize the new password loading function, 
improving clarity and preventing the loading of unnecessary variables.
    - Cleared the UNOMI_ELASTICSEARCH_ADDRESSES environment variable in the 
Elasticsearch clear script for better cleanup.
---
 clear-elasticsearch.sh |  1 +
 setup-elasticsearch.sh | 14 ++++++++++++--
 setup-opensearch.sh    | 14 ++++++++++++--
 setup-utils.sh         | 36 ++++++++++++++++++++++++++++++++++++
 4 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/clear-elasticsearch.sh b/clear-elasticsearch.sh
index d264ba3ce..3e3c23ea4 100755
--- a/clear-elasticsearch.sh
+++ b/clear-elasticsearch.sh
@@ -50,6 +50,7 @@ fi
 
 # Clear Elasticsearch environment variables
 unset UNOMI_ELASTICSEARCH_CLUSTERNAME
+unset UNOMI_ELASTICSEARCH_ADDRESSES
 unset UNOMI_ELASTICSEARCH_USERNAME
 unset UNOMI_ELASTICSEARCH_PASSWORD
 unset UNOMI_ELASTICSEARCH_SSL_ENABLE
diff --git a/setup-elasticsearch.sh b/setup-elasticsearch.sh
index 1bd7da8ef..ac01cba03 100755
--- a/setup-elasticsearch.sh
+++ b/setup-elasticsearch.sh
@@ -53,8 +53,14 @@ _setup_elasticsearch() {
     # Clear OpenSearch environment variables first
     clear_opposite "${SCRIPT_DIR}" "opensearch" || { echo "WARNING: Failed to 
clear OpenSearch variables" >&2; }
 
-    # Load .env.local file if it exists
-    load_env_local "${SCRIPT_DIR}"
+    # Load only the Elasticsearch password from .env.local if it exists
+    # This ensures we don't load the OpenSearch password
+    if ! load_password_from_env_local "${SCRIPT_DIR}" 
"UNOMI_ELASTICSEARCH_PASSWORD"; then
+        # If not found in .env.local, check if it's already set in environment
+        if [ -z "${UNOMI_ELASTICSEARCH_PASSWORD}" ]; then
+            echo "Note: UNOMI_ELASTICSEARCH_PASSWORD not found in .env.local 
or environment"
+        fi
+    fi
 
     # Check if password is set
     if ! check_password "UNOMI_ELASTICSEARCH_PASSWORD"; then
@@ -69,7 +75,11 @@ _setup_elasticsearch() {
     export UNOMI_ELASTICSEARCH_SSL_TRUST_ALL_CERTIFICATES=true
     # Password is already set from .env.local or environment
 
+    # Set the distribution to use Elasticsearch (default, but explicit for 
clarity)
+    export UNOMI_DISTRIBUTION=unomi-distribution-elasticsearch
+
     echo "Elasticsearch 9 environment variables configured."
+    echo "  Distribution: ${UNOMI_DISTRIBUTION}"
     echo "  Cluster: ${UNOMI_ELASTICSEARCH_CLUSTERNAME} (overridden from 
default: contextElasticSearch)"
     echo "  Username: ${UNOMI_ELASTICSEARCH_USERNAME} (overridden from 
default: empty)"
     echo "  SSL Enabled: ${UNOMI_ELASTICSEARCH_SSL_ENABLE} (overridden from 
default: false)"
diff --git a/setup-opensearch.sh b/setup-opensearch.sh
index 560d6482f..bc4e81d54 100755
--- a/setup-opensearch.sh
+++ b/setup-opensearch.sh
@@ -51,8 +51,14 @@ _setup_opensearch() {
     # Clear Elasticsearch environment variables first
     clear_opposite "${SCRIPT_DIR}" "elasticsearch" || { echo "WARNING: Failed 
to clear Elasticsearch variables" >&2; }
 
-    # Load .env.local file if it exists
-    load_env_local "${SCRIPT_DIR}"
+    # Load only the OpenSearch password from .env.local if it exists
+    # This ensures we don't load the Elasticsearch password
+    if ! load_password_from_env_local "${SCRIPT_DIR}" 
"UNOMI_OPENSEARCH_PASSWORD"; then
+        # If not found in .env.local, check if it's already set in environment
+        if [ -z "${UNOMI_OPENSEARCH_PASSWORD}" ]; then
+            echo "Note: UNOMI_OPENSEARCH_PASSWORD not found in .env.local or 
environment"
+        fi
+    fi
 
     # Check if password is set
     if ! check_password "UNOMI_OPENSEARCH_PASSWORD"; then
@@ -63,7 +69,11 @@ _setup_opensearch() {
     # Password is already set from .env.local or environment, just ensure it's 
exported
     export UNOMI_OPENSEARCH_PASSWORD
 
+    # Set the distribution to use OpenSearch
+    export UNOMI_DISTRIBUTION=unomi-distribution-opensearch
+
     echo "OpenSearch 3 environment variables configured."
+    echo "  Distribution: ${UNOMI_DISTRIBUTION}"
     echo "  Password: (set from .env.local or environment)"
     echo "  Note: Using Unomi defaults for other OpenSearch settings (cluster, 
addresses, username, SSL)"
     
diff --git a/setup-utils.sh b/setup-utils.sh
index 5530acb60..e1bd8a1f5 100755
--- a/setup-utils.sh
+++ b/setup-utils.sh
@@ -72,6 +72,42 @@ load_env_local() {
     fi
 }
 
+# Load a specific password variable from .env.local file if it exists
+# This function only loads the specified variable, not the entire file
+# Usage: load_password_from_env_local SCRIPT_DIR PASSWORD_VAR_NAME
+load_password_from_env_local() {
+    local script_dir="$1"
+    local password_var="$2"
+    local env_local="${script_dir}/.env.local"
+    
+    if [ -f "${env_local}" ]; then
+        # Use grep to find the line with the password variable
+        # Match lines that start with optional whitespace, 'export', one or 
more whitespace, variable name, '='
+        local matching_line
+        matching_line=$(grep -E 
"^[[:space:]]*export[[:space:]]+${password_var}=" "${env_local}" 2>/dev/null | 
head -n 1)
+        
+        if [ -n "${matching_line}" ]; then
+            # Extract just the variable assignment part (VAR=value)
+            # This handles cases where the line might have comments or extra 
whitespace
+            local var_assignment
+            # Remove 'export' keyword and trim whitespace
+            var_assignment="${matching_line#export}"
+            
var_assignment="${var_assignment#"${var_assignment%%[![:space:]]*}"}"
+            # Remove any trailing comments (everything after #)
+            var_assignment="${var_assignment%%#*}"
+            # Trim trailing whitespace
+            
var_assignment="${var_assignment%"${var_assignment##*[![:space:]]}"}"
+            
+            # Export the variable by evaluating the assignment
+            # This is safe because we've already validated the variable name 
matches
+            eval "export ${var_assignment}"
+            echo "Loaded ${password_var} from .env.local"
+            return 0
+        fi
+    fi
+    return 1
+}
+
 # Check if password environment variable is set
 # Usage: check_password PASSWORD_VAR_NAME
 # Returns: 0 if password is set, 1 if not set

Reply via email to