win_pkg.py
- Added the option to use the windows task scheduler (`use_scheduler`)
- Applied to both pkg.install and pkg.remove

win_task.py
- Added new parameter `force` to task.create
- Forces the creation of the update of the task if it already exists
- Fixed problem with `task.run_wait` sometimes erroring when the task finished
- Seperated out the cmd and its arguments, they must be explicitely passed,
`task.add_action` will not try to parse the cmd parameter for arguments

win_service.py
- Added new parameters for win_task.py
- Explicitely send command and arguments
- Add `force` parameter to create the task everytime
This commit is contained in:
twangboy 2015-10-15 12:37:07 -06:00
parent 7169fad02d
commit 5254ba18b3
3 changed files with 123 additions and 57 deletions

View file

@ -635,37 +635,61 @@ def install(name=None, refresh=False, pkgs=None, saltenv='base', **kwargs):
cached_pkg = cached_pkg.replace('/', '\\')
cache_path, _ = os.path.split(cached_pkg)
# Get settings for msiexec and allusers
msiexec = pkginfo[version_num].get('msiexec')
all_users = pkginfo[version_num].get('allusers')
# all_users defaults to True
if all_users is None:
all_users = True
# Get install flags
install_flags = '{0}'.format(pkginfo[version_num].get('install_flags'))
if options and options.get('extra_install_flags'):
install_flags = '{0} {1}'.format(install_flags,
options.get('extra_install_flags', ''))
# Build the install command
cmd = []
if msiexec:
cmd.extend(['msiexec', '/i'])
cmd.append(cached_pkg)
cmd.extend(shlex.split(install_flags))
if msiexec and all_users:
cmd.append('ALLUSERS="1"')
# Install the software
result = __salt__['cmd.run_stdout'](cmd, cache_path, output_loglevel='trace', python_shell=False)
if result:
log.error('Failed to install {0}'.format(pkg_name))
log.error('error message: {0}'.format(result))
ret[pkg_name] = {'failed': result}
# Check Use Scheduler Option
if pkginfo[version_num].get('use_scheduler', False):
# Build Scheduled Task Parameters
if pkginfo[version_num].get('msiexec'):
cmd = 'msiexec.exe'
arguments = ['/i', cached_pkg]
if pkginfo['version_num'].get('allusers', True):
arguments.append('ALLUSERS="1"')
arguments.extend(shlex.split(install_flags))
else:
cmd = cached_pkg
arguments = shlex.split(install_flags)
# Create Scheduled Task
__salt__['task.create_task'](name='update-salt-software',
user_name='System',
force=True,
action_type='Execute',
cmd=cmd,
arguments=' '.join(arguments),
start_in=cache_path,
trigger_type='Once',
start_date='1975-01-01',
start_time='01:00')
# Run Scheduled Task
__salt__['task.run_wait'](name='update-salt-software')
else:
changed.append(pkg_name)
# Build the install command
cmd = []
if pkginfo[version_num].get('msiexec'):
cmd.extend(['msiexec', '/i', cached_pkg])
if pkginfo[version_num].get('allusers', True):
cmd.append('ALLUSERS="1"')
else:
cmd.append(cached_pkg)
cmd.extend(shlex.split(install_flags))
# Launch the command
result = __salt__['cmd.run_stdout'](cmd,
cache_path,
output_loglevel='trace',
python_shell=False)
if result:
log.error('Failed to install {0}'.format(pkg_name))
log.error('error message: {0}'.format(result))
ret[pkg_name] = {'failed': result}
else:
changed.append(pkg_name)
# Get a new list of installed software
new = list_pkgs()
@ -829,33 +853,61 @@ def remove(name=None, pkgs=None, version=None, **kwargs):
# Fix non-windows slashes
cached_pkg = cached_pkg.replace('/', '\\')
cache_path, _ = os.path.split(cached_pkg)
# Get parameters for cmd
expanded_cached_pkg = str(os.path.expandvars(cached_pkg))
uninstall_flags = ''
if pkginfo[version_num].get('uninstall_flags'):
uninstall_flags = '{0}'.format(pkginfo[version_num].get('uninstall_flags'))
# Get uninstall flags
uninstall_flags = '{0}'.format(pkginfo[version_num].get('uninstall_flags', ''))
if kwargs.get('extra_uninstall_flags'):
uninstall_flags = '{0} {1}'.format(uninstall_flags,
kwargs.get('extra_uninstall_flags', ""))
# Build the install command
cmd = []
if pkginfo[version_num].get('msiexec'):
cmd.extend(['msiexec', '/x'])
cmd.append(expanded_cached_pkg)
cmd.extend(shlex.split(uninstall_flags))
# Uninstall the software
result = __salt__['cmd.run_stdout'](cmd, output_loglevel='trace', python_shell=False)
if result:
log.error('Failed to install {0}'.format(target))
log.error('error message: {0}'.format(result))
ret[target] = {'failed': result}
# Check Use Scheduler Option
if pkginfo[version_num].get('use_scheduler', False):
# Build Scheduled Task Parameters
if pkginfo[version_num].get('msiexec'):
cmd = 'msiexec.exe'
arguments = ['/x']
arguments.extend(shlex.split(uninstall_flags))
else:
cmd = expanded_cached_pkg
arguments = shlex.split(uninstall_flags)
# Create Scheduled Task
__salt__['task.create_task'](name='update-salt-software',
user_name='System',
force=True,
action_type='Execute',
cmd=cmd,
arguments=' '.join(arguments),
start_in=cache_path,
trigger_type='Once',
start_date='1975-01-01',
start_time='01:00')
# Run Scheduled Task
__salt__['task.run_wait'](name='update-salt-software')
else:
changed.append(target)
# Build the install command
cmd = []
if pkginfo[version_num].get('msiexec'):
cmd.extend(['msiexec', '/x', expanded_cached_pkg])
else:
cmd.append(expanded_cached_pkg)
cmd.extend(shlex.split(uninstall_flags))
# Launch the command
result = __salt__['cmd.run_stdout'](cmd,
output_loglevel='trace',
python_shell=False)
if result:
log.error('Failed to install {0}'.format(target))
log.error('error message: {0}'.format(result))
ret[target] = {'failed': result}
else:
changed.append(target)
# Get a new list of installed software
new = list_pkgs()

View file

@ -228,11 +228,14 @@ def create_win_salt_restart_task():
salt '*' service.create_win_salt_restart_task()
'''
cmd = 'cmd /c ping -n 3 127.0.0.1 && net stop salt-minion && net start salt-minion'
cmd = 'cmd'
args = '/c ping -n 3 127.0.0.1 && net stop salt-minion && net start salt-minion'
return __salt__['task.create_task'](name='restart-salt-minion',
user_name='System',
force=True,
action_type='Execute',
cmd=cmd,
arguments=args,
trigger_type='Once',
start_date='1975-01-01',
start_time='01:00')

View file

@ -384,6 +384,7 @@ def create_task(name,
location='\\',
user_name='System',
password=None,
force=False,
**kwargs):
r'''
Create a new task in the designated location. This function has many keyword
@ -407,11 +408,13 @@ def create_task(name,
the task to run whether the user is logged in or not, but is currently not
working.
:param bool force: If the task exists, overwrite the existing task.
:return: True if successful, False if unsuccessful
:rtype: bool
'''
# Check for existing task
if name in list_tasks(location):
if name in list_tasks(location) and not force:
# Connect to an existing task definition
return '{0} already exists'.format(name)
@ -1052,12 +1055,13 @@ def run_wait(name, location='\\'):
while running:
running = False
running_tasks = task_service.GetRunningTasks(0)
if running_tasks.Count:
for item in running_tasks:
if item.Name == name:
running = True
else:
try:
running_tasks = task_service.GetRunningTasks(0)
if running_tasks.Count:
for item in running_tasks:
if item.Name == name:
running = True
except pythoncom.com_error:
running = False
return True
@ -1245,13 +1249,21 @@ def add_action(name=None,
*Execute*
Execute a command or an executable.
:param str cmd: (required) The command/executable to run along with any
required arguments. For launching a script the first command will need to be
the interpreter for the script. For example, to run a vbscript you would
first call `cscript.exe` and pass the script as an argument as follows:
- ``cscript.exe c:\scripts\myscript.vbs``
:param str cmd: (required) The command / executable to run.
:param str start_in: The current working directory for the command.
:param str arguments: (optional) Arguments to be passed to the command /
executable. To launch a script the first command will need to be the
interpreter for the script. For example, to run a vbscript you would
pass `cscript.exe` in the `cmd` parameter and pass the script in the
`arguments` parameter as follows:
- ``cmd='cscript.exe' arguments='c:\scripts\myscript.vbs'``
Batch files do not need an interpreter and may be passed to the cmd
parameter directly.
:param str start_in: (optional) The current working directory for the
command.
*Email*
Send and email. Requires ``server``, ``from``, and ``to`` or ``cc``.
@ -1310,11 +1322,10 @@ def add_action(name=None,
if action_types[action_type] == TASK_ACTION_EXEC:
task_action.Id = 'Execute_ID1'
if kwargs.get('cmd', False):
cmd = kwargs.get('cmd').split()
task_action.Path = cmd[0]
task_action.Arguments = u' '.join(cmd[1:])
task_action.Path = kwargs.get('cmd')
else:
return 'Required parameter "cmd" not found'
task_action.Arguments = kwargs.get('arguments', '')
task_action.WorkingDirectory = kwargs.get('start_in', '')
elif action_types[action_type] == TASK_ACTION_SEND_EMAIL: