From bff8cd27ebc8a37756b9f2bae63f686d7d4ae4b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Tr=C4=99bski?= Date: Wed, 1 Mar 2017 14:03:05 +0100 Subject: [PATCH] Adding WSGI support Following commits adds possiblity to run monasca-api with Apache2 (mod_wsgi). Needed-By: Idcc181bbb19a68c622eca51dc9822bde9a91f6a9 Change-Id: Id07dcc249965d97644da9396b6d288255ffaab43 --- README.md | 46 ++++++++++++++++++++++++++++++++++++++- monasca_api/api/server.py | 36 ++++++++++++++++++++++++++---- monasca_api/api/wsgi.py | 20 +++++++++++++++++ 3 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 monasca_api/api/wsgi.py diff --git a/README.md b/README.md index 1c49e5ece..c68db5029 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ located:: /etc/monasca/api-config.ini /etc/monasca/api-config.conf + /etc/monasca/api-logging.conf Once the configurations are modified to match your environment, you can start up the server by following the following instructions. @@ -123,8 +124,51 @@ from the root directory of this project To run all the unit test cases, run the following command from the root directory of this project - $ tox -e py27 (or -e py26, -e py33) + $ tox -e py27 +### Start the Server -- for Apache + +To start the server using Apache: create a modwsgi file, +create a modwsgi configuration file, and enable the wsgi module +in Apache. + +The modwsgi configuration file may look something like this, and the site will need to be enabled: + +```apache + Listen 8070 + + + + WSGIDaemonProcess monasca-api processes=4 threads=1 socket-timeout=120 user=mon-api group=monasca python-path=/usr/local/lib/python2.7/site-packages + WSGIProcessGroup monasca-api + WSGIApplicationGroup monasca-api + WSGIScriptAlias / /usr/local/lib/python2.7/site-packages/monasca_log_api/api/wsgi/monasca_api.py + + WSGIPassAuthorization On + + LogLevel info + ErrorLog /var/log/monasca-api/wsgi.log + CustomLog /var/log/monasca-api/wsgi-access.log combined + + + Require all granted + + + SetEnv no-gzip 1 + + + +``` + +The wsgi file may look something like this: + +```py + + from monasca_log_api.api import server + + application = server.get_wsgi_app(config_base_path='/etc/monasca') + +``` # License diff --git a/monasca_api/api/server.py b/monasca_api/api/server.py index 058701ce0..ebab1c89a 100644 --- a/monasca_api/api/server.py +++ b/monasca_api/api/server.py @@ -1,5 +1,6 @@ # Copyright 2014 IBM Corp # (C) Copyright 2015,2016 Hewlett Packard Enterprise Development LP +# Copyright 2017 Fujitsu LIMITED # # 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 @@ -60,7 +61,10 @@ cfg.CONF.register_opts(dispatcher_opts, dispatcher_group) LOG = log.getLogger(__name__) -def launch(conf, config_file="/etc/monasca/api-config.conf"): +def launch(conf): + # use default, but try to access one passed from conf first + config_file = conf.get('config_file', "/etc/monasca/api-config.conf") + log.register_options(cfg.CONF) log.set_defaults() cfg.CONF(args=[], @@ -130,9 +134,33 @@ def launch(conf, config_file="/etc/monasca/api-config.conf"): return app +def get_wsgi_app(config_base_path=None, **kwargs): + + # allow to override names of the configuration files + config_file = kwargs.get('config_file', 'api-config.conf') + paste_file = kwargs.get('paste_file', 'api-config.ini') + + if config_base_path is None: + # allow monasca-api to be run in dev mode from __main__ + config_base_path = os.path.join( + os.path.dirname(os.path.realpath(__file__)), '../../etc') + + config_file = os.path.join(config_base_path, config_file) + global_conf = {'config_file': config_file} + + LOG.debug('Initializing WSGI application using configuration from %s', + config_base_path) + + return ( + paste.deploy.loadapp( + 'config:%s' % paste_file, + relative_to=config_base_path, + global_conf=global_conf + ) + ) + + if __name__ == '__main__': - wsgi_app = ( - paste.deploy.loadapp('config:etc/api-config.ini', - relative_to=os.getcwd())) + wsgi_app = get_wsgi_app() httpd = simple_server.make_server('127.0.0.1', 8070, wsgi_app) httpd.serve_forever() diff --git a/monasca_api/api/wsgi.py b/monasca_api/api/wsgi.py new file mode 100644 index 000000000..3e4b1c6ac --- /dev/null +++ b/monasca_api/api/wsgi.py @@ -0,0 +1,20 @@ +# Copyright 2017 FUJITSU LIMITED +# +# 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. + +# extremely simple way to setup of monasca-api +# with wsgi + +from monasca_api.api import server + +application = server.get_wsgi_app(config_base_path='/etc/monasca')