Merge lp:~gandelman-a/charms/precise/rabbitmq-server/cluster_fix into lp:charms/rabbitmq-server

Proposed by Adam Gandelman
Status: Merged
Merged at revision: 41
Proposed branch: lp:~gandelman-a/charms/precise/rabbitmq-server/cluster_fix
Merge into: lp:charms/rabbitmq-server
Diff against target: 190 lines (+64/-53)
3 files modified
hooks/rabbitmq-common (+9/-7)
hooks/rabbitmq-relations (+54/-45)
revision (+1/-1)
To merge this branch: bzr merge lp:~gandelman-a/charms/precise/rabbitmq-server/cluster_fix
Reviewer Review Type Date Requested Status
James Page Needs Fixing
Review via email: mp+138614@code.launchpad.net
To post a comment you must log in.
Revision history for this message
James Page (james-page) wrote :

Hi Adam

I gave this a try; the changes generally look OK BUT I do hit issues when `relation-get private-address` returns an IP address instead of a hostname; this will happen when using the openstack and local providers so I think the charm needs to deal with this as well.

Cheers

James

review: Needs Fixing
Revision history for this message
Clint Byrum (clint-fewbar) wrote :

Excerpts from James Page's message of 2012-12-07 10:03:27 UTC:
> Review: Needs Fixing
>
> Hi Adam
>
> I gave this a try; the changes generally look OK BUT I do hit issues when `relation-get private-address` returns an IP address instead of a hostname; this will happen when using the openstack and local providers so I think the charm needs to deal with this as well.
>

Smells like something for charm helper to do.

the sh/net.sh helper already has a 'ch_is_ip' that uses awk to test if the input is an ipv4 address.

Even better would be something like

ch_relation_get_private_ip

and

ch_unit_get_private_ip

Which would hide all the details of turning that into an IP.

43. By Adam Gandelman

Revert use of 'relation-get private-address'

Revision history for this message
Adam Gandelman (gandelman-a) wrote :

I've reverted the charm's peer relations back to their previous behavior. Turns out, when clustering peers, rabbitmq needs to address its peers by the peers' node name, which is the local hostname of that system. Using anything else (IP, FQDN, CNAME) wont work. This is why the original hook passed output of `hostname` across the relation as the hostname and did not use anything from the provider. I've tested on both the EC2 and OSAPI provider and clustering works as expected.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/rabbitmq-common'
2--- hooks/rabbitmq-common 2011-12-05 19:55:29 +0000
3+++ hooks/rabbitmq-common 2012-12-07 20:44:21 +0000
4@@ -18,8 +18,10 @@
5 # You should have received a copy of the GNU General Public License
6 # along with this program. If not, see <http://www.gnu.org/licenses/>.
7
8+set -ue
9+
10 RABBIT_CTL='rabbitmqctl'
11-HOSTNAME=`hostname -f`
12+
13 ERLANG_COOKIE="/var/lib/rabbitmq/.erlang.cookie"
14
15 function user_exists {
16@@ -31,25 +33,25 @@
17 }
18
19 function vhost_exists {
20- $RABBIT_CTL list_vhosts | grep "^$VHOST\$" >/dev/null
21+ $RABBIT_CTL list_vhosts | grep "^$1\$" >/dev/null
22 }
23
24 function create_vhost {
25- $RABBIT_CTL add_vhost $VHOST
26+ juju-log "Creating vhost: $1"
27+ $RABBIT_CTL add_vhost "$1"
28 }
29
30 function user_create {
31 juju-log "rabbitmq: Creating user $1."
32-
33- $RABBIT_CTL add_user $1 $PASSWORD || return 1
34+ $RABBIT_CTL add_user "$1" "$3" || return 1
35
36 # grant the user all permissions on the default vhost /
37 # TODO: investigate sane permissions
38 juju-log "rabbitmq: Granting permission to $1 on vhost /"
39 $RABBIT_CTL set_permissions -p $VHOST $1 ".*" ".*" ".*"
40
41- if [[ $2 == 'admin' ]] ; then
42- user_is_admin $1 && return 0
43+ if [[ "$2" == 'admin' ]] ; then
44+ user_is_admin "$1" && return 0
45 juju-log "rabbitmq: Granting user $1 admin access"
46 $RABBIT_CTL set_user_tags "$1" administrator || return 1
47 fi
48
49=== modified file 'hooks/rabbitmq-relations'
50--- hooks/rabbitmq-relations 2012-11-21 14:47:12 +0000
51+++ hooks/rabbitmq-relations 2012-12-07 20:44:21 +0000
52@@ -19,14 +19,15 @@
53 # You should have received a copy of the GNU General Public License
54 # along with this program. If not, see <http://www.gnu.org/licenses/>.
55
56-set -u
57-FORMULA_DIR=$(dirname $0)
58+set -ue
59+
60+CHARM_DIR=$(dirname $0)
61 ARG0=${0##*/}
62
63-if [[ -e $FORMULA_DIR/rabbitmq-common ]] ; then
64- . $FORMULA_DIR/rabbitmq-common
65+if [[ -e $CHARM_DIR/rabbitmq-common ]] ; then
66+ . $CHARM_DIR/rabbitmq-common
67 else
68- juju-log "rabbitmq-server: ERROR Could not load $FORMULA_DIR/rabbitmq-common"
69+ juju-log "rabbitmq-server: ERROR Could not load $CHARM_DIR/rabbitmq-common"
70 exit 1
71 fi
72
73@@ -46,62 +47,70 @@
74 # In reponse, we generate a password for new users,
75 # grant the user access on the default vhost "/",
76 # and tell it where to reach us.
77- RABBIT_USER=`relation-get username`
78- VHOST=`relation-get vhost`
79- if [[ -z $RABBIT_USER ]] || [[ -z $VHOST ]] ; then
80- juju-log "rabbitmq-server: RABBIT_USER||VHOST not yet received from peer."
81+ local rabbit_user=`relation-get username`
82+ local vhost=`relation-get vhost`
83+ if [[ -z "$rabbit_user" ]] || [[ -z "$vhost" ]] ; then
84+ juju-log "rabbitmq-server: rabbit_user||vhost not yet received from peer."
85 exit 0
86 fi
87- PASSWD_FILE="/var/lib/juju/$RABBIT_USER.passwd"
88- if [[ -e $PASSWD_FILE ]] ; then
89- PASSWORD=$(cat $PASSWD_FILE)
90- else
91- PASSWORD=$(pwgen 10 1)
92- echo $PASSWORD >$PASSWD_FILE
93- chmod 0400 $PASSWD_FILE
94- fi
95- if ! vhost_exists ; then
96- juju-log "rabbitmq-server: Creating vhost $VHOST"
97- create_vhost
98- fi
99- if ! user_exists $RABBIT_USER ; then
100- juju-log "rabbitmq-server: Creating user $RABBIT_USER"
101- user_create $RABBIT_USER admin || exit 1
102- else
103- juju-log "rabbitmq-server: user $RABBIT_USER already exists."
104- fi
105- juju-log "rabbitmq-server: Returning credentials for $RABBIT_USER@$HOSTNAME"
106- relation-set hostname=`unit-get private-address`
107- relation-set password=$PASSWORD
108+ local passwd_file="/var/lib/juju/$rabbit_user.passwd"
109+ local password=""
110+ if [[ -e $passwd_file ]] ; then
111+ password=$(cat $passwd_file)
112+ else
113+ password=$(pwgen 10 1)
114+ echo $password >$passwd_file
115+ chmod 0400 $passwd_file
116+ fi
117+ if ! vhost_exists "$vhost" ; then
118+ juju-log "rabbitmq-server: Creating vhost $vhost"
119+ create_vhost "$vhost"
120+ fi
121+ if ! user_exists "$rabbit_user" ; then
122+ juju-log "rabbitmq-server: Creating user $rabbit_user"
123+ user_create $rabbit_user admin || exit 1
124+ else
125+ juju-log "rabbitmq-server: user $rabbit_user already exists."
126+ fi
127+ local remote_host="$(relation-get private-address)"
128+ juju-log "rabbitmq-server: Returning credentials for $rabbit_user@$remote_host"
129+ relation-set password="$password"
130 }
131
132 function cluster_joined {
133- REMOTE_UNIT_ID=$(echo $JUJU_REMOTE_UNIT | cut -d/ -f2)
134- LOCAL_UNIT_ID=$(echo $JUJU_UNIT_NAME | cut -d/ -f2)
135- [[ $LOCAL_UNIT_ID -gt $REMOTE_UNIT_ID ]] && echo "Relation greater" && exit 0
136+ local remote_unit_id=$(echo $JUJU_REMOTE_UNIT | cut -d/ -f2)
137+ local local_unit_id=$(echo $JUJU_UNIT_NAME | cut -d/ -f2)
138+ [[ $local_unit_id -gt $remote_unit_id ]] && echo "Relation greater" && exit 0
139 if [[ ! -e $ERLANG_COOKIE ]] ; then
140 juju-log "rabbitmq-server: ERROR Could not find cookie at $ERLANG_COOKIE"
141 exit 1
142 fi
143- relation-set cookie=$(cat $ERLANG_COOKIE) host=$HOSTNAME
144+ relation-set cookie="$(cat $ERLANG_COOKIE)" host="$(hostname)"
145 }
146
147 function cluster_changed {
148- REMOTE_UNIT_ID=$(echo $JUJU_REMOTE_UNIT | cut -d/ -f2)
149- LOCAL_UNIT_ID=$(echo $JUJU_UNIT_NAME | cut -d/ -f2)
150- [[ $LOCAL_UNIT_ID -lt $REMOTE_UNIT_ID ]] && echo "Relation lesser" && exit 0
151-
152- REMOTE_HOST=$(relation-get host)
153- COOKIE_VALUE=$(relation-get cookie)
154- [[ -z $REMOTE_HOST ]] || [[ -z $COOKIE_VALUE ]] && \
155- juju-log "rabbimtq-server: REMOTE_HOST||COOKIE_VALUE not yet set." \
156+ local remote_unit_id=$(echo $JUJU_REMOTE_UNIT | cut -d/ -f2)
157+ local local_unit_id=$(echo $JUJU_UNIT_NAME | cut -d/ -f2)
158+
159+ [[ $local_unit_id -lt $remote_unit_id ]] && echo "Relation lesser" && exit 0
160+
161+ local remote_host=$(relation-get host)
162+ local cookie_value=$(relation-get cookie)
163+
164+ [[ -z "$remote_host" ]] || [[ -z "$cookie_value" ]] && \
165+ juju-log "rabbimtq-server: remote_host||cookie_value not yet set." &&
166 exit 0
167
168+ # Sync the erlang cookie to that of remote host.
169 service rabbitmq-server stop
170- echo -n $COOKIE_VALUE > $ERLANG_COOKIE
171+ echo -n "$cookie_value" > $ERLANG_COOKIE
172 service rabbitmq-server start
173- rabbitmqctl reset
174- rabbitmqctl cluster rabbit@$HOSTNAME rabbit@$REMOTE_HOST
175+
176+ # Configure clustering.
177+ # rabbitmq apparently does not like FQDNs.
178+ local short_host=$(echo $remote_host | sed -e 's/\./ /g' | awk '{ print $1 }')
179+ rabbitmqctl stop_app
180+ rabbitmqctl cluster rabbit@$short_host
181 rabbitmqctl start_app
182 }
183
184
185=== modified file 'revision'
186--- revision 2012-11-21 15:30:39 +0000
187+++ revision 2012-12-07 20:44:21 +0000
188@@ -1,1 +1,1 @@
189-37
190+39

Subscribers

People subscribed via source and target branches