Fix #2125 and clean up a few things along the way

reviewable/pr18780/r1
Daniel Hokka Zakrisson 12 years ago
parent 8b46201dff
commit 4ff37b8369

17
cron

@ -167,8 +167,9 @@ def find_job(name,tmpfile):
return [] return []
def add_job(module,name,job,tmpfile): def add_job(module,name,job,tmpfile):
cmd = "echo \"#Ansible: %s\n%s\" >> %s" % (name,job,tmpfile) f = open(tmpfile, 'a')
return module.run_command(cmd) f.write("#Ansible: %s\n%s\n" % (name, job))
f.close()
def update_job(name,job,tmpfile): def update_job(name,job,tmpfile):
return _update_job(name,job,tmpfile,do_add_job) return _update_job(name,job,tmpfile,do_add_job)
@ -191,6 +192,7 @@ def _update_job(name,job,tmpfile,addlinesfunction):
ansiblename = "#Ansible: %s" % (name) ansiblename = "#Ansible: %s" % (name)
f = open(tmpfile) f = open(tmpfile)
lines = f.read().splitlines() lines = f.read().splitlines()
f.close()
newlines = [] newlines = []
comment = None comment = None
for l in lines: for l in lines:
@ -201,7 +203,6 @@ def _update_job(name,job,tmpfile,addlinesfunction):
comment = l comment = l
else: else:
newlines.append(l) newlines.append(l)
f.close()
f = open(tmpfile, 'w') f = open(tmpfile, 'w')
for l in newlines: for l in newlines:
f.write(l) f.write(l)
@ -209,9 +210,9 @@ def _update_job(name,job,tmpfile,addlinesfunction):
f.close() f.close()
if len(newlines) == 0: if len(newlines) == 0:
return (0,"","",True) return True
else: else:
return (0,"","",False) # TODO add some more error testing return False # TODO add some more error testing
def get_cron_job(minute,hour,day,month,weekday,job,user,cron_file,reboot): def get_cron_job(minute,hour,day,month,weekday,job,user,cron_file,reboot):
if reboot: if reboot:
@ -314,15 +315,15 @@ def main():
old_job = find_job(name,backupfile) old_job = find_job(name,backupfile)
if do_install: if do_install:
if len(old_job) == 0: if len(old_job) == 0:
(rc, out, err) = add_job(module,name,job,tmpfile.name) add_job(module,name,job,tmpfile.name)
changed = True changed = True
if len(old_job) > 0 and old_job[1] != job: if len(old_job) > 0 and old_job[1] != job:
(rc, out, err) = update_job(name,job,tmpfile.name) update_job(name,job,tmpfile.name)
changed = True changed = True
else: else:
if len(old_job) > 0: if len(old_job) > 0:
# if rm is true after the next line, file will be deleted afterwards # if rm is true after the next line, file will be deleted afterwards
(rc, out, err, rm) = remove_job(name,tmpfile.name) rm = remove_job(name,tmpfile.name)
changed = True changed = True
else: else:
# there is no old_jobs for deletion - we should leave everything # there is no old_jobs for deletion - we should leave everything

Loading…
Cancel
Save