This commit is contained in:
ai-dev
2025-09-22 00:02:25 +02:00
parent 4399eef12c
commit cf4f565cce
2 changed files with 36 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
cluster.name: ${CLUSTER_NAME} cluster.name: "${CLUSTER_NAME}"
node.name: ${NODE_NAME} node.name: "${NODE_NAME}"
node.roles: [${NODE_ROLE}] node.roles: [${NODE_ROLE}]
path.data: /usr/share/opensearch/data path.data: /usr/share/opensearch/data

View File

@@ -5,8 +5,7 @@ read_config() {
if command -v bashio >/dev/null 2>&1; then if command -v bashio >/dev/null 2>&1; then
bashio::config "$1" bashio::config "$1"
else else
# Fallback for testing outside Home Assistant # Fallback for testing outside Home Assistant - return empty to avoid issues
echo "Testing mode - using default values"
case "$1" in case "$1" in
"node_role") echo "master" ;; "node_role") echo "master" ;;
"cluster_name") echo "hass-opensearch-cluster" ;; "cluster_name") echo "hass-opensearch-cluster" ;;
@@ -14,7 +13,7 @@ read_config() {
"heap_size") echo "1g" ;; "heap_size") echo "1g" ;;
"http_port") echo "9200" ;; "http_port") echo "9200" ;;
"transport_port") echo "9300" ;; "transport_port") echo "9300" ;;
"discovery_seed_hosts") echo '["os-master:9300", "os-data-1:9300"]' ;; "discovery_seed_hosts") echo '["os-master:9300"]' ;;
"initial_master_nodes") echo '["os-master"]' ;; "initial_master_nodes") echo '["os-master"]' ;;
"security_enabled") echo "true" ;; "security_enabled") echo "true" ;;
"admin_password") echo "SecurePass123!" ;; "admin_password") echo "SecurePass123!" ;;
@@ -40,27 +39,42 @@ ADMIN_PASSWORD=$(read_config 'admin_password')
export OPENSEARCH_INITIAL_ADMIN_PASSWORD="${ADMIN_PASSWORD}" export OPENSEARCH_INITIAL_ADMIN_PASSWORD="${ADMIN_PASSWORD}"
export DISCOVERY_TYPE="multi-node" export DISCOVERY_TYPE="multi-node"
# Set heap size # Set heap size with proper format (remove any extra spaces or invalid characters)
echo "-Xms${HEAP_SIZE}" > /usr/share/opensearch/config/jvm.options.d/heap.options CLEAN_HEAP_SIZE=$(echo "$HEAP_SIZE" | tr -d ' ' | grep -oE '[0-9]+[mg]')
echo "-Xmx${HEAP_SIZE}" >> /usr/share/opensearch/config/jvm.options.d/heap.options if [ -z "$CLEAN_HEAP_SIZE" ]; then
CLEAN_HEAP_SIZE="1g" # Default if invalid
fi
# Export variables for envsubst # Set heap size in jvm.options.d (proper format)
export CLUSTER_NAME NODE_NAME NODE_ROLE HTTP_PORT TRANSPORT_PORT mkdir -p /usr/share/opensearch/config/jvm.options.d
export DISCOVERY_SEEDS INITIAL_MASTERS SECURITY_ENABLED echo "-Xms${CLEAN_HEAP_SIZE}" > /usr/share/opensearch/config/jvm.options.d/heap.options
echo "-Xmx${CLEAN_HEAP_SIZE}" >> /usr/share/opensearch/config/jvm.options.d/heap.options
# Export variables for envsubst (ensure they have proper values)
export CLUSTER_NAME="${CLUSTER_NAME:-hass-opensearch-cluster}"
export NODE_NAME="${NODE_NAME:-os-node-1}"
export NODE_ROLE="${NODE_ROLE:-master}"
export HTTP_PORT="${HTTP_PORT:-9200}"
export TRANSPORT_PORT="${TRANSPORT_PORT:-9300}"
export DISCOVERY_SEEDS="${DISCOVERY_SEEDS:-os-master:9300}"
export INITIAL_MASTERS="${INITIAL_MASTERS:-os-master}"
export SECURITY_ENABLED="${SECURITY_ENABLED:-false}"
# Generate opensearch.yml from template # Generate opensearch.yml from template
envsubst < /etc/opensearch/opensearch.yml.template > /usr/share/opensearch/config/opensearch.yml envsubst < /etc/opensearch/opensearch.yml.template > /usr/share/opensearch/config/opensearch.yml
# Install plugins if specified # Install plugins if specified (only if not in test mode)
PLUGINS_JSON=$(read_config 'plugins') PLUGINS_JSON=$(read_config 'plugins')
if [ -n "$PLUGINS_JSON" ] && [ "$PLUGINS_JSON" != "[]" ]; then if [ -n "$PLUGINS_JSON" ] && [ "$PLUGINS_JSON" != "[]" ] && command -v bashio >/dev/null 2>&1; then
# Extract plugins from JSON array # Extract plugins from JSON array properly
PLUGINS=$(echo "$PLUGINS_JSON" | tr -d '[]"' | tr ',' '\n' | sed 's/^ *//;s/ *$//') PLUGINS=$(echo "$PLUGINS_JSON" | jq -r '.[]' 2>/dev/null || echo "")
if [ -n "$PLUGINS" ]; then
for plugin in $PLUGINS; do for plugin in $PLUGINS; do
echo "Installing plugin: $plugin" echo "Installing plugin: $plugin"
/usr/share/opensearch/bin/opensearch-plugin install --batch "$plugin" || true /usr/share/opensearch/bin/opensearch-plugin install --batch "$plugin" || true
done done
fi fi
fi
# Start OpenSearch (use exec to replace the current process) # Start OpenSearch
exec /usr/share/opensearch/bin/opensearch exec /usr/share/opensearch/bin/opensearch