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