blob: a89cd26939a34f5530cf8f5bf194fb328ffd264a [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#
Chris Dent4d601752016-07-12 19:34:09 +00006
7# Dependencies:
8#
9# - ``functions`` file
10# - ``DEST``, ``DATA_DIR``, ``STACK_USER`` must be defined
11# - ``FILES``
12
13# ``stack.sh`` calls the entry points in this order:
14#
15# - install_placement
16# - cleanup_placement
17# - configure_placement
18# - init_placement
19# - start_placement
20# - stop_placement
21
22# Save trace setting
23_XTRACE_LIB_PLACEMENT=$(set +o | grep xtrace)
24set +o xtrace
25
26# Defaults
27# --------
28
Chris Dent78a564b2018-10-05 10:17:56 +010029PLACEMENT_DIR=$DEST/placement
30PLACEMENT_CONF_DIR=/etc/placement
31PLACEMENT_CONF=$PLACEMENT_CONF_DIR/placement.conf
32PLACEMENT_AUTH_CACHE_DIR=${PLACEMENT_AUTH_CACHE_DIR:-/var/cache/placement}
33PLACEMENT_AUTH_STRATEGY=${PLACEMENT_AUTH_STRATEGY:-keystone}
34# Placement virtual environment
Sean Dague64ffff92017-04-13 13:36:42 -040035if [[ ${USE_VENV} = True ]]; then
Chris Dent78a564b2018-10-05 10:17:56 +010036 PROJECT_VENV["placement"]=${PLACEMENT_DIR}.venv
37 PLACEMENT_BIN_DIR=${PROJECT_VENV["placement"]}/bin
Sean Dague64ffff92017-04-13 13:36:42 -040038else
39 PLACEMENT_BIN_DIR=$(get_python_exec_prefix)
40fi
Chris Dent78a564b2018-10-05 10:17:56 +010041PLACEMENT_UWSGI=$PLACEMENT_BIN_DIR/placement-api
Sean Dague64ffff92017-04-13 13:36:42 -040042PLACEMENT_UWSGI_CONF=$PLACEMENT_CONF_DIR/placement-uwsgi.ini
Chris Dent4d601752016-07-12 19:34:09 +000043
Sean Daguef3b2f4c2017-04-13 10:11:48 -040044if is_service_enabled tls-proxy; then
Chris Dent4d601752016-07-12 19:34:09 +000045 PLACEMENT_SERVICE_PROTOCOL="https"
46fi
47
48# Public facing bits
49PLACEMENT_SERVICE_PROTOCOL=${PLACEMENT_SERVICE_PROTOCOL:-$SERVICE_PROTOCOL}
50PLACEMENT_SERVICE_HOST=${PLACEMENT_SERVICE_HOST:-$SERVICE_HOST}
Chris Dent4d601752016-07-12 19:34:09 +000051
52# Functions
53# ---------
54
55# Test if any placement services are enabled
56# is_placement_enabled
57function is_placement_enabled {
Sean Dague51a225c2016-12-15 16:32:08 -050058 [[ ,${ENABLED_SERVICES} =~ ,"placement-api" ]] && return 0
Chris Dent4d601752016-07-12 19:34:09 +000059 return 1
60}
61
62# cleanup_placement() - Remove residual data files, anything left over from previous
63# runs that a clean run would need to clean up
64function cleanup_placement {
65 sudo rm -f $(apache_site_config_for placement-api)
Chris Dent1489b9e2017-12-05 23:46:58 +000066 remove_uwsgi_config "$PLACEMENT_UWSGI_CONF" "$PLACEMENT_UWSGI"
Chris Dent78a564b2018-10-05 10:17:56 +010067 sudo rm -f $PLACEMENT_AUTH_CACHE_DIR/*
Chris Dent4d601752016-07-12 19:34:09 +000068}
69
70# _config_placement_apache_wsgi() - Set WSGI config files
71function _config_placement_apache_wsgi {
Chris Dent4d601752016-07-12 19:34:09 +000072 local placement_api_apache_conf
Chris Dent4d601752016-07-12 19:34:09 +000073 local venv_path=""
Chris Dent78a564b2018-10-05 10:17:56 +010074 local placement_bin_dir=""
75 placement_bin_dir=$(get_python_exec_prefix)
Chris Dent4d601752016-07-12 19:34:09 +000076 placement_api_apache_conf=$(apache_site_config_for placement-api)
77
Chris Dent4d601752016-07-12 19:34:09 +000078 if [[ ${USE_VENV} = True ]]; then
Chris Dent78a564b2018-10-05 10:17:56 +010079 venv_path="python-path=${PROJECT_VENV["placement"]}/lib/$(python_version)/site-packages"
80 placement_bin_dir=${PROJECT_VENV["placement"]}/bin
Chris Dent4d601752016-07-12 19:34:09 +000081 fi
82
Chris Dent4d601752016-07-12 19:34:09 +000083 sudo cp $FILES/apache-placement-api.template $placement_api_apache_conf
84 sudo sed -e "
Chris Dent4d601752016-07-12 19:34:09 +000085 s|%APACHE_NAME%|$APACHE_NAME|g;
Chris Dent78a564b2018-10-05 10:17:56 +010086 s|%PUBLICWSGI%|$placement_bin_dir/placement-api|g;
Chris Dent4d601752016-07-12 19:34:09 +000087 s|%SSLENGINE%|$placement_ssl|g;
88 s|%SSLCERTFILE%|$placement_certfile|g;
89 s|%SSLKEYFILE%|$placement_keyfile|g;
90 s|%USER%|$STACK_USER|g;
91 s|%VIRTUALENV%|$venv_path|g
92 s|%APIWORKERS%|$API_WORKERS|g
93 " -i $placement_api_apache_conf
94}
95
Chris Dent3027c202018-11-20 22:18:26 +000096# create_placement_conf() - Write config
Chris Dent78a564b2018-10-05 10:17:56 +010097function create_placement_conf {
98 rm -f $PLACEMENT_CONF
99 iniset $PLACEMENT_CONF placement_database connection `database_connection_url placement`
100 iniset $PLACEMENT_CONF DEFAULT debug "$ENABLE_DEBUG_LOG_LEVEL"
101 iniset $PLACEMENT_CONF api auth_strategy $PLACEMENT_AUTH_STRATEGY
102 configure_auth_token_middleware $PLACEMENT_CONF placement $PLACEMENT_AUTH_CACHE_DIR
103 setup_logging $PLACEMENT_CONF
Sean Dague51a225c2016-12-15 16:32:08 -0500104}
Chris Dent4d601752016-07-12 19:34:09 +0000105
Sean Dague51a225c2016-12-15 16:32:08 -0500106# configure_placement() - Set config files, create data dirs, etc
107function configure_placement {
Chris Dent78a564b2018-10-05 10:17:56 +0100108 sudo install -d -o $STACK_USER $PLACEMENT_CONF_DIR
109 create_placement_conf
Chris Dente0be9e32017-04-18 16:52:25 +0100110
Sean Dague64ffff92017-04-13 13:36:42 -0400111 if [[ "$WSGI_MODE" == "uwsgi" ]]; then
112 write_uwsgi_config "$PLACEMENT_UWSGI_CONF" "$PLACEMENT_UWSGI" "/placement"
113 else
114 _config_placement_apache_wsgi
115 fi
Chris Dent4d601752016-07-12 19:34:09 +0000116}
117
118# create_placement_accounts() - Set up required placement accounts
119# and service and endpoints.
120function create_placement_accounts {
121 create_service_user "placement" "admin"
122 local placement_api_url="$PLACEMENT_SERVICE_PROTOCOL://$PLACEMENT_SERVICE_HOST/placement"
123 get_or_create_service "placement" "placement" "Placement Service"
124 get_or_create_endpoint \
125 "placement" \
126 "$REGION_NAME" \
Chris Dent4d601752016-07-12 19:34:09 +0000127 "$placement_api_url"
128}
129
Chris Dent78a564b2018-10-05 10:17:56 +0100130# create_placement_cache_dir() - Create directories for keystone cache
131function create_placement_cache_dir {
132 # Create cache dir
133 sudo install -d -o $STACK_USER $PLACEMENT_AUTH_CACHE_DIR
134 rm -f $PLACEMENT_AUTH_CACHE_DIR/*
135}
136
Chris Dent4d601752016-07-12 19:34:09 +0000137# init_placement() - Create service user and endpoints
Chris Dent4d601752016-07-12 19:34:09 +0000138function init_placement {
Chris Dent78a564b2018-10-05 10:17:56 +0100139 recreate_database placement
140 $PLACEMENT_BIN_DIR/placement-manage db sync
Chris Dent4d601752016-07-12 19:34:09 +0000141 create_placement_accounts
Chris Dent78a564b2018-10-05 10:17:56 +0100142 create_placement_cache_dir
Chris Dent4d601752016-07-12 19:34:09 +0000143}
144
145# install_placement() - Collect source and prepare
146function install_placement {
147 install_apache_wsgi
Roman Podoliakaa066abe2017-04-18 16:18:14 +0300148 # Install the openstackclient placement client plugin for CLI
Chris Dent78a564b2018-10-05 10:17:56 +0100149 pip_install_gr osc-placement
150 git_clone $PLACEMENT_REPO $PLACEMENT_DIR $PLACEMENT_BRANCH
151 setup_develop $PLACEMENT_DIR
Chris Dent4d601752016-07-12 19:34:09 +0000152}
153
154# start_placement_api() - Start the API processes ahead of other things
155function start_placement_api {
Sean Dague64ffff92017-04-13 13:36:42 -0400156 if [[ "$WSGI_MODE" == "uwsgi" ]]; then
Davanum Srinivasaceb27e2017-08-17 08:59:59 -0400157 run_process "placement-api" "$PLACEMENT_BIN_DIR/uwsgi --procname-prefix placement --ini $PLACEMENT_UWSGI_CONF"
Sean Dague64ffff92017-04-13 13:36:42 -0400158 else
159 enable_apache_site placement-api
160 restart_apache_server
161 tail_log placement-api /var/log/$APACHE_NAME/placement-api.log
162 fi
Chris Dent4d601752016-07-12 19:34:09 +0000163
164 echo "Waiting for placement-api to start..."
165 if ! wait_for_service $SERVICE_TIMEOUT $PLACEMENT_SERVICE_PROTOCOL://$PLACEMENT_SERVICE_HOST/placement; then
166 die $LINENO "placement-api did not start"
167 fi
168}
169
170function start_placement {
171 start_placement_api
172}
173
174# stop_placement() - Disable the api service and stop it.
175function stop_placement {
Sean Dague64ffff92017-04-13 13:36:42 -0400176 if [[ "$WSGI_MODE" == "uwsgi" ]]; then
177 stop_process "placement-api"
Sean Dague64ffff92017-04-13 13:36:42 -0400178 else
179 disable_apache_site placement-api
180 restart_apache_server
181 fi
Chris Dent4d601752016-07-12 19:34:09 +0000182}
183
184# Restore xtrace
185$_XTRACE_LIB_PLACEMENT
186
187# Tell emacs to use shell-script-mode
188## Local variables:
189## mode: shell-script
190## End: