Some cleanup.

This commit is contained in:
Richard C Isaacson 2014-03-10 10:31:08 -05:00
parent aba86e3657
commit c6fbb0059b

View file

@ -75,6 +75,8 @@ EXAMPLES = '''
import os import os
import tempfile import tempfile
def add_job(module, result, at_cmd, count, units, command, script_file): def add_job(module, result, at_cmd, count, units, command, script_file):
at_command = "%s now + %s %s -f %s" % (at_cmd, count, units, script_file) at_command = "%s now + %s %s -f %s" % (at_cmd, count, units, script_file)
rc, out, err = module.run_command(at_command, check_rc=True) rc, out, err = module.run_command(at_command, check_rc=True)
@ -82,8 +84,9 @@ def add_job(module, result, at_cmd, count, units, command, script_file):
os.unlink(script_file) os.unlink(script_file)
result['changed'] = True result['changed'] = True
def delete_job(module, result, at_cmd, command, script_file): def delete_job(module, result, at_cmd, command, script_file):
for matching_job in matching_jobs(module, at_cmd, script_file): for matching_job in get_matching_jobs(module, at_cmd, script_file):
at_command = "%s -d %s" % (at_cmd, matching_job) at_command = "%s -d %s" % (at_cmd, matching_job)
rc, out, err = module.run_command(at_command, check_rc=True) rc, out, err = module.run_command(at_command, check_rc=True)
result['changed'] = True result['changed'] = True
@ -91,13 +94,14 @@ def delete_job(module, result, at_cmd, command, script_file):
os.unlink(script_file) os.unlink(script_file)
module.exit_json(**result) module.exit_json(**result)
def matching_jobs(module, at_cmd, script_file):
def get_matching_jobs(module, at_cmd, script_file):
matching_jobs = [] matching_jobs = []
atq_cmd = module.get_bin_path('atq', True) atq_cmd = module.get_bin_path('atq', True)
# Get list of job numbers for the user. # Get list of job numbers for the user.
atq_command = "%s" % (atq_cmd) atq_command = "%s" % atq_cmd
rc, out, err = module.run_command(atq_command, check_rc=True) rc, out, err = module.run_command(atq_command, check_rc=True)
current_jobs = out.splitlines() current_jobs = out.splitlines()
if len(current_jobs) == 0: if len(current_jobs) == 0:
@ -118,6 +122,7 @@ def matching_jobs(module, at_cmd, script_file):
# Return the list. # Return the list.
return matching_jobs return matching_jobs
def create_tempfile(command): def create_tempfile(command):
filed, script_file = tempfile.mkstemp(prefix='at') filed, script_file = tempfile.mkstemp(prefix='at')
fileh = os.fdopen(filed, 'w') fileh = os.fdopen(filed, 'w')
@ -125,7 +130,6 @@ def create_tempfile(command):
fileh.close() fileh.close()
return script_file return script_file
#================================================
def main(): def main():
@ -149,9 +153,9 @@ def main():
default=False, default=False,
type='bool') type='bool')
), ),
mutually_exclusive = [['command', 'script_file']], mutually_exclusive=[['command', 'script_file']],
required_one_of = [['command', 'script_file']], required_one_of=[['command', 'script_file']],
supports_check_mode = False supports_check_mode=False
) )
at_cmd = module.get_bin_path('at', True) at_cmd = module.get_bin_path('at', True)
@ -163,12 +167,10 @@ def main():
state = module.params['state'] state = module.params['state']
unique = module.params['unique'] unique = module.params['unique']
if ((state == 'present') and (not count or not units)): if (state == 'present') and (not count or not units):
module.fail_json(msg="present state requires count and units") module.fail_json(msg="present state requires count and units")
result = {} result = {'state': state, 'changed': False}
result['state'] = state
result['changed'] = False
# If command transform it into a script_file # If command transform it into a script_file
if command: if command:
@ -180,7 +182,7 @@ def main():
# if unique if existing return unchanged # if unique if existing return unchanged
if unique: if unique:
if len(matching_jobs(module, at_cmd, script_file)) != 0: if len(get_matching_jobs(module, at_cmd, script_file)) != 0:
if command: if command:
os.unlink(script_file) os.unlink(script_file)
module.exit_json(**result) module.exit_json(**result)