From eb703692833994ace397e8791d05cc527afe8878 Mon Sep 17 00:00:00 2001 From: Adam Gandelman Date: Wed, 16 Jan 2013 10:27:55 -0800 Subject: [PATCH] RabbitMQ 3.0 syntax fixes, clustering fixes. --- hooks/rabbitmq-common | 24 ++++++++++++++++-------- hooks/rabbitmq-relations | 35 ++++++++++++++++++++++++++++++----- revision | 2 +- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/hooks/rabbitmq-common b/hooks/rabbitmq-common index b313b5a6..fcaaed31 100755 --- a/hooks/rabbitmq-common +++ b/hooks/rabbitmq-common @@ -18,7 +18,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -set -ue +set -e RABBIT_CTL='rabbitmqctl' @@ -42,17 +42,25 @@ function create_vhost { } function user_create { - juju-log "rabbitmq: Creating user $1." - $RABBIT_CTL add_user "$1" "$3" || return 1 + local user="$1" + local passwd="$2" + local vhost="$3" + local admin="$4" + juju-log "rabbitmq: Creating user $1 (vhost: $3" + $RABBIT_CTL add_user "$user" "$passwd" || return 1 # grant the user all permissions on the default vhost / # TODO: investigate sane permissions juju-log "rabbitmq: Granting permission to $1 on vhost /" - $RABBIT_CTL set_permissions -p $VHOST $1 ".*" ".*" ".*" + $RABBIT_CTL set_permissions -p "$vhost" "$user" ".*" ".*" ".*" - if [[ "$2" == 'admin' ]] ; then - user_is_admin "$1" && return 0 - juju-log "rabbitmq: Granting user $1 admin access" - $RABBIT_CTL set_user_tags "$1" administrator || return 1 + if [[ "$admin" == "admin" ]] ; then + user_is_admin "$user" && return 0 + juju-log "rabbitmq: Granting user $user admin access" + $RABBIT_CTL set_user_tags "$user" administrator || return 1 fi } + +function rabbit_version { + echo "$(dpkg -l | grep rabbitmq-server | awk '{ print $3 }')" +} diff --git a/hooks/rabbitmq-relations b/hooks/rabbitmq-relations index 0cba6327..b6640d16 100755 --- a/hooks/rabbitmq-relations +++ b/hooks/rabbitmq-relations @@ -19,7 +19,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -set -ue +set -e CHARM_DIR=$(dirname $0) ARG0=${0##*/} @@ -47,6 +47,21 @@ function amqp_changed() { # In reponse, we generate a password for new users, # grant the user access on the default vhost "/", # and tell it where to reach us. + + # Skip managing rabbit queue config unless we are the + # highest numbered unit in the service cluster. + local local_unit_id=$(echo $JUJU_UNIT_NAME | cut -d/ -f2) + local remote_unit_id="" + for relid in $(relation-ids cluster) ; do + for unit in $(relation-list -r "$relid") ; do + remote_unit_id="$(echo $unit | cut -d/ -f2)" + if [[ "$remote_unit_id" -gt "$local_unit_id" ]]; then + juju-log "amqp_changed(): Deferring amqp_changed to leader." + exit 0 + fi + done + done + local rabbit_user=`relation-get username` local vhost=`relation-get vhost` if [[ -z "$rabbit_user" ]] || [[ -z "$vhost" ]] ; then @@ -68,7 +83,7 @@ function amqp_changed() { fi if ! user_exists "$rabbit_user" ; then juju-log "rabbitmq-server: Creating user $rabbit_user" - user_create $rabbit_user admin || exit 1 + user_create "$rabbit_user" "$password" "$vhost" admin || exit 1 else juju-log "rabbitmq-server: user $rabbit_user already exists." fi @@ -109,9 +124,19 @@ function cluster_changed { # Configure clustering. # rabbitmq apparently does not like FQDNs. local short_host=$(echo $remote_host | sed -e 's/\./ /g' | awk '{ print $1 }') - rabbitmqctl stop_app - rabbitmqctl cluster rabbit@$short_host - rabbitmqctl start_app + local cur_vers=$(rabbit_version) + local cluster_cmd="cluster" + if dpkg --compare-versions "$cur_vers" ge "3.0.1-1" ; then + cluster_cmd="join_cluster" + fi + if ! rabbitmqctl cluster_status | grep -q "rabbit@$short_host" ; then + juju-log "Clustering with new rabbitmq peer @ $short_host." + rabbitmqctl stop_app + rabbitmqctl $cluster_cmd rabbit@$short_host + rabbitmqctl start_app + else + juju-log "Already clustered with rabbitmq peer @ $short_host." + fi } case $ARG0 in diff --git a/revision b/revision index a2720097..abac1ea7 100644 --- a/revision +++ b/revision @@ -1 +1 @@ -39 +47