blob: 1d68f8a185e2ddc6a707455b156b5fd070f455c2 [file] [log] [blame]
Chris Dent4d601752016-07-12 19:34:09 +00001#!/bin/bash
2#
3# lib/placement
4# Functions to control the configuration and operation of the **Placement** service
5#
6# Currently the placement service is embedded in nova. Eventually we
7# expect this to change so this file is started as a separate entity
8# despite making use of some *NOVA* variables and files.
9
10# Dependencies:
11#
12# - ``functions`` file
13# - ``DEST``, ``DATA_DIR``, ``STACK_USER`` must be defined
14# - ``FILES``
15
16# ``stack.sh`` calls the entry points in this order:
17#
18# - install_placement
19# - cleanup_placement
20# - configure_placement
21# - init_placement
22# - start_placement
23# - stop_placement
24
25# Save trace setting
26_XTRACE_LIB_PLACEMENT=$(set +o | grep xtrace)
27set +o xtrace
28
29# Defaults
30# --------
31
32PLACEMENT_CONF_DIR=/etc/nova
33PLACEMENT_CONF=$PLACEMENT_CONF_DIR/nova.conf
34PLACEMENT_AUTH_STRATEGY=${PLACEMENT_AUTH_STRATEGY:-placement}
Sean Dague64ffff92017-04-13 13:36:42 -040035# Nova virtual environment
36if [[ ${USE_VENV} = True ]]; then
37 PROJECT_VENV["nova"]=${NOVA_DIR}.venv
38 PLACEMENT_BIN_DIR=${PROJECT_VENV["nova"]}/bin
39else
40 PLACEMENT_BIN_DIR=$(get_python_exec_prefix)
41fi
42PLACEMENT_UWSGI=$PLACEMENT_BIN_DIR/nova-placement-api
43PLACEMENT_UWSGI_CONF=$PLACEMENT_CONF_DIR/placement-uwsgi.ini
Chris Dent4d601752016-07-12 19:34:09 +000044
45# The placement service can optionally use a separate database
46# connection. Set PLACEMENT_DB_ENABLED to True to use it.
47# NOTE(cdent): This functionality depends on some code that is not
48# yet merged in nova but is coming soon.
49PLACEMENT_DB_ENABLED=$(trueorfalse False PLACEMENT_DB_ENABLED)
50
Sean Daguef3b2f4c2017-04-13 10:11:48 -040051if is_service_enabled tls-proxy; then
Chris Dent4d601752016-07-12 19:34:09 +000052 PLACEMENT_SERVICE_PROTOCOL="https"
53fi
54
55# Public facing bits
56PLACEMENT_SERVICE_PROTOCOL=${PLACEMENT_SERVICE_PROTOCOL:-$SERVICE_PROTOCOL}
57PLACEMENT_SERVICE_HOST=${PLACEMENT_SERVICE_HOST:-$SERVICE_HOST}
Chris Dent4d601752016-07-12 19:34:09 +000058
59# Functions
60# ---------
61
62# Test if any placement services are enabled
63# is_placement_enabled
64function is_placement_enabled {
Sean Dague51a225c2016-12-15 16:32:08 -050065 [[ ,${ENABLED_SERVICES} =~ ,"placement-api" ]] && return 0
Chris Dent4d601752016-07-12 19:34:09 +000066 return 1
67}
68
69# cleanup_placement() - Remove residual data files, anything left over from previous
70# runs that a clean run would need to clean up
71function cleanup_placement {
Sean Dague803acff2017-05-01 10:52:38 -040072 sudo rm -f $(apache_site_config_for nova-placement-api)
Chris Dent4d601752016-07-12 19:34:09 +000073 sudo rm -f $(apache_site_config_for placement-api)
Chris Dent1489b9e2017-12-05 23:46:58 +000074 remove_uwsgi_config "$PLACEMENT_UWSGI_CONF" "$PLACEMENT_UWSGI"
Chris Dent4d601752016-07-12 19:34:09 +000075}
76
77# _config_placement_apache_wsgi() - Set WSGI config files
78function _config_placement_apache_wsgi {
Chris Dent4d601752016-07-12 19:34:09 +000079 local placement_api_apache_conf
Chris Dent4d601752016-07-12 19:34:09 +000080 local venv_path=""
Sean Dague43ff27b2016-08-30 21:13:15 -040081 local nova_bin_dir=""
82 nova_bin_dir=$(get_python_exec_prefix)
Chris Dent4d601752016-07-12 19:34:09 +000083 placement_api_apache_conf=$(apache_site_config_for placement-api)
84
Chris Dent4d601752016-07-12 19:34:09 +000085 # reuse nova's venv if there is one as placement code lives
86 # there
87 if [[ ${USE_VENV} = True ]]; then
88 venv_path="python-path=${PROJECT_VENV["nova"]}/lib/$(python_version)/site-packages"
Sean Dague43ff27b2016-08-30 21:13:15 -040089 nova_bin_dir=${PROJECT_VENV["nova"]}/bin
Chris Dent4d601752016-07-12 19:34:09 +000090 fi
91
Chris Dent4d601752016-07-12 19:34:09 +000092 sudo cp $FILES/apache-placement-api.template $placement_api_apache_conf
93 sudo sed -e "
Chris Dent4d601752016-07-12 19:34:09 +000094 s|%APACHE_NAME%|$APACHE_NAME|g;
Sean Dague43ff27b2016-08-30 21:13:15 -040095 s|%PUBLICWSGI%|$nova_bin_dir/nova-placement-api|g;
Chris Dent4d601752016-07-12 19:34:09 +000096 s|%SSLENGINE%|$placement_ssl|g;
97 s|%SSLCERTFILE%|$placement_certfile|g;
98 s|%SSLKEYFILE%|$placement_keyfile|g;
99 s|%USER%|$STACK_USER|g;
100 s|%VIRTUALENV%|$venv_path|g
101 s|%APIWORKERS%|$API_WORKERS|g
102 " -i $placement_api_apache_conf
103}
104
Sean Dague51a225c2016-12-15 16:32:08 -0500105function configure_placement_nova_compute {
Matt Riedemannf6d566c2017-12-22 11:39:29 -0500106 # Use the provided config file path or default to $NOVA_CONF.
107 local conf=${1:-$NOVA_CONF}
108 iniset $conf placement auth_type "password"
109 iniset $conf placement auth_url "$KEYSTONE_SERVICE_URI"
110 iniset $conf placement username placement
111 iniset $conf placement password "$SERVICE_PASSWORD"
112 iniset $conf placement user_domain_name "$SERVICE_DOMAIN_NAME"
113 iniset $conf placement project_name "$SERVICE_TENANT_NAME"
114 iniset $conf placement project_domain_name "$SERVICE_DOMAIN_NAME"
115 iniset $conf placement os_region_name "$REGION_NAME"
Chris Dent4d601752016-07-12 19:34:09 +0000116 # TODO(cdent): auth_strategy, which is common to see in these
117 # blocks is not currently used here. For the time being the
118 # placement api uses the auth_strategy configuration setting
119 # established by the nova api. This avoids, for the time, being,
120 # creating redundant configuration items that are just used for
121 # testing.
Sean Dague51a225c2016-12-15 16:32:08 -0500122}
Chris Dent4d601752016-07-12 19:34:09 +0000123
Sean Dague51a225c2016-12-15 16:32:08 -0500124# configure_placement() - Set config files, create data dirs, etc
125function configure_placement {
126 if [ "$PLACEMENT_DB_ENABLED" != False ]; then
127 iniset $PLACEMENT_CONF placement_database connection `database_connection_url placement`
128 fi
Chris Dente0be9e32017-04-18 16:52:25 +0100129
Sean Dague64ffff92017-04-13 13:36:42 -0400130 if [[ "$WSGI_MODE" == "uwsgi" ]]; then
131 write_uwsgi_config "$PLACEMENT_UWSGI_CONF" "$PLACEMENT_UWSGI" "/placement"
132 else
133 _config_placement_apache_wsgi
134 fi
Chris Dent4d601752016-07-12 19:34:09 +0000135}
136
137# create_placement_accounts() - Set up required placement accounts
138# and service and endpoints.
139function create_placement_accounts {
140 create_service_user "placement" "admin"
141 local placement_api_url="$PLACEMENT_SERVICE_PROTOCOL://$PLACEMENT_SERVICE_HOST/placement"
142 get_or_create_service "placement" "placement" "Placement Service"
143 get_or_create_endpoint \
144 "placement" \
145 "$REGION_NAME" \
Chris Dent4d601752016-07-12 19:34:09 +0000146 "$placement_api_url"
147}
148
149# init_placement() - Create service user and endpoints
150# If PLACEMENT_DB_ENABLED is true, create the separate placement db
151# using, for now, the api_db migrations.
152function init_placement {
153 if [ "$PLACEMENT_DB_ENABLED" != False ]; then
154 recreate_database placement
Clark Boylan633dbc32017-06-14 12:09:21 -0700155 time_start "dbsync"
Chris Dent4d601752016-07-12 19:34:09 +0000156 $NOVA_BIN_DIR/nova-manage --config-file $NOVA_CONF api_db sync
Clark Boylan633dbc32017-06-14 12:09:21 -0700157 time_stop "dbsync"
Chris Dent4d601752016-07-12 19:34:09 +0000158 fi
159 create_placement_accounts
160}
161
162# install_placement() - Collect source and prepare
163function install_placement {
164 install_apache_wsgi
Roman Podoliakaa066abe2017-04-18 16:18:14 +0300165 # Install the openstackclient placement client plugin for CLI
166 # TODO(mriedem): Use pip_install_gr once osc-placement is in g-r.
167 pip_install osc-placement
Chris Dent4d601752016-07-12 19:34:09 +0000168}
169
170# start_placement_api() - Start the API processes ahead of other things
171function start_placement_api {
Sean Dague64ffff92017-04-13 13:36:42 -0400172 if [[ "$WSGI_MODE" == "uwsgi" ]]; then
Davanum Srinivasaceb27e2017-08-17 08:59:59 -0400173 run_process "placement-api" "$PLACEMENT_BIN_DIR/uwsgi --procname-prefix placement --ini $PLACEMENT_UWSGI_CONF"
Sean Dague64ffff92017-04-13 13:36:42 -0400174 else
175 enable_apache_site placement-api
176 restart_apache_server
177 tail_log placement-api /var/log/$APACHE_NAME/placement-api.log
178 fi
Chris Dent4d601752016-07-12 19:34:09 +0000179
180 echo "Waiting for placement-api to start..."
181 if ! wait_for_service $SERVICE_TIMEOUT $PLACEMENT_SERVICE_PROTOCOL://$PLACEMENT_SERVICE_HOST/placement; then
182 die $LINENO "placement-api did not start"
183 fi
184}
185
186function start_placement {
187 start_placement_api
188}
189
190# stop_placement() - Disable the api service and stop it.
191function stop_placement {
Sean Dague64ffff92017-04-13 13:36:42 -0400192 if [[ "$WSGI_MODE" == "uwsgi" ]]; then
193 stop_process "placement-api"
Sean Dague64ffff92017-04-13 13:36:42 -0400194 else
195 disable_apache_site placement-api
196 restart_apache_server
197 fi
Chris Dent4d601752016-07-12 19:34:09 +0000198}
199
200# Restore xtrace
201$_XTRACE_LIB_PLACEMENT
202
203# Tell emacs to use shell-script-mode
204## Local variables:
205## mode: shell-script
206## End: