2014-01-14 07:18:47 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# (c) 2014, Richard Isaacson <richard.c.isaacson@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-01-14 07:53:18 +01:00
|
|
|
DOCUMENTATION = '''
|
2014-01-14 07:18:47 +01:00
|
|
|
---
|
|
|
|
module: at
|
|
|
|
short_description: Schedule the execution of a command or scripts via the at command.
|
|
|
|
description:
|
|
|
|
- Use this module to schedule a command or script to run once in the future.
|
2014-01-15 08:32:19 +01:00
|
|
|
- All jobs are executed in the a queue.
|
2014-01-14 07:53:18 +01:00
|
|
|
version_added: "0.0"
|
|
|
|
options:
|
|
|
|
user:
|
|
|
|
description:
|
|
|
|
- The user to execute the at command as.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
command:
|
|
|
|
description:
|
|
|
|
- A command to be executed in the future.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
script_file:
|
|
|
|
description:
|
|
|
|
- An existing script to be executed in the future.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
unit_count:
|
|
|
|
description:
|
|
|
|
- The count of units in the future to execute the command or script.
|
|
|
|
required: true
|
|
|
|
unit_type:
|
|
|
|
description:
|
|
|
|
- The type of units in the future to execute the command or script.
|
|
|
|
required: true
|
|
|
|
choices: ["minutes", "hours", "days", "weeks"]
|
2014-01-15 08:32:19 +01:00
|
|
|
action:
|
|
|
|
description:
|
|
|
|
- The action to take for the job defaulting to add. Unique will verify that there is only one entry in the queue.
|
|
|
|
- Delete will remove all existing queued jobs.
|
|
|
|
required: true
|
|
|
|
choices: ["add", "delete", "unique"]
|
|
|
|
default: add
|
2014-01-14 07:18:47 +01:00
|
|
|
requirements:
|
|
|
|
- at
|
|
|
|
author: Richard Isaacson
|
2014-01-14 07:53:18 +01:00
|
|
|
'''
|
2014-01-14 07:18:47 +01:00
|
|
|
|
2014-01-14 07:53:18 +01:00
|
|
|
EXAMPLES = '''
|
2014-01-14 07:18:47 +01:00
|
|
|
# Schedule a command to execute in 20 minutes as root.
|
2014-01-15 08:32:19 +01:00
|
|
|
- at: command="ls -d / > /dev/null" unit_count=20 unit_type="minutes"
|
2014-01-14 07:18:47 +01:00
|
|
|
|
|
|
|
# Schedule a script to execute in 1 hour as the neo user.
|
|
|
|
- at: script_file="/some/script.sh" user="neo" unit_count=1 unit_type="hours"
|
2014-01-15 08:32:19 +01:00
|
|
|
|
|
|
|
# Match a command to an existing job and delete the job.
|
|
|
|
- at: command="ls -d / > /dev/null" action="delete"
|
|
|
|
|
|
|
|
# Schedule a command to execute in 20 minutes making sure it is unique in the queue.
|
|
|
|
- at: command="ls -d / > /dev/null" action="unique" unit_count=20 unit_type="minutes"
|
2014-01-14 07:53:18 +01:00
|
|
|
'''
|
2014-01-14 07:18:47 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
|
2014-01-15 08:32:19 +01:00
|
|
|
def matching_jobs(module, at_cmd, script_file, user=None):
|
|
|
|
matching_jobs = []
|
|
|
|
|
|
|
|
atq_cmd = module.get_bin_path('atq', True)
|
|
|
|
|
|
|
|
# Get list of job numbers for the user.
|
|
|
|
atq_command = "%s" % (atq_cmd)
|
|
|
|
if user:
|
|
|
|
atq_command = "su '%s' -c '%s'" % (user, atq_command)
|
|
|
|
rc, out, err = module.run_command(atq_command)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
current_jobs = out.splitlines()
|
|
|
|
if len(current_jobs) == 0:
|
|
|
|
return matching_jobs
|
|
|
|
|
|
|
|
# Read script_file into a string.
|
2014-01-15 09:17:04 +01:00
|
|
|
script_file_string = open(script_file).read().strip()
|
2014-01-15 08:32:19 +01:00
|
|
|
|
|
|
|
# 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_command = "%s -c %s" % (at_cmd, split_current_job[0])
|
|
|
|
if user:
|
|
|
|
at_command = "su '%s' -c '%s'" % (user, at_command)
|
|
|
|
rc, out, err = module.run_command(at_command)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
if script_file_string in out:
|
|
|
|
matching_jobs.append(split_current_job[0])
|
|
|
|
|
|
|
|
# Return the list.
|
|
|
|
return matching_jobs
|
|
|
|
|
2014-01-14 07:18:47 +01:00
|
|
|
#================================================
|
|
|
|
|
|
|
|
def main():
|
2014-01-15 06:50:36 +01:00
|
|
|
|
2014-01-14 07:18:47 +01:00
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
2014-01-14 07:53:18 +01:00
|
|
|
user=dict(required=False),
|
2014-01-14 07:18:47 +01:00
|
|
|
command=dict(required=False),
|
|
|
|
script_file=dict(required=False),
|
2014-01-15 09:17:04 +01:00
|
|
|
unit_count=dict(required=False,
|
2014-01-15 08:32:19 +01:00
|
|
|
type='int'),
|
2014-01-15 09:17:04 +01:00
|
|
|
unit_type=dict(required=False,
|
2014-01-14 17:41:37 +01:00
|
|
|
default=None,
|
|
|
|
choices=["minutes", "hours", "days", "weeks"],
|
2014-01-15 08:32:19 +01:00
|
|
|
type="str"),
|
|
|
|
action=dict(required=False,
|
|
|
|
default="add",
|
|
|
|
choices=["add", "delete", "unique"],
|
|
|
|
type="str")
|
2014-01-14 07:18:47 +01:00
|
|
|
),
|
|
|
|
supports_check_mode = False,
|
|
|
|
)
|
|
|
|
|
2014-01-15 08:32:19 +01:00
|
|
|
at_cmd = module.get_bin_path('at', True)
|
2014-01-15 06:50:36 +01:00
|
|
|
|
2014-01-15 08:32:19 +01:00
|
|
|
user = module.params['user']
|
2014-01-14 07:18:47 +01:00
|
|
|
command = module.params['command']
|
|
|
|
script_file = module.params['script_file']
|
|
|
|
unit_count = module.params['unit_count']
|
|
|
|
unit_type = module.params['unit_type']
|
2014-01-15 08:32:19 +01:00
|
|
|
action = module.params['action']
|
|
|
|
|
2014-01-15 09:17:04 +01:00
|
|
|
if ((action == 'add') and (not unit_count or not unit_type)):
|
|
|
|
module.fail_json(msg="add action requires unit_count and unit_type")
|
|
|
|
|
2014-01-15 08:32:19 +01:00
|
|
|
if (not command) and (not script_file):
|
|
|
|
module.fail_json(msg="command or script_file not specified")
|
2014-01-14 07:18:47 +01:00
|
|
|
|
|
|
|
if command and script_file:
|
|
|
|
module.fail_json(msg="command and script_file are mutually exclusive")
|
|
|
|
|
|
|
|
result = {}
|
2014-01-15 09:17:04 +01:00
|
|
|
result['action'] = action
|
2014-01-15 08:32:19 +01:00
|
|
|
result['changed'] = False
|
2014-01-14 07:18:47 +01:00
|
|
|
|
2014-01-15 08:32:19 +01:00
|
|
|
# If command transform it into a script_file
|
2014-01-14 07:18:47 +01:00
|
|
|
if command:
|
2014-01-15 08:32:19 +01:00
|
|
|
filed, script_file = tempfile.mkstemp(prefix='at')
|
2014-01-14 07:18:47 +01:00
|
|
|
fileh = os.fdopen(filed, 'w')
|
|
|
|
fileh.write(command)
|
|
|
|
fileh.close()
|
2014-01-15 08:32:19 +01:00
|
|
|
|
|
|
|
# if delete then return
|
|
|
|
if action == 'delete':
|
|
|
|
for matching_job in matching_jobs(module, at_cmd, script_file, user):
|
|
|
|
at_command = "%s -d %s" % (at_cmd, matching_job)
|
|
|
|
if user:
|
|
|
|
at_command = "su '%s' -c '%s'" % (user, at_ccommand)
|
|
|
|
rc, out, err = module.run_command(at_command)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
result['changed'] = True
|
2014-01-15 08:41:20 +01:00
|
|
|
module.exit_json(**result)
|
2014-01-15 08:32:19 +01:00
|
|
|
|
|
|
|
# if unique if existing return unchanged
|
|
|
|
if action == 'unique':
|
2014-01-15 09:17:04 +01:00
|
|
|
if len(matching_jobs(module, at_cmd, script_file, user)) != 0:
|
2014-01-15 08:32:19 +01:00
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
result['script_file'] = script_file
|
|
|
|
result['unit_count'] = unit_count
|
|
|
|
result['unit_type'] = unit_type
|
|
|
|
|
|
|
|
at_command = "%s now + %s %s -f %s" % (at_cmd, unit_count, unit_type, script_file)
|
|
|
|
if user:
|
|
|
|
# We expect that if this is an installed the permissions are already correct for the user to execute it.
|
|
|
|
at_command = "su '%s' -c '%s'" % (user, at_command)
|
|
|
|
rc, out, err = module.run_command(at_command)
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
if command:
|
|
|
|
os.unlink(script_file)
|
|
|
|
result['changed'] = True
|
2014-01-14 07:18:47 +01:00
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
main()
|