Merge "Add Mistral file actions to tripleo-common"
This commit is contained in:
commit
e62c095324
@ -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
|
||||
|
82
tripleo_common/actions/files.py
Normal file
82
tripleo_common/actions/files.py
Normal file
@ -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)})
|
68
tripleo_common/tests/actions/test_files.py
Normal file
68
tripleo_common/tests/actions/test_files.py
Normal file
@ -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'])
|
Loading…
x
Reference in New Issue
Block a user