summaryrefslogtreecommitdiff
path: root/system/cronvar.py
blob: 1a26182be88c7bd97d896a7060ed114f74e8a187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# 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/>.
#
# Cronvar Plugin: The goal of this plugin is to provide an indempotent
# method for set cron variable values.  It should play well with the
# existing cron module as well as allow for manually added variables.
# Each variable entered will be preceded with a comment describing the
# variable so that it can be found later.  This is required to be
# present in order for this plugin to find/modify the variable
#
# This module is based on the crontab module.
#

DOCUMENTATION = """
---
module: cronvar
short_description: Manage variables in crontabs
description:
  - Use this module to manage crontab variables. This module allows
    you to create, update, or delete cron variable definitions.
version_added: "2.0"
options:
  name:
    description:
      - Name of the crontab variable.
    default: null
    required: true
  value:
    description:
      - The value to set this variable to.  Required if state=present.
    required: false
    default: null
  insertafter:
    required: false
    default: null
    description:
      - Used with C(state=present). If specified, the variable will be inserted
        after the variable specified.
  insertbefore:
    required: false
    default: null
    description:
      - Used with C(state=present). If specified, the variable will be inserted
        just before the variable specified.
  state:
    description:
      - Whether to ensure that the variable is present or absent.
    required: false
    default: present
    choices: [ "present", "absent" ]
  user:
    description:
      - The specific user whose crontab should be modified.
    required: false
    default: root
  cron_file:
    description:
      - If specified, uses this file instead of an individual user's crontab.
        Without a leading /, this is assumed to be in /etc/cron.d.  With a leading
        /, this is taken as absolute.
    required: false
    default: null
  backup:
    description:
      - If set, create a backup of the crontab before it is modified.
        The location of the backup is returned in the C(backup) variable by this module.
    required: false
    default: false
requirements:
  - cron
author: "Doug Luce (@dougluce)"
"""

EXAMPLES = '''
# Ensure a variable exists.
# Creates an entry like "EMAIL=doug@ansibmod.con.com"
- cronvar:
    name: EMAIL
    value: doug@ansibmod.con.com

# Make sure a variable is gone.  This will remove any variable named
# "LEGACY"
- cronvar:
    name: LEGACY
    state: absent

# Adds a variable to a file under /etc/cron.d
- cronvar:
    name: LOGFILE
    value: /var/log/yum-autoupdate.log
    user: root
    cron_file: ansible_yum-autoupdate
'''

import os
import re
import tempfile
import platform
import pipes
import shlex
from ansible.module_utils.basic import *
from ansible.module_utils.pycompat24 import get_exception

CRONCMD = "/usr/bin/crontab"

class CronVarError(Exception):
    pass

class CronVar(object):
    """
        CronVar object to write variables to crontabs.

        user      - the user of the crontab (defaults to root)
        cron_file - a cron file under /etc/cron.d
    """
    def __init__(self, module, user=None, cron_file=None):
        self.module = module
        self.user = user
        if self.user is None:
            self.user = 'root'
        self.lines = None
        self.wordchars = ''.join(chr(x) for x in range(128) if chr(x) not in ('=', "'", '"', ))

        if cron_file:
            self.cron_file = ""
            if os.path.isabs(cron_file):
                self.cron_file = cron_file
            else:
                self.cron_file = os.path.join('/etc/cron.d', cron_file)
        else:
            self.cron_file = None

        self.read()

    def read(self):
        # Read in the crontab from the system
        self.lines = []
        if self.cron_file:
            # read the cronfile
            try:
                f = open(self.cron_file, 'r')
                self.lines = f.read().splitlines()
                f.close()
            except IOError:
                e = get_exception()
                # cron file does not exist
                return
            except:
                raise CronVarError("Unexpected error:", sys.exc_info()[0])
        else:
            # using safely quoted shell for now, but this really should be two non-shell calls instead.  FIXME
            (rc, out, err) = self.module.run_command(self._read_user_execute(), use_unsafe_shell=True)

            if rc != 0 and rc != 1: # 1 can mean that there are no jobs.
                raise CronVarError("Unable to read crontab")

            lines = out.splitlines()
            count = 0
            for l in lines:
                if count > 2 or (not re.match( r'# DO NOT EDIT THIS FILE - edit the master and reinstall.', l) and
                                 not re.match( r'# \(/tmp/.*installed on.*\)', l) and
                                 not re.match( r'# \(.*version.*\)', l)):
                    self.lines.append(l)
                count += 1

    def log_message(self, message):
        self.module.debug('ansible: "%s"' % message)

    def write(self, backup_file=None):
        """
        Write the crontab to the system. Saves all information.
        """
        if backup_file:
            fileh = open(backup_file, 'w')
        elif self.cron_file:
            fileh = open(self.cron_file, 'w')
        else:
            filed, path = tempfile.mkstemp(prefix='crontab')
            fileh = os.fdopen(filed, 'w')

        fileh.write(self.render())
        fileh.close()

        # return if making a backup
        if backup_file:
            return

        # Add the entire crontab back to the user crontab
        if not self.cron_file:
            # quoting shell args for now but really this should be two non-shell calls.  FIXME
            (rc, out, err) = self.module.run_command(self._write_execute(path), use_unsafe_shell=True)
            os.unlink(path)

            if rc != 0:
                self.module.fail_json(msg=err)

    def remove_variable_file(self):
        try:
            os.unlink(self.cron_file)
            return True
        except OSError:
            e = get_exception()
            # cron file does not exist
            return False
        except:
            raise CronVarError("Unexpected error:", sys.exc_info()[0])

    def parse_for_var(self, line):
        lexer = shlex.shlex(line)
        lexer.wordchars = self.wordchars
        varname = lexer.get_token()
        is_env_var = lexer.get_token() == '='
        value = ''.join(lexer)
        if is_env_var:
            return (varname, value)
        raise CronVarError("Not a variable.")

    def find_variable(self, name):
        comment = None
        for l in self.lines:
            try:
                (varname, value) = self.parse_for_var(l)
                if varname == name:
                    return value
            except CronVarError:
                pass
        return None

    def get_var_names(self):
        var_names = []
        for l in self.lines:
            try:
                (var_name, _) = self.parse_for_var(l)
                var_names.append(var_name)
            except CronVarError:
                pass
        return var_names

    def add_variable(self, name, value, insertbefore, insertafter):
        if insertbefore is None and insertafter is None:
            # Add the variable to the top of the file.
            self.lines.insert(0, "%s=%s" % (name, value))
        else:
            newlines = []
            for l in self.lines:
                try:
                    (varname, _) = self.parse_for_var(l) # Throws if not a var line
                    if varname == insertbefore:
                        newlines.append("%s=%s" % (name, value))
                        newlines.append(l)
                    elif varname == insertafter:
                        newlines.append(l)
                        newlines.append("%s=%s" % (name, value))
                    else:
                        raise CronVarError # Append.
                except CronVarError:
                    newlines.append(l)

            self.lines = newlines

    def remove_variable(self, name):
        self.update_variable(name, None, remove=True)

    def update_variable(self, name, value, remove=False):
        newlines = []
        for l in self.lines:
            try:
                (varname, _) = self.parse_for_var(l) # Throws if not a var line
                if varname != name:
                    raise CronVarError # Append.
                if not remove:
                    newlines.append("%s=%s" % (name, value))
            except CronVarError:
                newlines.append(l)

        self.lines = newlines

    def render(self):
        """
        Render a proper crontab
        """
        result = '\n'.join(self.lines)
        if result and result[-1] not in ['\n', '\r']:
            result += '\n'
        return result

    def _read_user_execute(self):
        """
        Returns the command line for reading a crontab
        """
        user = ''

        if self.user:
            if platform.system() == 'SunOS':
                return "su %s -c '%s -l'" % (pipes.quote(self.user), pipes.quote(CRONCMD))
            elif platform.system() == 'AIX':
                return "%s -l %s" % (pipes.quote(CRONCMD), pipes.quote(self.user))
            elif platform.system() == 'HP-UX':
                return "%s %s %s" % (CRONCMD , '-l', pipes.quote(self.user))
            else:
                user = '-u %s' % pipes.quote(self.user)
        return "%s %s %s" % (CRONCMD , user, '-l')

    def _write_execute(self, path):
        """
        Return the command line for writing a crontab
        """
        user = ''
        if self.user:
            if platform.system() in ['SunOS', 'HP-UX', 'AIX']:
                return "chown %s %s ; su '%s' -c '%s %s'" % (pipes.quote(self.user), pipes.quote(path), pipes.quote(self.user), CRONCMD, pipes.quote(path))
            else:
                user = '-u %s' % pipes.quote(self.user)
        return "%s %s %s" % (CRONCMD , user, pipes.quote(path))

#==================================================

def main():
    # The following example playbooks:
    #
    # - cronvar: name="SHELL" value="/bin/bash"
    #
    # - name: Set the email
    #   cronvar: name="EMAILTO" value="doug@ansibmod.con.com"
    #
    # - name: Get rid of the old new host variable
    #   cronvar: name="NEW_HOST" state=absent
    #
    # Would produce:
    # SHELL = /bin/bash
    # EMAILTO = doug@ansibmod.con.com

    module = AnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            value=dict(required=False),
            user=dict(required=False),
            cron_file=dict(required=False),
            insertafter=dict(default=None),
            insertbefore=dict(default=None),
            state=dict(default='present', choices=['present', 'absent']),
            backup=dict(default=False, type='bool'),
        ),
        mutually_exclusive=[['insertbefore', 'insertafter']],
        supports_check_mode=False,
    )

    name = module.params['name']
    value = module.params['value']
    user = module.params['user']
    cron_file = module.params['cron_file']
    insertafter = module.params['insertafter']
    insertbefore = module.params['insertbefore']
    state = module.params['state']
    backup = module.params['backup']
    ensure_present = state == 'present'

    changed = False
    res_args = dict()

    # Ensure all files generated are only writable by the owning user.  Primarily relevant for the cron_file option.
    os.umask(int('022',8))
    cronvar = CronVar(module, user, cron_file)

    module.debug('cronvar instantiated - name: "%s"' % name)

    # --- user input validation ---

    if name is None and ensure_present:
        module.fail_json(msg="You must specify 'name' to insert a new cron variabale")

    if value is None and ensure_present:
        module.fail_json(msg="You must specify 'value' to insert a new cron variable")

    if name is None and not ensure_present:
        module.fail_json(msg="You must specify 'name' to remove a cron variable")

    # if requested make a backup before making a change
    if backup:
        (_, backup_file) = tempfile.mkstemp(prefix='cronvar')
        cronvar.write(backup_file)

    if cronvar.cron_file and not name and not ensure_present:
        changed = cronvar.remove_job_file()
        module.exit_json(changed=changed, cron_file=cron_file, state=state)

    old_value = cronvar.find_variable(name)

    if ensure_present:
        if old_value is None:
            cronvar.add_variable(name, value, insertbefore, insertafter)
            changed = True
        elif old_value != value:
            cronvar.update_variable(name, value)
            changed = True
    else:
        if old_value is not None:
            cronvar.remove_variable(name)
            changed = True

    res_args = {
        "vars": cronvar.get_var_names(),
        "changed": changed
    }

    if changed:
        cronvar.write()

    # retain the backup only if crontab or cron file have changed
    if backup:
        if changed:
            res_args['backup_file'] = backup_file
        else:
            os.unlink(backup_file)

    if cron_file:
        res_args['cron_file'] = cron_file

    module.exit_json(**res_args)

    # --- should never get here
    module.exit_json(msg="Unable to execute cronvar task.")


if __name__ == '__main__':
    main()