Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit f81372d

Browse files
committed
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
1 parent 5a6423b commit f81372d

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ mistral.actions =
111111
tripleo.validations.list_validations = tripleo_common.actions.validations:ListValidationsAction
112112
tripleo.validations.run_validation = tripleo_common.actions.validations:RunValidationAction
113113
tripleo.validations.verify_profiles = tripleo_common.actions.validations:VerifyProfilesAction
114+
tripleo.files.file_exists = tripleo_common.actions.files:FileExists
115+
tripleo.files.make_temp_dir = tripleo_common.actions.files:MakeTempDir
116+
tripleo.files.remove_temp_dir = tripleo_common.actions.files:RemoveTempDir
114117
# deprecated for pike release, will be removed in queens
115118
tripleo.ansible = tripleo_common.actions.ansible:AnsibleAction
116119
tripleo.ansible-playbook = tripleo_common.actions.ansible:AnsiblePlaybookAction

tripleo_common/actions/files.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright 2017 Red Hat, Inc.
2+
# All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
# not use this file except in compliance with the License. You may obtain
6+
# a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations
14+
# under the License.
15+
import os
16+
import re
17+
import shutil
18+
import six
19+
import tempfile
20+
21+
22+
from mistral_lib import actions
23+
from mistral_lib.actions import base
24+
25+
26+
class FileExists(base.Action):
27+
"""Verifies if a path exists on the localhost (undercloud)"""
28+
def __init__(self, path):
29+
self.path = path
30+
31+
def run(self):
32+
if (isinstance(self.path, six.string_types) and
33+
os.path.exists(self.path)):
34+
msg = "Found file %s" % self.path
35+
return actions.Result(data={"msg": msg})
36+
else:
37+
msg = "File %s not found" % self.path
38+
return actions.Result(error={"msg": msg})
39+
40+
41+
class MakeTempDir(base.Action):
42+
"""Creates temporary directory on localhost
43+
44+
The directory created will match the regular expression
45+
^/tmp/file-mistral-action[A-Za-z0-9_]{6}$
46+
"""
47+
48+
def __init__(self):
49+
pass
50+
51+
def run(self):
52+
try:
53+
_path = tempfile.mkdtemp(dir='/tmp/',
54+
prefix='file-mistral-action')
55+
return actions.Result(data={"path": _path})
56+
except Exception as msg:
57+
return actions.Result(error={"msg": six.text_type(msg)})
58+
59+
60+
class RemoveTempDir(base.Action):
61+
"""Removes temporary directory on localhost by path.
62+
63+
The path must match the regular expression
64+
^/tmp/file-mistral-action[A-Za-z0-9_]{6}$
65+
"""
66+
67+
def __init__(self, path):
68+
self.path = path
69+
70+
def run(self):
71+
# regex from tempfile's _RandomNameSequence characters
72+
_regex = '^/tmp/file-mistral-action[A-Za-z0-9_]{6}$'
73+
if (not isinstance(self.path, six.string_types) or
74+
not re.match(_regex, self.path)):
75+
msg = "Path does not match %s" % _regex
76+
return actions.Result(error={"msg": msg})
77+
try:
78+
shutil.rmtree(self.path)
79+
msg = "Deleted directory %s" % self.path
80+
return actions.Result(data={"msg": msg})
81+
except Exception as msg:
82+
return actions.Result(error={"msg": six.text_type(msg)})
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright 2017 Red Hat, Inc.
2+
# All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
# not use this file except in compliance with the License. You may obtain
6+
# a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations
14+
# under the License.
15+
import mock
16+
17+
from tripleo_common.actions import files
18+
from tripleo_common.tests import base
19+
20+
21+
class FileExistsTest(base.TestCase):
22+
23+
def setUp(self):
24+
super(FileExistsTest, self).setUp()
25+
self.path = '/etc/issue'
26+
27+
@mock.patch("os.path.exists")
28+
def test_file_exists(self, mock_exists):
29+
mock_exists.return_value = True
30+
action = files.FileExists(self.path)
31+
action_result = action.run()
32+
self.assertFalse(action_result.cancel)
33+
self.assertIsNone(action_result.error)
34+
self.assertEqual('Found file /etc/issue',
35+
action_result.data['msg'])
36+
37+
38+
class MakeTempDirTest(base.TestCase):
39+
40+
def setUp(self):
41+
super(MakeTempDirTest, self).setUp()
42+
43+
@mock.patch("tempfile.mkdtemp")
44+
def test_make_temp_dir(self, mock_mkdtemp):
45+
mock_mkdtemp.return_value = "/tmp/file-mistral-actionxFLfYz"
46+
action = files.MakeTempDir()
47+
action_result = action.run()
48+
self.assertFalse(action_result.cancel)
49+
self.assertIsNone(action_result.error)
50+
self.assertEqual('/tmp/file-mistral-actionxFLfYz',
51+
action_result.data['path'])
52+
53+
54+
class RemoveTempDirTest(base.TestCase):
55+
56+
def setUp(self):
57+
super(RemoveTempDirTest, self).setUp()
58+
self.path = "/tmp/file-mistral-actionxFLfYz"
59+
60+
@mock.patch("shutil.rmtree")
61+
def test_sucess_remove_temp_dir(self, mock_rmtree):
62+
mock_rmtree.return_value = None # rmtree has no return value
63+
action = files.RemoveTempDir(self.path)
64+
action_result = action.run()
65+
self.assertFalse(action_result.cancel)
66+
self.assertIsNone(action_result.error)
67+
self.assertEqual('Deleted directory /tmp/file-mistral-actionxFLfYz',
68+
action_result.data['msg'])

0 commit comments

Comments
 (0)