Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # lib/placement |
| 4 | # Functions to control the configuration and operation of the **Placement** service |
| 5 | # |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 6 | |
| 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) |
| 24 | set +o xtrace |
| 25 | |
| 26 | # Defaults |
| 27 | # -------- |
| 28 | |
Chris Dent | 78a564b | 2018-10-05 10:17:56 +0100 | [diff] [blame^] | 29 | PLACEMENT_DIR=$DEST/placement |
| 30 | PLACEMENT_CONF_DIR=/etc/placement |
| 31 | PLACEMENT_CONF=$PLACEMENT_CONF_DIR/placement.conf |
| 32 | PLACEMENT_AUTH_CACHE_DIR=${PLACEMENT_AUTH_CACHE_DIR:-/var/cache/placement} |
| 33 | PLACEMENT_AUTH_STRATEGY=${PLACEMENT_AUTH_STRATEGY:-keystone} |
| 34 | # Placement virtual environment |
Sean Dague | 64ffff9 | 2017-04-13 13:36:42 -0400 | [diff] [blame] | 35 | if [[ ${USE_VENV} = True ]]; then |
Chris Dent | 78a564b | 2018-10-05 10:17:56 +0100 | [diff] [blame^] | 36 | PROJECT_VENV["placement"]=${PLACEMENT_DIR}.venv |
| 37 | PLACEMENT_BIN_DIR=${PROJECT_VENV["placement"]}/bin |
Sean Dague | 64ffff9 | 2017-04-13 13:36:42 -0400 | [diff] [blame] | 38 | else |
| 39 | PLACEMENT_BIN_DIR=$(get_python_exec_prefix) |
| 40 | fi |
Chris Dent | 78a564b | 2018-10-05 10:17:56 +0100 | [diff] [blame^] | 41 | PLACEMENT_UWSGI=$PLACEMENT_BIN_DIR/placement-api |
Sean Dague | 64ffff9 | 2017-04-13 13:36:42 -0400 | [diff] [blame] | 42 | PLACEMENT_UWSGI_CONF=$PLACEMENT_CONF_DIR/placement-uwsgi.ini |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 43 | |
Sean Dague | f3b2f4c | 2017-04-13 10:11:48 -0400 | [diff] [blame] | 44 | if is_service_enabled tls-proxy; then |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 45 | PLACEMENT_SERVICE_PROTOCOL="https" |
| 46 | fi |
| 47 | |
| 48 | # Public facing bits |
| 49 | PLACEMENT_SERVICE_PROTOCOL=${PLACEMENT_SERVICE_PROTOCOL:-$SERVICE_PROTOCOL} |
| 50 | PLACEMENT_SERVICE_HOST=${PLACEMENT_SERVICE_HOST:-$SERVICE_HOST} |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 51 | |
| 52 | # Functions |
| 53 | # --------- |
| 54 | |
| 55 | # Test if any placement services are enabled |
| 56 | # is_placement_enabled |
| 57 | function is_placement_enabled { |
Sean Dague | 51a225c | 2016-12-15 16:32:08 -0500 | [diff] [blame] | 58 | [[ ,${ENABLED_SERVICES} =~ ,"placement-api" ]] && return 0 |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 59 | 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 |
| 64 | function cleanup_placement { |
Sean Dague | 803acff | 2017-05-01 10:52:38 -0400 | [diff] [blame] | 65 | sudo rm -f $(apache_site_config_for nova-placement-api) |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 66 | sudo rm -f $(apache_site_config_for placement-api) |
Chris Dent | 1489b9e | 2017-12-05 23:46:58 +0000 | [diff] [blame] | 67 | remove_uwsgi_config "$PLACEMENT_UWSGI_CONF" "$PLACEMENT_UWSGI" |
Chris Dent | 78a564b | 2018-10-05 10:17:56 +0100 | [diff] [blame^] | 68 | sudo rm -f $PLACEMENT_AUTH_CACHE_DIR/* |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | # _config_placement_apache_wsgi() - Set WSGI config files |
| 72 | function _config_placement_apache_wsgi { |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 73 | local placement_api_apache_conf |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 74 | local venv_path="" |
Chris Dent | 78a564b | 2018-10-05 10:17:56 +0100 | [diff] [blame^] | 75 | local placement_bin_dir="" |
| 76 | placement_bin_dir=$(get_python_exec_prefix) |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 77 | placement_api_apache_conf=$(apache_site_config_for placement-api) |
| 78 | |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 79 | if [[ ${USE_VENV} = True ]]; then |
Chris Dent | 78a564b | 2018-10-05 10:17:56 +0100 | [diff] [blame^] | 80 | venv_path="python-path=${PROJECT_VENV["placement"]}/lib/$(python_version)/site-packages" |
| 81 | placement_bin_dir=${PROJECT_VENV["placement"]}/bin |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 82 | fi |
| 83 | |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 84 | sudo cp $FILES/apache-placement-api.template $placement_api_apache_conf |
| 85 | sudo sed -e " |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 86 | s|%APACHE_NAME%|$APACHE_NAME|g; |
Chris Dent | 78a564b | 2018-10-05 10:17:56 +0100 | [diff] [blame^] | 87 | s|%PUBLICWSGI%|$placement_bin_dir/placement-api|g; |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 88 | s|%SSLENGINE%|$placement_ssl|g; |
| 89 | s|%SSLCERTFILE%|$placement_certfile|g; |
| 90 | s|%SSLKEYFILE%|$placement_keyfile|g; |
| 91 | s|%USER%|$STACK_USER|g; |
| 92 | s|%VIRTUALENV%|$venv_path|g |
| 93 | s|%APIWORKERS%|$API_WORKERS|g |
| 94 | " -i $placement_api_apache_conf |
| 95 | } |
| 96 | |
Sean Dague | 51a225c | 2016-12-15 16:32:08 -0500 | [diff] [blame] | 97 | function configure_placement_nova_compute { |
Matt Riedemann | f6d566c | 2017-12-22 11:39:29 -0500 | [diff] [blame] | 98 | # Use the provided config file path or default to $NOVA_CONF. |
| 99 | local conf=${1:-$NOVA_CONF} |
| 100 | iniset $conf placement auth_type "password" |
| 101 | iniset $conf placement auth_url "$KEYSTONE_SERVICE_URI" |
| 102 | iniset $conf placement username placement |
| 103 | iniset $conf placement password "$SERVICE_PASSWORD" |
| 104 | iniset $conf placement user_domain_name "$SERVICE_DOMAIN_NAME" |
| 105 | iniset $conf placement project_name "$SERVICE_TENANT_NAME" |
| 106 | iniset $conf placement project_domain_name "$SERVICE_DOMAIN_NAME" |
Chris Dent | 78a564b | 2018-10-05 10:17:56 +0100 | [diff] [blame^] | 107 | iniset $conf placement auth_strategy $PLACEMENT_AUTH_STRATEGY |
| 108 | } |
| 109 | |
| 110 | # create_placement_conf() - Write confg |
| 111 | function create_placement_conf { |
| 112 | rm -f $PLACEMENT_CONF |
| 113 | iniset $PLACEMENT_CONF placement_database connection `database_connection_url placement` |
| 114 | iniset $PLACEMENT_CONF DEFAULT debug "$ENABLE_DEBUG_LOG_LEVEL" |
| 115 | iniset $PLACEMENT_CONF api auth_strategy $PLACEMENT_AUTH_STRATEGY |
| 116 | configure_auth_token_middleware $PLACEMENT_CONF placement $PLACEMENT_AUTH_CACHE_DIR |
| 117 | setup_logging $PLACEMENT_CONF |
Sean Dague | 51a225c | 2016-12-15 16:32:08 -0500 | [diff] [blame] | 118 | } |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 119 | |
Sean Dague | 51a225c | 2016-12-15 16:32:08 -0500 | [diff] [blame] | 120 | # configure_placement() - Set config files, create data dirs, etc |
| 121 | function configure_placement { |
Chris Dent | 78a564b | 2018-10-05 10:17:56 +0100 | [diff] [blame^] | 122 | sudo install -d -o $STACK_USER $PLACEMENT_CONF_DIR |
| 123 | create_placement_conf |
Chris Dent | e0be9e3 | 2017-04-18 16:52:25 +0100 | [diff] [blame] | 124 | |
Sean Dague | 64ffff9 | 2017-04-13 13:36:42 -0400 | [diff] [blame] | 125 | if [[ "$WSGI_MODE" == "uwsgi" ]]; then |
| 126 | write_uwsgi_config "$PLACEMENT_UWSGI_CONF" "$PLACEMENT_UWSGI" "/placement" |
| 127 | else |
| 128 | _config_placement_apache_wsgi |
| 129 | fi |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | # create_placement_accounts() - Set up required placement accounts |
| 133 | # and service and endpoints. |
| 134 | function create_placement_accounts { |
| 135 | create_service_user "placement" "admin" |
| 136 | local placement_api_url="$PLACEMENT_SERVICE_PROTOCOL://$PLACEMENT_SERVICE_HOST/placement" |
| 137 | get_or_create_service "placement" "placement" "Placement Service" |
| 138 | get_or_create_endpoint \ |
| 139 | "placement" \ |
| 140 | "$REGION_NAME" \ |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 141 | "$placement_api_url" |
| 142 | } |
| 143 | |
Chris Dent | 78a564b | 2018-10-05 10:17:56 +0100 | [diff] [blame^] | 144 | # create_placement_cache_dir() - Create directories for keystone cache |
| 145 | function create_placement_cache_dir { |
| 146 | # Create cache dir |
| 147 | sudo install -d -o $STACK_USER $PLACEMENT_AUTH_CACHE_DIR |
| 148 | rm -f $PLACEMENT_AUTH_CACHE_DIR/* |
| 149 | } |
| 150 | |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 151 | # init_placement() - Create service user and endpoints |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 152 | function init_placement { |
Chris Dent | 78a564b | 2018-10-05 10:17:56 +0100 | [diff] [blame^] | 153 | recreate_database placement |
| 154 | $PLACEMENT_BIN_DIR/placement-manage db sync |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 155 | create_placement_accounts |
Chris Dent | 78a564b | 2018-10-05 10:17:56 +0100 | [diff] [blame^] | 156 | create_placement_cache_dir |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | # install_placement() - Collect source and prepare |
| 160 | function install_placement { |
| 161 | install_apache_wsgi |
Roman Podoliaka | a066abe | 2017-04-18 16:18:14 +0300 | [diff] [blame] | 162 | # Install the openstackclient placement client plugin for CLI |
Chris Dent | 78a564b | 2018-10-05 10:17:56 +0100 | [diff] [blame^] | 163 | pip_install_gr osc-placement |
| 164 | git_clone $PLACEMENT_REPO $PLACEMENT_DIR $PLACEMENT_BRANCH |
| 165 | setup_develop $PLACEMENT_DIR |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | # start_placement_api() - Start the API processes ahead of other things |
| 169 | function start_placement_api { |
Sean Dague | 64ffff9 | 2017-04-13 13:36:42 -0400 | [diff] [blame] | 170 | if [[ "$WSGI_MODE" == "uwsgi" ]]; then |
Davanum Srinivas | aceb27e | 2017-08-17 08:59:59 -0400 | [diff] [blame] | 171 | run_process "placement-api" "$PLACEMENT_BIN_DIR/uwsgi --procname-prefix placement --ini $PLACEMENT_UWSGI_CONF" |
Sean Dague | 64ffff9 | 2017-04-13 13:36:42 -0400 | [diff] [blame] | 172 | else |
| 173 | enable_apache_site placement-api |
| 174 | restart_apache_server |
| 175 | tail_log placement-api /var/log/$APACHE_NAME/placement-api.log |
| 176 | fi |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 177 | |
| 178 | echo "Waiting for placement-api to start..." |
| 179 | if ! wait_for_service $SERVICE_TIMEOUT $PLACEMENT_SERVICE_PROTOCOL://$PLACEMENT_SERVICE_HOST/placement; then |
| 180 | die $LINENO "placement-api did not start" |
| 181 | fi |
| 182 | } |
| 183 | |
| 184 | function start_placement { |
| 185 | start_placement_api |
| 186 | } |
| 187 | |
| 188 | # stop_placement() - Disable the api service and stop it. |
| 189 | function stop_placement { |
Sean Dague | 64ffff9 | 2017-04-13 13:36:42 -0400 | [diff] [blame] | 190 | if [[ "$WSGI_MODE" == "uwsgi" ]]; then |
| 191 | stop_process "placement-api" |
Sean Dague | 64ffff9 | 2017-04-13 13:36:42 -0400 | [diff] [blame] | 192 | else |
| 193 | disable_apache_site placement-api |
| 194 | restart_apache_server |
| 195 | fi |
Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | # Restore xtrace |
| 199 | $_XTRACE_LIB_PLACEMENT |
| 200 | |
| 201 | # Tell emacs to use shell-script-mode |
| 202 | ## Local variables: |
| 203 | ## mode: shell-script |
| 204 | ## End: |