Permalink
Browse files

Add Mistral file actions to tripleo-common

This patch adds three actions for file operations on the undercloud
Mistral to 1. verify if a path exists, 2. create a temporary
directory, and 3. remove a temporary directory.

Change-Id: I18669820d76f1185e937bf53fcc35c0d4cde7de3
  • Loading branch information...
fultonj committed Jun 26, 2017
1 parent 5a6423b commit f81372d85a0a92de455eeaa93162faf09be670cf
Showing with 153 additions and 0 deletions.
  1. +3 −0 setup.cfg
  2. +82 −0 tripleo_common/actions/files.py
  3. +68 −0 tripleo_common/tests/actions/test_files.py
View
@@ -111,6 +111,9 @@ mistral.actions =
tripleo.validations.list_validations = tripleo_common.actions.validations:ListValidationsAction
tripleo.validations.run_validation = tripleo_common.actions.validations:RunValidationAction
tripleo.validations.verify_profiles = tripleo_common.actions.validations:VerifyProfilesAction
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
# deprecated for pike release, will be removed in queens
tripleo.ansible = tripleo_common.actions.ansible:AnsibleAction
tripleo.ansible-playbook = tripleo_common.actions.ansible:AnsiblePlaybookAction
@@ -0,0 +1,82 @@
# Copyright 2017 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# 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 tempfile
from mistral_lib import actions
from mistral_lib.actions import base
class FileExists(base.Action):
"""Verifies if a path exists on the localhost (undercloud)"""
def __init__(self, path):
self.path = path
def run(self):
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.Action):
"""Creates temporary directory on localhost
The directory created will match the regular expression
^/tmp/file-mistral-action[A-Za-z0-9_]{6}$
"""
def __init__(self):
pass
def run(self):
try:
_path = tempfile.mkdtemp(dir='/tmp/',
prefix='file-mistral-action')
return actions.Result(data={"path": _path})
except Exception as msg:
return actions.Result(error={"msg": six.text_type(msg)})
class RemoveTempDir(base.Action):
"""Removes temporary directory on localhost by path.
The path must match the regular expression
^/tmp/file-mistral-action[A-Za-z0-9_]{6}$
"""
def __init__(self, path):
self.path = path
def run(self):
# 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:
shutil.rmtree(self.path)
msg = "Deleted directory %s" % self.path
return actions.Result(data={"msg": msg})
except Exception as msg:
return actions.Result(error={"msg": six.text_type(msg)})
@@ -0,0 +1,68 @@
# Copyright 2017 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# 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 mock
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()
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):
super(MakeTempDirTest, self).setUp()
@mock.patch("tempfile.mkdtemp")
def test_make_temp_dir(self, mock_mkdtemp):
mock_mkdtemp.return_value = "/tmp/file-mistral-actionxFLfYz"
action = files.MakeTempDir()
action_result = action.run()
self.assertFalse(action_result.cancel)
self.assertIsNone(action_result.error)
self.assertEqual('/tmp/file-mistral-actionxFLfYz',
action_result.data['path'])
class RemoveTempDirTest(base.TestCase):
def setUp(self):
super(RemoveTempDirTest, self).setUp()
self.path = "/tmp/file-mistral-actionxFLfYz"
@mock.patch("shutil.rmtree")
def test_sucess_remove_temp_dir(self, mock_rmtree):
mock_rmtree.return_value = None # rmtree has no return value
action = files.RemoveTempDir(self.path)
action_result = action.run()
self.assertFalse(action_result.cancel)
self.assertIsNone(action_result.error)
self.assertEqual('Deleted directory /tmp/file-mistral-actionxFLfYz',
action_result.data['msg'])

0 comments on commit f81372d

Please sign in to comment.