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 |
| 24 | XTRACE=$(set +o | grep xtrace) |
| 25 | set +o xtrace |
| 26 | |
Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 27 | # Functions |
| 28 | # --------- |
Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 29 | |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 30 | # clean up after rpc backend - eradicate all traces so changing backends |
| 31 | # produces a clean switch |
| 32 | function cleanup_rpc_backend { |
| 33 | if is_service_enabled rabbit; then |
| 34 | # Obliterate rabbitmq-server |
| 35 | uninstall_package rabbitmq-server |
Sean Dague | 9a413ab | 2015-02-04 12:44:18 -0500 | [diff] [blame] | 36 | # in case it's not actually running, /bin/true at the end |
| 37 | sudo killall epmd || sudo killall -9 epmd || /bin/true |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 38 | if is_ubuntu; then |
| 39 | # And the Erlang runtime too |
Sahid Orentino Ferdjaoui | e964827 | 2014-02-23 18:55:51 +0100 | [diff] [blame] | 40 | apt_get purge -y erlang* |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 41 | fi |
Kenneth Giusti | 7e58c06 | 2014-07-23 16:44:37 -0400 | [diff] [blame] | 42 | fi |
Dean Troyer | 995eb92 | 2013-03-07 16:11:40 -0600 | [diff] [blame] | 43 | } |
| 44 | |
Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 45 | # install rpc backend |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 46 | function install_rpc_backend { |
Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 47 | if is_service_enabled rabbit; then |
| 48 | # Install rabbitmq-server |
Ian Wienand | 7ccf4e0 | 2014-07-23 14:24:11 +1000 | [diff] [blame] | 49 | install_package rabbitmq-server |
Kenneth Giusti | a1875b7 | 2014-09-15 14:21:55 -0400 | [diff] [blame] | 50 | fi |
Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | # restart the rpc backend |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 54 | function restart_rpc_backend { |
Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 55 | if is_service_enabled rabbit; then |
| 56 | # Start rabbitmq-server |
| 57 | echo_summary "Starting RabbitMQ" |
Ben Nemec | ec5918f | 2014-01-30 16:07:23 +0000 | [diff] [blame] | 58 | # NOTE(bnemec): Retry initial rabbitmq configuration to deal with |
| 59 | # the fact that sometimes it fails to start properly. |
Ian Wienand | 64b56a5 | 2014-12-16 09:53:36 +1100 | [diff] [blame] | 60 | # Reference: https://bugzilla.redhat.com/show_bug.cgi?id=1144100 |
Tony Breeds | 6bc905c | 2015-05-15 12:51:43 +1000 | [diff] [blame] | 61 | # NOTE(tonyb): Extend the orginal retry logic to only restart rabbitmq |
| 62 | # 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 Troyer | 3ef23bc | 2014-07-25 14:56:22 -0500 | [diff] [blame] | 68 | local i |
Tony Breeds | 6bc905c | 2015-05-15 12:51:43 +1000 | [diff] [blame] | 69 | for i in `seq 20`; do |
Ian Wienand | 64b56a5 | 2014-12-16 09:53:36 +1100 | [diff] [blame] | 70 | local rc=0 |
| 71 | |
Tony Breeds | 6bc905c | 2015-05-15 12:51:43 +1000 | [diff] [blame] | 72 | [[ $i -eq "20" ]] && die $LINENO "Failed to set rabbitmq password" |
Ian Wienand | 64b56a5 | 2014-12-16 09:53:36 +1100 | [diff] [blame] | 73 | |
Tony Breeds | 6bc905c | 2015-05-15 12:51:43 +1000 | [diff] [blame] | 74 | if [[ $(( i % 2 )) == "0" ]] ; then |
| 75 | restart_service rabbitmq-server |
| 76 | fi |
Ian Wienand | 64b56a5 | 2014-12-16 09:53:36 +1100 | [diff] [blame] | 77 | |
| 78 | rabbit_setuser "$RABBIT_USERID" "$RABBIT_PASSWORD" || rc=$? |
| 79 | if [ $rc -ne 0 ]; then |
| 80 | continue |
| 81 | fi |
| 82 | |
Ben Nemec | ec5918f | 2014-01-30 16:07:23 +0000 | [diff] [blame] | 83 | # change the rabbit password since the default is "guest" |
Ian Wienand | 64b56a5 | 2014-12-16 09:53:36 +1100 | [diff] [blame] | 84 | sudo rabbitmqctl change_password \ |
| 85 | $RABBIT_USERID $RABBIT_PASSWORD || rc=$? |
| 86 | if [ $rc -ne 0 ]; then |
| 87 | continue; |
| 88 | fi |
| 89 | |
| 90 | break |
Ben Nemec | ec5918f | 2014-01-30 16:07:23 +0000 | [diff] [blame] | 91 | done |
Kieran Spear | fb2a3ae | 2013-03-11 23:55:49 +0000 | [diff] [blame] | 92 | 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 Chanda | d5b74c6 | 2014-12-12 02:15:55 +0530 | [diff] [blame] | 96 | sudo rabbitmqctl set_permissions -p child_cell $RABBIT_USERID ".*" ".*" ".*" |
Kieran Spear | fb2a3ae | 2013-03-11 23:55:49 +0000 | [diff] [blame] | 97 | fi |
| 98 | fi |
Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 99 | fi |
| 100 | } |
| 101 | |
gordon chung | b6197e6 | 2015-02-12 15:33:35 -0500 | [diff] [blame] | 102 | # builds transport url string |
| 103 | function get_transport_url { |
Sean Dague | 37eca48 | 2015-06-16 07:19:22 -0400 | [diff] [blame] | 104 | if is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then |
gordon chung | b6197e6 | 2015-02-12 15:33:35 -0500 | [diff] [blame] | 105 | echo "rabbit://$RABBIT_USERID:$RABBIT_PASSWORD@$RABBIT_HOST:5672/" |
| 106 | fi |
| 107 | } |
| 108 | |
Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 109 | # iniset cofiguration |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 110 | function iniset_rpc_backend { |
Akihiro MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 111 | local package=$1 |
| 112 | local file=$2 |
Brant Knudson | 2dd110c | 2015-03-14 12:39:14 -0500 | [diff] [blame] | 113 | local section=${3:-DEFAULT} |
Sean Dague | 37eca48 | 2015-06-16 07:19:22 -0400 | [diff] [blame] | 114 | if is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then |
Li Ma | 529f811 | 2015-01-23 03:10:49 -0800 | [diff] [blame] | 115 | iniset $file $section rpc_backend "rabbit" |
Joe Gordon | d01ff96 | 2015-03-23 15:05:39 -0700 | [diff] [blame] | 116 | 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 Abaakouk | 7cf7a8f | 2015-04-09 11:46:56 +0200 | [diff] [blame] | 119 | 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 MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 125 | fi |
| 126 | } |
| 127 | |
Abhishek Chanda | d5b74c6 | 2014-12-12 02:15:55 +0530 | [diff] [blame] | 128 | function 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 MOTOKI | b0f1c38 | 2013-01-13 17:58:12 +0900 | [diff] [blame] | 143 | # Restore xtrace |
| 144 | $XTRACE |
Sean Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 145 | |
Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 146 | # Tell emacs to use shell-script-mode |
| 147 | ## Local variables: |
| 148 | ## mode: shell-script |
| 149 | ## End: |