3
# rabbitmq-relations - relations to be used by formula, referenced
6
# Copyright (C) 2011 Canonical Ltd.
7
# Author: Adam Gandelman <adam.gandelman@canonical.com>
9
# This program is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU General Public License as published by
11
# the Free Software Foundation, either version 3 of the License, or
12
# (at your option) any later version.
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU General Public License for more details.
19
# You should have received a copy of the GNU General Public License
20
# along with this program. If not, see <http://www.gnu.org/licenses/>.
23
FORMULA_DIR=$(dirname $0)
26
if [[ -e $FORMULA_DIR/rabbitmq-common ]] ; then
27
. $FORMULA_DIR/rabbitmq-common
29
juju-log "rabbitmq-server: ERROR Could not load $FORMULA_DIR/rabbitmq-common"
33
juju-log "rabbitmq-server: Firing hook $ARG0."
35
function install_hook() {
36
[ -d exec.d ] && ( for f in exec.d/*/charm-pre-install; do [ -x $f ] && /bin/sh -c "$f";done )
38
[[ ! `which pwgen` ]] && apt-get -y install pwgen
39
DEBIAN_FRONTEND=noninteractive apt-get -qqy \
40
install --no-install-recommends rabbitmq-server
42
service rabbitmq-server stop
46
function upgrade_charm {
47
[ -d exec.d ] && ( for f in exec.d/*/charm-pre-install; do [ -x $f ] && /bin/sh -c "$f";done )
50
function amqp_changed() {
51
# Connecting clients should request a username and vhost.
52
# In reponse, we generate a password for new users,
53
# grant the user access on the default vhost "/",
54
# and tell it where to reach us.
55
RABBIT_USER=`relation-get username`
56
VHOST=`relation-get vhost`
57
if [[ -z $RABBIT_USER ]] || [[ -z $VHOST ]] ; then
58
juju-log "rabbitmq-server: RABBIT_USER||VHOST not yet received from peer."
61
PASSWD_FILE="/var/lib/juju/$RABBIT_USER.passwd"
62
if [[ -e $PASSWD_FILE ]] ; then
63
PASSWORD=$(cat $PASSWD_FILE)
65
PASSWORD=$(pwgen 10 1)
66
echo $PASSWORD >$PASSWD_FILE
67
chmod 0400 $PASSWD_FILE
69
if ! vhost_exists ; then
70
juju-log "rabbitmq-server: Creating vhost $VHOST"
73
if ! user_exists $RABBIT_USER ; then
74
juju-log "rabbitmq-server: Creating user $RABBIT_USER"
75
user_create $RABBIT_USER admin || exit 1
77
juju-log "rabbitmq-server: user $RABBIT_USER already exists."
79
juju-log "rabbitmq-server: Returning credentials for $RABBIT_USER@$HOSTNAME"
80
relation-set hostname=`unit-get private-address`
81
relation-set password=$PASSWORD
84
function cluster_joined {
85
REMOTE_UNIT_ID=$(echo $JUJU_REMOTE_UNIT | cut -d/ -f2)
86
LOCAL_UNIT_ID=$(echo $JUJU_UNIT_NAME | cut -d/ -f2)
87
[[ $LOCAL_UNIT_ID -gt $REMOTE_UNIT_ID ]] && echo "Relation greater" && exit 0
88
if [[ ! -e $ERLANG_COOKIE ]] ; then
89
juju-log "rabbitmq-server: ERROR Could not find cookie at $ERLANG_COOKIE"
92
relation-set cookie=$(cat $ERLANG_COOKIE) host=$HOSTNAME
95
function cluster_changed {
96
REMOTE_UNIT_ID=$(echo $JUJU_REMOTE_UNIT | cut -d/ -f2)
97
LOCAL_UNIT_ID=$(echo $JUJU_UNIT_NAME | cut -d/ -f2)
98
[[ $LOCAL_UNIT_ID -lt $REMOTE_UNIT_ID ]] && echo "Relation lesser" && exit 0
100
REMOTE_HOST=$(relation-get host)
101
COOKIE_VALUE=$(relation-get cookie)
102
[[ -z $REMOTE_HOST ]] || [[ -z $COOKIE_VALUE ]] && \
103
juju-log "rabbimtq-server: REMOTE_HOST||COOKIE_VALUE not yet set." \
106
service rabbitmq-server stop
107
echo -n $COOKIE_VALUE > $ERLANG_COOKIE
108
service rabbitmq-server start
110
rabbitmqctl cluster rabbit@$HOSTNAME rabbit@$REMOTE_HOST
111
rabbitmqctl start_app
115
"install") install_hook ;;
116
"start") service rabbitmq-server status || service rabbitmq-server start ;;
117
"stop") service rabbitmq-server status && service rabbitmq-server stop ;;
118
"upgrade-charm") upgrade_charm ;;
119
"amqp-relation-joined") exit 0 ;;
120
"amqp-relation-changed") amqp_changed ;;
121
"cluster-relation-joined") cluster_joined ;;
122
"cluster-relation-changed") cluster_changed ;;
126
juju-log "rabbitmq-server: Hook $ARG0 complete. Exiting $rc"