Add test interface to cron state

This commit is contained in:
Thomas S Hatch 2012-04-11 00:15:14 -06:00
parent c02f6cec87
commit d31eb6f4df

View File

@ -35,6 +35,21 @@ Then the existing cron will be updated, but if the cron command is changed,
then a new cron job will be added to the user's crontab.
'''
def _check_cron(cmd, user, minute, hour, dom, month, dow):
'''
Return the changes
'''
lst = __salt__['cron.list_tab'](user)
for cron in lst['crons']:
if cmd == cron['cmd']:
if not minute == cron['min'] or \
not hour == cron['hour'] or \
not dom == cron['daymonth'] or \
not month == cron['month'] or \
not dow == cron['dayweek']:
return 'update'
return 'present'
return 'absent'
def present(name,
user='root',
@ -80,6 +95,26 @@ def present(name,
'comment': '',
'name': name,
'result': True}
if __opts__['test']:
status = _check_cron(
name,
user,
minute,
hour,
daymonth,
month,
dayweek)
ret['result'] = None
if status == 'absent':
ret['comment'] = 'Cron {0} is set to be added'.format(name)
elif status == 'present':
ret['result'] = True
ret['comment'] = 'Cron {0} already present'.format(name)
elif status == 'update':
ret['comment'] = 'Cron {0} is set to be updated'.format(name)
return ret
data = __salt__['cron.set_job'](
dom=daymonth,
dow=dayweek,
@ -149,6 +184,24 @@ def absent(name,
'result': True,
'changes': {},
'comment': ''}
if __opts__['test']:
status = _check_cron(
name,
user,
minute,
hour,
daymonth,
month,
dayweek)
ret['result'] = None
if status == 'absent':
ret['result'] = True
ret['comment'] = 'Cron {0} is absent'.format(name)
elif status == 'present' or status == 'update':
ret['comment'] = 'Cron {0} is set to be removed'.format(name)
return ret
data = __salt__['cron.rm_job'](
user,
minute,