mirror of
https://github.com/ansible-collections/ansible.posix.git
synced 2026-01-13 08:05:19 +01:00
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:
parent
3514f9d3dc
commit
7d4d234ee9
4 changed files with 55 additions and 13 deletions
4
changelogs/fragments/228-at_fix_delete_logic.yml
Normal file
4
changelogs/fragments/228-at_fix_delete_logic.yml
Normal 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.
|
||||
|
|
@ -86,9 +86,9 @@ def add_job(module, result, at_cmd, count, units, command, script_file):
|
|||
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):
|
||||
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)
|
||||
result['changed'] = True
|
||||
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):
|
||||
matching_jobs = []
|
||||
|
||||
os_type = platform.system().lower()
|
||||
atq_cmd = module.get_bin_path('atq', True)
|
||||
|
||||
# Get list of job numbers for the user.
|
||||
atq_command = "%s" % atq_cmd
|
||||
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:
|
||||
return matching_jobs
|
||||
|
||||
|
|
@ -115,12 +119,20 @@ def get_matching_jobs(module, at_cmd, script_file):
|
|||
# Loop through the jobs.
|
||||
# If the script text is contained in a job add job number to list.
|
||||
for current_job in current_jobs:
|
||||
split_current_job = current_job.split()
|
||||
at_opt = '-c' if platform.system() != 'AIX' else '-lv'
|
||||
at_command = "%s %s %s" % (at_cmd, at_opt, split_current_job[0])
|
||||
job_id = get_id_from_jobqueue(os_type, current_job)
|
||||
at_opt = '-c' if os_type != 'AIX' else '-lv'
|
||||
|
||||
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)
|
||||
if script_file_string in out:
|
||||
matching_jobs.append(split_current_job[0])
|
||||
matching_jobs.append(job_id)
|
||||
|
||||
# Return the list.
|
||||
return matching_jobs
|
||||
|
|
@ -134,6 +146,20 @@ def create_tempfile(command):
|
|||
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():
|
||||
|
||||
module = AnsibleModule(
|
||||
|
|
@ -151,6 +177,7 @@ def main():
|
|||
)
|
||||
|
||||
at_cmd = module.get_bin_path('at', True)
|
||||
atrm_cmd = module.get_bin_path('atrm', True)
|
||||
|
||||
command = module.params['command']
|
||||
script_file = module.params['script_file']
|
||||
|
|
@ -173,7 +200,7 @@ def main():
|
|||
|
||||
# if absent remove existing and return
|
||||
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:
|
||||
|
|
|
|||
|
|
@ -1,3 +1,2 @@
|
|||
shippable/posix/group1
|
||||
destructive
|
||||
disabled # fixme package
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@
|
|||
## at
|
||||
##
|
||||
|
||||
- name: define distros to attempt installing at on
|
||||
- name: define distros to attempt installing at on
|
||||
set_fact:
|
||||
package_distros:
|
||||
- RedHat
|
||||
|
|
@ -51,8 +51,8 @@
|
|||
count: 20
|
||||
units: minutes
|
||||
register: at_test0
|
||||
- debug: var=at_test0
|
||||
- name: validate results
|
||||
|
||||
- name: validate results for schedule creation
|
||||
assert:
|
||||
that:
|
||||
- 'at_test0.changed is defined'
|
||||
|
|
@ -60,3 +60,15 @@
|
|||
- 'at_test0.script_file is defined'
|
||||
- 'at_test0.state 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"'
|
||||
|
|
|
|||
Loading…
Reference in a new issue