summaryrefslogtreecommitdiffstats
path: root/roles/lib_openshift/library/oc_route.py
diff options
context:
space:
mode:
authorThomas Wiest <twiest@redhat.com>2017-01-29 14:47:07 -0500
committerThomas Wiest <twiest@redhat.com>2017-02-08 01:04:00 -0500
commitaa97c28b6a9b468498fffe565d314b07141f163b (patch)
tree1ad83e300ec589f9d4888293d2f62dc2c287af04 /roles/lib_openshift/library/oc_route.py
parent5a933ed4b33131423b2f349eb0bc33ead99ec360 (diff)
downloadopenshift-aa97c28b6a9b468498fffe565d314b07141f163b.tar.gz
openshift-aa97c28b6a9b468498fffe565d314b07141f163b.tar.bz2
openshift-aa97c28b6a9b468498fffe565d314b07141f163b.tar.xz
openshift-aa97c28b6a9b468498fffe565d314b07141f163b.zip
Changed lib_openshift to use real temporary files.
Diffstat (limited to 'roles/lib_openshift/library/oc_route.py')
-rw-r--r--roles/lib_openshift/library/oc_route.py54
1 files changed, 37 insertions, 17 deletions
diff --git a/roles/lib_openshift/library/oc_route.py b/roles/lib_openshift/library/oc_route.py
index d1b2ccb14..6ee34bafb 100644
--- a/roles/lib_openshift/library/oc_route.py
+++ b/roles/lib_openshift/library/oc_route.py
@@ -38,6 +38,7 @@ import os
import re
import shutil
import subprocess
+import tempfile
# pylint: disable=import-error
import ruamel.yaml as yaml
from ansible.module_utils.basic import AnsibleModule
@@ -773,7 +774,8 @@ class OpenShiftCLI(object):
if not res['results']:
return res
- fname = '/tmp/%s' % rname
+ fname = Utils.create_tmpfile(rname + '-')
+
yed = Yedit(fname, res['results'][0], separator=sep)
changes = []
for key, value in content.items():
@@ -797,7 +799,7 @@ class OpenShiftCLI(object):
def _create_from_content(self, rname, content):
'''create a temporary file and then call oc create on it'''
- fname = '/tmp/%s' % rname
+ fname = Utils.create_tmpfile(rname + '-')
yed = Yedit(fname, content=content)
yed.write()
@@ -840,7 +842,7 @@ class OpenShiftCLI(object):
if results['returncode'] != 0 or not create:
return results
- fname = '/tmp/%s' % template_name
+ fname = Utils.create_tmpfile(template_name + '-')
yed = Yedit(fname, results['results'])
yed.write()
@@ -1021,32 +1023,50 @@ class OpenShiftCLI(object):
class Utils(object):
''' utilities for openshiftcli modules '''
+
+ @staticmethod
+ def _write(filename, contents):
+ ''' Actually write the file contents to disk. This helps with mocking. '''
+
+ with open(filename, 'w') as sfd:
+ sfd.write(contents)
+
@staticmethod
- def create_file(rname, data, ftype='yaml'):
+ def create_tmp_file_from_contents(rname, data, ftype='yaml'):
''' create a file in tmp with name and contents'''
- path = os.path.join('/tmp', rname)
- with open(path, 'w') as fds:
- if ftype == 'yaml':
- fds.write(yaml.dump(data, Dumper=yaml.RoundTripDumper))
- elif ftype == 'json':
- fds.write(json.dumps(data))
- else:
- fds.write(data)
+ tmp = Utils.create_tmpfile(prefix=rname)
+
+ if ftype == 'yaml':
+ Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper))
+ elif ftype == 'json':
+ Utils._write(tmp, json.dumps(data))
+ else:
+ Utils._write(tmp, data)
# Register cleanup when module is done
- atexit.register(Utils.cleanup, [path])
- return path
+ atexit.register(Utils.cleanup, [tmp])
+ return tmp
+
+ @staticmethod
+ def create_tmpfile(prefix=None):
+ ''' Generates and returns a temporary file name '''
+
+ with tempfile.NamedTemporaryFile(prefix=prefix, delete=False) as tmp:
+ return tmp.name
@staticmethod
- def create_files_from_contents(content, content_type=None):
+ def create_tmp_files_from_contents(content, content_type=None):
'''Turn an array of dict: filename, content into a files array'''
if not isinstance(content, list):
content = [content]
files = []
for item in content:
- path = Utils.create_file(item['path'], item['data'], ftype=content_type)
- files.append({'name': os.path.basename(path), 'path': path})
+ path = Utils.create_tmp_file_from_contents(item['path'] + '-',
+ item['data'],
+ ftype=content_type)
+ files.append({'name': os.path.basename(item['path']),
+ 'path': path})
return files
@staticmethod