From 9909d9b176368cd8a88a356565048c22780f3f40 Mon Sep 17 00:00:00 2001 From: Juan Larriba Date: Thu, 14 Jul 2022 15:01:04 +0200 Subject: [PATCH] Support remove and revert actions The CLI should not only support creating snapshots, but it also must support two other actions: remove and revert. Remove deletes all the snapshots that were created in a previous execution, while revert loads the information stored in the snapshots to the main disks, leaving them in the same state they were when the snapshots were created. Change-Id: I75925c749acd26cbdf62991e7e0ec87d906d5def --- .../tests/v1/overcloud_backup/test_backup.py | 88 ++++++++++++++++++- tripleoclient/v1/overcloud_backup.py | 28 +++++- 2 files changed, 112 insertions(+), 4 deletions(-) diff --git a/tripleoclient/tests/v1/overcloud_backup/test_backup.py b/tripleoclient/tests/v1/overcloud_backup/test_backup.py index 44693d9cf..f5f3ecf36 100644 --- a/tripleoclient/tests/v1/overcloud_backup/test_backup.py +++ b/tripleoclient/tests/v1/overcloud_backup/test_backup.py @@ -403,7 +403,89 @@ class TestOvercloudSnapshot(utils.TestCommand): workdir=mock.ANY, playbook='cli-overcloud-snapshot.yaml', inventory=parsed_args.inventory, - tags=None, + tags='create_snapshots', + skip_tags=None, + playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS, + verbosity=3, + extra_vars={} + ) + + @mock.patch('os.path.isfile') + @mock.patch('os.access') + @mock.patch('tripleoclient.utils.run_ansible_playbook', + autospec=True) + def test_overcloud_snapshot_revert_remove(self, + mock_playbook, + mock_access, + mock_isfile): + arglist = [ + '--remove', + '--revert', + ] + verifylist = [] + mock_isfile.return_value = True + mock_access.return_value = True + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.assertRaisesRegex( + RuntimeError, + '--revert and --remove are mutually exclusive', + self.cmd.take_action, + parsed_args) + + @mock.patch('os.path.isfile') + @mock.patch('os.access') + @mock.patch('tripleoclient.utils.run_ansible_playbook', + autospec=True) + def test_overcloud_snapshot_revert(self, + mock_playbook, + mock_access, + mock_isfile): + arglist = [ + '--revert', + ] + verifylist = [] + mock_isfile.return_value = True + mock_access.return_value = True + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + mock_playbook.assert_called_once_with( + workdir=mock.ANY, + playbook='cli-overcloud-snapshot.yaml', + inventory=parsed_args.inventory, + tags='revert_snapshots', + skip_tags=None, + playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS, + verbosity=3, + extra_vars={} + ) + + @mock.patch('os.path.isfile') + @mock.patch('os.access') + @mock.patch('tripleoclient.utils.run_ansible_playbook', + autospec=True) + def test_overcloud_snapshot_remove(self, + mock_playbook, + mock_access, + mock_isfile): + arglist = [ + '--remove', + ] + verifylist = [] + mock_isfile.return_value = True + mock_access.return_value = True + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + mock_playbook.assert_called_once_with( + workdir=mock.ANY, + playbook='cli-overcloud-snapshot.yaml', + inventory=parsed_args.inventory, + tags='remove_snapshots', skip_tags=None, playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS, verbosity=3, @@ -425,7 +507,7 @@ class TestOvercloudSnapshot(utils.TestCommand): workdir=mock.ANY, playbook='cli-overcloud-snapshot.yaml', inventory=parsed_args.inventory, - tags=None, + tags='create_snapshots', skip_tags=None, playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS, verbosity=3, @@ -458,7 +540,7 @@ class TestOvercloudSnapshot(utils.TestCommand): workdir=mock.ANY, playbook='cli-overcloud-snapshot.yaml', inventory=parsed_args.inventory, - tags=None, + tags='create_snapshots', skip_tags=None, playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS, verbosity=3, diff --git a/tripleoclient/v1/overcloud_backup.py b/tripleoclient/v1/overcloud_backup.py index 29d45b439..f4a8ff332 100644 --- a/tripleoclient/v1/overcloud_backup.py +++ b/tripleoclient/v1/overcloud_backup.py @@ -293,6 +293,22 @@ class BackupSnapshot(command.Command): "Defaults to: " + INVENTORY) ) + parser.add_argument( + '--remove', + default=False, + action='store_true', + help=_("Removes all the snapshot volumes " + "that were created.") + ) + + parser.add_argument( + '--revert', + default=False, + action='store_true', + help=_("Reverts all the disks to the moment " + "when the snapshot was created.") + ) + parser.add_argument( '--extra-vars', default=None, @@ -339,11 +355,21 @@ class BackupSnapshot(command.Command): _('The inventory file {} does not exist or is not ' 'readable'.format(parsed_args.inventory))) + if parsed_args.remove is True and parsed_args.revert is True: + raise RuntimeError( + _('--revert and --remove are mutually exclusive')) + if parsed_args.remove is True and parsed_args.revert is False: + tags = 'remove_snapshots' + if parsed_args.revert is True and parsed_args.remove is False: + tags = 'revert_snapshots' + if parsed_args.remove is False and parsed_args.revert is False: + tags = 'create_snapshots' + self.log.debug(_('Starting Overcloud Snapshot')) self._run_ansible_playbook( playbook='cli-overcloud-snapshot.yaml', inventory=parsed_args.inventory, - tags=None, + tags=tags, skip_tags=None, extra_vars=extra_vars )