blob: 05e303e3e770c8da2215faab4a2ca97b86b5478c [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
Kyle Mestery75633262016-01-07 16:46:37 -060050 if is_fedora; then
51 sudo systemctl enable rabbitmq-server
52 fi
Zhang Jinnan4d8c03a2015-08-20 10:00:20 -040053 fi
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090054}
55
56# restart the rpc backend
Ian Wienandaee18c72014-02-21 15:35:08 +110057function restart_rpc_backend {
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090058 if is_service_enabled rabbit; then
59 # Start rabbitmq-server
60 echo_summary "Starting RabbitMQ"
Ben Nemecec5918f2014-01-30 16:07:23 +000061 # NOTE(bnemec): Retry initial rabbitmq configuration to deal with
62 # the fact that sometimes it fails to start properly.
Ian Wienand64b56a52014-12-16 09:53:36 +110063 # Reference: https://bugzilla.redhat.com/show_bug.cgi?id=1144100
Atsushi SAKAIfe7b56c2015-11-13 17:06:16 +090064 # NOTE(tonyb): Extend the original retry logic to only restart rabbitmq
Tony Breeds6bc905c2015-05-15 12:51:43 +100065 # every second time around the loop.
66 # See: https://bugs.launchpad.net/devstack/+bug/1449056 for details on
67 # why this is needed. This can bee seen on vivid and Debian unstable
68 # (May 2015)
69 # TODO(tonyb): Remove this when Debian and Ubuntu have a fixed systemd
70 # service file.
Dean Troyer3ef23bc2014-07-25 14:56:22 -050071 local i
Tony Breeds6bc905c2015-05-15 12:51:43 +100072 for i in `seq 20`; do
Ian Wienand64b56a52014-12-16 09:53:36 +110073 local rc=0
74
Tony Breeds6bc905c2015-05-15 12:51:43 +100075 [[ $i -eq "20" ]] && die $LINENO "Failed to set rabbitmq password"
Ian Wienand64b56a52014-12-16 09:53:36 +110076
Tony Breeds6bc905c2015-05-15 12:51:43 +100077 if [[ $(( i % 2 )) == "0" ]] ; then
78 restart_service rabbitmq-server
79 fi
Ian Wienand64b56a52014-12-16 09:53:36 +110080
81 rabbit_setuser "$RABBIT_USERID" "$RABBIT_PASSWORD" || rc=$?
82 if [ $rc -ne 0 ]; then
83 continue
84 fi
85
Ben Nemecec5918f2014-01-30 16:07:23 +000086 # change the rabbit password since the default is "guest"
Ian Wienand64b56a52014-12-16 09:53:36 +110087 sudo rabbitmqctl change_password \
88 $RABBIT_USERID $RABBIT_PASSWORD || rc=$?
89 if [ $rc -ne 0 ]; then
90 continue;
91 fi
92
93 break
Ben Nemecec5918f2014-01-30 16:07:23 +000094 done
Kieran Spearfb2a3ae2013-03-11 23:55:49 +000095 if is_service_enabled n-cell; then
96 # Add partitioned access for the child cell
97 if [ -z `sudo rabbitmqctl list_vhosts | grep child_cell` ]; then
98 sudo rabbitmqctl add_vhost child_cell
Abhishek Chandad5b74c62014-12-12 02:15:55 +053099 sudo rabbitmqctl set_permissions -p child_cell $RABBIT_USERID ".*" ".*" ".*"
Kieran Spearfb2a3ae2013-03-11 23:55:49 +0000100 fi
101 fi
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900102 fi
103}
104
gordon chungb6197e62015-02-12 15:33:35 -0500105# builds transport url string
106function get_transport_url {
Sean Dague37eca482015-06-16 07:19:22 -0400107 if is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then
gordon chungb6197e62015-02-12 15:33:35 -0500108 echo "rabbit://$RABBIT_USERID:$RABBIT_PASSWORD@$RABBIT_HOST:5672/"
109 fi
110}
111
Atsushi SAKAIfe7b56c2015-11-13 17:06:16 +0900112# iniset configuration
Ian Wienandaee18c72014-02-21 15:35:08 +1100113function iniset_rpc_backend {
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900114 local package=$1
115 local file=$2
Brant Knudson2dd110c2015-03-14 12:39:14 -0500116 local section=${3:-DEFAULT}
Sean Dague37eca482015-06-16 07:19:22 -0400117 if is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then
Li Ma529f8112015-01-23 03:10:49 -0800118 iniset $file $section rpc_backend "rabbit"
Joe Gordond01ff962015-03-23 15:05:39 -0700119 iniset $file oslo_messaging_rabbit rabbit_hosts $RABBIT_HOST
120 iniset $file oslo_messaging_rabbit rabbit_password $RABBIT_PASSWORD
121 iniset $file oslo_messaging_rabbit rabbit_userid $RABBIT_USERID
Mehdi Abaakouk7cf7a8f2015-04-09 11:46:56 +0200122 if [ -n "$RABBIT_HEARTBEAT_TIMEOUT_THRESHOLD" ]; then
123 iniset $file oslo_messaging_rabbit heartbeat_timeout_threshold $RABBIT_HEARTBEAT_TIMEOUT_THRESHOLD
124 fi
125 if [ -n "$RABBIT_HEARTBEAT_RATE" ]; then
126 iniset $file oslo_messaging_rabbit heartbeat_rate $RABBIT_HEARTBEAT_RATE
127 fi
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900128 fi
129}
130
Abhishek Chandad5b74c62014-12-12 02:15:55 +0530131function rabbit_setuser {
132 local user="$1" pass="$2" found="" out=""
133 out=$(sudo rabbitmqctl list_users) ||
134 { echo "failed to list users" 1>&2; return 1; }
135 found=$(echo "$out" | awk '$1 == user { print $1 }' "user=$user")
136 if [ "$found" = "$user" ]; then
137 sudo rabbitmqctl change_password "$user" "$pass" ||
138 { echo "failed changing pass for '$user'" 1>&2; return 1; }
139 else
140 sudo rabbitmqctl add_user "$user" "$pass" ||
141 { echo "failed changing pass for $user"; return 1; }
142 fi
143 sudo rabbitmqctl set_permissions "$user" ".*" ".*" ".*"
144}
145
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900146# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100147$_XTRACE_RPC_BACKEND
Sean Dague584d90e2013-03-29 14:34:53 -0400148
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100149# Tell emacs to use shell-script-mode
150## Local variables:
151## mode: shell-script
152## End: