110 lines
3.5 KiB
Python
110 lines
3.5 KiB
Python
# 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.
|
|
|
|
from flask import Flask
|
|
import os
|
|
from oslo_config import cfg
|
|
import oslo_messaging
|
|
from python_nemesis.config import collect_sqlalchemy_opts
|
|
from python_nemesis.config import register_opts
|
|
from python_nemesis.extensions import db
|
|
from python_nemesis.extensions import keystone
|
|
from python_nemesis.extensions import log
|
|
|
|
|
|
def configure_blueprints(app, blueprints):
|
|
"""Register configured blueprints into app object.
|
|
|
|
:param app: The application object to which configuration should
|
|
be applied.
|
|
:type app: :py:class:`flask.Flask`
|
|
:param blueprints: list of blueprints to be registered.
|
|
:type blueprints: list(:py:class:`flask.Blueprint`)
|
|
"""
|
|
for blueprint in blueprints:
|
|
app.register_blueprint(blueprint)
|
|
|
|
|
|
def configure_app(app):
|
|
"""Retrieve App Configuration.
|
|
|
|
configure_app first loads default configuration and then attempts
|
|
to override defaults using a file specified in env:NEMESIS_CONFIG.
|
|
|
|
:param app: The application object to which configuration should
|
|
be applied.
|
|
:type app: :py:class:`flask.Flask`
|
|
"""
|
|
app.config.from_object('python_nemesis.default_config')
|
|
app.config["cfg"] = cfg.CONF
|
|
config_file = os.environ.get(
|
|
"NEMESIS_CONFIG",
|
|
"/etc/nemesis/nemesis.conf")
|
|
|
|
register_opts(app.config["cfg"], config_file)
|
|
collect_sqlalchemy_opts(app, app.config["cfg"])
|
|
|
|
|
|
def configure_extensions(app):
|
|
"""Initialize extensions for Flask.
|
|
|
|
This function is intended for use with the app factory style
|
|
of Flask deployment.
|
|
|
|
:param app: The application object to which configuration should
|
|
be applied.
|
|
:type app: :py:class:`flask.Flask`
|
|
"""
|
|
db.init_app(app)
|
|
log.init_app(app)
|
|
keystone.init_app(app)
|
|
|
|
|
|
def configure_notifier(app):
|
|
transport = oslo_messaging.get_notification_transport(app.config['cfg'])
|
|
topics = ['nemesis_notifications']
|
|
app.config["notifier"] = oslo_messaging.Notifier(transport,
|
|
'nemesis.api',
|
|
driver='messagingv2',
|
|
topics=topics)
|
|
|
|
|
|
def create_app(app_name=None, blueprints=None):
|
|
"""Create the flask app.
|
|
|
|
This function is intended to be used with the app factory
|
|
style of Flask deployment.
|
|
|
|
:param str app_name: Name to be used internally within flask.
|
|
:param blueprints: Blueprints to be registered.
|
|
:type blueprints: list(:py:class:`flask.Blueprint`)
|
|
:returns: The created app.
|
|
:rtype: :py:class:`flask.Flask`
|
|
"""
|
|
app = Flask(app_name)
|
|
|
|
configure_app(app)
|
|
configure_extensions(app)
|
|
configure_notifier(app)
|
|
|
|
# Here we register the application blueprints.
|
|
from python_nemesis.api.v1 import V1_API
|
|
blueprints = [V1_API]
|
|
configure_blueprints(app, blueprints)
|
|
|
|
return app
|
|
|
|
|
|
if __name__ == "__main__": # pragma: no cover
|
|
app = create_app('nemesis-api')
|
|
app.run(threaded=True)
|