blob: 14c78fbf7b6bd7cd313bb1fbdd0406e5e9963e16 [file] [log] [blame]
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +09001# lib/rpc_backend
2# Interface for interactig with different rpc backend
3# rpc backend settings
4
5# Dependencies:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01006#
7# - ``functions`` file
8# - ``RABBIT_{HOST|PASSWORD}`` must be defined when RabbitMQ is used
Kenneth Giusti7e58c062014-07-23 16:44:37 -04009# - ``RPC_MESSAGING_PROTOCOL`` option for configuring the messaging protocol
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
16# - iniset_rpc_backend
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090017
18# Save trace setting
19XTRACE=$(set +o | grep xtrace)
20set +o xtrace
21
Dean Troyercc6b4432013-04-08 15:38:03 -050022
23# Functions
24# ---------
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090025
Matthieu Huin7a7a4662013-04-15 17:13:41 +020026
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090027# Make sure we only have one rpc backend enabled.
28# Also check the specified rpc backend is available on your platform.
Ian Wienandaee18c72014-02-21 15:35:08 +110029function check_rpc_backend {
Dean Troyer3ef23bc2014-07-25 14:56:22 -050030 local c svc
31
Matthieu Huin7a7a4662013-04-15 17:13:41 +020032 local rpc_needed=1
33 # We rely on the fact that filenames in lib/* match the service names
34 # that can be passed as arguments to is_service_enabled.
35 # We check for a call to iniset_rpc_backend in these files, meaning
36 # the service needs a backend.
Vishvananda Ishaya78a53d92013-05-09 17:20:31 -070037 rpc_candidates=$(grep -rl iniset_rpc_backend $TOP_DIR/lib/ | awk -F/ '{print $NF}')
Matthieu Huin7a7a4662013-04-15 17:13:41 +020038 for c in ${rpc_candidates}; do
39 if is_service_enabled $c; then
40 rpc_needed=0
41 break
42 fi
43 done
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090044 local rpc_backend_cnt=0
45 for svc in qpid zeromq rabbit; do
46 is_service_enabled $svc &&
Dean Troyerffd17682014-08-02 16:07:03 -050047 (( rpc_backend_cnt++ )) || true
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090048 done
49 if [ "$rpc_backend_cnt" -gt 1 ]; then
50 echo "ERROR: only one rpc backend may be enabled,"
51 echo " set only one of 'rabbit', 'qpid', 'zeromq'"
52 echo " via ENABLED_SERVICES."
Matthieu Huin7a7a4662013-04-15 17:13:41 +020053 elif [ "$rpc_backend_cnt" == 0 ] && [ "$rpc_needed" == 0 ]; then
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090054 echo "ERROR: at least one rpc backend must be enabled,"
55 echo " set one of 'rabbit', 'qpid', 'zeromq'"
56 echo " via ENABLED_SERVICES."
57 fi
58
59 if is_service_enabled qpid && ! qpid_is_supported; then
Nachi Ueno07115eb2013-02-26 12:38:18 -080060 die $LINENO "Qpid support is not available for this version of your distribution."
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +090061 fi
62}
63
Dean Troyer995eb922013-03-07 16:11:40 -060064# clean up after rpc backend - eradicate all traces so changing backends
65# produces a clean switch
66function cleanup_rpc_backend {
67 if is_service_enabled rabbit; then
68 # Obliterate rabbitmq-server
69 uninstall_package rabbitmq-server
DennyZhang557744f2013-10-14 09:50:13 -050070 sudo killall epmd || sudo killall -9 epmd
Dean Troyer995eb922013-03-07 16:11:40 -060071 if is_ubuntu; then
72 # And the Erlang runtime too
Sahid Orentino Ferdjaouie9648272014-02-23 18:55:51 +010073 apt_get purge -y erlang*
Dean Troyer995eb922013-03-07 16:11:40 -060074 fi
75 elif is_service_enabled qpid; then
76 if is_fedora; then
zhhuabj5595fdc2013-05-08 18:27:20 +080077 uninstall_package qpid-cpp-server
Dean Troyer995eb922013-03-07 16:11:40 -060078 elif is_ubuntu; then
79 uninstall_package qpidd
80 else
81 exit_distro_not_supported "qpid installation"
82 fi
83 elif is_service_enabled zeromq; then
84 if is_fedora; then
Eric Windisch800bf382013-05-24 11:21:11 -040085 uninstall_package zeromq python-zmq redis
Dean Troyer995eb922013-03-07 16:11:40 -060086 elif is_ubuntu; then
Eric Windisch800bf382013-05-24 11:21:11 -040087 uninstall_package libzmq1 python-zmq redis-server
Dean Troyer995eb922013-03-07 16:11:40 -060088 elif is_suse; then
Eric Windisch800bf382013-05-24 11:21:11 -040089 uninstall_package libzmq1 python-pyzmq redis
Dean Troyer995eb922013-03-07 16:11:40 -060090 else
91 exit_distro_not_supported "zeromq installation"
92 fi
93 fi
Kenneth Giusti7e58c062014-07-23 16:44:37 -040094
95 # Remove the AMQP 1.0 messaging libraries
96 if [ "$RPC_MESSAGING_PROTOCOL" == "AMQP1" ]; then
97 if is_fedora; then
98 uninstall_package qpid-proton-c-devel
99 uninstall_package python-qpid-proton
100 fi
101 # TODO(kgiusti) ubuntu cleanup
102 fi
Dean Troyer995eb922013-03-07 16:11:40 -0600103}
104
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900105# install rpc backend
Ian Wienandaee18c72014-02-21 15:35:08 +1100106function install_rpc_backend {
Kenneth Giusti7e58c062014-07-23 16:44:37 -0400107 # Regardless of the broker used, if AMQP 1.0 is configured load
108 # the necessary messaging client libraries for oslo.messaging
109 if [ "$RPC_MESSAGING_PROTOCOL" == "AMQP1" ]; then
110 if is_fedora; then
111 install_package qpid-proton-c-devel
112 install_package python-qpid-proton
113 elif is_ubuntu; then
114 # TODO(kgiusti) The QPID AMQP 1.0 protocol libraries
115 # are not yet in the ubuntu repos. Enable these installs
116 # once they are present:
117 #install_package libqpid-proton2-dev
118 #install_package python-qpid-proton
119 # Also add 'uninstall' directives in cleanup_rpc_backend()!
120 exit_distro_not_supported "QPID AMQP 1.0 Proton libraries"
121 else
122 exit_distro_not_supported "QPID AMQP 1.0 Proton libraries"
123 fi
124 # Install pyngus client API
125 # TODO(kgiusti) can remove once python qpid bindings are
126 # available on all supported platforms _and_ pyngus is added
127 # to the requirements.txt file in oslo.messaging
128 pip_install pyngus
129 fi
130
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900131 if is_service_enabled rabbit; then
132 # Install rabbitmq-server
Ian Wienand7ccf4e02014-07-23 14:24:11 +1000133 install_package rabbitmq-server
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900134 elif is_service_enabled qpid; then
135 if is_fedora; then
zhhuabj5595fdc2013-05-08 18:27:20 +0800136 install_package qpid-cpp-server
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900137 elif is_ubuntu; then
138 install_package qpidd
139 else
140 exit_distro_not_supported "qpid installation"
141 fi
Kenneth Giusti062a3c32014-09-30 10:14:08 -0400142 _configure_qpid
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900143 elif is_service_enabled zeromq; then
Eric Windisch800bf382013-05-24 11:21:11 -0400144 # NOTE(ewindisch): Redis is not strictly necessary
145 # but there is a matchmaker driver that works
146 # really well & out of the box for multi-node.
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900147 if is_fedora; then
Eric Windisch800bf382013-05-24 11:21:11 -0400148 install_package zeromq python-zmq redis
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900149 elif is_ubuntu; then
Eric Windisch800bf382013-05-24 11:21:11 -0400150 install_package libzmq1 python-zmq redis-server
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900151 elif is_suse; then
Eric Windisch800bf382013-05-24 11:21:11 -0400152 install_package libzmq1 python-pyzmq redis
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900153 else
154 exit_distro_not_supported "zeromq installation"
155 fi
Vincent Hou93a7a502013-09-27 06:16:54 -0400156 # Necessary directory for socket location.
157 sudo mkdir -p /var/run/openstack
158 sudo chown $STACK_USER /var/run/openstack
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900159 fi
Kenneth Giustia1875b72014-09-15 14:21:55 -0400160
161 # If using the QPID broker, install the QPID python client API
162 if is_service_enabled qpid || [ -n "$QPID_HOST" ]; then
163 install_package python-qpid
164 fi
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900165}
166
167# restart the rpc backend
Ian Wienandaee18c72014-02-21 15:35:08 +1100168function restart_rpc_backend {
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900169 if is_service_enabled rabbit; then
170 # Start rabbitmq-server
171 echo_summary "Starting RabbitMQ"
Ben Nemecec5918f2014-01-30 16:07:23 +0000172 # NOTE(bnemec): Retry initial rabbitmq configuration to deal with
173 # the fact that sometimes it fails to start properly.
174 # Reference: https://bugzilla.redhat.com/show_bug.cgi?id=1059028
Dean Troyer3ef23bc2014-07-25 14:56:22 -0500175 local i
Ben Nemecec5918f2014-01-30 16:07:23 +0000176 for i in `seq 10`; do
177 if is_fedora || is_suse; then
178 # service is not started by default
179 restart_service rabbitmq-server
180 fi
181 # change the rabbit password since the default is "guest"
182 sudo rabbitmqctl change_password guest $RABBIT_PASSWORD && break
183 [[ $i -eq "10" ]] && die $LINENO "Failed to set rabbitmq password"
184 done
Kieran Spearfb2a3ae2013-03-11 23:55:49 +0000185 if is_service_enabled n-cell; then
186 # Add partitioned access for the child cell
187 if [ -z `sudo rabbitmqctl list_vhosts | grep child_cell` ]; then
188 sudo rabbitmqctl add_vhost child_cell
189 sudo rabbitmqctl set_permissions -p child_cell guest ".*" ".*" ".*"
190 fi
191 fi
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900192 elif is_service_enabled qpid; then
193 echo_summary "Starting qpid"
194 restart_service qpidd
195 fi
196}
197
198# iniset cofiguration
Ian Wienandaee18c72014-02-21 15:35:08 +1100199function iniset_rpc_backend {
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900200 local package=$1
201 local file=$2
202 local section=$3
203 if is_service_enabled zeromq; then
204 iniset $file $section rpc_backend ${package}.openstack.common.rpc.impl_zmq
Eric Windisch800bf382013-05-24 11:21:11 -0400205 iniset $file $section rpc_zmq_matchmaker \
206 ${package}.openstack.common.rpc.matchmaker_redis.MatchMakerRedis
207 # Set MATCHMAKER_REDIS_HOST if running multi-node.
208 MATCHMAKER_REDIS_HOST=${MATCHMAKER_REDIS_HOST:-127.0.0.1}
209 iniset $file matchmaker_redis host $MATCHMAKER_REDIS_HOST
Jason Dillaman056df822013-07-01 08:52:13 -0400210 elif is_service_enabled qpid || [ -n "$QPID_HOST" ]; then
Kenneth Giusti7e58c062014-07-23 16:44:37 -0400211 # For Qpid use the 'amqp' oslo.messaging transport when AMQP 1.0 is used
212 if [ "$RPC_MESSAGING_PROTOCOL" == "AMQP1" ]; then
213 iniset $file $section rpc_backend "amqp"
214 else
215 iniset $file $section rpc_backend ${package}.openstack.common.rpc.impl_qpid
216 fi
Attila Fazekasa3dc3992013-07-11 11:26:35 +0200217 iniset $file $section qpid_hostname ${QPID_HOST:-$SERVICE_HOST}
Kenneth Giusti062a3c32014-09-30 10:14:08 -0400218 if [ -n "$QPID_USERNAME" ]; then
219 iniset $file $section qpid_username $QPID_USERNAME
Eoghan Glynn8c11f562013-03-01 12:09:01 +0000220 iniset $file $section qpid_password $QPID_PASSWORD
Eoghan Glynn8c11f562013-03-01 12:09:01 +0000221 fi
jiajun xu4a30b842013-01-22 11:49:03 +0800222 elif is_service_enabled rabbit || { [ -n "$RABBIT_HOST" ] && [ -n "$RABBIT_PASSWORD" ]; }; then
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900223 iniset $file $section rpc_backend ${package}.openstack.common.rpc.impl_kombu
Nicolas Simonds8f084c62014-02-28 17:01:41 -0800224 iniset $file $section rabbit_hosts $RABBIT_HOST
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900225 iniset $file $section rabbit_password $RABBIT_PASSWORD
226 fi
227}
228
229# Check if qpid can be used on the current distro.
230# qpid_is_supported
Ian Wienandaee18c72014-02-21 15:35:08 +1100231function qpid_is_supported {
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900232 if [[ -z "$DISTRO" ]]; then
233 GetDistro
234 fi
235
Sean Dague2bb483d2014-01-03 09:41:27 -0500236 # Qpid is not in openSUSE
237 ( ! is_suse )
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900238}
239
Kenneth Giusti062a3c32014-09-30 10:14:08 -0400240# Set up the various configuration files used by the qpidd broker
241function _configure_qpid {
242
243 # the location of the configuration files have changed since qpidd 0.14
244 local qpid_conf_file
245 if [ -e /etc/qpid/qpidd.conf ]; then
246 qpid_conf_file=/etc/qpid/qpidd.conf
247 elif [ -e /etc/qpidd.conf ]; then
248 qpid_conf_file=/etc/qpidd.conf
249 else
250 exit_distro_not_supported "qpidd.conf file not found!"
251 fi
252
253 # force the ACL file to a known location
254 local qpid_acl_file=/etc/qpid/qpidd.acl
255 if [ ! -e $qpid_acl_file ]; then
256 sudo mkdir -p -m 755 `dirname $qpid_acl_file`
257 sudo touch $qpid_acl_file
258 sudo chmod o+r $qpid_acl_file
259 fi
260 sudo sed -i.bak '/^acl-file=/d' $qpid_conf_file
261 echo "acl-file=$qpid_acl_file" | sudo tee --append $qpid_conf_file
262
263 sudo sed -i '/^auth=/d' $qpid_conf_file
264 if [ -z "$QPID_USERNAME" ]; then
265 # no QPID user configured, so disable authentication
266 # and access control
267 echo "auth=no" | sudo tee --append $qpid_conf_file
268 cat <<EOF | sudo tee $qpid_acl_file
269acl allow all all
270EOF
271 else
272 # Configure qpidd to use PLAIN authentication, and add
273 # QPID_USERNAME to the ACL:
274 echo "auth=yes" | sudo tee --append $qpid_conf_file
275 if [ -z "$QPID_PASSWORD" ]; then
276 read_password QPID_PASSWORD "ENTER A PASSWORD FOR QPID USER $QPID_USERNAME"
277 fi
278 # Create ACL to allow $QPID_USERNAME full access
279 cat <<EOF | sudo tee $qpid_acl_file
280group admin ${QPID_USERNAME}@QPID
281acl allow admin all
282acl deny all all
283EOF
284 # Add user to SASL database
285 if is_ubuntu; then
286 install_package sasl2-bin
287 elif is_fedora; then
288 install_package cyrus-sasl-lib
289 fi
290 local sasl_conf_file=/etc/sasl2/qpidd.conf
291 sudo sed -i.bak '/PLAIN/!s/mech_list: /mech_list: PLAIN /' $sasl_conf_file
292 local sasl_db=`sudo grep sasldb_path $sasl_conf_file | cut -f 2 -d ":" | tr -d [:blank:]`
293 if [ ! -e $sasl_db ]; then
294 sudo mkdir -p -m 755 `dirname $sasl_db`
295 fi
296 echo $QPID_PASSWORD | sudo saslpasswd2 -c -p -f $sasl_db -u QPID $QPID_USERNAME
297 sudo chmod o+r $sasl_db
298 fi
299
300 # If AMQP 1.0 is specified, ensure that the version of the
301 # broker can support AMQP 1.0 and configure the queue and
302 # topic address patterns used by oslo.messaging.
303 if [ "$RPC_MESSAGING_PROTOCOL" == "AMQP1" ]; then
304 QPIDD=$(type -p qpidd)
305 if ! $QPIDD --help | grep -q "queue-patterns"; then
306 exit_distro_not_supported "qpidd with AMQP 1.0 support"
307 fi
308 if ! grep -q "queue-patterns=exclusive" $qpid_conf_file; then
309 cat <<EOF | sudo tee --append $qpid_conf_file
310queue-patterns=exclusive
311queue-patterns=unicast
312topic-patterns=broadcast
313EOF
314 fi
315 fi
316}
Dean Troyercc6b4432013-04-08 15:38:03 -0500317
Akihiro MOTOKIb0f1c382013-01-13 17:58:12 +0900318# Restore xtrace
319$XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400320
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100321# Tell emacs to use shell-script-mode
322## Local variables:
323## mode: shell-script
324## End: