mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 05:10:22 -07:00
Letsencrypt: cleaning up tempfile code (2) (#35278)
* Cleaning up another tempfile opening. * Avoid exception.
This commit is contained in:
parent
5959b93248
commit
b50ab8eebd
1 changed files with 10 additions and 4 deletions
|
@ -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()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue