blob: 97b1aa409af35cdf05e713a6f7e1c262f8e1bec1 [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}
28
Dean Troyercc6b4432013-04-08 15:38:03 -050029# Functions
30# ---------
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090031
Dean Troyer995eb922013-03-07 16:11:40 -060032# clean up after rpc backend - eradicate all traces so changing backends
33# produces a clean switch
34function cleanup_rpc_backend {
35 if is_service_enabled rabbit; then
36 # Obliterate rabbitmq-server
37 uninstall_package rabbitmq-server
Sean Dague9a413ab2015-02-04 12:44:18 -050038 # in case it's not actually running, /bin/true at the end
39 sudo killall epmd || sudo killall -9 epmd || /bin/true
Dean Troyer995eb922013-03-07 16:11:40 -060040 if is_ubuntu; then
41 # And the Erlang runtime too
Sahid Orentino Ferdjaouie9648272014-02-23 18:55:51 +010042 apt_get purge -y erlang*
Dean Troyer995eb922013-03-07 16:11:40 -060043 fi
Kenneth Giusti7e58c062014-07-23 16:44:37 -040044 fi
Dean Troyer995eb922013-03-07 16:11:40 -060045}
46
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090047# install rpc backend
Ian Wienandaee18c72014-02-21 15:35:08 +110048function install_rpc_backend {
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090049 if is_service_enabled rabbit; then
50 # Install rabbitmq-server
Ian Wienand7ccf4e02014-07-23 14:24:11 +100051 install_package rabbitmq-server
Kyle Mestery75633262016-01-07 16:46:37 -060052 if is_fedora; then
53 sudo systemctl enable rabbitmq-server
54 fi
Zhang Jinnan4d8c03a2015-08-20 10:00:20 -040055 fi
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090056}
57
58# restart the rpc backend
Ian Wienandaee18c72014-02-21 15:35:08 +110059function restart_rpc_backend {
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090060 if is_service_enabled rabbit; then
61 # Start rabbitmq-server
62 echo_summary "Starting RabbitMQ"
Ben Nemecec5918f2014-01-30 16:07:23 +000063 # NOTE(bnemec): Retry initial rabbitmq configuration to deal with
64 # the fact that sometimes it fails to start properly.
Ian Wienand64b56a52014-12-16 09:53:36 +110065 # Reference: https://bugzilla.redhat.com/show_bug.cgi?id=1144100
Atsushi SAKAIfe7b56c2015-11-13 17:06:16 +090066 # NOTE(tonyb): Extend the original retry logic to only restart rabbitmq
Tony Breeds6bc905c2015-05-15 12:51:43 +100067 # every second time around the loop.
68 # See: https://bugs.launchpad.net/devstack/+bug/1449056 for details on
69 # why this is needed. This can bee seen on vivid and Debian unstable
70 # (May 2015)
71 # TODO(tonyb): Remove this when Debian and Ubuntu have a fixed systemd
72 # service file.
Dean Troyer3ef23bc2014-07-25 14:56:22 -050073 local i
Tony Breeds6bc905c2015-05-15 12:51:43 +100074 for i in `seq 20`; do
Ian Wienand64b56a52014-12-16 09:53:36 +110075 local rc=0
76
Tony Breeds6bc905c2015-05-15 12:51:43 +100077 [[ $i -eq "20" ]] && die $LINENO "Failed to set rabbitmq password"
Ian Wienand64b56a52014-12-16 09:53:36 +110078
Tony Breeds6bc905c2015-05-15 12:51:43 +100079 if [[ $(( i % 2 )) == "0" ]] ; then
80 restart_service rabbitmq-server
81 fi
Ian Wienand64b56a52014-12-16 09:53:36 +110082
83 rabbit_setuser "$RABBIT_USERID" "$RABBIT_PASSWORD" || rc=$?
84 if [ $rc -ne 0 ]; then
85 continue
86 fi
87
Ben Nemecec5918f2014-01-30 16:07:23 +000088 # change the rabbit password since the default is "guest"
Ian Wienand64b56a52014-12-16 09:53:36 +110089 sudo rabbitmqctl change_password \
90 $RABBIT_USERID $RABBIT_PASSWORD || rc=$?
91 if [ $rc -ne 0 ]; then
92 continue;
93 fi
94
95 break
Ben Nemecec5918f2014-01-30 16:07:23 +000096 done
Kieran Spearfb2a3ae2013-03-11 23:55:49 +000097 if is_service_enabled n-cell; then
98 # Add partitioned access for the child cell
99 if [ -z `sudo rabbitmqctl list_vhosts | grep child_cell` ]; then
100 sudo rabbitmqctl add_vhost child_cell
Abhishek Chandad5b74c62014-12-12 02:15:55 +0530101 sudo rabbitmqctl set_permissions -p child_cell $RABBIT_USERID ".*" ".*" ".*"
Kieran Spearfb2a3ae2013-03-11 23:55:49 +0000102 fi
103 fi
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900104 fi
105}
106
gordon chungb6197e62015-02-12 15:33:35 -0500107# builds transport url string
108function get_transport_url {
Mehdi Abaakouk6176ae62016-05-18 12:10:08 +0200109 local virtual_host=$1
Sean Dague37eca482015-06-16 07:19:22 -0400110 if is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then
Mehdi Abaakouk6176ae62016-05-18 12:10:08 +0200111 echo "rabbit://$RABBIT_USERID:$RABBIT_PASSWORD@$RABBIT_HOST:5672/$virtual_host"
gordon chungb6197e62015-02-12 15:33:35 -0500112 fi
113}
114
Atsushi SAKAIfe7b56c2015-11-13 17:06:16 +0900115# iniset configuration
Ian Wienandaee18c72014-02-21 15:35:08 +1100116function iniset_rpc_backend {
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900117 local package=$1
118 local file=$2
Brant Knudson2dd110c2015-03-14 12:39:14 -0500119 local section=${3:-DEFAULT}
Mehdi Abaakouk6176ae62016-05-18 12:10:08 +0200120 local virtual_host=$4
Sean Dague37eca482015-06-16 07:19:22 -0400121 if is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then
Mehdi Abaakouk6176ae62016-05-18 12:10:08 +0200122 iniset $file $section transport_url $(get_transport_url "$virtual_host")
Mehdi Abaakouk7cf7a8f2015-04-09 11:46:56 +0200123 if [ -n "$RABBIT_HEARTBEAT_TIMEOUT_THRESHOLD" ]; then
124 iniset $file oslo_messaging_rabbit heartbeat_timeout_threshold $RABBIT_HEARTBEAT_TIMEOUT_THRESHOLD
125 fi
126 if [ -n "$RABBIT_HEARTBEAT_RATE" ]; then
127 iniset $file oslo_messaging_rabbit heartbeat_rate $RABBIT_HEARTBEAT_RATE
128 fi
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900129 fi
130}
131
Abhishek Chandad5b74c62014-12-12 02:15:55 +0530132function rabbit_setuser {
133 local user="$1" pass="$2" found="" out=""
134 out=$(sudo rabbitmqctl list_users) ||
135 { echo "failed to list users" 1>&2; return 1; }
136 found=$(echo "$out" | awk '$1 == user { print $1 }' "user=$user")
137 if [ "$found" = "$user" ]; then
138 sudo rabbitmqctl change_password "$user" "$pass" ||
139 { echo "failed changing pass for '$user'" 1>&2; return 1; }
140 else
141 sudo rabbitmqctl add_user "$user" "$pass" ||
142 { echo "failed changing pass for $user"; return 1; }
143 fi
144 sudo rabbitmqctl set_permissions "$user" ".*" ".*" ".*"
145}
146
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900147# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100148$_XTRACE_RPC_BACKEND
Sean Dague584d90e2013-03-29 14:34:53 -0400149
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100150# Tell emacs to use shell-script-mode
151## Local variables:
152## mode: shell-script
153## End: