From 365cfa35164e411ae88d7404394b5f073c64d126 Mon Sep 17 00:00:00 2001 From: Maurice Escher Date: Tue, 26 May 2020 18:22:54 +0200 Subject: [PATCH] add share server update to manila-manage share update_host Change-Id: Ifb19d926a84fae986b8d296959cfce71bf0c2e9e Closes-Bug: #1881098 --- manila/cmd/manage.py | 7 ++++++- manila/db/api.py | 5 +++++ manila/db/sqlalchemy/api.py | 14 ++++++++++++++ manila/tests/cmd/test_manage.py | 14 ++++++++++---- ...-manage-share-update-host-bbbc4fe2da48cae9.yaml | 5 +++++ 5 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 releasenotes/notes/bug-1881098-add-share-server-update-to-manila-manage-share-update-host-bbbc4fe2da48cae9.yaml diff --git a/manila/cmd/manage.py b/manila/cmd/manage.py index b0a2e5ac04..4d09e37c0a 100644 --- a/manila/cmd/manage.py +++ b/manila/cmd/manage.py @@ -364,7 +364,7 @@ class ShareCommands(object): @args('--force', required=False, type=bool, default=False, help="Ignore validations.") def update_host(self, current_host, new_host, force=False): - """Modify the host name associated with a share. + """Modify the host name associated with a share and share server. Particularly to recover from cases where one has moved their Manila Share node, or modified their 'host' opt @@ -378,6 +378,11 @@ class ShareCommands(object): "to %(nhost)s." % {'count': updated, 'chost': current_host, 'nhost': new_host}) + servers = db.share_servers_host_update(ctxt, current_host, new_host) + print("Updated host of %(count)s share servers on %(chost)s " + "to %(nhost)s." % {'count': servers, 'chost': current_host, + 'nhost': new_host}) + CATEGORIES = { 'config': ConfigCommands, diff --git a/manila/db/api.py b/manila/db/api.py index 73c1bba153..60bbc3ebe5 100644 --- a/manila/db/api.py +++ b/manila/db/api.py @@ -991,6 +991,11 @@ def share_server_backend_details_set(context, share_server_id, server_details): server_details) +def share_servers_host_update(context, current_host, new_host): + """Update the host attr of all share servers that are on current_host.""" + return IMPL.share_servers_host_update(context, current_host, new_host) + + ################## diff --git a/manila/db/sqlalchemy/api.py b/manila/db/sqlalchemy/api.py index b950463d7a..a20a8f486a 100644 --- a/manila/db/sqlalchemy/api.py +++ b/manila/db/sqlalchemy/api.py @@ -3867,6 +3867,20 @@ def share_server_backend_details_delete(context, share_server_id, item.soft_delete(session) +@require_admin_context +def share_servers_host_update(context, current_host, new_host): + session = get_session() + host_field = models.ShareServer.host + with session.begin(): + query = model_query( + context, models.ShareServer, session=session, read_deleted="no", + ).filter(host_field.like('{}%'.format(current_host))) + result = query.update( + {host_field: func.replace(host_field, current_host, new_host)}, + synchronize_session=False) + return result + + ################### def _driver_private_data_query(session, context, entity_id, key=None, diff --git a/manila/tests/cmd/test_manage.py b/manila/tests/cmd/test_manage.py index fecc70e45a..5fc4eea818 100644 --- a/manila/tests/cmd/test_manage.py +++ b/manila/tests/cmd/test_manage.py @@ -392,13 +392,19 @@ class ManilaCmdManageTestCase(test.TestCase): mock.Mock(return_value='admin_ctxt')) self.mock_object(db, 'share_instances_host_update', mock.Mock(return_value=20)) + self.mock_object(db, 'share_servers_host_update', + mock.Mock(return_value=5)) with mock.patch('sys.stdout', new=six.StringIO()) as intercepted_op: self.share_cmds.update_host(current_host, new_host, force) - expected_op = ("Updated host of 20 share instances on " - "%(chost)s to %(nhost)s." % - {'chost': current_host, 'nhost': new_host}) - self.assertEqual(expected_op, intercepted_op.getvalue().strip()) + expected_op_si = ("Updated host of 20 share instances on " + "%(chost)s to %(nhost)s." % + {'chost': current_host, 'nhost': new_host}) + expected_op_sv = ("Updated host of 5 share servers on " + "%(chost)s to %(nhost)s." % + {'chost': current_host, 'nhost': new_host}) + self.assertEqual(expected_op_si + "\n" + expected_op_sv, + intercepted_op.getvalue().strip()) db.share_instances_host_update.assert_called_once_with( 'admin_ctxt', current_host, new_host) diff --git a/releasenotes/notes/bug-1881098-add-share-server-update-to-manila-manage-share-update-host-bbbc4fe2da48cae9.yaml b/releasenotes/notes/bug-1881098-add-share-server-update-to-manila-manage-share-update-host-bbbc4fe2da48cae9.yaml new file mode 100644 index 0000000000..54082f821d --- /dev/null +++ b/releasenotes/notes/bug-1881098-add-share-server-update-to-manila-manage-share-update-host-bbbc4fe2da48cae9.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + The ``manila-manage share update_host`` command now updates the host + attribute of share servers in addition to shares.