Last active
December 12, 2015 10:39
-
-
Save iconara/4760720 to your computer and use it in GitHub Desktop.
Script that starts a local RabbitMQ cluster, tested with 2.7, 2.8 and 3.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: clusterctl start|stop | |
# | |
# Launches a local RabbitMQ cluster | |
# | |
# The name returned by `hostname -s` must resolve to 127.0.0.1 | |
CLUSTER_SIZE=4 | |
NODENAME_PREFIX='rmq' | |
START_PORT=6672 | |
CLUSTER_BASE_DIR='/tmp/rmq_cluster' | |
function start_rmq_node { | |
nodename=$1 | |
port=$2 | |
mgmt_port_2x=$(($port + 50000)) | |
mgmt_port_3x=$(($port + 10000)) | |
mgmt_port_args_2x="-rabbitmq_mochiweb listeners [{mgmt,[{port,$mgmt_port_2x}]}]" | |
mgmt_port_args_3x="-rabbitmq_management listener [{port,$mgmt_port_3x}]" | |
RABBITMQ_NODE_PORT=$port \ | |
RABBITMQ_NODENAME=$nodename \ | |
RABBITMQ_MNESIA_BASE=$CLUSTER_BASE_DIR/mnesia \ | |
RABBITMQ_LOG_BASE=$CLUSTER_BASE_DIR/log \ | |
RABBITMQ_SERVER_START_ARGS="$mgmt_port_args_2x $mgmt_port_args_3x" \ | |
rabbitmq-server -detached | |
} | |
function join_cluster { | |
nodename=$1 | |
host=$(hostname -s) | |
master_node="${NODENAME_PREFIX}0" | |
join_command=$(rabbitmqctl -q 2>/dev/null | egrep -o ' (join_)?cluster ' | tr -d ' ') | |
rabbitmqctl -n $nodename@$host stop_app | |
rabbitmqctl -n $nodename@$host $join_command $master_node@$host | |
rabbitmqctl -n $nodename@$host start_app | |
} | |
function start_cluster { | |
test -d $CLUSTER_BASE_DIR/mnesia && rm -r $CLUSTER_BASE_DIR/mnesia | |
mkdir -p $CLUSTER_BASE_DIR/mnesia $CLUSTER_BASE_DIR/log | |
start_rmq_node "${NODENAME_PREFIX}0" $START_PORT | |
for ((n=1; n < $CLUSTER_SIZE; n++)); do | |
port=$(($START_PORT + $n)) | |
nodename="$NODENAME_PREFIX$n" | |
start_rmq_node $nodename $port | |
done | |
if rabbitmqctl -q 2> /dev/null | grep -q '^ eval'; then | |
host=$(hostname -s) | |
for ((n=0; n < $CLUSTER_SIZE; n++)); do | |
nodename="$NODENAME_PREFIX$n" | |
rabbitmqctl -n $nodename@$host eval 'erlang:function_exported(rabbit,await_startup,0) andalso rabbit:await_startup().' > /dev/null | |
done | |
fi | |
for ((n=1; n < $CLUSTER_SIZE; n++)); do | |
nodename="$NODENAME_PREFIX$n" | |
join_cluster $nodename | |
done | |
} | |
function stop_cluster { | |
for ((n=0; n < $CLUSTER_SIZE; n++)); do | |
rabbitmqctl -n "$NODENAME_PREFIX$n" stop | |
done | |
} | |
arp $(hostname -s) 2>&1 | grep 127.0.0.1 > /dev/null | |
if [ $? -gt 0 ]; then | |
echo 'The name returned by `hostname -s` must resolve to 127.0.0.1' 1>&2 | |
exit 1 | |
fi | |
set -e | |
case "$1" in | |
"start" ) | |
start_cluster ;; | |
"stop" ) | |
stop_cluster ;; | |
* ) | |
echo "Usage: clusterctl start|stop" 1>&2 ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment