adding ability to move and copy scheduled jobs to another minion or minons. Also updated the publish module to be able to send keyword arguments as a list.

This commit is contained in:
Gareth J. Greenaway 2014-07-10 12:50:37 -07:00
parent 2c0c11a6ab
commit b9085deb39
2 changed files with 190 additions and 2 deletions

View File

@ -55,7 +55,10 @@ def _publish(
log.info('Function name is \'publish.publish\'. Returning {}')
return {}
arg = [salt.utils.args.yamlify_arg(arg)]
if not isinstance(arg, list):
arg = [salt.utils.args.yamlify_arg(arg)]
else:
arg = [salt.utils.args.yamlify_arg(x) for x in arg]
if len(arg) == 1 and arg[0] is None:
arg = []
@ -169,6 +172,13 @@ def publish(tgt, fun, arg=None, expr_form='glob', returner='', timeout=5):
salt '*' publish.publish test.kwarg arg='cheese=spam'
Multiple keyword arguments should be passed as a list.
.. code-block:: bash
salt '*' publish.publish test.kwarg arg="['cheese=spam','spam=cheese']"
'''
return _publish(tgt,
@ -223,7 +233,10 @@ def runner(fun, arg=None, timeout=5):
salt publish.runner manage.down
'''
arg = [salt.utils.args.yamlify_arg(arg)]
if not isinstance(arg, list):
arg = [salt.utils.args.yamlify_arg(arg)]
else:
arg = [salt.utils.args.yamlify_arg(x) for x in arg]
if len(arg) == 1 and arg[0] is None:
arg = []

View File

@ -270,6 +270,7 @@ def add(name, **kwargs):
ret = {'comment': [],
'result': True}
log.debug('kwargs {0}'.format(kwargs))
current_schedule = __opts__['schedule'].copy()
if 'schedule' in __pillar__:
current_schedule.update(__pillar__['schedule'])
@ -666,3 +667,177 @@ def reload_():
ret['comment'].append('Failed to reload schedule on minion. Saved file is empty or invalid.')
ret['result'] = False
return ret
def move(name, target, **kwargs):
'''
Move scheduled job to another minion or minions.
CLI Example:
.. code-block:: bash
salt '*' schedule.move jobname target
'''
ret = {'comment': [],
'result': True}
if not name:
ret['comment'] = 'Job name is required.'
ret['result'] = False
if name in __opts__['schedule']:
if 'test' in kwargs and kwargs['test']:
ret['comment'] = 'Job: {0} would be moved from schedule.'.format(name)
else:
out = delete(name)
schedule_opts = []
for key, value in __opts__['schedule'][name].iteritems():
temp = '{0}={1}'.format(key, value)
schedule_opts.append(temp)
response = __salt__['publish.publish'](target, 'schedule.add', schedule_opts)
# Get errors and list of affeced minions
errors = []
minions = []
for minion in response:
minions.append(minion)
if not response[minion]:
errors.append(minion)
# parse response
if not response:
ret['comment'] = 'no servers answered the published schedule.add command'
return ret
elif len(errors) > 0:
ret['comment'] = 'the following minions return False'
ret['minions'] = errors
return ret
else:
ret['result'] = True
ret['comment'] = 'Moved Job {0} from schedule.'.format(name)
ret['minions'] = minions
return ret
elif 'schedule' in __pillar__ and name in __pillar__['schedule']:
if 'test' in kwargs and kwargs['test']:
ret['comment'] = 'Job: {0} would be moved from schedule.'.format(name)
else:
out = delete(name, where='pillar')
schedule_opts = []
for key, value in __opts__['schedule'][name].iteritems():
temp = '{0}={1}'.format(key, value)
schedule_opts.append(temp)
response = __salt__['publish.publish'](target, 'schedule.add', schedule_opts)
# Get errors and list of affeced minions
errors = []
minions = []
for minion in response:
minions.append(minion)
if not response[minion]:
errors.append(minion)
# parse response
if not response:
ret['comment'] = 'no servers answered the published schedule.add command'
return ret
elif len(errors) > 0:
ret['comment'] = 'the following minions return False'
ret['minions'] = errors
return ret
else:
ret['result'] = True
ret['comment'] = 'Moved Job {0} from schedule.'.format(name)
ret['minions'] = minions
return ret
else:
ret['comment'] = 'Job {0} does not exist.'.format(name)
ret['result'] = False
return ret
def copy(name, target, **kwargs):
'''
Copy scheduled job to another minion or minions.
CLI Example:
.. code-block:: bash
salt '*' schedule.copy jobname target
'''
ret = {'comment': [],
'result': True}
if not name:
ret['comment'] = 'Job name is required.'
ret['result'] = False
if name in __opts__['schedule']:
if 'test' in kwargs and kwargs['test']:
ret['comment'] = 'Job: {0} would be copied.'.format(name)
else:
schedule_opts = []
for key, value in __opts__['schedule'][name].iteritems():
temp = '{0}={1}'.format(key, value)
schedule_opts.append(temp)
response = __salt__['publish.publish'](target, 'schedule.add', schedule_opts)
# Get errors and list of affeced minions
errors = []
minions = []
for minion in response:
minions.append(minion)
if not response[minion]:
errors.append(minion)
# parse response
if not response:
ret['comment'] = 'no servers answered the published schedule.add command'
return ret
elif len(errors) > 0:
ret['comment'] = 'the following minions return False'
ret['minions'] = errors
return ret
else:
ret['result'] = True
ret['comment'] = 'Moved Job {0} from schedule.'.format(name)
ret['minions'] = minions
return ret
elif 'schedule' in __pillar__ and name in __pillar__['schedule']:
if 'test' in kwargs and kwargs['test']:
ret['comment'] = 'Job: {0} would be moved from schedule.'.format(name)
else:
schedule_opts = []
for key, value in __opts__['schedule'][name].iteritems():
temp = '{0}={1}'.format(key, value)
schedule_opts.append(temp)
response = __salt__['publish.publish'](target, 'schedule.add', schedule_opts)
# Get errors and list of affeced minions
errors = []
minions = []
for minion in response:
minions.append(minion)
if not response[minion]:
errors.append(minion)
# parse response
if not response:
ret['comment'] = 'no servers answered the published schedule.add command'
return ret
elif len(errors) > 0:
ret['comment'] = 'the following minions return False'
ret['minions'] = errors
return ret
else:
ret['result'] = True
ret['comment'] = 'Moved Job {0} from schedule.'.format(name)
ret['minions'] = minions
return ret
else:
ret['comment'] = 'Job {0} does not exist.'.format(name)
ret['result'] = False
return ret