Fixed wrong job delete logic with RHEL6, CentOS6, *BSD and Solaris

* To avoid the "invalid option" error on RHEL6 and CentOS6,
  modified delete logic to use "atrm" command instead of "at -r".
* Fixed search logic for job_id to perform delete correctly on *BSD and Solaris.

Signed-off-by: Hideki Saito <saito@fgrep.org>
This commit is contained in:
Hideki Saito 2021-07-23 14:08:30 +09:00
parent 3514f9d3dc
commit 7d4d234ee9
4 changed files with 55 additions and 13 deletions

View file

@ -0,0 +1,4 @@
---
bugfixes:
- at - use ``atrm`` command instead of ``at -r`` to avoid ``invalid option`` error on ``RHEL6`` and ``CentOS6``.
- at - fixed search logic for job_id to perform correctly on ``*BSD`` and Solaris.

View file

@ -86,9 +86,9 @@ def add_job(module, result, at_cmd, count, units, command, 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, atrm_cmd, command, script_file):
for matching_job in get_matching_jobs(module, at_cmd, script_file): for matching_job in get_matching_jobs(module, at_cmd, script_file):
at_command = "%s -r %s" % (at_cmd, matching_job) at_command = "%s %s" % (atrm_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
if command: if command:
@ -98,13 +98,17 @@ def delete_job(module, result, at_cmd, command, script_file):
def get_matching_jobs(module, at_cmd, script_file): def get_matching_jobs(module, at_cmd, script_file):
matching_jobs = [] matching_jobs = []
os_type = platform.system().lower()
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() if os_type in ['sunos', 'openbsd']:
# Skip header in the command-line output
current_jobs = out.splitlines()[1:]
else:
current_jobs = out.splitlines()
if len(current_jobs) == 0: if len(current_jobs) == 0:
return matching_jobs return matching_jobs
@ -115,12 +119,20 @@ def get_matching_jobs(module, at_cmd, script_file):
# Loop through the jobs. # Loop through the jobs.
# If the script text is contained in a job add job number to list. # If the script text is contained in a job add job number to list.
for current_job in current_jobs: for current_job in current_jobs:
split_current_job = current_job.split() job_id = get_id_from_jobqueue(os_type, current_job)
at_opt = '-c' if platform.system() != 'AIX' else '-lv' at_opt = '-c' if os_type != 'AIX' else '-lv'
at_command = "%s %s %s" % (at_cmd, at_opt, split_current_job[0])
if os_type == 'sunos':
# at -c option is different purpose in Solaris.
# So it needs to read job spool file(/var/spool/cron/atjobs/<job_ID>) directly.
at_cmd = 'cat'
at_dir = '/var/spool/cron/atjobs/'
at_command = '%s %s/%s' % (at_cmd, at_dir, job_id)
else:
at_command = "%s %s %s" % (at_cmd, at_opt, job_id)
rc, out, err = module.run_command(at_command, check_rc=True) rc, out, err = module.run_command(at_command, check_rc=True)
if script_file_string in out: if script_file_string in out:
matching_jobs.append(split_current_job[0]) matching_jobs.append(job_id)
# Return the list. # Return the list.
return matching_jobs return matching_jobs
@ -134,6 +146,20 @@ def create_tempfile(command):
return script_file return script_file
def get_id_from_jobqueue(os_type, current_job):
# Linux: job_id is located at the beginning of the atq output.
# FreeBSD and NetBSD: job_id is located at the end of the atq output,
# OpenBSD and Solaris: job_id is located in middle of the atq output.
split_current_job = current_job.split()
if os_type in ['freebsd', 'netbsd']:
job_id = split_current_job[-1]
elif os_type in ['openbsd', 'sunos']:
job_id = split_current_job[6]
else:
job_id = split_current_job[0]
return job_id
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
@ -151,6 +177,7 @@ def main():
) )
at_cmd = module.get_bin_path('at', True) at_cmd = module.get_bin_path('at', True)
atrm_cmd = module.get_bin_path('atrm', True)
command = module.params['command'] command = module.params['command']
script_file = module.params['script_file'] script_file = module.params['script_file']
@ -173,7 +200,7 @@ def main():
# if absent remove existing and return # if absent remove existing and return
if state == 'absent': if state == 'absent':
delete_job(module, result, at_cmd, command, script_file) delete_job(module, result, at_cmd, atrm_cmd, command, script_file)
# if unique if existing return unchanged # if unique if existing return unchanged
if unique: if unique:

View file

@ -1,3 +1,2 @@
shippable/posix/group1 shippable/posix/group1
destructive destructive
disabled # fixme package

View file

@ -51,8 +51,8 @@
count: 20 count: 20
units: minutes units: minutes
register: at_test0 register: at_test0
- debug: var=at_test0
- name: validate results - name: validate results for schedule creation
assert: assert:
that: that:
- 'at_test0.changed is defined' - 'at_test0.changed is defined'
@ -60,3 +60,15 @@
- 'at_test0.script_file is defined' - 'at_test0.script_file is defined'
- 'at_test0.state is defined' - 'at_test0.state is defined'
- 'at_test0.units is defined' - 'at_test0.units is defined'
- name: remove first example schedule
at:
command: "ls -d / > /dev/null"
state: absent
register: at_test1
- name: validate results for schedule deletion
assert:
that:
- 'at_test1.changed'
- 'at_test1.state == "absent"'