1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

cron: PEP8 compliancy and doc fixes (#30884)

This PR includes:
- PEP8 compliancy fixes
- Documentation fixes
This commit is contained in:
Dag Wieers 2017-09-26 01:48:22 +02:00 committed by ansibot
parent 863fcb5ace
commit 4647713be9
2 changed files with 101 additions and 134 deletions

View file

@ -1,27 +1,24 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
#
# (c) 2012, Dane Summers <dsummers@pinedesk.biz> # Copyright: (c) 2012, Dane Summers <dsummers@pinedesk.biz>
# (c) 2013, Mike Grozak <mike.grozak@gmail.com> # Copyright: (c) 2013, Mike Grozak <mike.grozak@gmail.com>
# (c) 2013, Patrick Callahan <pmc@patrickcallahan.com> # Copyright: (c) 2013, Patrick Callahan <pmc@patrickcallahan.com>
# (c) 2015, Evan Kaufman <evan@digitalflophouse.com> # Copyright: (c) 2015, Evan Kaufman <evan@digitalflophouse.com>
# (c) 2015, Luca Berruti <nadirio@gmail.com> # Copyright: (c) 2015, Luca Berruti <nadirio@gmail.com>
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function from __future__ import absolute_import, division, print_function
__metaclass__ = type __metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1', ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'], 'status': ['preview'],
'supported_by': 'community'} 'supported_by': 'community'}
DOCUMENTATION = """ DOCUMENTATION = """
--- ---
module: cron module: cron
short_description: Manage cron.d and crontab entries. short_description: Manage cron.d and crontab entries
description: description:
- Use this module to manage crontab and environment variables entries. This module allows - Use this module to manage crontab and environment variables entries. This module allows
you to create environment variables and named crontab entries, update, or delete them. you to create environment variables and named crontab entries, update, or delete them.
@ -41,27 +38,21 @@ options:
- Description of a crontab entry or, if env is set, the name of environment variable. - Description of a crontab entry or, if env is set, the name of environment variable.
Required if state=absent. Note that if name is not set and state=present, then a Required if state=absent. Note that if name is not set and state=present, then a
new crontab entry will always be created, regardless of existing ones. new crontab entry will always be created, regardless of existing ones.
default: null
required: false
user: user:
description: description:
- The specific user whose crontab should be modified. - The specific user whose crontab should be modified.
required: false
default: root default: root
job: job:
description: description:
- The command to execute or, if env is set, the value of environment variable. - The command to execute or, if env is set, the value of environment variable.
The command should not contain line breaks. The command should not contain line breaks.
Required if state=present. Required if state=present.
required: false aliases: [ value ]
aliases: ['value']
default: null
state: state:
description: description:
- Whether to ensure the job or environment variable is present or absent. - Whether to ensure the job or environment variable is present or absent.
required: false choices: [ absent, present ]
default: present default: present
choices: [ "present", "absent" ]
cron_file: cron_file:
description: description:
- If specified, uses this file instead of an individual user's crontab. - If specified, uses this file instead of an individual user's crontab.
@ -70,92 +61,77 @@ options:
Many linux distros expect (and some require) the filename portion to consist solely Many linux distros expect (and some require) the filename portion to consist solely
of upper- and lower-case letters, digits, underscores, and hyphens. of upper- and lower-case letters, digits, underscores, and hyphens.
To use the C(cron_file) parameter you must specify the C(user) as well. To use the C(cron_file) parameter you must specify the C(user) as well.
required: false
default: null
backup: backup:
description: description:
- If set, create a backup of the crontab before it is modified. - If set, create a backup of the crontab before it is modified.
The location of the backup is returned in the C(backup_file) variable by this module. The location of the backup is returned in the C(backup_file) variable by this module.
required: false type: bool
choices: [ "yes", "no" ] default: 'no'
default: no
minute: minute:
description: description:
- Minute when the job should run ( 0-59, *, */2, etc ) - Minute when the job should run ( 0-59, *, */2, etc )
required: false
default: "*" default: "*"
hour: hour:
description: description:
- Hour when the job should run ( 0-23, *, */2, etc ) - Hour when the job should run ( 0-23, *, */2, etc )
required: false
default: "*" default: "*"
day: day:
description: description:
- Day of the month the job should run ( 1-31, *, */2, etc ) - Day of the month the job should run ( 1-31, *, */2, etc )
required: false
default: "*" default: "*"
aliases: [ "dom" ] aliases: [ dom ]
month: month:
description: description:
- Month of the year the job should run ( 1-12, *, */2, etc ) - Month of the year the job should run ( 1-12, *, */2, etc )
required: false
default: "*" default: "*"
weekday: weekday:
description: description:
- Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc ) - Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )
required: false
default: "*" default: "*"
aliases: [ "dow" ] aliases: [ dow ]
reboot: reboot:
description: description:
- If the job should be run at reboot. This option is deprecated. Users should use special_time. - If the job should be run at reboot. This option is deprecated. Users should use special_time.
version_added: "1.0" version_added: "1.0"
required: false type: bool
default: "no" default: "no"
choices: [ "yes", "no" ]
special_time: special_time:
description: description:
- Special time specification nickname. - Special time specification nickname.
choices: [ reboot, yearly, annually, monthly, weekly, daily, hourly ]
version_added: "1.3" version_added: "1.3"
required: false
default: null
choices: [ "reboot", "yearly", "annually", "monthly", "weekly", "daily", "hourly" ]
disabled: disabled:
description: description:
- If the job should be disabled (commented out) in the crontab. Only has effect if state=present - If the job should be disabled (commented out) in the crontab.
- Only has effect if C(state=present).
type: bool
default: 'no'
version_added: "2.0" version_added: "2.0"
required: false
default: false
env: env:
description: description:
- If set, manages a crontab's environment variable. New variables are added on top of crontab. - If set, manages a crontab's environment variable. New variables are added on top of crontab.
"name" and "value" parameters are the name and the value of environment variable. "name" and "value" parameters are the name and the value of environment variable.
version_added: "2.1" type: bool
required: false
default: "no" default: "no"
choices: [ "yes", "no" ] version_added: "2.1"
insertafter: insertafter:
description: description:
- Used with C(state=present) and C(env). If specified, the environment variable will be - Used with C(state=present) and C(env). If specified, the environment variable will be
inserted after the declaration of specified environment variable. inserted after the declaration of specified environment variable.
version_added: "2.1" version_added: "2.1"
required: false
default: null
insertbefore: insertbefore:
description: description:
- Used with C(state=present) and C(env). If specified, the environment variable will be - Used with C(state=present) and C(env). If specified, the environment variable will be
inserted before the declaration of specified environment variable. inserted before the declaration of specified environment variable.
version_added: "2.1" version_added: "2.1"
required: false
default: null
requirements: requirements:
- cron - cron
author: author:
- "Dane Summers (@dsummersl)" - Dane Summers (@dsummersl)
- 'Mike Grozak' - Mike Grozak
- 'Patrick Callahan' - Patrick Callahan
- 'Evan Kaufman (@EvanK)' - Evan Kaufman (@EvanK)
- 'Luca Berruti (@lberruti)' - Luca Berruti (@lberruti)
""" """
EXAMPLES = ''' EXAMPLES = '''
@ -242,12 +218,12 @@ class CronTab(object):
cron_file - a cron file under /etc/cron.d, or an absolute path cron_file - a cron file under /etc/cron.d, or an absolute path
""" """
def __init__(self, module, user=None, cron_file=None): def __init__(self, module, user=None, cron_file=None):
self.module = module self.module = module
self.user = user self.user = user
self.root = (os.getuid() == 0) self.root = (os.getuid() == 0)
self.lines = None self.lines = None
self.ansible = "#Ansible: " self.ansible = "#Ansible: "
self.existing = '' self.existing = ''
if cron_file: if cron_file:
if os.path.isabs(cron_file): if os.path.isabs(cron_file):
@ -278,7 +254,7 @@ class CronTab(object):
# using safely quoted shell for now, but this really should be two non-shell calls instead. FIXME # 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) (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. if rc != 0 and rc != 1: # 1 can mean that there are no jobs.
raise CronTabError("Unable to read crontab") raise CronTabError("Unable to read crontab")
self.existing = out self.existing = out
@ -286,9 +262,9 @@ class CronTab(object):
lines = out.splitlines() lines = out.splitlines()
count = 0 count = 0
for l in lines: for l in lines:
if count > 2 or (not re.match( r'# DO NOT EDIT THIS FILE - edit the master and reinstall.', l) and 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'# \(/tmp/.*installed on.*\)', l) and
not re.match( r'# \(.*version.*\)', l)): not re.match(r'# \(.*version.*\)', l)):
self.lines.append(l) self.lines.append(l)
else: else:
pattern = re.escape(l) + '[\r\n]?' pattern = re.escape(l) + '[\r\n]?'
@ -370,7 +346,7 @@ class CronTab(object):
other_decl = self.find_env(other_name) other_decl = self.find_env(other_name)
if len(other_decl) > 0: if len(other_decl) > 0:
if insertafter: if insertafter:
index = other_decl[0]+1 index = other_decl[0] + 1
elif insertbefore: elif insertbefore:
index = other_decl[0] index = other_decl[0]
self.lines.insert(index, decl) self.lines.insert(index, decl)
@ -409,32 +385,32 @@ class CronTab(object):
return [comment, l] return [comment, l]
else: else:
comment = None comment = None
elif re.match( r'%s' % self.ansible, l): elif re.match(r'%s' % self.ansible, l):
comment = re.sub( r'%s' % self.ansible, '', l) comment = re.sub(r'%s' % self.ansible, '', l)
# failing that, attempt to find job by exact match # failing that, attempt to find job by exact match
if job: if job:
for i, l in enumerate(self.lines): for i, l in enumerate(self.lines):
if l == job: if l == job:
# if no leading ansible header, insert one # if no leading ansible header, insert one
if not re.match( r'%s' % self.ansible, self.lines[i-1]): if not re.match(r'%s' % self.ansible, self.lines[i - 1]):
self.lines.insert(i, self.do_comment(name)) self.lines.insert(i, self.do_comment(name))
return [self.lines[i], l, True] return [self.lines[i], l, True]
# if a leading blank ansible header AND job has a name, update header # if a leading blank ansible header AND job has a name, update header
elif name and self.lines[i-1] == self.do_comment(None): elif name and self.lines[i - 1] == self.do_comment(None):
self.lines[i-1] = self.do_comment(name) self.lines[i - 1] = self.do_comment(name)
return [self.lines[i-1], l, True] return [self.lines[i - 1], l, True]
return [] return []
def find_env(self, name): def find_env(self, name):
for index, l in enumerate(self.lines): for index, l in enumerate(self.lines):
if re.match( r'^%s=' % name, l): if re.match(r'^%s=' % name, l):
return [index, l] return [index, l]
return [] return []
def get_cron_job(self,minute,hour,day,month,weekday,job,special,disabled): def get_cron_job(self, minute, hour, day, month, weekday, job, special, disabled):
# normalize any leading/trailing newlines (ansible/ansible-modules-core#3791) # normalize any leading/trailing newlines (ansible/ansible-modules-core#3791)
job = job.strip('\r\n') job = job.strip('\r\n')
@ -450,17 +426,16 @@ class CronTab(object):
return "%s@%s %s" % (disable_prefix, special, job) return "%s@%s %s" % (disable_prefix, special, job)
else: else:
if self.cron_file: if self.cron_file:
return "%s%s %s %s %s %s %s %s" % (disable_prefix,minute,hour,day,month,weekday,self.user,job) return "%s%s %s %s %s %s %s %s" % (disable_prefix, minute, hour, day, month, weekday, self.user, job)
else: else:
return "%s%s %s %s %s %s %s" % (disable_prefix,minute,hour,day,month,weekday,job) return "%s%s %s %s %s %s %s" % (disable_prefix, minute, hour, day, month, weekday, job)
def get_jobnames(self): def get_jobnames(self):
jobnames = [] jobnames = []
for l in self.lines: for l in self.lines:
if re.match( r'%s' % self.ansible, l): if re.match(r'%s' % self.ansible, l):
jobnames.append(re.sub( r'%s' % self.ansible, '', l)) jobnames.append(re.sub(r'%s' % self.ansible, '', l))
return jobnames return jobnames
@ -468,7 +443,7 @@ class CronTab(object):
envnames = [] envnames = []
for l in self.lines: for l in self.lines:
if re.match( r'^\S+=' , l): if re.match(r'^\S+=', l):
envnames.append(l.split('=')[0]) envnames.append(l.split('=')[0])
return envnames return envnames
@ -492,13 +467,13 @@ class CronTab(object):
if len(newlines) == 0: if len(newlines) == 0:
return True return True
else: else:
return False # TODO add some more error testing return False # TODO add some more error testing
def _update_env(self, name, decl, addenvfunction): def _update_env(self, name, decl, addenvfunction):
newlines = [] newlines = []
for l in self.lines: for l in self.lines:
if re.match( r'^%s=' % name, l): if re.match(r'^%s=' % name, l):
addenvfunction(newlines, decl) addenvfunction(newlines, decl)
else: else:
newlines.append(l) newlines.append(l)
@ -529,10 +504,10 @@ class CronTab(object):
elif platform.system() == 'AIX': elif platform.system() == 'AIX':
return "%s -l %s" % (pipes.quote(CRONCMD), pipes.quote(self.user)) return "%s -l %s" % (pipes.quote(CRONCMD), pipes.quote(self.user))
elif platform.system() == 'HP-UX': elif platform.system() == 'HP-UX':
return "%s %s %s" % (CRONCMD , '-l', pipes.quote(self.user)) return "%s %s %s" % (CRONCMD, '-l', pipes.quote(self.user))
elif pwd.getpwuid(os.getuid())[0] != self.user: elif pwd.getpwuid(os.getuid())[0] != self.user:
user = '-u %s' % pipes.quote(self.user) user = '-u %s' % pipes.quote(self.user)
return "%s %s %s" % (CRONCMD , user, '-l') return "%s %s %s" % (CRONCMD, user, '-l')
def _write_execute(self, path): def _write_execute(self, path):
""" """
@ -544,12 +519,9 @@ class CronTab(object):
return "chown %s %s ; su '%s' -c '%s %s'" % (pipes.quote(self.user), pipes.quote(path), pipes.quote(self.user), CRONCMD, pipes.quote(path)) return "chown %s %s ; su '%s' -c '%s %s'" % (pipes.quote(self.user), pipes.quote(path), pipes.quote(self.user), CRONCMD, pipes.quote(path))
elif pwd.getpwuid(os.getuid())[0] != self.user: elif pwd.getpwuid(os.getuid())[0] != self.user:
user = '-u %s' % pipes.quote(self.user) user = '-u %s' % pipes.quote(self.user)
return "%s %s %s" % (CRONCMD , user, pipes.quote(path)) return "%s %s %s" % (CRONCMD, user, pipes.quote(path))
#==================================================
def main(): def main():
# The following example playbooks: # The following example playbooks:
# #
@ -572,63 +544,60 @@ def main():
# * * 5,2 * * /some/dir/job.sh # * * 5,2 * * /some/dir/job.sh
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec=dict(
name=dict(required=False), name=dict(type='str'),
user=dict(required=False), user=dict(type='str'),
job=dict(required=False, aliases=['value']), job=dict(type='str', aliases=['value']),
cron_file=dict(required=False), cron_file=dict(type='str'),
state=dict(default='present', choices=['present', 'absent']), state=dict(type='str', default='present', choices=['present', 'absent']),
backup=dict(default=False, type='bool'), backup=dict(type='bool', default=False),
minute=dict(default='*'), minute=dict(type='str', default='*'),
hour=dict(default='*'), hour=dict(type='str', default='*'),
day=dict(aliases=['dom'], default='*'), day=dict(type='str', default='*', aliases=['dom']),
month=dict(default='*'), month=dict(type='str', default='*'),
weekday=dict(aliases=['dow'], default='*'), weekday=dict(type='str', default='*', aliases=['dow']),
reboot=dict(required=False, default=False, type='bool'), reboot=dict(type='bool', default=False),
special_time=dict(required=False, special_time=dict(type='str', choices=["reboot", "yearly", "annually", "monthly", "weekly", "daily", "hourly"]),
default=None, disabled=dict(type='bool', default=False),
choices=["reboot", "yearly", "annually", "monthly", "weekly", "daily", "hourly"], env=dict(type='bool'),
type='str'), insertafter=dict(type='str'),
disabled=dict(default=False, type='bool'), insertbefore=dict(type='str'),
env=dict(required=False, type='bool'),
insertafter=dict(required=False),
insertbefore=dict(required=False),
), ),
supports_check_mode = True, supports_check_mode=True,
mutually_exclusive=[ mutually_exclusive=[
['reboot', 'special_time'], ['reboot', 'special_time'],
['insertafter', 'insertbefore'], ['insertafter', 'insertbefore'],
] ],
) )
name = module.params['name'] name = module.params['name']
user = module.params['user'] user = module.params['user']
job = module.params['job'] job = module.params['job']
cron_file = module.params['cron_file'] cron_file = module.params['cron_file']
state = module.params['state'] state = module.params['state']
backup = module.params['backup'] backup = module.params['backup']
minute = module.params['minute'] minute = module.params['minute']
hour = module.params['hour'] hour = module.params['hour']
day = module.params['day'] day = module.params['day']
month = module.params['month'] month = module.params['month']
weekday = module.params['weekday'] weekday = module.params['weekday']
reboot = module.params['reboot'] reboot = module.params['reboot']
special_time = module.params['special_time'] special_time = module.params['special_time']
disabled = module.params['disabled'] disabled = module.params['disabled']
env = module.params['env'] env = module.params['env']
insertafter = module.params['insertafter'] insertafter = module.params['insertafter']
insertbefore = module.params['insertbefore'] insertbefore = module.params['insertbefore']
do_install = state == 'present' do_install = state == 'present'
changed = False changed = False
res_args = dict() res_args = dict()
warnings = list() warnings = list()
if cron_file: if cron_file:
cron_file_basename = os.path.basename(cron_file) cron_file_basename = os.path.basename(cron_file)
if not re.search(r'^[A-Z0-9_-]+$', cron_file_basename, re.I): if not re.search(r'^[A-Z0-9_-]+$', cron_file_basename, re.I):
warnings.append('Filename portion of cron_file ("%s") should consist' % cron_file_basename warnings.append('Filename portion of cron_file ("%s") should consist' % cron_file_basename +
+ ' solely of upper- and lower-case letters, digits, underscores, and hyphens') ' solely of upper- and lower-case letters, digits, underscores, and hyphens')
# Ensure all files generated are only writable by the owning user. Primarily relevant for the cron_file option. # Ensure all files generated are only writable by the owning user. Primarily relevant for the cron_file option.
os.umask(int('022', 8)) os.umask(int('022', 8))
@ -675,7 +644,6 @@ def main():
(backuph, backup_file) = tempfile.mkstemp(prefix='crontab') (backuph, backup_file) = tempfile.mkstemp(prefix='crontab')
crontab.write(backup_file) crontab.write(backup_file)
if crontab.cron_file and not name and not do_install: if crontab.cron_file and not name and not do_install:
if module._diff: if module._diff:
diff['after'] = '' diff['after'] = ''
@ -686,7 +654,7 @@ def main():
changed = os.path.isfile(crontab.cron_file) changed = os.path.isfile(crontab.cron_file)
else: else:
changed = crontab.remove_job_file() changed = crontab.remove_job_file()
module.exit_json(changed=changed,cron_file=cron_file,state=state,diff=diff) module.exit_json(changed=changed, cron_file=cron_file, state=state, diff=diff)
if env: if env:
if ' ' in name: if ' ' in name:
@ -737,10 +705,10 @@ def main():
changed = True changed = True
res_args = dict( res_args = dict(
jobs = crontab.get_jobnames(), jobs=crontab.get_jobnames(),
envs = crontab.get_envnames(), envs=crontab.get_envnames(),
warnings = warnings, warnings=warnings,
changed = changed changed=changed
) )
if changed: if changed:

View file

@ -404,7 +404,6 @@ lib/ansible/modules/storage/zfs/zfs.py
lib/ansible/modules/system/aix_inittab.py lib/ansible/modules/system/aix_inittab.py
lib/ansible/modules/system/at.py lib/ansible/modules/system/at.py
lib/ansible/modules/system/capabilities.py lib/ansible/modules/system/capabilities.py
lib/ansible/modules/system/cron.py
lib/ansible/modules/system/cronvar.py lib/ansible/modules/system/cronvar.py
lib/ansible/modules/system/crypttab.py lib/ansible/modules/system/crypttab.py
lib/ansible/modules/system/debconf.py lib/ansible/modules/system/debconf.py