diff --git a/salt/common/tools/sbin/so-common b/salt/common/tools/sbin/so-common index 9e98204a1..9fd5d6576 100755 --- a/salt/common/tools/sbin/so-common +++ b/salt/common/tools/sbin/so-common @@ -404,6 +404,25 @@ is_single_node_grid() { grep "role: so-" /etc/salt/grains | grep -E "eval|standalone|import" &> /dev/null } +initialize_elasticsearch_indices() { + local index_names=$1 + local default_entry=${2:-'{"@timestamp":"0"}'} + + for idx in $index_names; do + if ! so-elasticsearch-query "$idx" --fail --retry 3 --retry-delay 30 >/dev/null 2>&1; then + echo "Index does not already exist. Initializing $idx index." + + if retry 3 10 "so-elasticsearch-query "$idx/_doc" -d '$default_entry' -XPOST --fail 2>/dev/null" '"successful":1'; then + echo "Successfully initialized $idx index." + else + echo "Failed to initialize $idx index after 3 attempts." + fi + else + echo "Index $idx already exists. No action needed." + fi + done +} + lookup_bond_interfaces() { cat /proc/net/bonding/bond0 | grep "Slave Interface:" | sed -e "s/Slave Interface: //g" } diff --git a/salt/manager/tools/sbin/soup b/salt/manager/tools/sbin/soup index 92f3b6d14..b0ac226d2 100755 --- a/salt/manager/tools/sbin/soup +++ b/salt/manager/tools/sbin/soup @@ -692,6 +692,8 @@ post_to_2.4.210() { disable_redis_heavynodes + initialize_elasticsearch_indices "so-case so-casehistory so-assistant-session so-assistant-chat" + echo "Regenerating Elastic Agent Installers" /sbin/so-elastic-agent-gen-installers diff --git a/setup/so-setup b/setup/so-setup index c2f589a45..1fa078b78 100755 --- a/setup/so-setup +++ b/setup/so-setup @@ -828,6 +828,7 @@ if ! [[ -f $install_opt_file ]]; then fi checkin_at_boot set_initial_firewall_access + initialize_elasticsearch_indices "so-case so-casehistory so-assistant-session so-assistant-chat" # run a final highstate before enabling scheduled highstates. # this will ensure so-elasticsearch-ilm-policy-load and so-elasticsearch-templates-load have a chance to run after elasticfleet is setup info "Running final highstate for setup"