130 lines
4.3 KiB
Bash
Executable File
130 lines
4.3 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# rabbitmq-relations - relations to be used by formula, referenced
|
|
# via symlink
|
|
#
|
|
# Copyright (C) 2011 Canonical Ltd.
|
|
# Author: Adam Gandelman <adam.gandelman@canonical.com>
|
|
#
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
set -ue
|
|
|
|
CHARM_DIR=$(dirname $0)
|
|
ARG0=${0##*/}
|
|
|
|
if [[ -e $CHARM_DIR/rabbitmq-common ]] ; then
|
|
. $CHARM_DIR/rabbitmq-common
|
|
else
|
|
juju-log "rabbitmq-server: ERROR Could not load $CHARM_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=$?
|
|
service rabbitmq-server stop
|
|
open-port 5672/tcp
|
|
}
|
|
|
|
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.
|
|
local rabbit_user=`relation-get username`
|
|
local 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
|
|
local passwd_file="/var/lib/juju/$rabbit_user.passwd"
|
|
local password=""
|
|
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 "$vhost" ; then
|
|
juju-log "rabbitmq-server: Creating vhost $vhost"
|
|
create_vhost "$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
|
|
local remote_host="$(relation-get private-address)"
|
|
juju-log "rabbitmq-server: Returning credentials for $rabbit_user@$remote_host"
|
|
relation-set password="$password"
|
|
}
|
|
|
|
function cluster_joined {
|
|
local remote_unit_id=$(echo $JUJU_REMOTE_UNIT | cut -d/ -f2)
|
|
local 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)
|
|
}
|
|
|
|
function cluster_changed {
|
|
local remote_unit_id=$(echo $JUJU_REMOTE_UNIT | cut -d/ -f2)
|
|
local local_unit_id=$(echo $JUJU_UNIT_NAME | cut -d/ -f2)
|
|
|
|
[[ $local_unit_id -lt $remote_unit_id ]] && echo "Relation lesser" && exit 0
|
|
|
|
local remote_host=$(relation-get private-address)
|
|
local cookie_value=$(relation-get cookie)
|
|
|
|
[[ -z "$remote_host" ]] || [[ -z "$cookie_value" ]] && \
|
|
juju-log "rabbimtq-server: remote_host||cookie_value not yet set." \
|
|
exit 0
|
|
|
|
# Sync the erlang cookie to that of remote host.
|
|
service rabbitmq-server stop
|
|
echo -n "$cookie_value" > $ERLANG_COOKIE
|
|
service rabbitmq-server start
|
|
|
|
# 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
|
|
}
|
|
|
|
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
|