Skip to content

Instantly share code, notes, and snippets.

Created May 15, 2015 20:09
Show Gist options
  • Save anonymous/337db6e92066fe1afba8 to your computer and use it in GitHub Desktop.
Save anonymous/337db6e92066fe1afba8 to your computer and use it in GitHub Desktop.
#!/bin/bash
KAFKA_URL='http://mirror.cogentco.com/pub/apache/kafka/0.8.2.1/kafka_2.10-0.8.2.1.tgz'
KAFKA_HOME='/tmp/kafka-es'
DATA_DIR_KAFKA='/tmp/kafka-logs'
DATA_DIR_ZOOKEEPER='/tmp/zookeeper'
ZOOKEEPER_PID=$KAFKA_HOME/zookeeper.pid
KAFKA_PID=$KAFKA_HOME/kafka.pid
install ()
{
if [ -d "$KAFKA_HOME" ]; then
echo "already installed. start!"
exit 1
fi
if [ -f "$KAFKA_HOME.tgz" ]; then
echo "skipping fetch. existing kafka archive found."
else
echo "fetching..."
curl -s -o $KAFKA_HOME.tgz $KAFKA_URL
fi
echo "extracting..."
mkdir -p $KAFKA_HOME
tar -xf $KAFKA_HOME.tgz -C $KAFKA_HOME --strip-components=1
echo "installed. start!"
}
status ()
{
if [ -f "$ZOOKEEPER_PID" ]; then
echo "zookeeper: running."
else
echo "zookeeper: stopped."
fi
if [ -f "$KAFKA_PID" ]; then
echo "kafka: running."
else
echo "kafka: stopped."
fi
}
check_rogue ()
{
rogue_count=0
if [ `jps | grep -c QuorumPeerMain` != 0 ]; then
rogue_count=$((rogue_count + 1))
echo "another zookeeper is running. please shut it off."
elif [ `jps | grep -c Kafka` != 0 ]; then
rogue_count=$((rogue_count + 1))
echo "another kafka is running. please shut it off."
fi
if [ $rogue_count -gt 0 ]; then
echo "to kill -- run: jps | grep 'QuorumPeerMain\|Kafka' | cut -d' ' -f1 | xargs -n1 kill -9"
exit 1
fi
}
start ()
{
if [ ! -d "$KAFKA_HOME" ]; then
echo "no kafka installed."
exit 1
fi
our_count=0
if [ -f "$ZOOKEEPER_PID" ]; then
our_count=$((our_count + 1))
echo "our zookeeper is already running."
fi
if [ -f "$KAFKA_PID" ]; then
our_count=$((our_count + 1))
echo "our kafka is already running."
fi
if [ $our_count -gt 0 ]; then
echo "to shutdown -- run: $0 stop"
exit 1
fi
check_rogue
cd $KAFKA_HOME
echo "starting zookeeper..."
bin/zookeeper-server-start.sh config/zookeeper.properties > $KAFKA_HOME/zookeeper.stdout 2> $KAFKA_HOME/zookeeper.stderr & echo $! > zookeeper.pid
sleep 1
echo "starting kafka..."
JMX_PORT=9999 bin/kafka-server-start.sh config/server.properties > $KAFKA_HOME/kafka.stdout 2> $KAFKA_HOME/kafka.stderr & echo $! > kafka.pid
sleep 1
z_pid=`cat $KAFKA_HOME/zookeeper.pid`
k_pid=`cat $KAFKA_HOME/kafka.pid`
echo "started."
echo "zookeeper-pid= $z_pid ($ZOOKEEPER_PID)"
echo "kafka-pid= $k_pid ($KAFKA_PID)"
}
stop ()
{
if [ ! -d "$KAFKA_HOME" ]; then
echo "no kafka installed."
exit 1
fi
if [ -f "$ZOOKEEPER_PID" ]; then
pid=`cat $ZOOKEEPER_PID`
kill -9 $pid && rm $ZOOKEEPER_PID
else
echo "no pid found for zookeeper. nothing to stop."
fi
if [ -f "$KAFKA_PID" ]; then
pid=`cat $KAFKA_PID`
kill -9 $pid && rm $KAFKA_PID
else
echo "no pid found for kafka. nothing to stop."
fi
echo "all is stopped."
}
clean_data ()
{
check_rogue && rm -rf $DATA_DIR_ZOOKEEPER $DATA_DIR_KAFKA && echo "data. be gone!"
}
clean ()
{
if [ ! -d "$KAFKA_HOME" ]; then
echo "no kafka installed."
exit 1
fi
stop
clean_data
rm -rf $KAFKA_HOME $KAFKA_HOME.tgz
echo "the \$KAFKA_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