mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 05:10:22 -07:00
Changed atomic_replace to atomic_move, now ti DOES move atomically in the last
step Signed-off-by: Brian Coca <briancoca+dev@gmail.com>
This commit is contained in:
parent
f876d51c66
commit
caf6bd6ce5
6 changed files with 34 additions and 26 deletions
|
@ -807,8 +807,9 @@ class AnsibleModule(object):
|
|||
self.fail_json(msg='Could not make backup of %s to %s: %s' % (fn, backupdest, e))
|
||||
return backupdest
|
||||
|
||||
def atomic_replace(self, src, dest):
|
||||
'''atomically replace dest with src, copying attributes from dest'''
|
||||
def atomic_move(self, src, dest):
|
||||
'''atomically move src to dest, copying attributes from dest, returns true on success'''
|
||||
rc = False
|
||||
if os.path.exists(dest):
|
||||
st = os.stat(dest)
|
||||
os.chmod(src, st.st_mode & 07777)
|
||||
|
@ -824,7 +825,25 @@ class AnsibleModule(object):
|
|||
if self.selinux_enabled():
|
||||
context = self.selinux_default_context(dest)
|
||||
self.set_context_if_different(src, context, False)
|
||||
os.rename(src, dest)
|
||||
# Ensure file is on same partition to make replacement atomic
|
||||
dest_dir = os.path.dirname(dest)
|
||||
dest_file = os.path.basename(dest)
|
||||
tmp_dest = "%s/.%s.%s.%s" % (dest_dir,dest_file,os.getpid(),time.time())
|
||||
try:
|
||||
shutil.move(src, tmp_dest)
|
||||
os.rename(tmp_dest, dest)
|
||||
rc = True
|
||||
except (shutil.Error, OSError, IOError), e:
|
||||
self.fail_json(msg='Could not replace file: %s to %s: %s' % (src, dest, e))
|
||||
finally:
|
||||
# Clean up in case of failure (don't leave nasty temps around)
|
||||
if os.path.exists(tmp_dest):
|
||||
try:
|
||||
#TODO: would be nice to respect 'keep_remote_files'
|
||||
os.unlink(tmp_dest)
|
||||
except OSError, e:
|
||||
sys.stderr.write("could not cleanup %s: %s" % (tmp_dest, e))
|
||||
return rc
|
||||
|
||||
def run_command(self, args, check_rc=False, close_fds=False, executable=None, data=None):
|
||||
'''
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue