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:
Brian Coca 2013-04-23 21:52:23 -04:00 committed by Michael DeHaan
parent 1ff82e88d2
commit 21f9f7c6fd
5 changed files with 12 additions and 23 deletions

View file

@ -162,7 +162,7 @@ def writekeys(module, filename, keys):
except IOError, e:
module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, str(e)))
f.close()
module.atomic_replace(tmp_path, filename)
module.atomic_move(tmp_path, filename)
def enforce_state(module, params):
"""

15
copy
View file

@ -19,7 +19,6 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import os
import shutil
import time
DOCUMENTATION = '''
@ -144,17 +143,15 @@ def main():
if os.path.islink(dest):
os.unlink(dest)
open(dest, 'w').close()
# TODO:pid + epoch should avoid most collisions, hostname/mac for those using nfs?
# might be an issue with exceeding path length
dest_tmp = "%s.%s.%s.tmp" % (dest,os.getpid(),time.time())
shutil.copyfile(src, dest_tmp)
if validate:
(rc,out,err) = module.run_command(validate % dest_tmp)
(rc,out,err) = module.run_command(validate % src)
if rc != 0:
module.fail_json(msg="failed to validate: rc:%s error:%s" % (rc,err))
module.atomic_replace(dest_tmp, dest)
except shutil.Error:
module.fail_json(msg="failed to copy: %s and %s are the same" % (src, dest))
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))
except IOError:
module.fail_json(msg="failed to copy: %s to %s" % (src, dest))
changed = True

10
cron
View file

@ -145,15 +145,7 @@ def get_jobs_file(module, user, tmpfile, cron_file):
def install_jobs(module, user, tmpfile, cron_file):
if cron_file:
cron_file = '/etc/cron.d/%s' % cron_file
try:
module.atomic_replace(tmpfile, cron_file)
except (OSError, IOError), e:
return (1, "", str(e))
except:
return (1, "", str(e))
else:
return (0, "", "")
module.atomic_move(tmpfile, cron_file)
else:
cmd = "crontab %s %s" % (user, tmpfile)
return module.run_command(cmd)

View file

@ -334,7 +334,7 @@ class Service(object):
os.close(TMP_RCCONF)
# Replace previous rc.conf.
self.module.atomic_replace(tmp_rcconf_file, self.rcconf_file)
self.module.atomic_move(tmp_rcconf_file, self.rcconf_file)
# ===========================================
# Subclass: Linux

2
sysctl
View file

@ -112,7 +112,7 @@ def write_sysctl(module, lines, **sysctl_args):
f.close()
# replace the real one
module.atomic_replace(tmp_path, sysctl_args['sysctl_file'])
module.atomic_move(tmp_path, sysctl_args['sysctl_file'])
# end
return sysctl_args