8ec879ddf3
Return to the previous version of init scripts that can recreate Mnesia if cluster gets broken. Change-Id: Id4801acd53f04743759f83544ce2db373db964ab Partial-Bug: 1318936
273 lines
7.6 KiB
Plaintext
273 lines
7.6 KiB
Plaintext
#!/bin/sh
|
|
#
|
|
# rabbitmq-server RabbitMQ broker
|
|
#
|
|
# chkconfig: - 80 05
|
|
# description: Enable AMQP service provided by RabbitMQ
|
|
#
|
|
|
|
### BEGIN INIT INFO
|
|
# Provides: rabbitmq-server
|
|
# Required-Start: $remote_fs $network
|
|
# Required-Stop: $remote_fs $network
|
|
# Description: RabbitMQ broker
|
|
# Short-Description: Enable AMQP service provided by RabbitMQ broker
|
|
### END INIT INFO
|
|
|
|
PATH=/sbin:/usr/sbin:/bin:/usr/bin
|
|
NAME=rabbitmq-server
|
|
DAEMON=/usr/sbin/${NAME}
|
|
CONTROL=/usr/sbin/rabbitmqctl
|
|
DESC=rabbitmq-server
|
|
USER=rabbitmq
|
|
ROTATE_SUFFIX=
|
|
INIT_LOG_DIR=/var/log/rabbitmq
|
|
PID_FILE=/var/run/rabbitmq/pid
|
|
|
|
START_PROG="runuser rabbitmq --session-command"
|
|
LOCK_FILE=/var/lock/subsys/$NAME
|
|
|
|
<% if @node_ip_address != 'UNSET' -%>
|
|
export RABBITMQ_NODE_IP_ADDRESS="<%= @node_ip_address %>"
|
|
<% end -%>
|
|
export RABBITMQ_NODE_PORT="<%= @port %>"
|
|
|
|
test -x $DAEMON || exit 0
|
|
test -x $CONTROL || exit 0
|
|
|
|
RETVAL=0
|
|
set -e
|
|
|
|
[ -f /etc/default/${NAME} ] && . /etc/default/${NAME}
|
|
|
|
ensure_pid_dir () {
|
|
PID_DIR=`dirname ${PID_FILE}`
|
|
if [ ! -d ${PID_DIR} ] ; then
|
|
mkdir -p ${PID_DIR}
|
|
chown -R ${USER}:${USER} ${PID_DIR}
|
|
chmod 755 ${PID_DIR}
|
|
fi
|
|
}
|
|
|
|
remove_pid () {
|
|
rm -f ${PID_FILE}
|
|
rmdir `dirname ${PID_FILE}` || :
|
|
}
|
|
|
|
set_nova_rabbit_credentials () {
|
|
local NOVA_CONFIG_FILE=/etc/nova/nova.conf
|
|
#These variables must be empty in the beginning
|
|
local NEW_RABBIT_USER=
|
|
local NEW_RABBIT_PASSWORD=
|
|
local RABBIT_VIRTUAL_HOST=
|
|
if [ -f ${NOVA_CONFIG_FILE} ] ; then
|
|
NEW_RABBIT_USER=$(grep -v '^#' $NOVA_CONFIG_FILE \
|
|
| grep rabbit_userid \
|
|
| awk -F "=" '{print $2}')
|
|
|
|
NEW_RABBIT_PASSWORD=$(grep -v '^#' $NOVA_CONFIG_FILE \
|
|
| grep rabbit_password \
|
|
| awk -F "=" '{print $2}')
|
|
|
|
RABBIT_VIRTUAL_HOST=$(grep -v '^#' $NOVA_CONFIG_FILE \
|
|
| grep rabbit_virtual_host \
|
|
| awk -F "=" '{print $2}')
|
|
fi
|
|
|
|
if [ "x" != "x${NEW_RABBIT_USER}" ] && [ "x" != "x${NEW_RABBIT_PASSWORD}" ]; then
|
|
printf '%s\n' "Restoring Nova user credentials..."
|
|
set +e
|
|
$CONTROL add_user $NEW_RABBIT_USER $NEW_RABBIT_PASSWORD
|
|
$CONTROL set_user_tags $NEW_RABBIT_USER administrator
|
|
|
|
if [ "x" != "x${RABBIT_VIRTUAL_HOST}" ] ; then
|
|
RABBIT_VIRTUAL_HOST=/
|
|
fi
|
|
if [ ${RABBIT_VIRTUAL_HOST} != '/' ] ; then
|
|
$CONTROL add_vhost $RABBIT_VIRTUAL_HOST
|
|
fi
|
|
$CONTROL set_permissions -p $RABBIT_VIRTUAL_HOST \
|
|
$NEW_RABBIT_USER '.*' '.*' '.*'
|
|
$CONTROL delete_user guest
|
|
set -e
|
|
printf '%s\n' "Done restoring user credentials."
|
|
fi
|
|
}
|
|
|
|
move_out_rabbit_database_to_backup () {
|
|
local RABBIT_DEFAULTS_FILE=/usr/lib/rabbitmq/bin/rabbitmq-defaults
|
|
local RABBIT_CONFIG_FILE=/etc/rabbitmq/rabbitmq-env.conf
|
|
local DATE=$(date +"%Y-%m-%d_%H-%M-%S")
|
|
local C_DIR=$(pwd)
|
|
|
|
[ -f ${RABBIT_DEFAULTS_FILE} ] && . ${RABBIT_DEFAULTS_FILE}
|
|
[ -f ${RABBIT_CONFIG_FILE} ] && . ${RABBIT_CONFIG_FILE}
|
|
|
|
if [ -d ${MNESIA_BASE} ] ; then
|
|
cd ${MNESIA_BASE}/..
|
|
local BACKUPS_DIR=`dirname ${MNESIA_BASE}`/backups
|
|
|
|
if [ ! -d ${BACKUPS_DIR} ] ; then
|
|
mkdir -p ${BACKUPS_DIR}
|
|
chown -R ${USER}:${USER} ${BACKUPS_DIR}
|
|
chmod 755 ${BACKUPS_DIR}
|
|
fi
|
|
|
|
printf '%s\n' " "
|
|
printf '%s\n' "Backing up mnesia base in: ${MNESIA_BASE}"
|
|
mv `basename ${MNESIA_BASE}` \
|
|
${BACKUPS_DIR}/`basename ${MNESIA_BASE}`_${DATE}
|
|
printf '%s\n' "Old Mnesia base has moved to " \
|
|
${BACKUPS_DIR}/`basename ${MNESIA_BASE}`_${DATE}
|
|
cd ${C_DIR}
|
|
fi
|
|
}
|
|
|
|
start_rabbitmq () {
|
|
status_rabbitmq quiet
|
|
if [ $RETVAL = 0 ] ; then
|
|
echo RabbitMQ is currently running
|
|
else
|
|
#Slave nodes fail to start until master is not up and running
|
|
#So, give slaves several attempts to start
|
|
#Rabbit database will be moved out before last attempt.
|
|
local MAX_START_ATTEMPTS=3
|
|
printf '%s\n' "RabbitMQ is going to make ${MAX_START_ATTEMPTS} \
|
|
attempts to find master node and start."
|
|
while test $MAX_START_ATTEMPTS -ne 0
|
|
do
|
|
RETVAL=0
|
|
ensure_pid_dir
|
|
printf '%s\n' "${MAX_START_ATTEMPTS} attempts left to start \
|
|
RabbitMQ Server before consider start failed."
|
|
if [ $MAX_START_ATTEMPTS = 1 ] ; then
|
|
move_out_rabbit_database_to_backup
|
|
fi
|
|
set +e
|
|
RABBITMQ_PID_FILE=$PID_FILE $START_PROG $DAEMON \
|
|
> "${INIT_LOG_DIR}/startup_log" \
|
|
2> "${INIT_LOG_DIR}/startup_err" \
|
|
0<&- &
|
|
$CONTROL wait $PID_FILE >/dev/null 2>&1
|
|
RETVAL=$?
|
|
set -e
|
|
case "$RETVAL" in
|
|
0)
|
|
echo SUCCESS
|
|
if [ -n "$LOCK_FILE" ] ; then
|
|
touch $LOCK_FILE
|
|
fi
|
|
if [ $MAX_START_ATTEMPTS = 1 ] ; then
|
|
set_nova_rabbit_credentials
|
|
RETVAL=0
|
|
fi
|
|
break
|
|
;;
|
|
*)
|
|
remove_pid
|
|
if [ $MAX_START_ATTEMPTS = 1 ] ; then
|
|
echo FAILED - check ${INIT_LOG_DIR}/startup_\{log, _err\}
|
|
fi
|
|
RETVAL=1
|
|
;;
|
|
esac
|
|
MAX_START_ATTEMPTS=$((MAX_START_ATTEMPTS - 1))
|
|
done
|
|
rabbitmqctl set_policy ha-all "." '{"ha-mode":"all", "ha-sync-mode":"automatic"}'
|
|
fi
|
|
}
|
|
|
|
stop_rabbitmq () {
|
|
status_rabbitmq quiet
|
|
if [ $RETVAL = 0 ] ; then
|
|
set +e
|
|
$CONTROL stop ${PID_FILE} > ${INIT_LOG_DIR}/shutdown_log 2> ${INIT_LOG_DIR}/shutdown_err
|
|
RETVAL=$?
|
|
set -e
|
|
if [ $RETVAL = 0 ] ; then
|
|
remove_pid
|
|
if [ -n "$LOCK_FILE" ] ; then
|
|
rm -f $LOCK_FILE
|
|
fi
|
|
else
|
|
echo FAILED - check ${INIT_LOG_DIR}/shutdown_log, _err
|
|
fi
|
|
else
|
|
echo RabbitMQ is not running
|
|
RETVAL=0
|
|
fi
|
|
}
|
|
|
|
status_rabbitmq() {
|
|
set +e
|
|
if [ "$1" != "quiet" ] ; then
|
|
$CONTROL status 2>&1
|
|
else
|
|
$CONTROL status > /dev/null 2>&1
|
|
fi
|
|
if [ $? != 0 ] ; then
|
|
RETVAL=3
|
|
fi
|
|
set -e
|
|
}
|
|
|
|
rotate_logs_rabbitmq() {
|
|
set +e
|
|
$CONTROL rotate_logs ${ROTATE_SUFFIX}
|
|
if [ $? != 0 ] ; then
|
|
RETVAL=1
|
|
fi
|
|
set -e
|
|
}
|
|
|
|
restart_running_rabbitmq () {
|
|
status_rabbitmq quiet
|
|
if [ $RETVAL = 0 ] ; then
|
|
restart_rabbitmq
|
|
else
|
|
echo RabbitMQ is not runnning
|
|
RETVAL=0
|
|
fi
|
|
}
|
|
|
|
restart_rabbitmq() {
|
|
stop_rabbitmq
|
|
start_rabbitmq
|
|
}
|
|
|
|
case "$1" in
|
|
start)
|
|
echo -n "Starting $DESC: "
|
|
start_rabbitmq
|
|
echo "$NAME."
|
|
;;
|
|
stop)
|
|
echo -n "Stopping $DESC: "
|
|
stop_rabbitmq
|
|
echo "$NAME."
|
|
;;
|
|
status)
|
|
status_rabbitmq
|
|
;;
|
|
rotate-logs)
|
|
echo -n "Rotating log files for $DESC: "
|
|
rotate_logs_rabbitmq
|
|
;;
|
|
force-reload|reload|restart)
|
|
echo -n "Restarting $DESC: "
|
|
restart_rabbitmq
|
|
echo "$NAME."
|
|
;;
|
|
try-restart)
|
|
echo -n "Restarting $DESC: "
|
|
restart_running_rabbitmq
|
|
echo "$NAME."
|
|
;;
|
|
*)
|
|
echo "Usage: $0 {start|stop|status|rotate-logs|restart|condrestart|try-restart|reload|force-reload}" >&2
|
|
RETVAL=1
|
|
;;
|
|
esac
|
|
|
|
exit $RETVAL
|