Skip to content

Instantly share code, notes, and snippets.

@dNitza
Last active March 16, 2020 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dNitza/8672749e99d52157e6821477ff64ba89 to your computer and use it in GitHub Desktop.
Save dNitza/8672749e99d52157e6821477ff64ba89 to your computer and use it in GitHub Desktop.
Upgrade elasticsearch on semaphore
#!/bin/bash
###
# Script for uprading Elasticsearch to version 7.2.1
#
# Before running this script, please reset your dependency cache in Project Settings > Admin.
#
# Add the line below to your Setup commands in Project Settings (without the # at the beginning):
#
# wget https://gist.github.com/dNitza/8672749e99d52157e6821477ff64ba89/raw/elasticsearch-upgrade-semaphore.sh ; sudo bash elasticsearch-upgrade-semaphore.sh
#
###
ES_HOST="0.0.0.0"
ES_PORT="9200"
ES_VERSION=${1:-'7.2.1'}
DEB="elasticsearch-$ES_VERSION-amd64.deb"
URL="https://artifacts.elastic.co/downloads/elasticsearch/$DEB"
function wait_for_elasticsearch() {
printf "Waiting for ElasticSearch to become available"
while true; do
printf "."
nc -4 -w 5 $ES_HOST $ES_PORT 2>/dev/null && break
sleep 1
done
printf "\n"
}
function setup_java() {
source /opt/change-java-version.sh
change-java-version 10
}
function remove_installed_version() {
echo "Stopping the service, removing current Elasticsearch and its configuration..."
service elasticsearch stop
apt-get purge -y elasticsearch
rm -rf /var/lib/elasticsearch
}
function install_version() {
if ! [ -e $SEMAPHORE_CACHE_DIR/$DEB ]; then
echo "Downloading Elasticsearch $ES_VERSION..."
wget -O $SEMAPHORE_CACHE_DIR/$DEB $URL
fi
echo "Installing Elasticsearch $ES_VERSION..."
echo 'Y' | dpkg -i $SEMAPHORE_CACHE_DIR/$DEB
echo "Configuring Elasticsearch..."
# Set the service to start on all runlevels
update-rc.d elasticsearch defaults 95 10
service elasticsearch stop
# Default configuration for this version of Elasticsearch
# It may not work with latest versions, be careful
# echo 'index.number_of_shards: 1' >> /etc/elasticsearch/elasticsearch.yml
# echo 'index.number_of_replicas: 0' >> /etc/elasticsearch/elasticsearch.yml
# echo 'network.bind_host: "0.0.0.0"' >> /etc/elasticsearch/elasticsearch.yml
service elasticsearch start
echo "Installation completed successfully."
}
function run_health_check() {
echo "Running health check..."
curl http://"$ES_HOST":"$ES_PORT"/_cluster/health?pretty=true
}
if [ $EUID != 0 ]; then echo 'This script must be run as root, or by using sudo.'; exit 1; fi
setup_java
remove_installed_version
install_version
wait_for_elasticsearch
run_health_check
echo "Upgrade complete. Elasticsearch status:"
curl 'http://0.0.0.0:9200/?pretty'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment