#!/bin/bash # # rabbitmq-relations - relations to be used by formula, referenced # via symlink # # Copyright (C) 2011 Canonical Ltd. # Author: Adam Gandelman # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . set -u FORMULA_DIR=$(dirname $0) ARG0=${0##*/} if [[ -e $FORMULA_DIR/rabbitmq-common ]] ; then . $FORMULA_DIR/rabbitmq-common else juju-log "rabbitmq-server: ERROR Could not load $FORMULA_DIR/rabbitmq-common" exit 1 fi juju-log "rabbitmq-server: Firing hook $ARG0." function install_hook() { [[ ! `which pwgen` ]] && apt-get -y install pwgen DEBIAN_FRONTEND=noninteractive apt-get -qqy \ install --no-install-recommends rabbitmq-server rc=$? # give ubuntu user passwd-less sudo for log access echo "ubuntu ALL=(ALL) NOPASSWD:ALL" >/etc/sudoers.d/ubuntu-qa chmod 0440 /etc/sudoers.d/ubuntu-qa service rabbitmq-server stop } function amqp_changed() { # Connecting clients should request a username and vhost. # In reponse, we generate a password for new users, # grant the user access on the default vhost "/", # and tell it where to reach us. RABBIT_USER=`relation-get username` VHOST=`relation-get vhost` if [[ -z $RABBIT_USER ]] || [[ -z $VHOST ]] ; then juju-log "rabbitmq-server: RABBIT_USER||VHOST not yet received from peer." exit 0 fi PASSWD_FILE="/var/run/juju/$RABBIT_USER.passwd" if [[ -e $PASSWD_FILE ]] ; then PASSWORD=$(cat $PASSWD_FILE) else PASSWORD=$(pwgen 10 1) echo $PASSWORD >$PASSWD_FILE chmod 0400 $PASSWD_FILE fi if ! vhost_exists ; then juju-log "rabbitmq-server: Creating vhost $VHOST" create_vhost fi if ! user_exists $RABBIT_USER ; then juju-log "rabbitmq-server: Creating user $RABBIT_USER" user_create $RABBIT_USER admin || exit 1 else juju-log "rabbitmq-server: user $RABBIT_USER already exists." fi juju-log "rabbitmq-server: Returning credentials for $RABBIT_USER@$HOSTNAME" relation-set hostname=`unit-get private-address` relation-set password=$PASSWORD } function cluster_joined { REMOTE_UNIT_ID=$(echo $JUJU_REMOTE_UNIT | cut -d/ -f2) LOCAL_UNIT_ID=$(echo $JUJU_UNIT_NAME | cut -d/ -f2) [[ $LOCAL_UNIT_ID -gt $REMOTE_UNIT_ID ]] && echo "Relation greater" && exit 0 if [[ ! -e $ERLANG_COOKIE ]] ; then juju-log "rabbitmq-server: ERROR Could not find cookie at $ERLANG_COOKIE" exit 1 fi relation-set cookie=$(cat $ERLANG_COOKIE) host=$HOSTNAME } function cluster_changed { REMOTE_UNIT_ID=$(echo $JUJU_REMOTE_UNIT | cut -d/ -f2) LOCAL_UNIT_ID=$(echo $JUJU_UNIT_NAME | cut -d/ -f2) [[ $LOCAL_UNIT_ID -lt $REMOTE_UNIT_ID ]] && echo "Relation lesser" && exit 0 REMOTE_HOST=$(relation-get host) COOKIE_VALUE=$(relation-get cookie) [[ -z $REMOTE_HOST ]] || [[ -z $COOKIE_VALUE ]] && \ juju-log "rabbimtq-server: REMOTE_HOST||COOKIE_VALUE not yet set." \ exit 0 service rabbitmq-server stop echo -n $COOKIE_VALUE > $ERLANG_COOKIE service rabbitmq-server start rabbitmqctl reset rabbitmqctl cluster rabbit@$HOSTNAME rabbit@$REMOTE_HOST rabbitmqctl start_app } case $ARG0 in "install") install_hook ;; "start") service rabbitmq-server status || service rabbitmq-server start ;; "stop") service rabbitmq-server status && service rabbitmq-server stop ;; "amqp-relation-joined") exit 0 ;; "amqp-relation-changed") amqp_changed ;; "cluster-relation-joined") cluster_joined ;; "cluster-relation-changed") cluster_changed ;; esac rc=$? juju-log "rabbitmq-server: Hook $ARG0 complete. Exiting $rc" exit $rc