Merge pull request #36508 from twangboy/fix_chocolatey

Fix chocolatey
This commit is contained in:
Mike Place 2016-09-23 16:36:03 +09:00 committed by GitHub
commit 8104d5c92a

View File

@ -36,9 +36,13 @@ def __virtual__():
salt-minion running as SYSTEM.
'''
if not salt.utils.is_windows():
return (False, 'Cannot load module chocolatey: Chocolatey requires Windows')
elif __grains__['osrelease'] in ('XP', '2003Server'):
return (False, 'Cannot load module chocolatey: Chocolatey requires Windows Vista or later')
return (False, 'Cannot load module chocolatey: Chocolatey requires '
'Windows')
if __grains__['osrelease'] in ('XP', '2003Server'):
return (False, 'Cannot load module chocolatey: Chocolatey requires '
'Windows Vista or later')
return 'chocolatey'
@ -72,7 +76,7 @@ def _find_chocolatey(context, salt):
if 'chocolatey._path' in context:
return context['chocolatey._path']
choc_defaults = ['C:\\Chocolatey\\bin\\chocolatey.bat',
'C:\\ProgramData\\Chocolatey\\bin\\chocolatey.exe', ]
'C:\\ProgramData\\Chocolatey\\bin\\chocolatey.exe', ]
choc_path = salt['cmd.which']('chocolatey.exe')
if not choc_path:
@ -244,11 +248,11 @@ def list_(narrow=None,
if narrow:
cmd.append(narrow)
if salt.utils.is_true(all_versions):
cmd.append('-AllVersions')
cmd.append('-allversions')
if salt.utils.is_true(pre_versions):
cmd.append('-Prerelease')
cmd.append('-prerelease')
if source:
cmd.extend(['-Source', source])
cmd.extend(['-source', source])
if local_only:
cmd.extend(['-localonly'])
@ -277,6 +281,9 @@ def list_webpi():
Instructs Chocolatey to pull a full package list from the Microsoft Web PI
repository.
Returns:
str: List of webpi packages
CLI Example:
.. code-block:: bash
@ -284,7 +291,7 @@ def list_webpi():
salt '*' chocolatey.list_webpi
'''
choc_path = _find_chocolatey(__context__, __salt__)
cmd = [choc_path, 'list', '-Source', 'webpi']
cmd = [choc_path, 'list', '-source', 'webpi']
result = __salt__['cmd.run_all'](cmd, python_shell=False)
if result['retcode'] != 0:
@ -300,6 +307,9 @@ def list_windowsfeatures():
Instructs Chocolatey to pull a full package list from the Windows Features
list, via the Deployment Image Servicing and Management tool.
Returns:
str: List of Windows Features
CLI Example:
.. code-block:: bash
@ -307,7 +317,7 @@ def list_windowsfeatures():
salt '*' chocolatey.list_windowsfeatures
'''
choc_path = _find_chocolatey(__context__, __salt__)
cmd = [choc_path, 'list', '-Source', 'windowsfeatures']
cmd = [choc_path, 'list', '-source', 'windowsfeatures']
result = __salt__['cmd.run_all'](cmd, python_shell=False)
if result['retcode'] != 0:
@ -330,36 +340,52 @@ def install(name,
'''
Instructs Chocolatey to install a package.
name
The name of the package to be installed. Only accepts a single argument.
Args:
version
Install a specific version of the package. Defaults to latest version.
name (str):
The name of the package to be installed. Only accepts a single
argument.
source
Chocolatey repository (directory, share or remote URL feed) the package
comes from. Defaults to the official Chocolatey feed.
version (str):
Install a specific version of the package. Defaults to latest
version.
force
Reinstall the current version of an existing package.
source (str):
Chocolatey repository (directory, share or remote URL feed) the
package comes from. Defaults to the official Chocolatey feed.
pre_versions
Include pre-release packages. Defaults to False.
Alternative Sources:
install_args
A list of install arguments you want to pass to the installation process
i.e product key or feature list
- cygwin
- python
- ruby
- webpi
- windowsfeatures
override_args
Set to true if you want to override the original install arguments (for the native installer)
in the package and use your own. When this is set to False install_args will be appended to the end of the
default arguments
force (bool):
Reinstall the current version of an existing package.
force_x86
Force x86 (32bit) installation on 64 bit systems. Defaults to false.
pre_versions (bool):
Include pre-release packages. Defaults to False.
package_args
A list of arguments you want to pass to the package
install_args (str):
A list of install arguments you want to pass to the installation
process i.e product key or feature list
override_args (bool):
Set to true if you want to override the original install arguments
(for the native installer) in the package and use your own. When
this is set to False install_args will be appended to the end of the
default arguments
force_x86 (str):
Force x86 (32bit) installation on 64 bit systems. Defaults to false.
package_args (str):
A list of arguments you want to pass to the package
Returns:
str: The output of the ``chocolatey`` command
CLI Example:
@ -373,29 +399,30 @@ def install(name,
# chocolatey helpfully only supports a single package argument
cmd = [choc_path, 'install', name]
if version:
cmd.extend(['-Version', version])
cmd.extend(['-version', version])
if source:
cmd.extend(['-Source', source])
cmd.extend(['-source', source])
if salt.utils.is_true(force):
cmd.extend(['-Force'])
cmd.append('-force')
if salt.utils.is_true(pre_versions):
cmd.extend(['-PreRelease'])
cmd.append('-prerelease')
if install_args:
cmd.extend(['-InstallArguments', install_args])
cmd.extend(['-installarguments', install_args])
if override_args:
cmd.extend(['-OverrideArguments'])
cmd.append('-overridearguments')
if force_x86:
cmd.extend(['-forcex86'])
cmd.append('-forcex86')
if package_args:
cmd.extend(['-PackageParameters', package_args])
cmd.extend(['-packageparameters', package_args])
cmd.extend(_yes(__context__))
result = __salt__['cmd.run_all'](cmd, python_shell=False)
if result['retcode'] != 0:
if result['retcode'] not in [0, 1641, 3010]:
err = 'Running chocolatey failed: {0}'.format(result['stderr'])
log.error(err)
raise CommandExecutionError(err)
elif name == 'chocolatey':
if name == 'chocolatey':
_clear_context(__context__)
return result['stdout']
@ -424,21 +451,10 @@ def install_cygwin(name, install_args=None, override_args=False):
salt '*' chocolatey.install_cygwin <package name>
salt '*' chocolatey.install_cygwin <package name> install_args=<args> override_args=True
'''
choc_path = _find_chocolatey(__context__, __salt__)
cmd = [choc_path, 'cygwin', name]
if install_args:
cmd.extend(['-InstallArguments', install_args])
if override_args:
cmd.extend(['-OverrideArguments'])
cmd.extend(_yes(__context__))
result = __salt__['cmd.run_all'](cmd, python_shell=False)
if result['retcode'] != 0:
err = 'Running chocolatey failed: {0}'.format(result['stderr'])
log.error(err)
raise CommandExecutionError(err)
return result['stdout']
return install(name,
source='cygwin',
install_args=install_args,
override_args=override_args)
def install_gem(name, version=None, install_args=None, override_args=False):
@ -470,23 +486,11 @@ def install_gem(name, version=None, install_args=None, override_args=False):
salt '*' chocolatey.install_gem <package name> version=<package version>
salt '*' chocolatey.install_gem <package name> install_args=<args> override_args=True
'''
choc_path = _find_chocolatey(__context__, __salt__)
cmd = [choc_path, 'gem', name]
if version:
cmd.extend(['-Version', version])
if install_args:
cmd.extend(['-InstallArguments', install_args])
if override_args:
cmd.extend(['-OverrideArguments'])
cmd.extend(_yes(__context__))
result = __salt__['cmd.run_all'](cmd, python_shell=False)
if result['retcode'] != 0:
err = 'Running chocolatey failed: {0}'.format(result['stderr'])
log.error(err)
raise CommandExecutionError(err)
return result['stdout']
return install(name,
version=version,
source='ruby',
install_args=install_args,
override_args=override_args)
def install_missing(name, version=None, source=None):
@ -525,9 +529,9 @@ def install_missing(name, version=None, source=None):
# chocolatey helpfully only supports a single package argument
cmd = [choc_path, 'installmissing', name]
if version:
cmd.extend(['-Version', version])
cmd.extend(['-version', version])
if source:
cmd.extend(['-Source', source])
cmd.extend(['-source', source])
# Shouldn't need this as this code should never run on v0.9.9 and newer
cmd.extend(_yes(__context__))
result = __salt__['cmd.run_all'](cmd, python_shell=False)
@ -568,23 +572,11 @@ def install_python(name, version=None, install_args=None, override_args=False):
salt '*' chocolatey.install_python <package name> version=<package version>
salt '*' chocolatey.install_python <package name> install_args=<args> override_args=True
'''
choc_path = _find_chocolatey(__context__, __salt__)
cmd = [choc_path, 'python', name]
if version:
cmd.extend(['-Version', version])
if install_args:
cmd.extend(['-InstallArguments', install_args])
if override_args:
cmd.extend(['-OverrideArguments'])
cmd.extend(_yes(__context__))
result = __salt__['cmd.run_all'](cmd, python_shell=False)
if result['retcode'] != 0:
err = 'Running chocolatey failed: {0}'.format(result['stderr'])
log.error(err)
raise CommandExecutionError(err)
return result['stdout']
return install(name,
version=version,
source='python',
install_args=install_args,
override_args=override_args)
def install_windowsfeatures(name):
@ -601,17 +593,7 @@ def install_windowsfeatures(name):
salt '*' chocolatey.install_windowsfeatures <package name>
'''
choc_path = _find_chocolatey(__context__, __salt__)
cmd = [choc_path, 'windowsfeatures', name]
cmd.extend(_yes(__context__))
result = __salt__['cmd.run_all'](cmd, python_shell=False)
if result['retcode'] != 0:
err = 'Running chocolatey failed: {0}'.format(result['stderr'])
log.error(err)
raise CommandExecutionError(err)
return result['stdout']
return install(name, source='windowsfeatures')
def install_webpi(name, install_args=None, override_args=False):
@ -637,21 +619,10 @@ def install_webpi(name, install_args=None, override_args=False):
salt '*' chocolatey.install_webpi <package name>
salt '*' chocolatey.install_webpi <package name> install_args=<args> override_args=True
'''
choc_path = _find_chocolatey(__context__, __salt__)
cmd = [choc_path, 'webpi', name]
if install_args:
cmd.extend(['-InstallArguments', install_args])
if override_args:
cmd.extend(['-OverrideArguments'])
cmd.extend(_yes(__context__))
result = __salt__['cmd.run_all'](cmd, python_shell=False)
if result['retcode'] != 0:
err = 'Running chocolatey failed: {0}'.format(result['stderr'])
log.error(err)
raise CommandExecutionError(err)
return result['stdout']
return install(name,
source='webpi',
install_args=install_args,
override_args=override_args)
def uninstall(name, version=None, uninstall_args=None, override_args=False):
@ -686,15 +657,107 @@ def uninstall(name, version=None, uninstall_args=None, override_args=False):
# chocolatey helpfully only supports a single package argument
cmd = [choc_path, 'uninstall', name]
if version:
cmd.extend(['-Version', version])
cmd.extend(['-version', version])
if uninstall_args:
cmd.extend(['-UninstallArguments', uninstall_args])
cmd.extend(['-uninstallarguments', uninstall_args])
if override_args:
cmd.extend(['-OverrideArguments'])
cmd.extend(['-overridearguments'])
cmd.extend(_yes(__context__))
result = __salt__['cmd.run_all'](cmd, python_shell=False)
if result['retcode'] != 0:
if result['retcode'] not in [0, 1605, 1614, 1641]:
err = 'Running chocolatey failed: {0}'.format(result['stderr'])
log.error(err)
raise CommandExecutionError(err)
return result['stdout']
def upgrade(name,
version=None,
source=None,
force=False,
pre_versions=False,
install_args=None,
override_args=False,
force_x86=False,
package_args=None):
'''
.. version-added:: 2016.3.4
Instructs Chocolatey to upgrade packages on the system. (update is being
deprecated)
Args:
name (str):
The name of the package to update, or "all" to update everything
installed on the system.
version (str):
Install a specific version of the package. Defaults to latest
version.
source (str):
Chocolatey repository (directory, share or remote URL feed) the
package comes from. Defaults to the official Chocolatey feed.
force (bool):
Reinstall the **same** version already installed
pre_versions (bool):
Include pre-release packages in comparison. Defaults to False.
install_args (str):
A list of install arguments you want to pass to the installation
process i.e product key or feature list
override_args (str):
Set to true if you want to override the original install arguments
(for the native installer) in the package and use your own. When
this is set to False install_args will be appended to the end of the
default arguments
force_x86
Force x86 (32bit) installation on 64 bit systems. Defaults to false.
package_args
A list of arguments you want to pass to the package
Returns:
str: Results of the ``chocolatey`` command
CLI Example:
.. code-block:: bash
salt "*" chocolatey.upgrade all
salt "*" chocolatey.upgrade <package name> pre_versions=True
'''
# chocolatey helpfully only supports a single package argument
choc_path = _find_chocolatey(__context__, __salt__)
cmd = [choc_path, 'upgrade', name]
if version:
cmd.extend(['-version', version])
if source:
cmd.extend(['-source', source])
if salt.utils.is_true(force):
cmd.append('-force')
if salt.utils.is_true(pre_versions):
cmd.append('-prerelease')
if install_args:
cmd.extend(['-installarguments', install_args])
if override_args:
cmd.append('-overridearguments')
if force_x86:
cmd.append('-forcex86')
if package_args:
cmd.extend(['-packageparameters', package_args])
cmd.extend(_yes(__context__))
result = __salt__['cmd.run_all'](cmd, python_shell=False)
if result['retcode'] not in [0, 1641, 3010]:
err = 'Running chocolatey failed: {0}'.format(result['stderr'])
log.error(err)
raise CommandExecutionError(err)
@ -726,15 +789,19 @@ def update(name, source=None, pre_versions=False):
'''
# chocolatey helpfully only supports a single package argument
choc_path = _find_chocolatey(__context__, __salt__)
if _LooseVersion(chocolatey_version()) >= _LooseVersion('0.9.8.24'):
log.warning('update is deprecated, using upgrade')
return upgrade(name, source=source, pre_versions=pre_versions)
cmd = [choc_path, 'update', name]
if source:
cmd.extend(['-Source', source])
cmd.extend(['-source', source])
if salt.utils.is_true(pre_versions):
cmd.append('-PreRelease')
cmd.append('-prerelease')
cmd.extend(_yes(__context__))
result = __salt__['cmd.run_all'](cmd, python_shell=False)
if result['retcode'] != 0:
if result['retcode'] not in [0, 1641, 3010]:
err = 'Running chocolatey failed: {0}'.format(result['stderr'])
log.error(err)
raise CommandExecutionError(err)
@ -776,11 +843,11 @@ def version(name, check_remote=False, source=None, pre_versions=False):
cmd = [choc_path, 'list', name]
if not salt.utils.is_true(check_remote):
cmd.append('-LocalOnly')
cmd.append('-localonly')
if salt.utils.is_true(pre_versions):
cmd.append('-Prerelease')
cmd.append('-prerelease')
if source:
cmd.extend(['-Source', source])
cmd.extend(['-source', source])
result = __salt__['cmd.run_all'](cmd, python_shell=False)
@ -797,7 +864,8 @@ def version(name, check_remote=False, source=None, pre_versions=False):
for line in res:
if 'packages found' not in line and 'packages installed' not in line:
for name, ver in ver_re.findall(line):
ret[name] = ver
if name not in ['Did', 'Features?', 'Chocolatey']:
ret[name] = ver
return ret
@ -827,7 +895,7 @@ def add_source(name, source_location, username=None, password=None):
'''
choc_path = _find_chocolatey(__context__, __salt__)
cmd = [choc_path, 'sources', 'Add', '-Name', name, "-Source", source_location]
cmd = [choc_path, 'sources', 'add', '-name', name, "-source", source_location]
if username:
cmd.extend(['-u', username])
if password:
@ -854,7 +922,7 @@ def _change_source_state(name, state):
'''
choc_path = _find_chocolatey(__context__, __salt__)
cmd = [choc_path, 'source', state, "-Name", name]
cmd = [choc_path, 'source', state, "-name", name]
result = __salt__['cmd.run_all'](cmd, python_shell=False)
if result['retcode'] != 0: