Skip to content

Commit

Permalink
move restart to deploy script
Browse files Browse the repository at this point in the history
  • Loading branch information
makelicious committed Jul 30, 2024
1 parent 52666fc commit a6973c0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 42 deletions.
9 changes: 9 additions & 0 deletions infrastructure/deployment/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,15 @@ echo
echo "Waiting 2 mins for mongo to deploy before working with data. Please note it can take up to 10 minutes for the entire stack to deploy in some scenarios."
echo

echo 'Setting up elastalert indices'

while true; do
if configured_ssh "/opt/opencrvs/infrastructure/elasticsearch/setup-elastalert-indices.sh"; then
break
fi
sleep 5
done

echo "Setting up Kibana config & alerts"

while true; do
Expand Down
35 changes: 26 additions & 9 deletions infrastructure/elasticsearch/setup-elastalert-indices.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,38 @@
#
# Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.

set -eu
set -o pipefail
# Upgrading from 7 to 8 requires deleting elastalert indices. https://elastalert2.readthedocs.io/en/latest/recipes/faq.html#does-elastalert-2-support-elasticsearch-8

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

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

log 'Waiting for availability of Elasticsearch'
wait_for_elasticsearch
docker_command="docker run --rm --network=opencrvs_overlay_net curlimages/curl"

echo 'Waiting for availability of Elasticsearch'
ping_status_code=$($docker_command --connect-timeout 60 -u elastic:$ELASTICSEARCH_SUPERUSER_PASSWORD -o /dev/null -w '%{http_code}' "http://elasticsearch:9200")

if [ "$ping_status_code" -ne 200 ]; then
echo "Elasticsearch is not ready. API returned status code: $ping_status_code"
exit 1
fi



echo 'Scaling down Elastalert'

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

echo 'Deleting Elastalert indices'
indices='elastalert_status,elastalert_status_error,elastalert_status_past,elastalert_status_silence,elastalert_status_status'

delete_status_code=$($docker_command --connect-timeout 60 -u elastic:$ELASTICSEARCH_SUPERUSER_PASSWORD -o /dev/null -w '%{http_code}' "http://elasticsearch:9200/${indices}" -X DELETE)

if [ "$delete_status_code" -ne 200 ]; then
echo "Could not delete indices. API returned status code: $delete_status_code"
exit 1
fi

echo 'Scaling up Elastalert'
docker service scale opencrvs_elastalert=1

32 changes: 0 additions & 32 deletions infrastructure/elasticsearch/setup-helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -230,35 +230,3 @@ 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
}
1 change: 0 additions & 1 deletion infrastructure/elasticsearch/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ apt-get install curl -y

bash "$(dirname "${BASH_SOURCE[0]}")/setup-users.sh"
bash "$(dirname "${BASH_SOURCE[0]}")/setup-settings.sh"
bash "$(dirname "${BASH_SOURCE[0]}")/setup-elastalert-indices.sh"



Expand Down

0 comments on commit a6973c0

Please sign in to comment.