From d8f10c01ae4105a88e8caf58e89041d4c81f1670 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Fri, 19 Jan 2018 12:59:44 +0100 Subject: [PATCH] Allow data migrations to accept options This allows migration to be tuned to a specific deployment. For example, when we will migrate nodes to hardware types, an option will be used to specify what to do with missing interfaces. Change-Id: Ie5045b20b7420fc9b5d864bfb18258a4d8b93334 Related-Bug: #1690185 --- doc/source/cli/ironic-dbsync.rst | 7 ++- ironic/cmd/dbsync.py | 39 +++++++++++--- ironic/tests/unit/cmd/test_dbsync.py | 78 ++++++++++++++++++++-------- 3 files changed, 95 insertions(+), 29 deletions(-) diff --git a/doc/source/cli/ironic-dbsync.rst b/doc/source/cli/ironic-dbsync.rst index bba2633d8c..e45f22166e 100644 --- a/doc/source/cli/ironic-dbsync.rst +++ b/doc/source/cli/ironic-dbsync.rst @@ -112,6 +112,11 @@ online_data_migrations If not specified, all the objects will be migrated (in batches of 50 to avoid locking the database for long periods of time). +.. option:: --option + + If a migration accepts additional parameters, they can be passed via this + argument. It can be specified several times. + This command will migrate objects in the database to their most recent versions. This command must be successfully run (return code 0) before upgrading to a future release. @@ -124,7 +129,7 @@ It returns: * 0 (success) after migrations are finished or there are no data to migrate -* 127 (error) if max-count is not a positive value +* 127 (error) if max-count is not a positive value or an option is invalid * 2 (error) if the database is not compatible with this release. This command needs to be run using the previous release of ironic, before upgrading and diff --git a/ironic/cmd/dbsync.py b/ironic/cmd/dbsync.py index 0450ffede0..30b484df38 100644 --- a/ironic/cmd/dbsync.py +++ b/ironic/cmd/dbsync.py @@ -111,9 +111,10 @@ class DBCommand(object): def online_data_migrations(self): self._check_versions() - self._run_online_data_migrations(max_count=CONF.command.max_count) + self._run_online_data_migrations(max_count=CONF.command.max_count, + options=CONF.command.options) - def _run_migration_functions(self, context, max_count): + def _run_migration_functions(self, context, max_count, options): """Runs the migration functions. Runs the data migration functions in the ONLINE_MIGRATIONS list. @@ -124,6 +125,8 @@ class DBCommand(object): :param: context: an admin context :param: max_count: the maximum number of objects (rows) to migrate; a value >= 1. + :param: options: migration options - dict mapping migration name + to a dictionary of options for this migration. :raises: Exception from the migration function :returns: Boolean value indicating whether migrations are done. Returns False if max_count objects have been migrated (since at that @@ -135,10 +138,12 @@ class DBCommand(object): for migration_func_obj, migration_func_name in ONLINE_MIGRATIONS: migration_func = getattr(migration_func_obj, migration_func_name) + migration_opts = options.get(migration_func_name, {}) num_to_migrate = max_count - total_migrated try: total_to_do, num_migrated = migration_func(context, - num_to_migrate) + num_to_migrate, + **migration_opts) except Exception as e: print(_("Error while running %(migration)s: %(err)s.") % {'migration': migration_func.__name__, 'err': e}, @@ -165,7 +170,7 @@ class DBCommand(object): return True - def _run_online_data_migrations(self, max_count=None): + def _run_online_data_migrations(self, max_count=None, options=None): """Perform online data migrations for the release. Online data migrations are done by running all the data migration @@ -177,13 +182,27 @@ class DBCommand(object): :param: max_count: the maximum number of individual object migrations or modified rows, a value >= 1. If None, migrations are run in a loop in batches of 50, until completion. + :param: options: options to pass to migrations. List of values in the + form of .