blob: 3864adec32f81980458ee8f657936f5146b1eaba [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +09003# lib/rpc_backend
Sean Dague37eca482015-06-16 07:19:22 -04004# Interface for installing RabbitMQ on the system
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +09005
6# Dependencies:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01007#
8# - ``functions`` file
Abhishek Chandad5b74c62014-12-12 02:15:55 +05309# - ``RABBIT_{HOST|PASSWORD|USERID}`` must be defined when RabbitMQ is used
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090010
11# ``stack.sh`` calls the entry points in this order:
12#
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010013# - check_rpc_backend
14# - install_rpc_backend
15# - restart_rpc_backend
Sean Dague37eca482015-06-16 07:19:22 -040016# - iniset_rpc_backend (stable interface)
17#
18# Note: if implementing an out of tree plugin for an RPC backend, you
19# should install all services through normal plugin methods, then
20# redefine ``iniset_rpc_backend`` in your code. That's the one portion
21# of this file which is a standard interface.
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090022
23# Save trace setting
Ian Wienand523f4882015-10-13 11:03:03 +110024_XTRACE_RPC_BACKEND=$(set +o | grep xtrace)
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090025set +o xtrace
26
Dean Troyercc6b4432013-04-08 15:38:03 -050027# Functions
28# ---------
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090029
Dean Troyer995eb922013-03-07 16:11:40 -060030# clean up after rpc backend - eradicate all traces so changing backends
31# produces a clean switch
32function cleanup_rpc_backend {
33 if is_service_enabled rabbit; then
34 # Obliterate rabbitmq-server
35 uninstall_package rabbitmq-server
Sean Dague9a413ab2015-02-04 12:44:18 -050036 # in case it's not actually running, /bin/true at the end
37 sudo killall epmd || sudo killall -9 epmd || /bin/true
Dean Troyer995eb922013-03-07 16:11:40 -060038 if is_ubuntu; then
39 # And the Erlang runtime too
Sahid Orentino Ferdjaouie9648272014-02-23 18:55:51 +010040 apt_get purge -y erlang*
Dean Troyer995eb922013-03-07 16:11:40 -060041 fi
Kenneth Giusti7e58c062014-07-23 16:44:37 -040042 fi
Dean Troyer995eb922013-03-07 16:11:40 -060043}
44
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090045# install rpc backend
Ian Wienandaee18c72014-02-21 15:35:08 +110046function install_rpc_backend {
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090047 if is_service_enabled rabbit; then
48 # Install rabbitmq-server
Ian Wienand7ccf4e02014-07-23 14:24:11 +100049 install_package rabbitmq-server
Kenneth Giustia1875b72014-09-15 14:21:55 -040050 fi
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090051}
52
53# restart the rpc backend
Ian Wienandaee18c72014-02-21 15:35:08 +110054function restart_rpc_backend {
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090055 if is_service_enabled rabbit; then
56 # Start rabbitmq-server
57 echo_summary "Starting RabbitMQ"
Ben Nemecec5918f2014-01-30 16:07:23 +000058 # NOTE(bnemec): Retry initial rabbitmq configuration to deal with
59 # the fact that sometimes it fails to start properly.
Ian Wienand64b56a52014-12-16 09:53:36 +110060 # Reference: https://bugzilla.redhat.com/show_bug.cgi?id=1144100
Atsushi SAKAIfe7b56c2015-11-13 17:06:16 +090061 # NOTE(tonyb): Extend the original retry logic to only restart rabbitmq
Tony Breeds6bc905c2015-05-15 12:51:43 +100062 # every second time around the loop.
63 # See: https://bugs.launchpad.net/devstack/+bug/1449056 for details on
64 # why this is needed. This can bee seen on vivid and Debian unstable
65 # (May 2015)
66 # TODO(tonyb): Remove this when Debian and Ubuntu have a fixed systemd
67 # service file.
Dean Troyer3ef23bc2014-07-25 14:56:22 -050068 local i
Tony Breeds6bc905c2015-05-15 12:51:43 +100069 for i in `seq 20`; do
Ian Wienand64b56a52014-12-16 09:53:36 +110070 local rc=0
71
Tony Breeds6bc905c2015-05-15 12:51:43 +100072 [[ $i -eq "20" ]] && die $LINENO "Failed to set rabbitmq password"
Ian Wienand64b56a52014-12-16 09:53:36 +110073
Tony Breeds6bc905c2015-05-15 12:51:43 +100074 if [[ $(( i % 2 )) == "0" ]] ; then
75 restart_service rabbitmq-server
76 fi
Ian Wienand64b56a52014-12-16 09:53:36 +110077
78 rabbit_setuser "$RABBIT_USERID" "$RABBIT_PASSWORD" || rc=$?
79 if [ $rc -ne 0 ]; then
80 continue
81 fi
82
Ben Nemecec5918f2014-01-30 16:07:23 +000083 # change the rabbit password since the default is "guest"
Ian Wienand64b56a52014-12-16 09:53:36 +110084 sudo rabbitmqctl change_password \
85 $RABBIT_USERID $RABBIT_PASSWORD || rc=$?
86 if [ $rc -ne 0 ]; then
87 continue;
88 fi
89
90 break
Ben Nemecec5918f2014-01-30 16:07:23 +000091 done
Kieran Spearfb2a3ae2013-03-11 23:55:49 +000092 if is_service_enabled n-cell; then
93 # Add partitioned access for the child cell
94 if [ -z `sudo rabbitmqctl list_vhosts | grep child_cell` ]; then
95 sudo rabbitmqctl add_vhost child_cell
Abhishek Chandad5b74c62014-12-12 02:15:55 +053096 sudo rabbitmqctl set_permissions -p child_cell $RABBIT_USERID ".*" ".*" ".*"
Kieran Spearfb2a3ae2013-03-11 23:55:49 +000097 fi
98 fi
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090099 fi
100}
101
gordon chungb6197e62015-02-12 15:33:35 -0500102# builds transport url string
103function get_transport_url {
Sean Dague37eca482015-06-16 07:19:22 -0400104 if is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then
gordon chungb6197e62015-02-12 15:33:35 -0500105 echo "rabbit://$RABBIT_USERID:$RABBIT_PASSWORD@$RABBIT_HOST:5672/"
106 fi
107}
108
Atsushi SAKAIfe7b56c2015-11-13 17:06:16 +0900109# iniset configuration
Ian Wienandaee18c72014-02-21 15:35:08 +1100110function iniset_rpc_backend {
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900111 local package=$1
112 local file=$2
Brant Knudson2dd110c2015-03-14 12:39:14 -0500113 local section=${3:-DEFAULT}
Sean Dague37eca482015-06-16 07:19:22 -0400114 if is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then
Li Ma529f8112015-01-23 03:10:49 -0800115 iniset $file $section rpc_backend "rabbit"
Joe Gordond01ff962015-03-23 15:05:39 -0700116 iniset $file oslo_messaging_rabbit rabbit_hosts $RABBIT_HOST
117 iniset $file oslo_messaging_rabbit rabbit_password $RABBIT_PASSWORD
118 iniset $file oslo_messaging_rabbit rabbit_userid $RABBIT_USERID
Mehdi Abaakouk7cf7a8f2015-04-09 11:46:56 +0200119 if [ -n "$RABBIT_HEARTBEAT_TIMEOUT_THRESHOLD" ]; then
120 iniset $file oslo_messaging_rabbit heartbeat_timeout_threshold $RABBIT_HEARTBEAT_TIMEOUT_THRESHOLD
121 fi
122 if [ -n "$RABBIT_HEARTBEAT_RATE" ]; then
123 iniset $file oslo_messaging_rabbit heartbeat_rate $RABBIT_HEARTBEAT_RATE
124 fi
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900125 fi
126}
127
Abhishek Chandad5b74c62014-12-12 02:15:55 +0530128function rabbit_setuser {
129 local user="$1" pass="$2" found="" out=""
130 out=$(sudo rabbitmqctl list_users) ||
131 { echo "failed to list users" 1>&2; return 1; }
132 found=$(echo "$out" | awk '$1 == user { print $1 }' "user=$user")
133 if [ "$found" = "$user" ]; then
134 sudo rabbitmqctl change_password "$user" "$pass" ||
135 { echo "failed changing pass for '$user'" 1>&2; return 1; }
136 else
137 sudo rabbitmqctl add_user "$user" "$pass" ||
138 { echo "failed changing pass for $user"; return 1; }
139 fi
140 sudo rabbitmqctl set_permissions "$user" ".*" ".*" ".*"
141}
142
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900143# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100144$_XTRACE_RPC_BACKEND
Sean Dague584d90e2013-03-29 14:34:53 -0400145
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100146# Tell emacs to use shell-script-mode
147## Local variables:
148## mode: shell-script
149## End: