From ea89376494e87ac25f8c2720a3fed86b6ee50974 Mon Sep 17 00:00:00 2001 From: LisaLi Date: Fri, 19 Feb 2016 16:39:38 +0800 Subject: [PATCH] Add update_host for backup in cinder-manager As scaling backup is imported, if users sepecify backup_use_same_host, they have to manually update backups' host. Change-Id: Ic031cae16f4c58fa06d92e979fe2b83d244832fc --- cinder/cmd/manage.py | 14 ++++++++++++++ cinder/tests/unit/test_cmd.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/cinder/cmd/manage.py b/cinder/cmd/manage.py index 758c8d1b6..a983dda6e 100644 --- a/cinder/cmd/manage.py +++ b/cinder/cmd/manage.py @@ -414,6 +414,20 @@ class BackupCommands(object): backup['size'], object_count)) + @args('--currenthost', required=True, help='Existing backup host name') + @args('--newhost', required=True, help='New backup host name') + def update_backup_host(self, currenthost, newhost): + """Modify the host name associated with a backup. + + Particularly to recover from cases where one has moved + their Cinder Backup node, and not set backup_use_same_backend. + """ + ctxt = context.get_admin_context() + backups = objects.BackupList.get_all_by_host(ctxt, currenthost) + for bk in backups: + bk.host = newhost + bk.save() + class ServiceCommands(object): """Methods for managing services.""" diff --git a/cinder/tests/unit/test_cmd.py b/cinder/tests/unit/test_cmd.py index cbcccb849..a095caa9f 100644 --- a/cinder/tests/unit/test_cmd.py +++ b/cinder/tests/unit/test_cmd.py @@ -39,6 +39,7 @@ from cinder import context from cinder import exception from cinder.objects import fields from cinder import test +from cinder.tests.unit import fake_constants from cinder.tests.unit import fake_volume from cinder import version @@ -665,6 +666,34 @@ class TestCinderManageCmd(test.TestCase): None, None, None) self.assertEqual(expected_out, fake_out.getvalue()) + @mock.patch('cinder.db.backup_update') + @mock.patch('cinder.db.backup_get_all_by_host') + @mock.patch('cinder.context.get_admin_context') + def test_update_backup_host(self, get_admin_context, + backup_get_by_host, + backup_update): + ctxt = context.RequestContext('fake-user', 'fake-project') + get_admin_context.return_value = ctxt + backup = {'id': fake_constants.backup_id, + 'user_id': 'fake-user-id', + 'project_id': 'fake-project-id', + 'host': 'fake-host', + 'display_name': 'fake-display-name', + 'container': 'fake-container', + 'status': fields.BackupStatus.AVAILABLE, + 'size': 123, + 'object_count': 1, + 'volume_id': 'fake-volume-id', + } + backup_get_by_host.return_value = [backup] + backup_cmds = cinder_manage.BackupCommands() + backup_cmds.update_backup_host('fake_host', 'fake_host2') + + get_admin_context.assert_called_once_with() + backup_get_by_host.assert_called_once_with(ctxt, 'fake_host') + backup_update.assert_called_once_with(ctxt, fake_constants.backup_id, + {'host': 'fake_host2'}) + @mock.patch('cinder.utils.service_is_up') @mock.patch('cinder.db.service_get_all') @mock.patch('cinder.context.get_admin_context') -- 2.45.2