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