Letsencrypt: cleaning up tempfile code (2) (#35278)

* Cleaning up another tempfile opening.

* Avoid exception.
This commit is contained in:
Felix Fontein 2018-01-25 09:58:24 +01:00 committed by ansibot
commit b50ab8eebd

View file

@ -354,7 +354,10 @@ def write_file(module, dest, content):
checksum_dest = None checksum_dest = None
# raise an error if there is no tmpsrc file # raise an error if there is no tmpsrc file
if not os.path.exists(tmpsrc): if not os.path.exists(tmpsrc):
os.remove(tmpsrc) try:
os.remove(tmpsrc)
except:
pass
module.fail_json(msg="Source %s does not exist" % (tmpsrc)) module.fail_json(msg="Source %s does not exist" % (tmpsrc))
if not os.access(tmpsrc, os.R_OK): if not os.access(tmpsrc, os.R_OK):
os.remove(tmpsrc) os.remove(tmpsrc)
@ -449,14 +452,17 @@ class ACMEAccount(object):
# Create a key file from content, key (path) and key content are mutually exclusive # Create a key file from content, key (path) and key content are mutually exclusive
if self.key_content is not None: if self.key_content is not None:
_, tmpsrc = tempfile.mkstemp() fd, tmpsrc = tempfile.mkstemp()
module.add_cleanup_file(tmpsrc) # Ansible will delete the file on exit module.add_cleanup_file(tmpsrc) # Ansible will delete the file on exit
f = open(tmpsrc, 'wb') f = os.fdopen(fd, 'wb')
try: try:
f.write(self.key_content.encode('utf-8')) f.write(self.key_content.encode('utf-8'))
self.key = tmpsrc self.key = tmpsrc
except Exception as err: except Exception as err:
os.remove(tmpsrc) try:
f.close()
except:
pass
module.fail_json(msg="failed to create temporary content file: %s" % to_native(err), exception=traceback.format_exc()) module.fail_json(msg="failed to create temporary content file: %s" % to_native(err), exception=traceback.format_exc())
f.close() f.close()