Files
barbican/barbican/api/app.py
T
Ade Lee 0539a28ee9 Ensure only api app initializes secret store
Right now, the multiple secret store initialization code is run
whenever the db is initialized - whether it be running the clean
db script, starting the worker, starting the keystone listener
or manage db script.  This periodically causes deadlocks when
the worker,listener and app are started soon after each other.

Its not altogether clear why the deadlock happens, but the only
table that is being written to is the secret_store table, which
has no foreign keys etc.  In any case, though, it was never the
intention that anything other than the app itself initialize the
secret stores from its config file.

This patch makes sure that happens.

Change-Id: I711b91b19b9d65260a21b41d6f9e18b9e282138a
Closes-bug: 1738863
2017-12-20 10:54:43 -05:00

113 lines
3.3 KiB
Python

# Copyright (c) 2013-2015 Rackspace, Inc.
#
# 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.
"""
API application handler for Barbican
"""
import os
from paste import deploy
import pecan
try:
import newrelic.agent
newrelic_loaded = True
except ImportError:
newrelic_loaded = False
from oslo_log import log
from barbican.api.controllers import versions
from barbican.api import hooks
from barbican.common import config
from barbican.model import repositories
from barbican import queue
CONF = config.CONF
if newrelic_loaded:
newrelic.agent.initialize(
os.environ.get('NEW_RELIC_CONFIG_FILE', '/etc/newrelic/newrelic.ini'),
os.environ.get('NEW_RELIC_ENVIRONMENT')
)
def build_wsgi_app(controller=None, transactional=False):
"""WSGI application creation helper
:param controller: Overrides default application controller
:param transactional: Adds transaction hook for all requests
"""
request_hooks = [hooks.JSONErrorHook()]
if transactional:
request_hooks.append(hooks.BarbicanTransactionHook())
if newrelic_loaded:
request_hooks.insert(0, hooks.NewRelicHook())
# Create WSGI app
wsgi_app = pecan.Pecan(
controller or versions.AVAILABLE_VERSIONS[versions.DEFAULT_VERSION](),
hooks=request_hooks,
force_canonical=False
)
# clear the session created in controller initialization 60
repositories.clear()
return wsgi_app
def main_app(func):
def _wrapper(global_config, **local_conf):
# Queuing initialization
queue.init(CONF, is_server_side=False)
# Configure oslo logging and configuration services.
log.setup(CONF, 'barbican')
config.setup_remote_pydev_debug()
# Initializing the database engine and session factory before the app
# starts ensures we don't lose requests due to lazy initialization of
# db connections.
repositories.setup_database_engine_and_factory(
initialize_secret_stores=True
)
wsgi_app = func(global_config, **local_conf)
if newrelic_loaded:
wsgi_app = newrelic.agent.WSGIApplicationWrapper(wsgi_app)
LOG = log.getLogger(__name__)
LOG.info('Barbican app created and initialized')
return wsgi_app
return _wrapper
@main_app
def create_main_app(global_config, **local_conf):
"""uWSGI factory method for the Barbican-API application."""
# Setup app with transactional hook enabled
return build_wsgi_app(versions.V1Controller(), transactional=True)
def create_version_app(global_config, **local_conf):
wsgi_app = pecan.make_app(versions.VersionsController())
return wsgi_app
def get_api_wsgi_script():
conf = '/etc/barbican/barbican-api-paste.ini'
application = deploy.loadapp('config:%s' % conf)
return application