blob: 5479db3333bdc984ecff3a99ede821f41e50a533 [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
melanie witt0bf25502016-08-30 22:14:04 +000027RABBIT_USERID=${RABBIT_USERID:-stackrabbit}
Pawel Koniszewski20eb2742016-12-21 13:27:09 +010028if is_service_enabled rabbit; then
29 RABBIT_HOST=${RABBIT_HOST:-$SERVICE_HOST}
30fi
melanie witt0bf25502016-08-30 22:14:04 +000031
Dean Troyercc6b4432013-04-08 15:38:03 -050032# Functions
33# ---------
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090034
Dean Troyer995eb922013-03-07 16:11:40 -060035# clean up after rpc backend - eradicate all traces so changing backends
36# produces a clean switch
37function cleanup_rpc_backend {
38 if is_service_enabled rabbit; then
39 # Obliterate rabbitmq-server
40 uninstall_package rabbitmq-server
Sean Dague9a413ab2015-02-04 12:44:18 -050041 # in case it's not actually running, /bin/true at the end
42 sudo killall epmd || sudo killall -9 epmd || /bin/true
Dean Troyer995eb922013-03-07 16:11:40 -060043 if is_ubuntu; then
44 # And the Erlang runtime too
Sahid Orentino Ferdjaouie9648272014-02-23 18:55:51 +010045 apt_get purge -y erlang*
Dean Troyer995eb922013-03-07 16:11:40 -060046 fi
Kenneth Giusti7e58c062014-07-23 16:44:37 -040047 fi
Dean Troyer995eb922013-03-07 16:11:40 -060048}
49
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090050# install rpc backend
Ian Wienandaee18c72014-02-21 15:35:08 +110051function install_rpc_backend {
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090052 if is_service_enabled rabbit; then
53 # Install rabbitmq-server
Ian Wienand7ccf4e02014-07-23 14:24:11 +100054 install_package rabbitmq-server
Kyle Mestery75633262016-01-07 16:46:37 -060055 if is_fedora; then
56 sudo systemctl enable rabbitmq-server
57 fi
Zhang Jinnan4d8c03a2015-08-20 10:00:20 -040058 fi
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090059}
60
61# restart the rpc backend
Ian Wienandaee18c72014-02-21 15:35:08 +110062function restart_rpc_backend {
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090063 if is_service_enabled rabbit; then
64 # Start rabbitmq-server
65 echo_summary "Starting RabbitMQ"
Ben Nemecec5918f2014-01-30 16:07:23 +000066 # NOTE(bnemec): Retry initial rabbitmq configuration to deal with
67 # the fact that sometimes it fails to start properly.
Ian Wienand64b56a52014-12-16 09:53:36 +110068 # Reference: https://bugzilla.redhat.com/show_bug.cgi?id=1144100
Atsushi SAKAIfe7b56c2015-11-13 17:06:16 +090069 # NOTE(tonyb): Extend the original retry logic to only restart rabbitmq
Tony Breeds6bc905c2015-05-15 12:51:43 +100070 # every second time around the loop.
71 # See: https://bugs.launchpad.net/devstack/+bug/1449056 for details on
72 # why this is needed. This can bee seen on vivid and Debian unstable
73 # (May 2015)
74 # TODO(tonyb): Remove this when Debian and Ubuntu have a fixed systemd
75 # service file.
Dean Troyer3ef23bc2014-07-25 14:56:22 -050076 local i
Tony Breeds6bc905c2015-05-15 12:51:43 +100077 for i in `seq 20`; do
Ian Wienand64b56a52014-12-16 09:53:36 +110078 local rc=0
79
Tony Breeds6bc905c2015-05-15 12:51:43 +100080 [[ $i -eq "20" ]] && die $LINENO "Failed to set rabbitmq password"
Ian Wienand64b56a52014-12-16 09:53:36 +110081
Tony Breeds6bc905c2015-05-15 12:51:43 +100082 if [[ $(( i % 2 )) == "0" ]] ; then
83 restart_service rabbitmq-server
84 fi
Ian Wienand64b56a52014-12-16 09:53:36 +110085
86 rabbit_setuser "$RABBIT_USERID" "$RABBIT_PASSWORD" || rc=$?
87 if [ $rc -ne 0 ]; then
88 continue
89 fi
90
Ben Nemecec5918f2014-01-30 16:07:23 +000091 # change the rabbit password since the default is "guest"
Ian Wienand64b56a52014-12-16 09:53:36 +110092 sudo rabbitmqctl change_password \
93 $RABBIT_USERID $RABBIT_PASSWORD || rc=$?
94 if [ $rc -ne 0 ]; then
95 continue;
96 fi
97
98 break
Ben Nemecec5918f2014-01-30 16:07:23 +000099 done
Jens Harbott81f67fd2017-08-29 09:52:58 +0000100 # NOTE(frickler): Remove the default guest user
101 sudo rabbitmqctl delete_user guest || true
Dan Smith6f0205b2017-02-22 05:59:30 -0800102 fi
103}
104
105# adds a vhost to the rpc backend
106function rpc_backend_add_vhost {
107 local vhost="$1"
108 if is_service_enabled rabbit; then
109 if [ -z `sudo rabbitmqctl list_vhosts | grep $vhost` ]; then
110 sudo rabbitmqctl add_vhost $vhost
111 sudo rabbitmqctl set_permissions -p $vhost $RABBIT_USERID ".*" ".*" ".*"
Kieran Spearfb2a3ae2013-03-11 23:55:49 +0000112 fi
Dan Smith6f0205b2017-02-22 05:59:30 -0800113 else
114 echo 'RPC backend does not support vhosts'
115 return 1
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900116 fi
117}
118
gordon chungb6197e62015-02-12 15:33:35 -0500119# builds transport url string
120function get_transport_url {
Mehdi Abaakouk6176ae62016-05-18 12:10:08 +0200121 local virtual_host=$1
Sean Dague37eca482015-06-16 07:19:22 -0400122 if is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then
Mehdi Abaakouk6176ae62016-05-18 12:10:08 +0200123 echo "rabbit://$RABBIT_USERID:$RABBIT_PASSWORD@$RABBIT_HOST:5672/$virtual_host"
gordon chungb6197e62015-02-12 15:33:35 -0500124 fi
125}
126
Thomas Herve26e431d2017-04-13 14:27:35 +0200127# Repeat the definition, in case get_transport_url is overriden for RPC purpose.
128# get_notification_url can then be used to talk to rabbit for notifications.
129function get_notification_url {
130 local virtual_host=$1
131 if is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then
132 echo "rabbit://$RABBIT_USERID:$RABBIT_PASSWORD@$RABBIT_HOST:5672/$virtual_host"
133 fi
134}
135
Atsushi SAKAIfe7b56c2015-11-13 17:06:16 +0900136# iniset configuration
Ian Wienandaee18c72014-02-21 15:35:08 +1100137function iniset_rpc_backend {
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900138 local package=$1
139 local file=$2
Brant Knudson2dd110c2015-03-14 12:39:14 -0500140 local section=${3:-DEFAULT}
Mehdi Abaakouk6176ae62016-05-18 12:10:08 +0200141 local virtual_host=$4
Sean Dague37eca482015-06-16 07:19:22 -0400142 if is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then
Mehdi Abaakouk6176ae62016-05-18 12:10:08 +0200143 iniset $file $section transport_url $(get_transport_url "$virtual_host")
Mehdi Abaakouk7cf7a8f2015-04-09 11:46:56 +0200144 if [ -n "$RABBIT_HEARTBEAT_TIMEOUT_THRESHOLD" ]; then
145 iniset $file oslo_messaging_rabbit heartbeat_timeout_threshold $RABBIT_HEARTBEAT_TIMEOUT_THRESHOLD
146 fi
147 if [ -n "$RABBIT_HEARTBEAT_RATE" ]; then
148 iniset $file oslo_messaging_rabbit heartbeat_rate $RABBIT_HEARTBEAT_RATE
149 fi
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900150 fi
151}
152
Abhishek Chandad5b74c62014-12-12 02:15:55 +0530153function rabbit_setuser {
154 local user="$1" pass="$2" found="" out=""
155 out=$(sudo rabbitmqctl list_users) ||
156 { echo "failed to list users" 1>&2; return 1; }
157 found=$(echo "$out" | awk '$1 == user { print $1 }' "user=$user")
158 if [ "$found" = "$user" ]; then
159 sudo rabbitmqctl change_password "$user" "$pass" ||
160 { echo "failed changing pass for '$user'" 1>&2; return 1; }
161 else
162 sudo rabbitmqctl add_user "$user" "$pass" ||
163 { echo "failed changing pass for $user"; return 1; }
164 fi
165 sudo rabbitmqctl set_permissions "$user" ".*" ".*" ".*"
166}
167
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900168# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100169$_XTRACE_RPC_BACKEND
Sean Dague584d90e2013-03-29 14:34:53 -0400170
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100171# Tell emacs to use shell-script-mode
172## Local variables:
173## mode: shell-script
174## End: