Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed job delete logic to cover RHEL6, CentOS6, *BSD and Solaris #228

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelogs/fragments/228-at_fix_delete_logic.yml
Original file line number Diff line number Diff line change
@@ -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.
45 changes: 36 additions & 9 deletions plugins/modules/at.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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(
Expand All @@ -151,6 +177,7 @@ def main():
)

at_cmd = module.get_bin_path('at', True)
atrm_cmd = module.get_bin_path('atrm', True)
Akasurde marked this conversation as resolved.
Show resolved Hide resolved

command = module.params['command']
script_file = module.params['script_file']
Expand All @@ -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:
Expand Down
1 change: 0 additions & 1 deletion tests/integration/targets/at/aliases
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
shippable/posix/group1
destructive
disabled # fixme package
18 changes: 15 additions & 3 deletions tests/integration/targets/at/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -51,12 +51,24 @@
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'
- 'at_test0.count is defined'
- '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"'