Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generalized cleanup and removed atomic_move return #2842

Merged
merged 1 commit into from
May 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 8 additions & 11 deletions lib/ansible/module_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,9 +807,15 @@ def backup_local(self, fn):
self.fail_json(msg='Could not make backup of %s to %s: %s' % (fn, backupdest, e))
return backupdest

def cleanup(tmpfile):
if os.path.exists(tmpfile):
try:
os.unlink(tmpfile)
except OSError, e:
sys.stderr.write("could not cleanup %s: %s" % (tmpfile, e))

def atomic_move(self, src, dest):
'''atomically move src to dest, copying attributes from dest, returns true on success'''
rc = False
context = None
if os.path.exists(dest):
try:
Expand All @@ -829,13 +835,6 @@ def atomic_move(self, src, dest):
dest_file = os.path.basename(dest)
tmp_dest = "%s/.%s.%s.%s" % (dest_dir,dest_file,os.getpid(),time.time())

def cleanup():
if os.path.exists(tmp_dest):
try:
os.unlink(tmp_dest)
except OSError, e:
sys.stderr.write("could not cleanup %s: %s" % (tmp_dest, e))

try: # leaves tmp file behind when sudo and not root
if os.getenv("SUDO_USER") and os.getuid() != 0:
# cleanup will happen by 'rm' of tempdir
Expand All @@ -848,11 +847,9 @@ def cleanup():
if self.selinux_enabled():
# rename might not preserve context
self.set_context_if_different(dest, context, False)
rc = True
except (shutil.Error, OSError, IOError), e:
cleanup()
cleanup(tmp_dest)
self.fail_json(msg='Could not replace file: %s to %s: %s' % (src, dest, e))
return rc

def run_command(self, args, check_rc=False, close_fds=False, executable=None, data=None):
'''
Expand Down
6 changes: 1 addition & 5 deletions library/files/copy
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,7 @@ def main():
(rc,out,err) = module.run_command(validate % src)
if rc != 0:
module.fail_json(msg="failed to validate: rc:%s error:%s" % (rc,err))
if not module.atomic_move(src, dest):
try:
os.unlink(src) # cleanup tmp files on failure
except OSError, e:
sys.stderr.write("failed to clean up tmp file %s: %s\n" % (src, e))
module.atomic_move(src, dest):
except IOError:
module.fail_json(msg="failed to copy: %s to %s" % (src, dest))
changed = True
Expand Down