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
1ff82e88d2
commit
21f9f7c6fd
5 changed files with 12 additions and 23 deletions
|
@ -162,7 +162,7 @@ def writekeys(module, filename, keys):
|
||||||
except IOError, e:
|
except IOError, e:
|
||||||
module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, str(e)))
|
module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, str(e)))
|
||||||
f.close()
|
f.close()
|
||||||
module.atomic_replace(tmp_path, filename)
|
module.atomic_move(tmp_path, filename)
|
||||||
|
|
||||||
def enforce_state(module, params):
|
def enforce_state(module, params):
|
||||||
"""
|
"""
|
||||||
|
|
15
copy
15
copy
|
@ -19,7 +19,6 @@
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
|
@ -144,17 +143,15 @@ def main():
|
||||||
if os.path.islink(dest):
|
if os.path.islink(dest):
|
||||||
os.unlink(dest)
|
os.unlink(dest)
|
||||||
open(dest, 'w').close()
|
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:
|
if validate:
|
||||||
(rc,out,err) = module.run_command(validate % dest_tmp)
|
(rc,out,err) = module.run_command(validate % src)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
module.fail_json(msg="failed to validate: rc:%s error:%s" % (rc,err))
|
module.fail_json(msg="failed to validate: rc:%s error:%s" % (rc,err))
|
||||||
module.atomic_replace(dest_tmp, dest)
|
if not module.atomic_move(src, dest):
|
||||||
except shutil.Error:
|
try:
|
||||||
module.fail_json(msg="failed to copy: %s and %s are the same" % (src, dest))
|
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:
|
except IOError:
|
||||||
module.fail_json(msg="failed to copy: %s to %s" % (src, dest))
|
module.fail_json(msg="failed to copy: %s to %s" % (src, dest))
|
||||||
changed = True
|
changed = True
|
||||||
|
|
10
cron
10
cron
|
@ -145,15 +145,7 @@ def get_jobs_file(module, user, tmpfile, cron_file):
|
||||||
def install_jobs(module, user, tmpfile, cron_file):
|
def install_jobs(module, user, tmpfile, cron_file):
|
||||||
if cron_file:
|
if cron_file:
|
||||||
cron_file = '/etc/cron.d/%s' % cron_file
|
cron_file = '/etc/cron.d/%s' % cron_file
|
||||||
try:
|
module.atomic_move(tmpfile, cron_file)
|
||||||
module.atomic_replace(tmpfile, cron_file)
|
|
||||||
except (OSError, IOError), e:
|
|
||||||
return (1, "", str(e))
|
|
||||||
except:
|
|
||||||
return (1, "", str(e))
|
|
||||||
else:
|
|
||||||
return (0, "", "")
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
cmd = "crontab %s %s" % (user, tmpfile)
|
cmd = "crontab %s %s" % (user, tmpfile)
|
||||||
return module.run_command(cmd)
|
return module.run_command(cmd)
|
||||||
|
|
2
service
2
service
|
@ -334,7 +334,7 @@ class Service(object):
|
||||||
os.close(TMP_RCCONF)
|
os.close(TMP_RCCONF)
|
||||||
|
|
||||||
# Replace previous rc.conf.
|
# 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
|
# Subclass: Linux
|
||||||
|
|
6
sysctl
6
sysctl
|
@ -110,10 +110,10 @@ def write_sysctl(module, lines, **sysctl_args):
|
||||||
module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, str(e)))
|
module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, str(e)))
|
||||||
f.flush()
|
f.flush()
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
# replace the real one
|
# replace the real one
|
||||||
module.atomic_replace(tmp_path, sysctl_args['sysctl_file'])
|
module.atomic_move(tmp_path, sysctl_args['sysctl_file'])
|
||||||
|
|
||||||
# end
|
# end
|
||||||
return sysctl_args
|
return sysctl_args
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue