at 6.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/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/>.

Richard C Isaacson committed
21
DOCUMENTATION = '''
22 23
---
module: at
24
short_description: Schedule the execution of a command or script file via the at command.
25
description:
26 27
 - Use this module to schedule a command or script file to run once in the future.
 - All jobs are executed in the 'a' queue.
28
version_added: "1.5"
Richard C Isaacson committed
29 30 31 32 33 34 35 36
options:
  command:
    description:
     - A command to be executed in the future.
    required: false
    default: null
  script_file:
    description:
37
     - An existing script file to be executed in the future.
Richard C Isaacson committed
38 39
    required: false
    default: null
40
  count:
Richard C Isaacson committed
41
    description:
42
     - The count of units in the future to execute the command or script file.
Richard C Isaacson committed
43
    required: true
44
  units:
Richard C Isaacson committed
45
    description:
46
     - The type of units in the future to execute the command or script file.
Richard C Isaacson committed
47 48
    required: true
    choices: ["minutes", "hours", "days", "weeks"]
49
  state:
50
    description:
51
     - The state dictates if the command or script file should be evaluated as present(added) or absent(deleted).
52 53 54 55 56 57
    required: false
    choices: ["present", "absent"]
    default: "present"
  unique:
    description:
     - If a matching job is present a new job will not be added.
58 59
    required: false
    default: false
60 61 62
requirements:
 - at
author: Richard Isaacson
Richard C Isaacson committed
63
'''
64

Richard C Isaacson committed
65
EXAMPLES = '''
66
# Schedule a command to execute in 20 minutes as root.
67
- at: command="ls -d / > /dev/null" count=20 units="minutes"
68 69

# Match a command to an existing job and delete the job.
70
- at: command="ls -d / > /dev/null" state="absent"
71 72

# Schedule a command to execute in 20 minutes making sure it is unique in the queue.
73
- at: command="ls -d / > /dev/null" unique=true count=20 units="minutes"
Richard C Isaacson committed
74
'''
75 76 77

import os
import tempfile
Richard C Isaacson committed
78 79


80 81 82 83 84 85 86
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)
    rc, out, err = module.run_command(at_command, check_rc=True)
    if command:
        os.unlink(script_file)
    result['changed'] = True

Richard C Isaacson committed
87

88
def delete_job(module, result, at_cmd, command, script_file):
Richard C Isaacson committed
89
    for matching_job in get_matching_jobs(module, at_cmd, script_file):
90 91 92 93 94 95
        at_command = "%s -d %s" % (at_cmd, matching_job)
        rc, out, err = module.run_command(at_command, check_rc=True)
        result['changed'] = True
    if command:
        os.unlink(script_file)
    module.exit_json(**result)
96

Richard C Isaacson committed
97 98

def get_matching_jobs(module, at_cmd, script_file):
99 100 101 102 103
    matching_jobs = []

    atq_cmd = module.get_bin_path('atq', True)

    # Get list of job numbers for the user.
Richard C Isaacson committed
104
    atq_command = "%s" % atq_cmd
105
    rc, out, err = module.run_command(atq_command, check_rc=True)
106 107 108 109 110
    current_jobs = out.splitlines()
    if len(current_jobs) == 0:
        return matching_jobs

    # Read script_file into a string.
111
    script_file_string = open(script_file).read().strip()
112 113 114 115 116 117

    # 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])
118
        rc, out, err = module.run_command(at_command, check_rc=True)
119 120 121 122 123 124
        if script_file_string in out:
            matching_jobs.append(split_current_job[0])

    # Return the list.
    return matching_jobs

Richard C Isaacson committed
125

126 127 128 129 130 131 132
def create_tempfile(command):
    filed, script_file = tempfile.mkstemp(prefix='at')
    fileh = os.fdopen(filed, 'w')
    fileh.write(command)
    fileh.close()
    return script_file

133 134

def main():
135

136 137
    module = AnsibleModule(
        argument_spec = dict(
138
            command=dict(required=False,
139
                         type='str'),
140
            script_file=dict(required=False,
141
                             type='str'),
142
            count=dict(required=False,
143
                       type='int'),
144
            units=dict(required=False,
145 146 147
                       default=None,
                       choices=['minutes', 'hours', 'days', 'weeks'],
                       type='str'),
148
            state=dict(required=False,
149 150 151
                       default='present',
                       choices=['present', 'absent'],
                       type='str'),
152 153 154
            unique=dict(required=False,
                        default=False,
                        type='bool')
155
        ),
Richard C Isaacson committed
156 157 158
        mutually_exclusive=[['command', 'script_file']],
        required_one_of=[['command', 'script_file']],
        supports_check_mode=False
159 160
    )

161
    at_cmd = module.get_bin_path('at', True)
162

163 164
    command        = module.params['command']
    script_file    = module.params['script_file']
165 166 167 168
    count          = module.params['count']
    units          = module.params['units']
    state          = module.params['state']
    unique         = module.params['unique']
169

Richard C Isaacson committed
170
    if (state == 'present') and (not count or not units):
171
        module.fail_json(msg="present state requires count and units")
172

Richard C Isaacson committed
173
    result = {'state': state, 'changed': False}
174

175
    # If command transform it into a script_file
176
    if command:
177 178 179 180 181
        script_file = create_tempfile(command)

    # if absent remove existing and return
    if state == 'absent':
        delete_job(module, result, at_cmd, command, script_file)
182 183

    # if unique if existing return unchanged
184
    if unique:
Richard C Isaacson committed
185
        if len(get_matching_jobs(module, at_cmd, script_file)) != 0:
186 187
            if command:
                os.unlink(script_file)
188 189 190
            module.exit_json(**result)

    result['script_file'] = script_file
191 192 193 194
    result['count'] = count
    result['units'] = units

    add_job(module, result, at_cmd, count, units, command, script_file)
195 196 197 198 199 200

    module.exit_json(**result)

# import module snippets
from ansible.module_utils.basic import *
main()