', help='Current Database version')
**** CubicPower OpenStack Study ****
def sync(self, version=None, current_version=None):
"""
Place a database under migration control and upgrade/downgrade it,
creating first if necessary.
"""
if current_version not in (None, 'None'):
migration.db_version_control(db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH,
version=current_version)
migration.db_sync(db_api.get_engine(),
db_migration.MIGRATE_REPO_PATH,
version,
sanity_check=self._need_sanity_check())
**** CubicPower OpenStack Study ****
class DbLegacyCommands(object):
"""Class for managing the db using legacy commands"""
**** CubicPower OpenStack Study ****
def __init__(self, command_object):
self.command_object = command_object
**** CubicPower OpenStack Study ****
def version(self):
self.command_object.version()
**** CubicPower OpenStack Study ****
def upgrade(self, version=None):
self.command_object.upgrade(CONF.command.version)
**** CubicPower OpenStack Study ****
def downgrade(self, version=None):
self.command_object.downgrade(CONF.command.version)
**** CubicPower OpenStack Study ****
def version_control(self, version=None):
self.command_object.version_control(CONF.command.version)
**** CubicPower OpenStack Study ****
def sync(self, version=None, current_version=None):
self.command_object.sync(CONF.command.version,
CONF.command.current_version)
def add_legacy_command_parsers(command_object, subparsers):
legacy_command_object = DbLegacyCommands(command_object)
parser = subparsers.add_parser('db_version')
parser.set_defaults(action_fn=legacy_command_object.version)
parser.set_defaults(action='db_version')
parser = subparsers.add_parser('db_upgrade')
parser.set_defaults(action_fn=legacy_command_object.upgrade)
parser.add_argument('version', nargs='?')
parser.set_defaults(action='db_upgrade')
parser = subparsers.add_parser('db_downgrade')
parser.set_defaults(action_fn=legacy_command_object.downgrade)
parser.add_argument('version')
parser.set_defaults(action='db_downgrade')
parser = subparsers.add_parser('db_version_control')
parser.set_defaults(action_fn=legacy_command_object.version_control)
parser.add_argument('version', nargs='?')
parser.set_defaults(action='db_version_control')
parser = subparsers.add_parser('db_sync')
parser.set_defaults(action_fn=legacy_command_object.sync)
parser.add_argument('version', nargs='?')
parser.add_argument('current_version', nargs='?')
parser.set_defaults(action='db_sync')
def add_command_parsers(subparsers):
command_object = DbCommands()
parser = subparsers.add_parser('db')
parser.set_defaults(command_object=command_object)
category_subparsers = parser.add_subparsers(dest='action')
for (action, action_fn) in methods_of(command_object):
parser = category_subparsers.add_parser(action)
action_kwargs = []
for args, kwargs in getattr(action_fn, 'args', []):
# FIXME(basha): hack to assume dest is the arg name without
# the leading hyphens if no dest is supplied
kwargs.setdefault('dest', args[0][2:])
if kwargs['dest'].startswith('action_kwarg_'):
action_kwargs.append(
kwargs['dest'][len('action_kwarg_'):])
else:
action_kwargs.append(kwargs['dest'])
kwargs['dest'] = 'action_kwarg_' + kwargs['dest']
parser.add_argument(*args, **kwargs)
parser.set_defaults(action_fn=action_fn)
parser.set_defaults(action_kwargs=action_kwargs)
parser.add_argument('action_args', nargs='*')
add_legacy_command_parsers(command_object, subparsers)
command_opt = cfg.SubCommandOpt('command',
title='Commands',
help='Available commands',
handler=add_command_parsers)
def methods_of(obj):
"""Get all callable methods of an object that don't start with underscore
returns a list of tuples of the form (method_name, method)
"""
result = []
for i in dir(obj):
if callable(getattr(obj, i)) and not i.startswith('_'):
result.append((i, getattr(obj, i)))
return result
def main():
CONF.register_cli_opt(command_opt)
try:
cfg_files = cfg.find_config_files(project='glance',
prog='glance-registry')
cfg_files.extend(cfg.find_config_files(project='glance',
prog='glance-api'))
config.parse_args(default_config_files=cfg_files,
usage="%(prog)s [options] ") log.setup('glance')
except RuntimeError as e:
sys.exit("ERROR: %s" % e)
try:
if CONF.command.action.startswith('db'):
return CONF.command.action_fn()
else:
func_kwargs = {}
for k in CONF.command.action_kwargs:
v = getattr(CONF.command, 'action_kwarg_' + k)
if v is None:
continue
func_kwargs[k] = strutils.safe_decode(v)
func_args = [strutils.safe_decode(arg)
for arg in CONF.command.action_args]
return CONF.command.action_fn(*func_args, **func_kwargs)
except exception.GlanceException as e:
sys.exit("ERROR: %s" % e)
if __name__ == '__main__':
main()
**** CubicPower OpenStack Study ****
def add_legacy_command_parsers(command_object, subparsers):
legacy_command_object = DbLegacyCommands(command_object)
parser = subparsers.add_parser('db_version')
parser.set_defaults(action_fn=legacy_command_object.version)
parser.set_defaults(action='db_version')
parser = subparsers.add_parser('db_upgrade')
parser.set_defaults(action_fn=legacy_command_object.upgrade)
parser.add_argument('version', nargs='?')
parser.set_defaults(action='db_upgrade')
parser = subparsers.add_parser('db_downgrade')
parser.set_defaults(action_fn=legacy_command_object.downgrade)
parser.add_argument('version')
parser.set_defaults(action='db_downgrade')
parser = subparsers.add_parser('db_version_control')
parser.set_defaults(action_fn=legacy_command_object.version_control)
parser.add_argument('version', nargs='?')
parser.set_defaults(action='db_version_control')
parser = subparsers.add_parser('db_sync')
parser.set_defaults(action_fn=legacy_command_object.sync)
parser.add_argument('version', nargs='?')
parser.add_argument('current_version', nargs='?')
parser.set_defaults(action='db_sync')
**** CubicPower OpenStack Study ****
def add_command_parsers(subparsers):
command_object = DbCommands()
parser = subparsers.add_parser('db')
parser.set_defaults(command_object=command_object)
category_subparsers = parser.add_subparsers(dest='action')
for (action, action_fn) in methods_of(command_object):
parser = category_subparsers.add_parser(action)
action_kwargs = []
for args, kwargs in getattr(action_fn, 'args', []):
# FIXME(basha): hack to assume dest is the arg name without
# the leading hyphens if no dest is supplied
kwargs.setdefault('dest', args[0][2:])
if kwargs['dest'].startswith('action_kwarg_'):
action_kwargs.append(
kwargs['dest'][len('action_kwarg_'):])
else:
action_kwargs.append(kwargs['dest'])
kwargs['dest'] = 'action_kwarg_' + kwargs['dest']
parser.add_argument(*args, **kwargs)
parser.set_defaults(action_fn=action_fn)
parser.set_defaults(action_kwargs=action_kwargs)
parser.add_argument('action_args', nargs='*')
add_legacy_command_parsers(command_object, subparsers)
command_opt = cfg.SubCommandOpt('command',
title='Commands',
help='Available commands',
handler=add_command_parsers)
**** CubicPower OpenStack Study ****
def methods_of(obj):
"""Get all callable methods of an object that don't start with underscore
returns a list of tuples of the form (method_name, method)
"""
result = []
for i in dir(obj):
if callable(getattr(obj, i)) and not i.startswith('_'):
result.append((i, getattr(obj, i)))
return result
**** CubicPower OpenStack Study ****
def main():
CONF.register_cli_opt(command_opt)
try:
cfg_files = cfg.find_config_files(project='glance',
prog='glance-registry')
cfg_files.extend(cfg.find_config_files(project='glance',
prog='glance-api'))
config.parse_args(default_config_files=cfg_files,
usage="%(prog)s [options] ") log.setup('glance')
except RuntimeError as e:
sys.exit("ERROR: %s" % e)
try:
if CONF.command.action.startswith('db'):
return CONF.command.action_fn()
else:
func_kwargs = {}
for k in CONF.command.action_kwargs:
v = getattr(CONF.command, 'action_kwarg_' + k)
if v is None:
continue
func_kwargs[k] = strutils.safe_decode(v)
func_args = [strutils.safe_decode(arg)
for arg in CONF.command.action_args]
return CONF.command.action_fn(*func_args, **func_kwargs)
except exception.GlanceException as e:
sys.exit("ERROR: %s" % e)
if __name__ == '__main__':
main()