Skip to content

Instantly share code, notes, and snippets.

@hansohn
Last active February 10, 2021 22:14
Show Gist options
  • Save hansohn/6b151ff6518c178abdce0030a5add012 to your computer and use it in GitHub Desktop.
Save hansohn/6b151ff6518c178abdce0030a5add012 to your computer and use it in GitHub Desktop.
Elasticseach upgrade script
#!/usr/bin/env bash
set -eu
# function: usage
function usage () {
cat << EOF
Elasticsearch rolling upgrade tool. Process modeled after the following documentation:
https://www.elastic.co/guide/en/elasticsearch/reference/current/rolling-upgrades.html
Usage: $0 -u <username> -p <password> -v <version>
-u Elasticsearch api username. (required)
-p Elasticsearch api password. (required)
-v Elasticsearch package version to upgrade to (ex 6.8.13-1).
Uses latest available if left undefined.
-t Cluster health check.
EOF
}
function health_check () {
echo "==> Checking Elasticsearch Cluster health";
sleep ${1:-0}s;
curl \
-X GET \
-u "${username}:${password}" \
-H 'Content-Type:application/json;charset=UTF-8' \
"http://${ip}:9200/_cat/health?v=true&pretty";
# echo "==> Checking Elasticsearch Recovery";
# curl \
# -X GET \
# -u "${username}:${password}" \
# -H 'Content-Type:application/json;charset=UTF-8' \
# "http://${ip}:9200/_cat/recovery?pretty";
}
# default arguement values
username="";
password="";
version="";
test=false;
# arguement handling
while getopts u:p:v:th FLAG; do
case $FLAG in
u|username)
username=${OPTARG};
;;
p|password)
password=${OPTARG};
;;
v|version)
version=${OPTARG};
;;
t|test)
test=true;
;;
*)
usage
exit 0;
;;
esac
done
shift $((OPTIND -1))
# test for missing credentials
if [[ -z ${username} ]] || [[ -z ${password} ]]; then
echo -e "==> ERROR: Missing Elasticsearch API credentials!\n";
usage;
exit 1;
fi
# variables
ip=$(hostname -I | xargs);
# health check
if [[ "${test}" == "true" ]]; then
health_check
exit 0;
fi
# prepare elasticsearch for shutdown
# disable cluster routing and perform a synced flush
echo "==> Preparing Elasticsearch node for service shutdown: '$(hostname) (${ip})'"
curl \
-X PUT \
-u "${username}:${password}" \
-H 'Content-Type:application/json;charset=UTF-8' \
"http://${ip}:9200/_cluster/settings" \
-d '{ "persistent": { "cluster.routing.allocation.enable": "none" } }';
curl \
-X POST \
-u "${username}:${password}" \
-H 'Content-Type:application/json;charset=UTF-8' \
"http://${ip}:9200/_flush/synced";
echo ""
sleep 30s;
# shutdown elasticsearch
echo "==> Stopping Elasticsearch service"
systemctl stop elasticsearch;
# upgrade elasticsearch
echo "==> Upgrading Elasticsearch"
yum clean all;
if [[ -z ${version} ]]; then
yum update elasticsearch -y;
else
yum install elasticsearch-${version}
fi
# update elasticsearch plugins
IFS=$'\n'
export PATH="/usr/share/elasticsearch/bin:${PATH}"
plugins=($(/usr/share/elasticsearch/bin/elasticsearch-plugin list -s))
unset IFS
for plugin in ${plugins[@]}; do
echo "==> Updating Elasticsearch plugin: '${plugin}'"
/usr/share/elasticsearch/bin/elasticsearch-plugin remove -s ${plugin};
/usr/share/elasticsearch/bin/elasticsearch-plugin install -b -s ${plugin};
done
# start elasticsearch
echo "==> Starting Elasticsearch service"
systemctl start elasticsearch;
sleep 30s;
# enable elasticsearch
echo "==> Re-introducing Elasticsearch node back into cluster: '$(hostname) (${ip})'"
curl \
-X GET \
-u "${username}:${password}" \
-H 'Content-Type:application/json;charset=UTF-8' \
"http://${ip}:9200/_cat/nodes?pretty";
curl \
-X PUT \
-u "${username}:${password}" \
-H 'Content-Type:application/json;charset=UTF-8' \
"http://${ip}:9200/_cluster/settings" \
-d '{ "persistent": { "cluster.routing.allocation.enable": "all" } }';
echo ""
# get cluster health
health_check 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment