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
89 lines
2.8 KiB
Python
89 lines
2.8 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.
|
|
|
|
import six
|
|
import sys
|
|
|
|
from oslo_config import cfg
|
|
import pbr.version
|
|
|
|
from placement import conf
|
|
from placement.db.sqlalchemy import migration
|
|
from placement import db_api
|
|
from placement.i18n import _
|
|
|
|
version_info = pbr.version.VersionInfo('openstack-placement')
|
|
|
|
|
|
class DbCommands(object):
|
|
|
|
def db_sync(self):
|
|
# Let exceptions raise for now, they will go to stderr.
|
|
migration.upgrade('head')
|
|
return 0
|
|
|
|
def db_version(self):
|
|
print(migration.version())
|
|
return 0
|
|
|
|
|
|
def add_db_command_parsers(subparsers):
|
|
command_object = DbCommands()
|
|
|
|
# If we set False here, we avoid having an exit during the parse
|
|
# args part of CONF processing and we can thus print out meaningful
|
|
# help text.
|
|
subparsers.required = False
|
|
parser = subparsers.add_parser('db')
|
|
# Avoid https://bugs.python.org/issue9351 with cpython < 2.7.9
|
|
if not six.PY2:
|
|
parser.set_defaults(func=parser.print_help)
|
|
db_parser = parser.add_subparsers(description='database commands')
|
|
|
|
help = _('Sync the datatabse to the current version.')
|
|
sync_parser = db_parser.add_parser('sync', help=help, description=help)
|
|
sync_parser.set_defaults(func=command_object.db_sync)
|
|
|
|
help = _('Report the current database version.')
|
|
version_parser = db_parser.add_parser(
|
|
'version', help=help, description=help)
|
|
version_parser.set_defaults(func=command_object.db_version)
|
|
|
|
|
|
def setup_commands():
|
|
# This is a separate method because it facilitates unit testing.
|
|
# Use an additional SubCommandOpt and parser for each new sub command.
|
|
command_opt = cfg.SubCommandOpt(
|
|
'db', dest='command', title='Command', help=_('Available DB commands'),
|
|
handler=add_db_command_parsers)
|
|
return [command_opt]
|
|
|
|
|
|
def main():
|
|
config = cfg.ConfigOpts()
|
|
conf.register_opts(config)
|
|
command_opts = setup_commands()
|
|
config.register_cli_opts(command_opts)
|
|
config(sys.argv[1:], project='placement',
|
|
version=version_info.version_string(),
|
|
default_config_files=None)
|
|
db_api.configure(config)
|
|
|
|
try:
|
|
func = config.command.func
|
|
return_code = func()
|
|
# If return_code ends up None we assume 0.
|
|
sys.exit(return_code or 0)
|
|
except cfg.NoSuchOptError:
|
|
config.print_help()
|
|
sys.exit(1)
|