Skip to content

Instantly share code, notes, and snippets.

@reggieb
Last active April 25, 2017 08:12
Show Gist options
  • Save reggieb/c16ce698f482904ad0a8f81bd1fe7af4 to your computer and use it in GitHub Desktop.
Save reggieb/c16ce698f482904ad0a8f81bd1fe7af4 to your computer and use it in GitHub Desktop.
#!/bin/bash
###
# Script for downgrading Elasticsearch to version 1.3.9
#
# 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.githubusercontent.com/reggieb/c16ce698f482904ad0a8f81bd1fe7af4/raw/1fc5a9883ee85f74b6fc8415213deac69b002e55/elasticsearch-downgrade-semaphore.sh ; sudo bash elasticsearch-downgrade-semaphore.sh
#
###
ES_HOST="0.0.0.0"
ES_PORT="9200"
ES_VERSION=${1:-'1.3.9'}
DEB="elasticsearch-$ES_VERSION.deb"
URL="https://download.elastic.co/elasticsearch/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 7
}
function remove_installed_version() {
echo "Stopping the service, removing current Elasticsearch and its configuration..."
service elasticsearch stop
sleep 1
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
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 "Downgrade 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