Skip to content

Commit

Permalink
Merge pull request #34159 from christoe/2016.3
Browse files Browse the repository at this point in the history
Fixes to the win_task module
  • Loading branch information
Mike Place committed Jun 23, 2016
2 parents 0a02671 + f414366 commit 5f42fd4
Showing 1 changed file with 90 additions and 25 deletions.
115 changes: 90 additions & 25 deletions salt/modules/win_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@
'4 hours': 'PT4H',
'8 hours': 'PT8H',
'12 hours': 'PT12H',
'1 day': 'P1D',
'3 days': 'P3D',
'1 day': ['P1D', 'PT24H'],
'3 days': ['P3D', 'PT72H'],
'30 days': 'P30D',
'90 days': 'P90D',
'180 days': 'P180D',
Expand All @@ -117,6 +117,18 @@
'Email': TASK_ACTION_SEND_EMAIL,
'Message': TASK_ACTION_SHOW_MESSAGE}

trigger_types = {'Event': TASK_TRIGGER_EVENT,
'Once': TASK_TRIGGER_TIME,
'Daily': TASK_TRIGGER_DAILY,
'Weekly': TASK_TRIGGER_WEEKLY,
'Monthly': TASK_TRIGGER_MONTHLY,
'MonthlyDay': TASK_TRIGGER_MONTHLYDOW,
'OnIdle': TASK_TRIGGER_IDLE,
'OnTaskCreation': TASK_TRIGGER_REGISTRATION,
'OnBoot': TASK_TRIGGER_BOOT,
'OnLogon': TASK_TRIGGER_LOGON,
'OnSessionChange': TASK_TRIGGER_SESSION_STATE_CHANGE}

states = {TASK_STATE_UNKNOWN: 'Unknown',
TASK_STATE_DISABLED: 'Disabled',
TASK_STATE_QUEUED: 'Queued',
Expand Down Expand Up @@ -217,7 +229,36 @@ def _reverse_lookup(dictionary, value):
:return: Returns the first key to match the value
:rtype: str
'''
return dictionary.keys()[dictionary.values().index(value)]
value_index = -1
for idx, dict_value in enumerate(dictionary.values()):
if type(dict_value) == list:
if value in dict_value:
value_index = idx
break
elif value == dict_value:
value_index = idx
break

return dictionary.keys()[value_index]


def _lookup_first(dictionary, key):
'''
Lookup the first value given a key. Returns the first value if the key
refers to a list or the value itself.
:param dict dictionary: The dictionary to search
:param str key: The key to get
:return: Returns the first value available for the key
:rtype: str
'''
value = dictionary[key]
if type(value) == list:
return value[0]
else:
return value


def _save_task_definition(name,
Expand Down Expand Up @@ -435,7 +476,8 @@ def create_task(name,
# Modify task settings
edit_task(task_definition=task_definition,
user_name=user_name,
password=password)
password=password,
**kwargs)

# Add Action
add_action(task_definition=task_definition, **kwargs)
Expand Down Expand Up @@ -842,12 +884,12 @@ def edit_task(name=None,
task_definition.Settings.IdleSettings.RestartOnIdle = idle_restart
if idle_duration is not None:
if idle_duration in duration:
task_definition.Settings.IdleSettings.IdleDuration = duration[idle_duration]
task_definition.Settings.IdleSettings.IdleDuration = _lookup_first(duration, idle_duration)
else:
return 'Invalid value for "idle_duration"'
if idle_wait_timeout is not None:
if idle_wait_timeout in duration:
task_definition.Settings.IdleSettings.WaitTimeout = duration[idle_wait_timeout]
task_definition.Settings.IdleSettings.WaitTimeout = _lookup_first(duration, idle_wait_timeout)
else:
return 'Invalid value for "idle_wait_timeout"'

Expand Down Expand Up @@ -879,7 +921,7 @@ def edit_task(name=None,
task_definition.Settings.RestartInterval = ''
else:
if restart_every in duration:
task_definition.Settings.RestartInterval = duration[restart_every]
task_definition.Settings.RestartInterval = _lookup_first(duration, restart_every)
else:
return 'Invalid value for "restart_every"'
if task_definition.Settings.RestartInterval:
Expand All @@ -893,7 +935,7 @@ def edit_task(name=None,
task_definition.Settings.ExecutionTimeLimit = 'PT0S'
else:
if execution_time_limit in duration:
task_definition.Settings.ExecutionTimeLimit = duration[execution_time_limit]
task_definition.Settings.ExecutionTimeLimit = _lookup_first(duration, execution_time_limit)
else:
return 'Invalid value for "execution_time_limit"'
if force_stop is not None:
Expand All @@ -903,7 +945,7 @@ def edit_task(name=None,
if delete_after is False:
task_definition.Settings.DeleteExpiredTaskAfter = ''
if delete_after in duration:
task_definition.Settings.DeleteExpiredTaskAfter = duration[delete_after]
task_definition.Settings.DeleteExpiredTaskAfter = _lookup_first(duration, delete_after)
else:
return 'Invalid value for "delete_after"'
if multiple_instances is not None:
Expand Down Expand Up @@ -1221,8 +1263,43 @@ def info(name, location='\\'):
conditions['network_id'] = net_set.Id
conditions['network_name'] = net_set.Name

actions = []
for actionObj in task.Definition.Actions:
action = {}
action['action_type'] = _reverse_lookup(action_types, actionObj.Type)
if actionObj.Path:
action['cmd'] = actionObj.Path
if actionObj.Arguments:
action['arguments'] = actionObj.Arguments
if actionObj.WorkingDirectory:
action['working_dir'] = actionObj.WorkingDirectory
actions.append(action)

triggers = []
for triggerObj in task.Definition.Triggers:
trigger = {}
trigger['trigger_type'] = _reverse_lookup(trigger_types, triggerObj.Type)
if triggerObj.ExecutionTimeLimit:
trigger['execution_time_limit'] = _reverse_lookup(duration, triggerObj.ExecutionTimeLimit)
if triggerObj.StartBoundary:
start_date, start_time = triggerObj.StartBoundary.split('T', 1)
trigger['start_date'] = start_date
trigger['start_time'] = start_time
if triggerObj.EndBoundary:
end_date, end_time = triggerObj.EndBoundary.split('T', 1)
trigger['end_date'] = end_date
trigger['end_time'] = end_time
trigger['enabled'] = triggerObj.Enabled
if triggerObj.RandomDelay == '':
trigger['random_delay'] = False
else:
trigger['random_delay'] = _reverse_lookup(duration, triggerObj.RandomDelay)
triggers.append(trigger)

properties['settings'] = settings
properties['conditions'] = conditions
properties['actions'] = actions
properties['triggers'] = triggers
ret = properties

return ret
Expand Down Expand Up @@ -1651,18 +1728,6 @@ def add_trigger(name=None,
return 'Required parameter "trigger_type" not specified'

# Define lookup dictionaries
trigger_types = {'Event': TASK_TRIGGER_EVENT,
'Once': TASK_TRIGGER_TIME,
'Daily': TASK_TRIGGER_DAILY,
'Weekly': TASK_TRIGGER_WEEKLY,
'Monthly': TASK_TRIGGER_MONTHLY,
'MonthlyDay': TASK_TRIGGER_MONTHLYDOW,
'OnIdle': TASK_TRIGGER_IDLE,
'OnTaskCreation': TASK_TRIGGER_REGISTRATION,
'OnBoot': TASK_TRIGGER_BOOT,
'OnLogon': TASK_TRIGGER_LOGON,
'OnSessionChange': TASK_TRIGGER_SESSION_STATE_CHANGE}

state_changes = {'ConsoleConnect': 1,
'ConsoleDisconnect': 2,
'RemoteConnect': 3,
Expand Down Expand Up @@ -1809,14 +1874,14 @@ def add_trigger(name=None,
trigger.StartBoundary = start_boundary
# Advanced Settings
if random_delay:
trigger.RandomDelay = duration[random_delay]
trigger.RandomDelay = _lookup_first(duration, random_delay)
if repeat_interval:
trigger.Repetition.Interval = duration[repeat_interval]
trigger.Repetition.Interval = _lookup_first(duration, repeat_interval)
if repeat_duration:
trigger.Repetition.Duration = duration[repeat_duration]
trigger.Repetition.Duration = _lookup_first(duration, repeat_duration)
trigger.Repetition.StopAtDurationEnd = repeat_stop_at_duration_end
if execution_time_limit:
trigger.ExecutionTimeLimit = duration[execution_time_limit]
trigger.ExecutionTimeLimit = _lookup_first(duration, execution_time_limit)
if end_boundary:
trigger.EndBoundary = end_boundary
trigger.Enabled = trigger_enabled
Expand Down

0 comments on commit 5f42fd4

Please sign in to comment.