Skip to content

Instantly share code, notes, and snippets.

@artbikes
Created December 17, 2012 03:34
Show Gist options
  • Save artbikes/4315604 to your computer and use it in GitHub Desktop.
Save artbikes/4315604 to your computer and use it in GitHub Desktop.
Elasticsearch update - conary version with index flushing
#!/bin/bash
# esearch-update - Updates elasticsearch with various sanity checks
# blame: [email protected]
VERSION=0.18.6
NEWVERSION=0.19.0
CONFIG=/opt/elasticsearch/config/elasticsearch.yml
MONIT=/usr/bin/monit
LIMIT=20
NEWPORT1=9123
NEWPORT2=9124
APP=elasticsearch
declare -a DATASPACE=($(du -s -h /var/lib/elasticsearch/ | sed -n "s/\([0-9.]*\)\([GM]\).*/\1 \2/p"))
declare -a VARSPACE=($(df -h /var | sed -n '3p' | awk '{ print $3}' | sed -n "s/\([0-9.]*\)\([GM]\).*/\1\ \2/p"))
# This upgrade requires a flushing of indices
flushIndices () {
echo "Flushing indices"
if (curl -XPOST 'http://localhost:9123/_flush' | grep "true")
then
echo "Successfully flushed indices"
else
echo "### ALERT ! ALERT ###"
echo "Failed to flush indices"
exit 98
fi
}
# Check is space is available for a backup
checkSpace () {
# Return if unit is not G
if [ "${DATASPACE[1]}" != "G" ]
then
echo "Plenty of space for a backup."
return
fi
# Exit if data is more G than free space
DANGER=$(awk -vfree="${VARSPACE[0]}" -vdata="${DATASPACE[0]}" 'BEGIN{print (data<=free ? 0 : 1)}')
if [ "$DANGER" -eq 1 ]
then
echo "Sorry - there is no room for a backup"
echo "Please make some room"
exit
fi
}
# Set perms
zapPerms () {
chmod 644 $CONFIG
}
resetConf () {
mv -v $CONFIG.bak $CONFIG
}
#---
# Let's get this party started.
#---
# Check ES version
if (conary q elasticsearch-server --info | sed -n 2p | grep $VERSION >& /dev/null)
then
echo "Version $VERSION is installed"
else
echo "Version $VERSION is not installed"
exit 99
fi
# Is ES up and running with the expected version?
if (curl -s http://localhost:9200/ | grep "$VERSION" >& /dev/null)
then
echo "$APP is running"
else
echo "$APP is not running - Exiting!"
exit 100
fi
# Is the config file in the proper location?
if [ -f $CONFIG ]
then
echo "$APP Config is where we expect it"
else
echo "$APP Config is AWOL - Exiting!"
exit 101
fi
# Do we have space?
checkSpace
# rsync data
#BACKUP=`mktemp -d /var/esearchXXXXX`
rsync -avz /var/lib/elasticsearch /var/esearch23106
# STOP ES
#$MONIT stop $APP
/etc/init.d/$APP stop
# Configure ES to use non-standard ports
sed -i.bak -e's/port:\s* 9200/port: 9123/;s/port:\s*9300/port: 9124/' $CONFIG
zapPerms
# START ES with old version and new config
#$MONIT start $APP
/etc/init.d/$APP start
for ((a=1; a <= LIMIT ; a++))
do
if (netstat -pantu | grep "9123" >& /dev/null)
then
echo "$APP is running on the new port"
break
elif [ $a = $LIMIT ]
then
echo "$APP is not running on the new port - Exiting"
resetConf
exit 103
fi
echo -n "*"
sleep 2
done
# FLUSH
flushIndices
# STOP listening on the bogus port
#$MONIT stop $APP
/etc/init.d/$APP stop
# Install new version
conary update $APP-server --install-label=example-platform.localdomain@example:example-platform-1
# Check version again
if (conary q elasticsearch-server --info | sed -n 2p | grep $NEWVERSION >& /dev/null)
then
echo "Version $NEWVERSION is installed"
else
echo "#### OH BOY THIS IS REALLY BAD ####"
echo "Version $NEWVERSION is not installed"
resetConf
exit 105
fi
# mv backup back into place restart
resetConf
#$MONIT start $APP
/etc/init.d/$APP start
for ((a=1; a <= LIMIT ; a++))
do
if (netstat -pantu | grep "9200" >& /dev/null)
then
echo "$APP is running on the original port"
break
fi
echo -n "*"
done
# Check for bogus port
if (netstat -pantu | grep "9123" >& /dev/null)
then
echo "### ALERT ###"
echo "BOGUS PORT IN"
echo "### USE ###"
echo "Check with netstat -pantu | grep '9123' "
exit 106
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment