Skip to content

Instantly share code, notes, and snippets.

Created January 28, 2015 13:35
Show Gist options
  • Save anonymous/374c35100ebb01a839f5 to your computer and use it in GitHub Desktop.
Save anonymous/374c35100ebb01a839f5 to your computer and use it in GitHub Desktop.
#!/bin/bash
ES_URL='https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.1.tar.gz'
ES_HOME='/tmp/es'
DATA_DIR_ES='/tmp/es/data'
ES_PID=$ES_HOME/es.pid
update_config ()
{
config="cluster.name: temp-tal-elasticsearch
network.host: localhost
discovery.zen.ping.multicast.enabled: false"
echo "$config" >> $ES_HOME/config/elasticsearch.yml
}
install ()
{
if [ -d "$ES_HOME" ]; then
echo "already installed. start!"
exit 1
fi
if [ -f "$ES_HOME.tar.gz" ]; then
echo "skipping fetch. existing es archive found."
else
echo "fetching..."
curl -o $ES_HOME.tar.gz $ES_URL
fi
echo "extracting..."
mkdir -p $ES_HOME
tar -xf $ES_HOME.tar.gz -C $ES_HOME --strip-components=1
#update_config
echo "installed. start!"
}
status ()
{
if [ -f "$ES_PID" ]; then
echo "es: running."
else
echo "es: stopped."
fi
}
check_rogue ()
{
rogue_count=0
if [ `jps | grep -c Elasticsearch` != 0 ]; then
rogue_count=$((rogue_count + 1))
echo "another es cluster is running. please shut it off."
fi
if [ $rogue_count -gt 0 ]; then
echo "to kill -- run: jps | grep 'Elasticsearch' | cut -d' ' -f1 | xargs -n1 kill -9"
exit 1
fi
}
start ()
{
if [ ! -d "$ES_HOME" ]; then
echo "no es installed."
exit 1
fi
our_count=0
if [ -f "$ES_PID" ]; then
our_count=$((our_count + 1))
echo "our es is already running."
fi
if [ $our_count -gt 0 ]; then
echo "to shutdown -- run: $0 stop"
exit 1
fi
check_rogue
cd $ES_HOME
echo "starting es..."
bin/elasticsearch > $ES_HOME/es.stdout 2> $ES_HOME/es.stderr & echo $! > $ES_PID
es_pid=`cat $ES_PID`
echo "started."
echo "es-pid= $es_pid ($ES_PID)"
}
stop ()
{
if [ ! -d "$ES_HOME" ]; then
echo "no es installed."
exit 1
fi
if [ -f "$ES_PID" ]; then
pid=`cat $ES_PID`
kill -9 $pid && rm $ES_PID
else
echo "no pid found for es. nothing to stop."
fi
echo "all is stopped."
}
clean_data ()
{
check_rogue && rm -rf $DATA_DIR_ES && echo "data. be gone!"
}
clean ()
{
if [ ! -d "$ES_HOME" ]; then
echo "no es installed."
exit 1
fi
stop
clean_data
rm -rf $ES_HOME $ES_HOME.tar.gz
echo "the \$ES_HOME directory is no more :("
}
print_usage ()
{
echo "Usage: $0 (install|status|start|stop|clean_data|clean|help)"
}
main ()
{
if [ $1 == "install" ]; then
install
elif [ $1 == "stop" ]; then
stop
elif [ $1 == "status" ]; then
status
elif [ $1 == "start" ]; then
start
elif [ $1 == "clean_data" ]; then
clean_data
elif [ $1 == "clean" ]; then
clean
elif [ $1 == "help" ]; then
print_usage
else
echo "Unknown command."
print_usage
fi
}
[ $# -eq 0 ] && { echo "must specify command."; print_usage; exit 1; }
main $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment