| Sean Dague | e263c82 | 2014-12-05 14:25:28 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
|  | 2 | # | 
| Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 3 | # lib/rpc_backend | 
| Sean Dague | 37eca48 | 2015-06-16 07:19:22 -0400 | [diff] [blame] | 4 | # Interface for installing RabbitMQ on the system | 
| Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 5 |  | 
|  | 6 | # Dependencies: | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 7 | # | 
|  | 8 | # - ``functions`` file | 
| Abhishek Chanda | d5b74c6 | 2014-12-12 02:15:55 +0530 | [diff] [blame] | 9 | # - ``RABBIT_{HOST|PASSWORD|USERID}`` must be defined when RabbitMQ is used | 
| Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 10 |  | 
|  | 11 | # ``stack.sh`` calls the entry points in this order: | 
|  | 12 | # | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 13 | # - check_rpc_backend | 
|  | 14 | # - install_rpc_backend | 
|  | 15 | # - restart_rpc_backend | 
| Sean Dague | 37eca48 | 2015-06-16 07:19:22 -0400 | [diff] [blame] | 16 | # - 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 MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 22 |  | 
|  | 23 | # Save trace setting | 
| Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 24 | _XTRACE_RPC_BACKEND=$(set +o | grep xtrace) | 
| Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 25 | set +o xtrace | 
|  | 26 |  | 
| melanie witt | 0bf2550 | 2016-08-30 22:14:04 +0000 | [diff] [blame] | 27 | RABBIT_USERID=${RABBIT_USERID:-stackrabbit} | 
| Pawel Koniszewski | 20eb274 | 2016-12-21 13:27:09 +0100 | [diff] [blame] | 28 | if is_service_enabled rabbit; then | 
|  | 29 | RABBIT_HOST=${RABBIT_HOST:-$SERVICE_HOST} | 
|  | 30 | fi | 
| melanie witt | 0bf2550 | 2016-08-30 22:14:04 +0000 | [diff] [blame] | 31 |  | 
| Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 32 | # Functions | 
|  | 33 | # --------- | 
| Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 34 |  | 
| Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 35 | # clean up after rpc backend - eradicate all traces so changing backends | 
|  | 36 | # produces a clean switch | 
|  | 37 | function cleanup_rpc_backend { | 
|  | 38 | if is_service_enabled rabbit; then | 
|  | 39 | # Obliterate rabbitmq-server | 
|  | 40 | uninstall_package rabbitmq-server | 
| Sean Dague | 9a413ab | 2015-02-04 12:44:18 -0500 | [diff] [blame] | 41 | # in case it's not actually running, /bin/true at the end | 
|  | 42 | sudo killall epmd || sudo killall -9 epmd || /bin/true | 
| Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 43 | if is_ubuntu; then | 
|  | 44 | # And the Erlang runtime too | 
| Sahid Orentino Ferdjaoui | e964827 | 2014-02-23 18:55:51 +0100 | [diff] [blame] | 45 | apt_get purge -y erlang* | 
| Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 46 | fi | 
| Kenneth Giusti | 7e58c06 | 2014-07-23 16:44:37 -0400 | [diff] [blame] | 47 | fi | 
| Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 48 | } | 
|  | 49 |  | 
| Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 50 | # install rpc backend | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 51 | function install_rpc_backend { | 
| Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 52 | if is_service_enabled rabbit; then | 
|  | 53 | # Install rabbitmq-server | 
| Ian Wienand | 7ccf4e0 | 2014-07-23 14:24:11 +1000 | [diff] [blame] | 54 | install_package rabbitmq-server | 
| Martin Kopec | ec07b34 | 2023-01-24 17:38:45 +0100 | [diff] [blame] | 55 | if is_fedora; then | 
| Jan Gutter | 97096e0 | 2019-07-26 17:46:44 +0200 | [diff] [blame] | 56 | # NOTE(jangutter): If rabbitmq is not running (as in a fresh | 
|  | 57 | # install) then rabbit_setuser triggers epmd@0.0.0.0.socket with | 
|  | 58 | # socket activation. This fails the first time and does not get | 
|  | 59 | # cleared. It is benign, but the workaround is to start rabbitmq a | 
|  | 60 | # bit earlier for RPM based distros. | 
|  | 61 | sudo systemctl --now enable rabbitmq-server | 
| Kyle Mestery | 7563326 | 2016-01-07 16:46:37 -0600 | [diff] [blame] | 62 | fi | 
| Zhang Jinnan | 4d8c03a | 2015-08-20 10:00:20 -0400 | [diff] [blame] | 63 | fi | 
| Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
|  | 66 | # restart the rpc backend | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 67 | function restart_rpc_backend { | 
| Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 68 | if is_service_enabled rabbit; then | 
|  | 69 | # Start rabbitmq-server | 
|  | 70 | echo_summary "Starting RabbitMQ" | 
| Ben Nemec | ec5918f | 2014-01-30 16:07:23 +0000 | [diff] [blame] | 71 | # NOTE(bnemec): Retry initial rabbitmq configuration to deal with | 
|  | 72 | # the fact that sometimes it fails to start properly. | 
| Ian Wienand | 64b56a5 | 2014-12-16 09:53:36 +1100 | [diff] [blame] | 73 | # Reference: https://bugzilla.redhat.com/show_bug.cgi?id=1144100 | 
| Atsushi SAKAI | fe7b56c | 2015-11-13 17:06:16 +0900 | [diff] [blame] | 74 | # NOTE(tonyb): Extend the original retry logic to only restart rabbitmq | 
| Tony Breeds | 6bc905c | 2015-05-15 12:51:43 +1000 | [diff] [blame] | 75 | # every second time around the loop. | 
|  | 76 | # See: https://bugs.launchpad.net/devstack/+bug/1449056 for details on | 
|  | 77 | # why this is needed.  This can bee seen on vivid and Debian unstable | 
|  | 78 | # (May 2015) | 
|  | 79 | # TODO(tonyb): Remove this when Debian and Ubuntu have a fixed systemd | 
|  | 80 | # service file. | 
| Dean Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 81 | local i | 
| Tony Breeds | 6bc905c | 2015-05-15 12:51:43 +1000 | [diff] [blame] | 82 | for i in `seq 20`; do | 
| Ian Wienand | 64b56a5 | 2014-12-16 09:53:36 +1100 | [diff] [blame] | 83 | local rc=0 | 
|  | 84 |  | 
| Tony Breeds | 6bc905c | 2015-05-15 12:51:43 +1000 | [diff] [blame] | 85 | [[ $i -eq "20" ]] && die $LINENO "Failed to set rabbitmq password" | 
| Ian Wienand | 64b56a5 | 2014-12-16 09:53:36 +1100 | [diff] [blame] | 86 |  | 
| Tony Breeds | 6bc905c | 2015-05-15 12:51:43 +1000 | [diff] [blame] | 87 | if [[ $(( i % 2 )) == "0" ]] ; then | 
|  | 88 | restart_service rabbitmq-server | 
|  | 89 | fi | 
| Ian Wienand | 64b56a5 | 2014-12-16 09:53:36 +1100 | [diff] [blame] | 90 |  | 
|  | 91 | rabbit_setuser "$RABBIT_USERID" "$RABBIT_PASSWORD" || rc=$? | 
|  | 92 | if [ $rc -ne 0 ]; then | 
|  | 93 | continue | 
|  | 94 | fi | 
|  | 95 |  | 
| Ben Nemec | ec5918f | 2014-01-30 16:07:23 +0000 | [diff] [blame] | 96 | # change the rabbit password since the default is "guest" | 
| Ian Wienand | 64b56a5 | 2014-12-16 09:53:36 +1100 | [diff] [blame] | 97 | sudo rabbitmqctl change_password \ | 
|  | 98 | $RABBIT_USERID $RABBIT_PASSWORD || rc=$? | 
|  | 99 | if [ $rc -ne 0 ]; then | 
|  | 100 | continue; | 
|  | 101 | fi | 
|  | 102 |  | 
|  | 103 | break | 
| Ben Nemec | ec5918f | 2014-01-30 16:07:23 +0000 | [diff] [blame] | 104 | done | 
| Jens Harbott | 81f67fd | 2017-08-29 09:52:58 +0000 | [diff] [blame] | 105 | # NOTE(frickler): Remove the default guest user | 
|  | 106 | sudo rabbitmqctl delete_user guest || true | 
| Dan Smith | 6f0205b | 2017-02-22 05:59:30 -0800 | [diff] [blame] | 107 | fi | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | # adds a vhost to the rpc backend | 
|  | 111 | function rpc_backend_add_vhost { | 
|  | 112 | local vhost="$1" | 
|  | 113 | if is_service_enabled rabbit; then | 
|  | 114 | if [ -z `sudo rabbitmqctl list_vhosts | grep $vhost` ]; then | 
|  | 115 | sudo rabbitmqctl add_vhost $vhost | 
|  | 116 | sudo rabbitmqctl set_permissions -p $vhost $RABBIT_USERID ".*" ".*" ".*" | 
| Kieran Spear | fb2a3ae | 2013-03-11 23:55:49 +0000 | [diff] [blame] | 117 | fi | 
| Dan Smith | 6f0205b | 2017-02-22 05:59:30 -0800 | [diff] [blame] | 118 | else | 
|  | 119 | echo 'RPC backend does not support vhosts' | 
|  | 120 | return 1 | 
| Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 121 | fi | 
|  | 122 | } | 
|  | 123 |  | 
| Kenneth Giusti | b645904 | 2017-08-04 18:08:37 -0400 | [diff] [blame] | 124 | # Returns the address of the RPC backend in URL format. | 
| gordon chung | b6197e6 | 2015-02-12 15:33:35 -0500 | [diff] [blame] | 125 | function get_transport_url { | 
| Mehdi Abaakouk | 6176ae6 | 2016-05-18 12:10:08 +0200 | [diff] [blame] | 126 | local virtual_host=$1 | 
| Sean Dague | 37eca48 | 2015-06-16 07:19:22 -0400 | [diff] [blame] | 127 | if is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then | 
| Mehdi Abaakouk | 6176ae6 | 2016-05-18 12:10:08 +0200 | [diff] [blame] | 128 | echo "rabbit://$RABBIT_USERID:$RABBIT_PASSWORD@$RABBIT_HOST:5672/$virtual_host" | 
| gordon chung | b6197e6 | 2015-02-12 15:33:35 -0500 | [diff] [blame] | 129 | fi | 
|  | 130 | } | 
|  | 131 |  | 
| Kenneth Giusti | b645904 | 2017-08-04 18:08:37 -0400 | [diff] [blame] | 132 | # Returns the address of the Notification backend in URL format.  This | 
|  | 133 | # should be used to set the transport_url option in the | 
|  | 134 | # oslo_messaging_notifications group. | 
| Thomas Herve | 26e431d | 2017-04-13 14:27:35 +0200 | [diff] [blame] | 135 | function get_notification_url { | 
|  | 136 | local virtual_host=$1 | 
|  | 137 | if is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then | 
|  | 138 | echo "rabbit://$RABBIT_USERID:$RABBIT_PASSWORD@$RABBIT_HOST:5672/$virtual_host" | 
|  | 139 | fi | 
|  | 140 | } | 
|  | 141 |  | 
| Atsushi SAKAI | fe7b56c | 2015-11-13 17:06:16 +0900 | [diff] [blame] | 142 | # iniset configuration | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 143 | function iniset_rpc_backend { | 
| Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 144 | local package=$1 | 
|  | 145 | local file=$2 | 
| Brant Knudson | 2dd110c | 2015-03-14 12:39:14 -0500 | [diff] [blame] | 146 | local section=${3:-DEFAULT} | 
| Mehdi Abaakouk | 6176ae6 | 2016-05-18 12:10:08 +0200 | [diff] [blame] | 147 | local virtual_host=$4 | 
| Sean Dague | 37eca48 | 2015-06-16 07:19:22 -0400 | [diff] [blame] | 148 | if is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then | 
| Mehdi Abaakouk | 6176ae6 | 2016-05-18 12:10:08 +0200 | [diff] [blame] | 149 | iniset $file $section transport_url $(get_transport_url "$virtual_host") | 
| Mehdi Abaakouk | 7cf7a8f | 2015-04-09 11:46:56 +0200 | [diff] [blame] | 150 | if [ -n "$RABBIT_HEARTBEAT_TIMEOUT_THRESHOLD" ]; then | 
|  | 151 | iniset $file oslo_messaging_rabbit heartbeat_timeout_threshold $RABBIT_HEARTBEAT_TIMEOUT_THRESHOLD | 
|  | 152 | fi | 
|  | 153 | if [ -n "$RABBIT_HEARTBEAT_RATE" ]; then | 
|  | 154 | iniset $file oslo_messaging_rabbit heartbeat_rate $RABBIT_HEARTBEAT_RATE | 
|  | 155 | fi | 
| Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 156 | fi | 
|  | 157 | } | 
|  | 158 |  | 
| Abhishek Chanda | d5b74c6 | 2014-12-12 02:15:55 +0530 | [diff] [blame] | 159 | function rabbit_setuser { | 
|  | 160 | local user="$1" pass="$2" found="" out="" | 
|  | 161 | out=$(sudo rabbitmqctl list_users) || | 
|  | 162 | { echo "failed to list users" 1>&2; return 1; } | 
|  | 163 | found=$(echo "$out" | awk '$1 == user { print $1 }' "user=$user") | 
|  | 164 | if [ "$found" = "$user" ]; then | 
|  | 165 | sudo rabbitmqctl change_password "$user" "$pass" || | 
|  | 166 | { echo "failed changing pass for '$user'" 1>&2; return 1; } | 
|  | 167 | else | 
|  | 168 | sudo rabbitmqctl add_user "$user" "$pass" || | 
|  | 169 | { echo "failed changing pass for $user"; return 1; } | 
|  | 170 | fi | 
|  | 171 | sudo rabbitmqctl set_permissions "$user" ".*" ".*" ".*" | 
|  | 172 | } | 
|  | 173 |  | 
| Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 174 | # Restore xtrace | 
| Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 175 | $_XTRACE_RPC_BACKEND | 
| Sean Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 176 |  | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 177 | # Tell emacs to use shell-script-mode | 
|  | 178 | ## Local variables: | 
|  | 179 | ## mode: shell-script | 
|  | 180 | ## End: |