From 20d51467c7e02b51445778d704900df117423ec2 Mon Sep 17 00:00:00 2001 From: Amrith Kumar Date: Mon, 10 Apr 2017 19:25:28 -0400 Subject: [PATCH] enable trove-api behind mod-wsgi This change enables behind mod-wsgi as part of the community pike goal goal-deploy-api-in-wsgi. The change includes: - the wsgi script files to run trove api under apache - updates to the devstack plugin - a basic deploy doc which explains this change Change-Id: Icdd39b47a1be426e87a5d09f9e9d567af1974a9c Depends-On: I3d6f6649430ee40879de15fee0b215dc32e8b666 Closes-Bug: #1681478 --- devstack/files/apache-trove-api.template | 45 +++++++++++++++++ devstack/plugin.sh | 61 +++++++++++++++++++++++- devstack/settings | 9 ++++ doc/source/deploy/apache-mod-wsgi.rst | 49 +++++++++++++++++++ doc/source/index.rst | 1 + etc/apache2/trove | 36 ++++++++++++++ integration/scripts/trovestack | 1 + trove/cmd/app.wsgi | 40 ++++++++++++++++ 8 files changed, 240 insertions(+), 2 deletions(-) create mode 100644 devstack/files/apache-trove-api.template create mode 100644 doc/source/deploy/apache-mod-wsgi.rst create mode 100644 etc/apache2/trove create mode 100644 trove/cmd/app.wsgi diff --git a/devstack/files/apache-trove-api.template b/devstack/files/apache-trove-api.template new file mode 100644 index 0000000000..06c88b2685 --- /dev/null +++ b/devstack/files/apache-trove-api.template @@ -0,0 +1,45 @@ +# Copyright 2017 Amrith Kumar. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +# This is an example Apache2 configuration file for using the +# Watcher API through mod_wsgi. This version assumes you are +# running devstack to configure the software. + +Listen %TROVE_SERVICE_PORT% + + + WSGIDaemonProcess trove-api user=%USER% processes=%APIWORKERS% threads=1 display-name=%{GROUP} + WSGIScriptAlias / %TROVE_WSGI_DIR%/app.wsgi + WSGIApplicationGroup %{GLOBAL} + WSGIProcessGroup trove-api + WSGIPassAuthorization On + + ErrorLogFormat "%M" + ErrorLog /var/log/%APACHE_NAME%/trove-api.log + CustomLog /var/log/%APACHE_NAME%/trove-api-access.log combined + + + + WSGIProcessGroup trove-api + WSGIApplicationGroup %{GLOBAL} + = 2.4> + Require all granted + + + Order allow,deny + Allow from all + + + diff --git a/devstack/plugin.sh b/devstack/plugin.sh index b423ff27bc..e64e8b0db5 100644 --- a/devstack/plugin.sh +++ b/devstack/plugin.sh @@ -64,6 +64,14 @@ function create_trove_accounts { fi } +# _cleanup_trove_apache_wsgi - Removes all the WSGI related files and +# restart apache. +function _cleanup_trove_apache_wsgi { + sudo rm -rf $TROVE_WSGI_DIR + sudo rm -f ${apache_site_config_for trove-api} + restart_apache_server +} + # stack.sh entry points # --------------------- @@ -77,6 +85,11 @@ function cleanup_trove { if is_service_enabled horizon; then cleanup_trove_dashboard fi + + if [[ "${TROVE_USE_MOD_WSGI}" == "TRUE" ]]; then + echo "Cleaning up Trove's WSGI setup" + _cleanup_trove_apache_wsgi + fi } @@ -139,6 +152,27 @@ function configure_nova_kvm { echo "configure_nova_kvm: using virt_type: ${virt_type} for cpu: ${cpu}." } +# _config_trove_apache_wsgi() - Setup WSGI config files for Trove and +# enable the site +function _config_trove_apache_wsgi { + local trove_apache_conf + + sudo mkdir -p ${TROVE_WSGI_DIR} + sudo cp $TROVE_DIR/trove/cmd/app.wsgi $TROVE_WSGI_DIR/app.wsgi + trove_apache_conf=$(apache_site_config_for trove-api) + sudo cp $TROVE_DEVSTACK_FILES/apache-trove-api.template ${trove_apache_conf} + sudo sed -e " + s|%TROVE_SERVICE_PORT%|${TROVE_SERVICE_PORT}|g; + s|%TROVE_WSGI_DIR%|${TROVE_WSGI_DIR}|g; + s|%USER%|${STACK_USER}|g; + s|%APACHE_NAME%|${APACHE_NAME}|g; + s|%APIWORKERS%|${API_WORKERS}|g; + " -i ${trove_apache_conf} + enable_apache_site trove-api + tail_log trove-access /var/log/${APACHE_NAME}/trove-api-access.log + tail_log trove-api /var/log/${APACHE_NAME}/trove-api.log +} + # configure_trove() - Set config files, create data dirs, etc function configure_trove { setup_develop $TROVE_DIR @@ -185,6 +219,12 @@ function configure_trove { iniset $TROVE_CONF DEFAULT trove_auth_url $TROVE_AUTH_ENDPOINT fi + # configure apache related files + if [[ "${TROVE_USE_MOD_WSGI}" == "TRUE" ]]; then + echo "Configuring Trove to use mod-wsgi and Apache" + _config_trove_apache_wsgi + fi + # (Re)create trove taskmanager conf file if needed if is_service_enabled tr-tmgr; then # Use these values only if they're set @@ -265,6 +305,11 @@ function configure_trove { function install_trove { setup_develop $TROVE_DIR + if [[ "${TROVE_USE_MOD_WSGI}" == "TRUE" ]]; then + echo "Installing apache wsgi" + install_apache_wsgi + fi + if is_service_enabled horizon; then install_trove_dashboard fi @@ -491,7 +536,13 @@ function finalize_trove_network { # start_trove() - Start running processes, including screen function start_trove { - run_process tr-api "$TROVE_BIN_DIR/trove-api --config-file=$TROVE_CONF --debug" + if [[ ${TROVE_USE_MOD_WSGI}" == TRUE" ]]; then + echo "Restarting Apache server ..." + enable_apache_site trove-api + restart_apache_server + else + run_process tr-api "$TROVE_BIN_DIR/trove-api --config-file=$TROVE_CONF --debug" + fi run_process tr-tmgr "$TROVE_BIN_DIR/trove-taskmanager --config-file=$TROVE_TASKMANAGER_CONF --debug" run_process tr-cond "$TROVE_BIN_DIR/trove-conductor --config-file=$TROVE_CONDUCTOR_CONF --debug" } @@ -500,7 +551,13 @@ function start_trove { function stop_trove { # Kill the trove screen windows local serv - for serv in tr-api tr-tmgr tr-cond; do + if [[ ${TROVE_USE_MOD_WSGI} == "TRUE" ]]; then + echo "Disabling Trove API in Apache" + disable_apache_site trove-api + else + stop_process tr-api + fi + for serv in tr-tmgr tr-cond; do stop_process $serv done } diff --git a/devstack/settings b/devstack/settings index be3074f46e..2dff5fb390 100644 --- a/devstack/settings +++ b/devstack/settings @@ -61,4 +61,13 @@ TROVE_MANAGE=$TROVE_BIN_DIR/trove-manage # Tell Tempest this project is present TEMPEST_SERVICES+=,trove +# By default enable Trove API behind mod-wsgi. Change this to FALSE +# if you don't want Apache/mod-wsgi +TROVE_USE_MOD_WSGI=$(trueorfalse TRUE TROVE_USE_MOD_WSGI) + +TROVE_SERVICE_PORT=${TROVE_SERVICE_PORT:-8779} +TROVE_DEVSTACK_DIR=${TROVE_DIR}/devstack +TROVE_DEVSTACK_FILES=${TROVE_DEVSTACK_DIR}/files +TROVE_WSGI_DIR=${TROVE_WSGI_DIR:-/var/www/trove} + enable_service trove tr-api tr-tmgr tr-cond diff --git a/doc/source/deploy/apache-mod-wsgi.rst b/doc/source/deploy/apache-mod-wsgi.rst new file mode 100644 index 0000000000..f3b8ce527e --- /dev/null +++ b/doc/source/deploy/apache-mod-wsgi.rst @@ -0,0 +1,49 @@ +.. + Except where otherwise noted, this document is licensed under Creative + Commons Attribution 3.0 License. You can view the license at: + + https://creativecommons.org/licenses/by/3.0/ + + +Installing API behind mod_wsgi +============================== + +#. Install the Apache Service:: + + Fedora 21/RHEL7/CentOS7: + sudo yum install httpd + + Fedora 22 (or higher): + sudo dnf install httpd + + Debian/Ubuntu: + apt-get install apache2 + +#. Copy ``etc/apache2/trove`` under the apache sites:: + + Fedora/RHEL7/CentOS7: + sudo cp etc/apache2/trove /etc/httpd/conf.d/trove-api.conf + + Debian/Ubuntu: + sudo cp etc/apache2/trove /etc/apache2/sites-available/trove-api.conf + +#. Edit ``/trove-api.conf`` according to installation + and environment. + + * Modify the ``WSGIDaemonProcess`` directive to set the ``user`` and + ``group`` values to appropriate user on your server. + * Modify the ``WSGIScriptAlias`` directive to point to the + trove/api/app.wsgi script. + * Modify the ``Directory`` directive to set the path to the Trove API + code. + * Modify the ``ErrorLog and CustomLog`` to redirect the logs to the right + directory. + +#. Enable the apache trove site and reload:: + + Fedora/RHEL7/CentOS7: + sudo systemctl reload httpd + + Debian/Ubuntu: + sudo a2ensite trove + sudo service apache2 reload diff --git a/doc/source/index.rst b/doc/source/index.rst index f3ced705db..a7cb4ae21e 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -52,6 +52,7 @@ functionality, the following resources are provided. dev/notifier.rst dev/trove_api_extensions.rst dev/secure_oslo_messaging.rst + deploy/apache-mod-wsgi * Source Code Repositories diff --git a/etc/apache2/trove b/etc/apache2/trove new file mode 100644 index 0000000000..b6341f9567 --- /dev/null +++ b/etc/apache2/trove @@ -0,0 +1,36 @@ +# Copyright 2017 Amrith Kumar. +# All Rights Reserved. +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +# This is an example Apache2 configuration file for using Trove API +through mod_wsgi + +Listen 8779 + + + WSGIDaemonProcess trove-api user=stack group=stack processes=2 threads=2 display-name=%{GROUP} + WSGIScriptAlias / /opt/stack/trove/trove/cmd/app.wsgi + WSGIProcessGroup trove-api + + ErrorLog /var/log/httpd/trove_error.log + LogLevel info + CustomLog /var/log/httpd/trove_access.log combined + + + WSGIProcessGroup trove-api + WSGIApplicationGroup %{GLOBAL} + AllowOverride All + Require all granted + + + diff --git a/integration/scripts/trovestack b/integration/scripts/trovestack index 8b1ba4ec9b..d48cc92c10 100755 --- a/integration/scripts/trovestack +++ b/integration/scripts/trovestack @@ -74,6 +74,7 @@ export RELEASE=${RELEASE:-$DISTRO_RELEASE} # allow overrides from devstack if already set [[ -f $PATH_DEVSTACK_SRC/functions-common ]] && source $PATH_DEVSTACK_SRC/functions-common [[ -f $PATH_DEVSTACK_SRC/functions ]] && source $PATH_DEVSTACK_SRC/functions +[[ -f $PATH_DEVSTACK_SRC/lib/apache ]] && source $PATH_DEVSTACK_SRC/lib/apache # Set up variables for the CONF files - this has to happen after loading trovestack.rc, since # TROVE_CONF_DIR is defined there - these will be used by devstack too diff --git a/trove/cmd/app.wsgi b/trove/cmd/app.wsgi new file mode 100644 index 0000000000..097972e4d3 --- /dev/null +++ b/trove/cmd/app.wsgi @@ -0,0 +1,40 @@ +# Copyright 2017 Amrith Kumar. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +""" +Used for deploying Trove API through mod-wsgi +""" + +from oslo_log import log as logging +from trove.cmd.common import with_initialize +from trove.common import pastedeploy +from trove.common import profile + +LOG = logging.getLogger('trove.cmd.app') + +@with_initialize +def wsgimain(CONF): + from trove.common import cfg + from trove.common import notification + from trove.instance import models as inst_models + + notification.DBaaSAPINotification.register_notify_callback( + inst_models.persist_instance_fault) + cfg.set_api_config_defaults() + profile.setup_profiler('api', CONF.host) + conf_file = CONF.find_file(CONF.api_paste_config) + LOG.debug("Trove started on %s", CONF.host) + return pastedeploy.paste_deploy_app(conf_file, 'trove', {}) + +application = wsgimain()