| 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 | # | 
|  | 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) | 
|  | 27 | set +o xtrace | 
|  | 28 |  | 
|  | 29 | # Defaults | 
|  | 30 | # -------- | 
|  | 31 |  | 
|  | 32 | PLACEMENT_CONF_DIR=/etc/nova | 
|  | 33 | PLACEMENT_CONF=$PLACEMENT_CONF_DIR/nova.conf | 
|  | 34 | PLACEMENT_AUTH_STRATEGY=${PLACEMENT_AUTH_STRATEGY:-placement} | 
| Sean Dague | 64ffff9 | 2017-04-13 13:36:42 -0400 | [diff] [blame] | 35 | # Nova virtual environment | 
|  | 36 | if [[ ${USE_VENV} = True ]]; then | 
|  | 37 | PROJECT_VENV["nova"]=${NOVA_DIR}.venv | 
|  | 38 | PLACEMENT_BIN_DIR=${PROJECT_VENV["nova"]}/bin | 
|  | 39 | else | 
|  | 40 | PLACEMENT_BIN_DIR=$(get_python_exec_prefix) | 
|  | 41 | fi | 
|  | 42 | PLACEMENT_UWSGI=$PLACEMENT_BIN_DIR/nova-placement-api | 
|  | 43 | PLACEMENT_UWSGI_CONF=$PLACEMENT_CONF_DIR/placement-uwsgi.ini | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 44 |  | 
|  | 45 | # The placement service can optionally use a separate database | 
|  | 46 | # connection. Set PLACEMENT_DB_ENABLED to True to use it. | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 47 | PLACEMENT_DB_ENABLED=$(trueorfalse False PLACEMENT_DB_ENABLED) | 
|  | 48 |  | 
| Sean Dague | f3b2f4c | 2017-04-13 10:11:48 -0400 | [diff] [blame] | 49 | if is_service_enabled tls-proxy; then | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 50 | PLACEMENT_SERVICE_PROTOCOL="https" | 
|  | 51 | fi | 
|  | 52 |  | 
|  | 53 | # Public facing bits | 
|  | 54 | PLACEMENT_SERVICE_PROTOCOL=${PLACEMENT_SERVICE_PROTOCOL:-$SERVICE_PROTOCOL} | 
|  | 55 | PLACEMENT_SERVICE_HOST=${PLACEMENT_SERVICE_HOST:-$SERVICE_HOST} | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 56 |  | 
|  | 57 | # Functions | 
|  | 58 | # --------- | 
|  | 59 |  | 
|  | 60 | # Test if any placement services are enabled | 
|  | 61 | # is_placement_enabled | 
|  | 62 | function is_placement_enabled { | 
| Sean Dague | 51a225c | 2016-12-15 16:32:08 -0500 | [diff] [blame] | 63 | [[ ,${ENABLED_SERVICES} =~ ,"placement-api" ]] && return 0 | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 64 | return 1 | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | # cleanup_placement() - Remove residual data files, anything left over from previous | 
|  | 68 | # runs that a clean run would need to clean up | 
|  | 69 | function cleanup_placement { | 
| Sean Dague | 803acff | 2017-05-01 10:52:38 -0400 | [diff] [blame] | 70 | sudo rm -f $(apache_site_config_for nova-placement-api) | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 71 | sudo rm -f $(apache_site_config_for placement-api) | 
| Chris Dent | 1489b9e | 2017-12-05 23:46:58 +0000 | [diff] [blame] | 72 | remove_uwsgi_config "$PLACEMENT_UWSGI_CONF" "$PLACEMENT_UWSGI" | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
|  | 75 | # _config_placement_apache_wsgi() - Set WSGI config files | 
|  | 76 | function _config_placement_apache_wsgi { | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 77 | local placement_api_apache_conf | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 78 | local venv_path="" | 
| Sean Dague | 43ff27b | 2016-08-30 21:13:15 -0400 | [diff] [blame] | 79 | local nova_bin_dir="" | 
|  | 80 | nova_bin_dir=$(get_python_exec_prefix) | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 81 | placement_api_apache_conf=$(apache_site_config_for placement-api) | 
|  | 82 |  | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 83 | # reuse nova's venv if there is one as placement code lives | 
|  | 84 | # there | 
|  | 85 | if [[ ${USE_VENV} = True ]]; then | 
|  | 86 | venv_path="python-path=${PROJECT_VENV["nova"]}/lib/$(python_version)/site-packages" | 
| Sean Dague | 43ff27b | 2016-08-30 21:13:15 -0400 | [diff] [blame] | 87 | nova_bin_dir=${PROJECT_VENV["nova"]}/bin | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 88 | fi | 
|  | 89 |  | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 90 | sudo cp $FILES/apache-placement-api.template $placement_api_apache_conf | 
|  | 91 | sudo sed -e " | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 92 | s|%APACHE_NAME%|$APACHE_NAME|g; | 
| Sean Dague | 43ff27b | 2016-08-30 21:13:15 -0400 | [diff] [blame] | 93 | s|%PUBLICWSGI%|$nova_bin_dir/nova-placement-api|g; | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 94 | s|%SSLENGINE%|$placement_ssl|g; | 
|  | 95 | s|%SSLCERTFILE%|$placement_certfile|g; | 
|  | 96 | s|%SSLKEYFILE%|$placement_keyfile|g; | 
|  | 97 | s|%USER%|$STACK_USER|g; | 
|  | 98 | s|%VIRTUALENV%|$venv_path|g | 
|  | 99 | s|%APIWORKERS%|$API_WORKERS|g | 
|  | 100 | " -i $placement_api_apache_conf | 
|  | 101 | } | 
|  | 102 |  | 
| Sean Dague | 51a225c | 2016-12-15 16:32:08 -0500 | [diff] [blame] | 103 | function configure_placement_nova_compute { | 
| Matt Riedemann | f6d566c | 2017-12-22 11:39:29 -0500 | [diff] [blame] | 104 | # Use the provided config file path or default to $NOVA_CONF. | 
|  | 105 | local conf=${1:-$NOVA_CONF} | 
|  | 106 | iniset $conf placement auth_type "password" | 
|  | 107 | iniset $conf placement auth_url "$KEYSTONE_SERVICE_URI" | 
|  | 108 | iniset $conf placement username placement | 
|  | 109 | iniset $conf placement password "$SERVICE_PASSWORD" | 
|  | 110 | iniset $conf placement user_domain_name "$SERVICE_DOMAIN_NAME" | 
|  | 111 | iniset $conf placement project_name "$SERVICE_TENANT_NAME" | 
|  | 112 | iniset $conf placement project_domain_name "$SERVICE_DOMAIN_NAME" | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 113 | # TODO(cdent): auth_strategy, which is common to see in these | 
|  | 114 | # blocks is not currently used here. For the time being the | 
|  | 115 | # placement api uses the auth_strategy configuration setting | 
|  | 116 | # established by the nova api. This avoids, for the time, being, | 
|  | 117 | # creating redundant configuration items that are just used for | 
|  | 118 | # testing. | 
| Sean Dague | 51a225c | 2016-12-15 16:32:08 -0500 | [diff] [blame] | 119 | } | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 120 |  | 
| Sean Dague | 51a225c | 2016-12-15 16:32:08 -0500 | [diff] [blame] | 121 | # configure_placement() - Set config files, create data dirs, etc | 
|  | 122 | function configure_placement { | 
|  | 123 | if [ "$PLACEMENT_DB_ENABLED" != False ]; then | 
|  | 124 | iniset $PLACEMENT_CONF placement_database connection `database_connection_url placement` | 
|  | 125 | fi | 
| Chris Dent | e0be9e3 | 2017-04-18 16:52:25 +0100 | [diff] [blame] | 126 |  | 
| Sean Dague | 64ffff9 | 2017-04-13 13:36:42 -0400 | [diff] [blame] | 127 | if [[ "$WSGI_MODE" == "uwsgi" ]]; then | 
|  | 128 | write_uwsgi_config "$PLACEMENT_UWSGI_CONF" "$PLACEMENT_UWSGI" "/placement" | 
|  | 129 | else | 
|  | 130 | _config_placement_apache_wsgi | 
|  | 131 | fi | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
|  | 134 | # create_placement_accounts() - Set up required placement accounts | 
|  | 135 | # and service and endpoints. | 
|  | 136 | function create_placement_accounts { | 
|  | 137 | create_service_user "placement" "admin" | 
|  | 138 | local placement_api_url="$PLACEMENT_SERVICE_PROTOCOL://$PLACEMENT_SERVICE_HOST/placement" | 
|  | 139 | get_or_create_service "placement" "placement" "Placement Service" | 
|  | 140 | get_or_create_endpoint \ | 
|  | 141 | "placement" \ | 
|  | 142 | "$REGION_NAME" \ | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 143 | "$placement_api_url" | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | # init_placement() - Create service user and endpoints | 
|  | 147 | # If PLACEMENT_DB_ENABLED is true, create the separate placement db | 
|  | 148 | # using, for now, the api_db migrations. | 
|  | 149 | function init_placement { | 
|  | 150 | if [ "$PLACEMENT_DB_ENABLED" != False ]; then | 
|  | 151 | recreate_database placement | 
| Chris Dent | e8bad5c | 2018-04-25 13:01:03 +0100 | [diff] [blame] | 152 | # Database migration will be handled when nova does an api_db sync | 
|  | 153 | # TODO(cdent): When placement is extracted we'll do our own sync | 
|  | 154 | # here. | 
| Chris Dent | 4d60175 | 2016-07-12 19:34:09 +0000 | [diff] [blame] | 155 | fi | 
|  | 156 | create_placement_accounts | 
|  | 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 | 
|  | 163 | # TODO(mriedem): Use pip_install_gr once osc-placement is in g-r. | 
|  | 164 | pip_install osc-placement | 
| 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: |