
This change was driven out of trying to get nova functional tests working with an extracted placement, starting with getting the database fixture cleaner. Perhaps not surprisingly, trying to share the same 'cfg.CONF' between two services is rather fraught. Rather than trying to tease out all the individual issues, which is a very time consuming effort for not much gain, a different time consuming effort with great gain was tried instead. This patch removes the use of the default global cfg.CONF that oslo_config (optionally) provides and instead ensures that at the various ways in which one might enter placement: wsgi, cli, tests, the config is generated and managed in a more explicit fashion. Unfortunately this is a large change, but there's no easy way to do it in incremental chunks without getting very confused and having tests pass. There are a few classes of changes here, surrounded by various cleanups to address their addition. Quite a few holes were found in how config is managed, especially in tests where often we were getting what we wanted pretty much by accident. The big changes: * Importing placement.conf does not automatically register options with the global conf. Instead there is a now a register_opts method to which a ConfigOpts() is required. * Because of policy enforcement wanting access to conf, a convenient way of having the config pass through context.can() was needed. At the start of PlacementHandler (the main dispatch routine) the current config (provided to the PlacementHandler at application configuration time) is set as an attribute on the RequestContext. This is also used where CONF is required in the objects, such as randomizing the limited allocation canidates. * Passing in config to PlacementHandler changes the way the gabbi fixture loads the WSGI application. To work around a shortcoming in gabbi the fixture needs to CONF global. This is _not_ an oslo_config.cfg.CONF global, but something used locally in the fixture to set a different config per gabbi test suite. * The --sql command for alembic commands has been disabled. We don't really need that and it would require some messing about with config. The command lets you dump raw sql intead of migration files. * PlacementFixture, for use by nova, has been expanded to create and manage its config, database and policy requirements using non-global config. It can also accept a previously prepared config. * The Database fixtures calls 'reset()' in both setUp and cleanUp to be certain we are both starting and ending in a known state that will not disturb or be disturbed by other tests. This adds confidence (but not a guarantee) that in tests that run with eventlet (as in nova) things are in more consistent state. * Configuring the db in the Database fixture is moved into setUp where it should have been all along, but is important to be there _after_ 'reset()'. These of course cascade other changes all over the place. Especially the need to manually register_opts. There are probably refactorings that can be done or base classes that can be removed. Command line tools (e.g. status) which are mostly based on external libraries continue to use config in the pre-existing way. A lock fixture for the opportunistic migration tests has been added. There was a lock fixture previously, provided by oslo_concurrency, but it, as far as I can tell, requires global config. We don't want that. Things that will need to be changed as a result of these changes: * The goals doc at https://review.openstack.org/#/c/618811/ will need to be changed to say "keep it this way" rather than "get it this way". Change-Id: Icd629d7cd6d68ca08f9f3b4f0465c3d9a1efeb22
138 lines
4.1 KiB
Python
138 lines
4.1 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.
|
|
"""WSGI script for Placement API
|
|
|
|
WSGI handler for running Placement API under Apache2, nginx, gunicorn etc.
|
|
"""
|
|
|
|
import logging as py_logging
|
|
import os
|
|
import os.path
|
|
|
|
from oslo_config import cfg
|
|
from oslo_log import log as logging
|
|
from oslo_middleware import cors
|
|
from oslo_policy import opts as policy_opts
|
|
from oslo_utils import importutils
|
|
import pbr.version
|
|
|
|
from placement import conf
|
|
from placement import db_api
|
|
from placement import deploy
|
|
|
|
|
|
profiler = importutils.try_import('osprofiler.opts')
|
|
|
|
|
|
CONFIG_FILE = 'placement.conf'
|
|
|
|
|
|
# The distribution name is required here, not package.
|
|
version_info = pbr.version.VersionInfo('openstack-placement')
|
|
|
|
|
|
def setup_logging(config):
|
|
# Any dependent libraries that have unhelp debug levels should be
|
|
# pinned to a higher default.
|
|
extra_log_level_defaults = [
|
|
'routes=INFO',
|
|
]
|
|
logging.set_defaults(default_log_levels=logging.get_default_log_levels() +
|
|
extra_log_level_defaults)
|
|
logging.setup(config, 'placement')
|
|
py_logging.captureWarnings(True)
|
|
|
|
|
|
def _get_config_files(env=None):
|
|
"""Return a list of one file or None describing config location.
|
|
|
|
If None, that means oslo.config will look in the default locations
|
|
for a config file.
|
|
"""
|
|
if env is None:
|
|
env = os.environ
|
|
|
|
dirname = env.get('OS_PLACEMENT_CONFIG_DIR', '').strip()
|
|
if dirname:
|
|
return [os.path.join(dirname, CONFIG_FILE)]
|
|
else:
|
|
return None
|
|
|
|
|
|
def _parse_args(config, argv, default_config_files):
|
|
# register placement's config options
|
|
conf.register_opts(config)
|
|
logging.register_options(config)
|
|
|
|
if profiler:
|
|
profiler.set_defaults(config)
|
|
|
|
_set_middleware_defaults()
|
|
|
|
# This is needed so we can check [oslo_policy]/enforce_scope in the
|
|
# deploy module.
|
|
policy_opts.set_defaults(config)
|
|
|
|
config(argv[1:], project='placement',
|
|
version=version_info.version_string(),
|
|
default_config_files=default_config_files)
|
|
|
|
|
|
def _set_middleware_defaults():
|
|
"""Update default configuration options for oslo.middleware."""
|
|
cors.set_defaults(
|
|
allow_headers=['X-Auth-Token',
|
|
'X-Openstack-Request-Id',
|
|
'X-Identity-Status',
|
|
'X-Roles',
|
|
'X-Service-Catalog',
|
|
'X-User-Id',
|
|
'X-Tenant-Id'],
|
|
expose_headers=['X-Auth-Token',
|
|
'X-Openstack-Request-Id',
|
|
'X-Subject-Token',
|
|
'X-Service-Token'],
|
|
allow_methods=['GET',
|
|
'PUT',
|
|
'POST',
|
|
'DELETE',
|
|
'PATCH']
|
|
)
|
|
|
|
|
|
def init_application():
|
|
# initialize the config system
|
|
conffiles = _get_config_files()
|
|
|
|
config = cfg.ConfigOpts()
|
|
conf.register_opts(config)
|
|
|
|
# This will raise cfg.RequiredOptError when a required option is not set
|
|
# (notably the database connection string). We want this to be a hard fail
|
|
# that prevents the application from starting. The error will show up in
|
|
# the wsgi server's logs.
|
|
_parse_args(config, [], default_config_files=conffiles)
|
|
# initialize the logging system
|
|
setup_logging(config)
|
|
|
|
# configure database
|
|
db_api.configure(config)
|
|
|
|
# dump conf at debug if log_options
|
|
if config.log_options:
|
|
config.log_opt_values(
|
|
logging.getLogger(__name__),
|
|
logging.DEBUG)
|
|
|
|
# build and return our WSGI app
|
|
return deploy.loadapp(config)
|