diff --git a/devstack/README.md b/devstack/README.md index ae563ddfe..166354f26 100644 --- a/devstack/README.md +++ b/devstack/README.md @@ -143,6 +143,17 @@ enforce it in following way: APACHE_MIRROR=http://www-us.apache.org/dist/ ``` +## Using WSGI + +Monasca-api can be deployed with Apache using uwsgi and gunicorn. +By default monasca-api runs under uwsgi. +If you wish to use gunicorn make sure that ```devstack/local.conf``` +contains: + +```sh +MONASCA_API_USE_MOD_WSGI=False +``` + # License (c) Copyright 2015-2016 Hewlett Packard Enterprise Development Company LP diff --git a/devstack/Vagrantfile b/devstack/Vagrantfile index 891054908..48bef12d9 100644 --- a/devstack/Vagrantfile +++ b/devstack/Vagrantfile @@ -156,6 +156,8 @@ MONASCA_PERSISTER_IMPLEMENTATION_LANG=${MONASCA_PERSISTER_IMPLEMENTATION_LANG:-p # MONASCA_METRICS_DB=${MONASCA_METRICS_DB:-cassandra} MONASCA_METRICS_DB=${MONASCA_METRICS_DB:-influxdb} +MONASCA_API_USE_MOD_WSGI=${MONASCA_API_USE_MOD_WSGI:-True} + # Uncomment one of the following lines and modify accordingly to enable the Monasca DevStack Plugin enable_plugin monasca-api https://git.openstack.org/openstack/monasca-api # enable_plugin monasca-api file:///vagrant_home/Documents/repos/openstack/monasca-api.vertica diff --git a/devstack/files/monasca-api/apache-monasca-api.template b/devstack/files/monasca-api/apache-monasca-api.template new file mode 100644 index 000000000..5748e1443 --- /dev/null +++ b/devstack/files/monasca-api/apache-monasca-api.template @@ -0,0 +1,18 @@ +Listen %PUBLICPORT% + + + WSGIDaemonProcess monasca-api user=%USER% processes=%APIWORKERS% threads=1 display-name=%{GROUP} %VIRTUALENV% + WSGIProcessGroup monasca-api + WSGIScriptAlias / %PUBLICWSGI%/wsgi.py + WSGIApplicationGroup %{GLOBAL} + + WSGIPassAuthorization On + + LogLevel info + ErrorLog /var/log/%APACHE_NAME%/monasca-api.log + CustomLog /var/log/%APACHE_NAME%/monasca-api_access.log combined + + SetEnv no-gzip 1 + AddDefaultCharset utf-8 + + \ No newline at end of file diff --git a/devstack/plugin.sh b/devstack/plugin.sh index 9c2e43570..c4dc71b8a 100755 --- a/devstack/plugin.sh +++ b/devstack/plugin.sh @@ -93,7 +93,12 @@ if [[ ${USE_VENV} = True ]]; then else MONASCA_API_BIN_DIR=$(get_python_exec_prefix) fi -MONASCA_API_BASE_URI=${MONASCA_API_SERVICE_PROTOCOL}://${MONASCA_API_SERVICE_HOST}:${MONASCA_API_SERVICE_PORT} + +if [[ "${MONASCA_API_USE_MOD_WSGI}" == 'True' && "${MONASCA_API_IMPLEMENTATION_LANG}" == "python" ]]; then + MONASCA_API_BASE_URI=${MONASCA_API_SERVICE_PROTOCOL}://${MONASCA_API_SERVICE_HOST}/metrics +else + MONASCA_API_BASE_URI=${MONASCA_API_SERVICE_PROTOCOL}://${MONASCA_API_SERVICE_HOST}:${MONASCA_API_SERVICE_PORT} +fi MONASCA_API_URI_V2=${MONASCA_API_BASE_URI}/v2.0 # Files inside this directory will be visible in gates log @@ -759,9 +764,14 @@ function install_monasca-api { git_clone $MONASCA_API_REPO $MONASCA_API_DIR $MONASCA_API_BRANCH setup_develop $MONASCA_API_DIR - pip_install_gr gunicorn install_monasca_common + if [[ "${MONASCA_API_USE_MOD_WSGI}" == 'True' ]]; then + pip_install uwsgi + else + pip_install_gr gunicorn + fi + if [[ "${MONASCA_METRICS_DB,,}" == 'influxdb' ]]; then pip_install_gr influxdb fi @@ -854,9 +864,20 @@ function configure_monasca_api_python { ln -sf $MONASCA_API_PASTE_INI $MON_API_GATE_CONFIGURATION_DIR ln -sf $MONASCA_API_LOGGING_CONF $MON_API_GATE_CONFIGURATION_DIR + if [ "${MONASCA_API_USE_MOD_WSGI}" == 'True' ]; then + configure_monasca_api_python_uwsgi + fi + fi } +function configure_monasca_api_python_uwsgi { + rm -rf $MONASCA_API_UWSGI_CONF + + install -m 600 $MONASCA_API_DIR/etc/api-uwsgi.ini $MONASCA_API_UWSGI_CONF + write_uwsgi_config "$MONASCA_API_UWSGI_CONF" "$MONASCA_API_BIN_DIR/monasca-api-wsgi" "/metrics" +} + function start_monasca_api_python { if is_service_enabled monasca-api; then echo_summary "Starting monasca-api" @@ -866,10 +887,16 @@ function start_monasca_api_python { local gunicorn="$MONASCA_API_BIN_DIR/gunicorn" restart_service memcached - run_process "monasca-api" "$gunicorn --paste $MONASCA_API_PASTE_INI" + if [ "${MONASCA_API_USE_MOD_WSGI}" == 'True' ]; then + service_uri=$service_protocol://$MONASCA_API_SERVICE_HOST/api/v2.0 + run_process "monasca-api" "$MONASCA_API_BIN_DIR/uwsgi --ini $MONASCA_API_UWSGI_CONF" "" + else + service_uri=$service_protocol://$MONASCA_API_SERVICE_HOST:$service_port + run_process "monasca-api" "$gunicorn --paste $MONASCA_API_PASTE_INI" + fi echo "Waiting for monasca-api to start..." - if ! wait_for_service $SERVICE_TIMEOUT $service_protocol://$SERVICE_HOST:$service_port; then + if ! wait_for_service $SERVICE_TIMEOUT $service_uri; then die $LINENO "monasca-api did not start" fi fi @@ -918,6 +945,14 @@ function clean_monasca_api_python { apt_get -y purge libmysqlclient-dev fi + if [ "$MONASCA_API_USE_MOD_WSGI" == "True" ]; then + clean_monasca_api_uwsgi + fi + +} + +function clean_monasca_api_uwsgi { + sudo rm -rf $MONASCA_API_UWSGI_CONF } function start_monasca_api { diff --git a/devstack/settings b/devstack/settings index 22e5c26a7..868bbaac2 100644 --- a/devstack/settings +++ b/devstack/settings @@ -190,6 +190,9 @@ MONASCA_API_CONF=${MONASCA_API_CONF:-$MONASCA_API_CONF_DIR/api-config.conf} MONASCA_API_PASTE_INI=${MONASCA_API_PASTE_INI:-$MONASCA_API_CONF_DIR/api-config.ini} MONASCA_API_LOGGING_CONF=${MONASCA_API_LOGGING_CONF:-$MONASCA_API_CONF_DIR/api-logging.conf} MONASCA_API_LOG_DIR=${MONASCA_API_LOG_DIR:-/var/log/monasca/api} +MONASCA_API_USE_MOD_WSGI=${MONASCA_API_USE_MOD_WSGI:-$ENABLE_HTTPD_MOD_WSGI_SERVICES} +MONASCA_API_UWSGI_CONF=${MONASCA_API_UWSGI_CONF:-$MONASCA_API_CONF_DIR/api-uwsgi.ini} + ## storm settings STORM_UI_HOST=${STORM_UI_HOST:-${SERVICE_HOST}} diff --git a/etc/api-uwsgi.ini b/etc/api-uwsgi.ini new file mode 100644 index 000000000..74e0dc2c7 --- /dev/null +++ b/etc/api-uwsgi.ini @@ -0,0 +1,25 @@ +[uwsgi] +wsgi-file = /usr/local/bin/monasca-api-wsgi + +# Versions of mod_proxy_uwsgi>=2.0.6 should use a UNIX socket, see +# http://uwsgi-docs.readthedocs.org/en/latest/Apache.html#mod-proxy-uwsgi +uwsgi-socket = 127.0.0.1:8070 + +# Override the default size for headers from the 4k default. +buffer-size = 65535 + +# This is running standalone +master = true + +enable-threads = true + +# Tune this to your environment. +processes = 4 + +# uwsgi recommends this to prevent thundering herd on accept. +thunder-lock = true + +plugins = python + +# This ensures that file descriptors aren't shared between keystone processes. +lazy-apps = true \ No newline at end of file diff --git a/monasca_api/api/wsgi.py b/monasca_api/api/wsgi.py index 3e4b1c6ac..ff95b60e4 100644 --- a/monasca_api/api/wsgi.py +++ b/monasca_api/api/wsgi.py @@ -17,4 +17,9 @@ from monasca_api.api import server -application = server.get_wsgi_app(config_base_path='/etc/monasca') + +def main(): + return server.get_wsgi_app(config_base_path='/etc/monasca') + +if __name__ == '__main__': + application = main() diff --git a/setup.cfg b/setup.cfg index 61a1bc53f..e69f7d897 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,6 +36,9 @@ cassandra = console_scripts = monasca-api = monasca_api.api.server:launch +wsgi_scripts = + monasca-api-wsgi = monasca_api.api.wsgi:main + oslo.config.opts = monasca_api = monasca_api.conf:list_opts