Skip to content

Commit

Permalink
Restart and remove existing elastalert indices
Browse files Browse the repository at this point in the history
  • Loading branch information
makelicious authored and github-actions committed Jul 30, 2024
1 parent 4264ab1 commit 709c8d8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
29 changes: 29 additions & 0 deletions infrastructure/elasticsearch/setup-elastalert-indices.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# OpenCRVS is also distributed under the terms of the Civil Registration
# & Healthcare Disclaimer located at http://opencrvs.org/license.
#
# Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.

set -eu
set -o pipefail

source "$(dirname "${BASH_SOURCE[0]}")/setup-helpers.sh"

echo "-------- $(date) --------"

log 'Waiting for availability of Elasticsearch'
wait_for_elasticsearch


log 'Scaling down Elastalert'
docker service scale opencrvs_elastalert=0
log 'Deleting Elastalert indices'
delete_elastalert_indices
log 'Scaling up Elastalert'
docker service scale opencrvs_elastalert=1

31 changes: 31 additions & 0 deletions infrastructure/elasticsearch/setup-helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,34 @@ function ensure_settings {
return $result
}

# Upgrading from 7 to 8 requires deleting elastalert indices. https://elastalert2.readthedocs.io/en/latest/recipes/faq.html#does-elastalert-2-support-elasticsearch-8
# Elastalert depends on kibana/beat indices, so we delete can elastalert indices during each deployment.
function delete_elastalert_indices {
# Opt for explicity over wildcard since we are deleting indices
local indices='elastalert_status,elastalert_status_error,elastalert_status_past,elastalert_status_silence,elastalert_status_status'

local elasticsearch_host="${ELASTICSEARCH_HOST:-elasticsearch}"

local -a args=( '-s' '-D-' '-m15' '-w' '%{http_code}'
"http://${elasticsearch_host}:9200/${indices}"
'-X' 'DELETE'
)

if [[ -n "${ELASTIC_PASSWORD:-}" ]]; then
args+=( '-u' "elastic:${ELASTIC_PASSWORD}" )
fi

local -i result=1
local output

output="$(curl "${args[@]}")"
if [[ "${output: -3}" -eq 200 ]]; then
result=0
fi

if ((result)); then
echo -e "\n${output::-3}\n"
fi

return $result
}

0 comments on commit 709c8d8

Please sign in to comment.