From 5c19663bc49fa0884c807a043331f1fe20907531 Mon Sep 17 00:00:00 2001 From: Dougal Matthews Date: Thu, 1 Aug 2019 15:38:25 +0100 Subject: [PATCH] Remove the file exists action and save and restore to swift actions These were used by the Ceph Ansible/Mistral code, but since it is no longer in Mistral we don't need these actions. They are currently unused. Change-Id: Ieae67de0e681a5330a178da416a08c677e16a352 --- setup.cfg | 3 - tripleo_common/actions/files.py | 127 --------------------- tripleo_common/tests/actions/test_files.py | 99 ---------------- 3 files changed, 229 deletions(-) diff --git a/setup.cfg b/setup.cfg index 2f509a6db..a962cd04e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -145,11 +145,8 @@ mistral.actions = tripleo.validations.list_validations = tripleo_common.actions.validations:ListValidationsAction tripleo.validations.run_validation = tripleo_common.actions.validations:RunValidationAction tripleo.validations.upload = tripleo_common.actions.validations:UploadValidationsAction - tripleo.files.file_exists = tripleo_common.actions.files:FileExists tripleo.files.make_temp_dir = tripleo_common.actions.files:MakeTempDir tripleo.files.remove_temp_dir = tripleo_common.actions.files:RemoveTempDir - tripleo.files.save_temp_dir_to_swift = tripleo_common.actions.files:SaveTempDirToSwift - tripleo.files.restore_temp_dir_from_swift = tripleo_common.actions.files:RestoreTempDirFromSwift tripleo.ansible-generate-inventory = tripleo_common.actions.ansible:AnsibleGenerateInventoryAction tripleo.undercloud.get_free_space = tripleo_common.actions.undercloud:GetFreeSpace tripleo.undercloud.create_backup_dir = tripleo_common.actions.undercloud:CreateBackupDir diff --git a/tripleo_common/actions/files.py b/tripleo_common/actions/files.py index c05933c8e..e99705b7e 100644 --- a/tripleo_common/actions/files.py +++ b/tripleo_common/actions/files.py @@ -12,35 +12,13 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -import os import re import shutil import six -import sys import tempfile from mistral_lib import actions -from oslo_concurrency import processutils -from swiftclient import exceptions as swiftexceptions from tripleo_common.actions import base -from tripleo_common.utils import swift as swiftutils -from tripleo_common.utils import tarball -from tripleo_common.utils import time_functions as timeutils - - -class FileExists(base.TripleOAction): - """Verifies if a path exists on the localhost (undercloud)""" - def __init__(self, path): - self.path = path - - def run(self, context): - if (isinstance(self.path, six.string_types) and - os.path.exists(self.path)): - msg = "Found file %s" % self.path - return actions.Result(data={"msg": msg}) - else: - msg = "File %s not found" % self.path - return actions.Result(error={"msg": msg}) class MakeTempDir(base.TripleOAction): @@ -85,108 +63,3 @@ class RemoveTempDir(base.TripleOAction): return actions.Result(data={"msg": msg}) except Exception as msg: return actions.Result(error={"msg": six.text_type(msg)}) - - -class SaveTempDirToSwift(base.TripleOAction): - """Save temporary directory, identified by path, to Swift container - - The path must match the regular expression - ^/tmp/file-mistral-action[A-Za-z0-9_]{6}$ - - The Swift container must exist - - Contents from path will be packaged in a tarball before upload - - Older tarball(s) will be replaced with the one that is uploaded - """ - - def __init__(self, path, container): - super(SaveTempDirToSwift, self).__init__() - self.path = path - self.container = container - - def run(self, context): - swift = self.get_object_client(context) - swift_service = self.get_object_service(context) - tarball_name = 'temporary_dir-%s.tar.gz' \ - % timeutils.timestamp() - # regex from tempfile's _RandomNameSequence characters - _regex = '^/tmp/file-mistral-action[A-Za-z0-9_]{6}$' - if (not isinstance(self.path, six.string_types) or - not re.match(_regex, self.path)): - msg = "Path does not match %s" % _regex - return actions.Result(error={"msg": msg}) - try: - headers, objects = swift.get_container(self.container) - for o in objects: - swift.delete_object(self.container, o['name']) - swiftutils.create_and_upload_tarball( - swift_service, self.path, self.container, - tarball_name, delete_after=sys.maxsize) - except swiftexceptions.ClientException as err: - msg = "Error attempting an operation on container: %s" % err - return actions.Result(error={"msg": six.text_type(msg)}) - except (OSError, IOError) as err: - msg = "Error while writing file: %s" % err - return actions.Result(error={"msg": six.text_type(msg)}) - except processutils.ProcessExecutionError as err: - msg = "Error while creating a tarball: %s" % err - return actions.Result(error={"msg": six.text_type(msg)}) - except Exception as err: - msg = "Error exporting logs: %s" % err - return actions.Result(error={"msg": six.text_type(msg)}) - msg = "Saved tarball of directory: %s in Swift container: %s" % ( - self.path, self.container) - return actions.Result(data={"msg": msg}) - - -class RestoreTempDirFromSwift(base.TripleOAction): - """Unpack tarball from Swift container into temporary directory at path - - The path must exist and match the regular expression - ^/tmp/file-mistral-action[A-Za-z0-9_]{6}$ - - Container should contain a single tarball object - If container is empty, then no error is returned - """ - - def __init__(self, path, container): - super(RestoreTempDirFromSwift, self).__init__() - self.path = path - self.container = container - - def run(self, context): - swift = self.get_object_client(context) - # regex from tempfile's _RandomNameSequence characters - _regex = '^/tmp/file-mistral-action[A-Za-z0-9_]{6}$' - if (not isinstance(self.path, six.string_types) or - not re.match(_regex, self.path)): - msg = "Path does not match %s" % _regex - return actions.Result(error={"msg": msg}) - try: - swiftutils.download_container(swift, self.container, self.path) - filenames = os.listdir(self.path) - if len(filenames) == 0: - pass - elif len(filenames) == 1: - tarball.extract_tarball(self.path, filenames[0], remove=True) - else: - msg = "%d objects found in container: %s" \ - % (len(filenames), self.container) - msg += " but one object was expected." - return actions.Result(error={"msg": six.text_type(msg)}) - except swiftexceptions.ClientException as err: - msg = "Error attempting an operation on container: %s" % err - return actions.Result(error={"msg": six.text_type(msg)}) - except (OSError, IOError) as err: - msg = "Error while writing file: %s" % err - return actions.Result(error={"msg": six.text_type(msg)}) - except processutils.ProcessExecutionError as err: - msg = "Error while creating a tarball: %s" % err - return actions.Result(error={"msg": six.text_type(msg)}) - except Exception as err: - msg = "Error exporting logs: %s" % err - return actions.Result(error={"msg": six.text_type(msg)}) - msg = "Swift container: %s has been extracted to directory: %s" % ( - self.container, self.path) - return actions.Result(data={"msg": msg}) diff --git a/tripleo_common/tests/actions/test_files.py b/tripleo_common/tests/actions/test_files.py index 836c8a1b9..a87b5a69e 100644 --- a/tripleo_common/tests/actions/test_files.py +++ b/tripleo_common/tests/actions/test_files.py @@ -18,23 +18,6 @@ from tripleo_common.actions import files from tripleo_common.tests import base -class FileExistsTest(base.TestCase): - - def setUp(self): - super(FileExistsTest, self).setUp() - self.path = '/etc/issue' - - @mock.patch("os.path.exists") - def test_file_exists(self, mock_exists): - mock_exists.return_value = True - action = files.FileExists(self.path) - action_result = action.run(context={}) - self.assertFalse(action_result.cancel) - self.assertIsNone(action_result.error) - self.assertEqual('Found file /etc/issue', - action_result.data['msg']) - - class MakeTempDirTest(base.TestCase): def setUp(self): @@ -66,85 +49,3 @@ class RemoveTempDirTest(base.TestCase): self.assertIsNone(action_result.error) self.assertEqual('Deleted directory /tmp/file-mistral-actionxFLfYz', action_result.data['msg']) - - -class SaveTempDirToSwiftTest(base.TestCase): - def setUp(self): - super(SaveTempDirToSwiftTest, self).setUp() - self.path = "/tmp/file-mistral-actionxFLfYz" - self.container = "my_container" - self.tarball = "foo.tar.gz" - - @mock.patch("tripleo_common.utils.swift.create_and_upload_tarball") - @mock.patch("tripleo_common.actions.base.TripleOAction.get_object_service") - @mock.patch("tripleo_common.actions.base.TripleOAction.get_object_client") - def test_save_temp_dir_to_swift(self, mock_get_object_client, - mock_get_object_service, - mock_create_and_upload_tarball): - # Setup context, swift, swift_service, get_container, create_upload - mock_ctx = mock.MagicMock() - - swift = mock.MagicMock(url="http://test.com") - mock_get_object_client.return_value = swift - - swift_service = mock.MagicMock() - mock_get_object_service.return_value = swift_service - - def return_container_files(*args): - return ('headers', []) - - swift.get_container = mock.MagicMock( - side_effect=return_container_files) - mock_get_object_client.return_value = swift - - mock_create_and_upload_tarball.return_value = mock.MagicMock( - swift_service, self.path, self.container, self.tarball) - - # Test - action = files.SaveTempDirToSwift(self.path, self.container) - result = action.run(mock_ctx) - - # Verify - self.assertFalse(result.cancel) - self.assertIsNone(result.error) - msg = "Saved tarball of directory: %s in Swift container: %s" \ - % (self.path, self.container) - self.assertEqual(msg, result.data['msg']) - - -class RestoreTempDirFromSwiftTest(base.TestCase): - def setUp(self): - super(RestoreTempDirFromSwiftTest, self).setUp() - self.path = "/tmp/file-mistral-actionxFLfYz" - self.container = "my_container" - self.tarball = "foo.tar.gz" - - @mock.patch("os.listdir") - @mock.patch("tripleo_common.utils.tarball.extract_tarball") - @mock.patch("tripleo_common.utils.swift.download_container") - @mock.patch("tripleo_common.actions.base.TripleOAction.get_object_client") - def test_restore_temp_dir_from_swift(self, mock_get_object_client, - mock_download_container, - mock_extract_tarball, mock_listdir): - # Setup context, swift, listdir, tarball - mock_ctx = mock.MagicMock() - - swift = mock.MagicMock(url="http://test.com") - mock_get_object_client.return_value = swift - - mock_download_container.return_value = mock.MagicMock( - swift, self.container, self.path) - mock_extract_tarball.return_value = mock.MagicMock( - self.path, self.tarball) - mock_listdir.return_value = [self.tarball] - - # Test - action = files.RestoreTempDirFromSwift(self.path, self.container) - result = action.run(mock_ctx) - - # Verify - self.assertFalse(result.cancel) - self.assertIsNone(result.error) - msg = "Swift container: %s has been extracted to directory: %s" \ - % (self.container, self.path) - self.assertEqual(msg, result.data['msg'])