Improve reporting for errors caching binary packages

This commit catches MinionError exceptions encountered when caching
remote binary packages. In execution functions, a CLI-friendly
CommandExecutionError is raised. In states, a False outcome is returned
with a meaningful error message.
This commit is contained in:
Erik Johnson 2013-12-16 20:00:50 -06:00
parent 055beeb4a9
commit 0b30626195
13 changed files with 202 additions and 88 deletions

View File

@ -17,7 +17,9 @@ import yaml
# Import salt libs
import salt.utils
from salt._compat import string_types
from salt.exceptions import CommandExecutionError, SaltInvocationError
from salt.exceptions import (
CommandExecutionError, MinionError, SaltInvocationError
)
log = logging.getLogger(__name__)
@ -373,10 +375,12 @@ def install(name=None,
if debconf:
__salt__['debconf.set_file'](debconf)
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](name,
pkgs,
sources,
**kwargs)
try:
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](
name, pkgs, sources, **kwargs
)
except MinionError as exc:
raise CommandExecutionError(exc)
# Support old "repo" argument
repo = kwargs.get('repo', '')
@ -437,8 +441,11 @@ def _uninstall(action='remove', name=None, pkgs=None, **kwargs):
remove and purge do identical things but with different apt-get commands,
this function performs the common logic.
'''
try:
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
except MinionError as exc:
raise CommandExecutionError(exc)
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
old = list_pkgs()
old_removed = list_pkgs(removed=True)
targets = [x for x in pkg_params if x in old]

View File

@ -9,6 +9,7 @@ import logging
# Import salt libs
import salt.utils
from salt.exceptions import CommandExecutionError, MinionError
log = logging.getLogger(__name__)
@ -176,9 +177,13 @@ def remove(name=None, pkgs=None, **kwargs):
salt '*' pkg.remove <package1>,<package2>,<package3>
salt '*' pkg.remove pkgs='["foo", "bar"]'
'''
pkg_params = __salt__['pkg_resource.parse_targets'](name,
pkgs,
**kwargs)[0]
try:
pkg_params = __salt__['pkg_resource.parse_targets'](
name, pkgs, **kwargs
)[0]
except MinionError as exc:
raise CommandExecutionError(exc)
old = list_pkgs()
targets = [x for x in pkg_params if x in old]
if not targets:
@ -272,10 +277,13 @@ def install(name=None, pkgs=None, taps=None, options=None, **kwargs):
salt '*' pkg.install 'package package package'
'''
pkg_params, pkg_type = \
__salt__['pkg_resource.parse_targets'](name,
pkgs,
kwargs.get('sources', {}))
try:
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](
name, pkgs, kwargs.get('sources', {})
)
except MinionError as exc:
raise CommandExecutionError(exc)
if pkg_params is None or len(pkg_params) == 0:
return {}

View File

@ -15,6 +15,7 @@ import re
# Import salt libs
import salt.utils
from salt.exceptions import CommandExecutionError, MinionError
# Import third party libs
HAS_PORTAGE = False
@ -474,7 +475,6 @@ def install(name=None,
{'<package>': {'old': '<old-version>',
'new': '<new-version>'}}
'''
log.debug('Called modules.pkg.install: {0}'.format(
{
'name': name,
@ -487,10 +487,12 @@ def install(name=None,
if salt.utils.is_true(refresh):
refresh_db()
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](name,
pkgs,
sources,
**kwargs)
try:
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](
name, pkgs, sources, **kwargs
)
except MinionError as exc:
raise CommandExecutionError(exc)
# Handle version kwarg for a single package target
if pkgs is None and sources is None:
@ -676,9 +678,12 @@ def remove(name=None, slot=None, fromrepo=None, pkgs=None, **kwargs):
salt '*' pkg.remove <package1>,<package2>,<package3>
salt '*' pkg.remove pkgs='["foo", "bar"]'
'''
old = list_pkgs()
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
try:
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
except MinionError as exc:
raise CommandExecutionError(exc)
old = list_pkgs()
if name and not pkgs and (slot is not None or fromrepo is not None)and len(pkg_params) == 1:
fullatom = name
if slot is not None:
@ -765,9 +770,12 @@ def depclean(name=None, slot=None, fromrepo=None, pkgs=None):
salt '*' pkg.depclean <package name>
'''
old = list_pkgs()
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
try:
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
except MinionError as exc:
raise CommandExecutionError(exc)
old = list_pkgs()
if name and not pkgs and (slot is not None or fromrepo is not None)and len(pkg_params) == 1:
fullatom = name
if slot is not None:

View File

@ -73,6 +73,7 @@ import logging
# Import salt libs
import salt.utils
from salt.exceptions import CommandExecutionError, MinionError
log = logging.getLogger(__name__)
@ -311,10 +312,12 @@ def install(name=None,
salt '*' pkg.install <package name>
'''
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](name,
pkgs,
sources,
**kwargs)
try:
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](
name, pkgs, sources, **kwargs
)
except MinionError as exc:
raise CommandExecutionError(exc)
if not pkg_params:
return {}
@ -384,7 +387,10 @@ def remove(name=None, pkgs=None, **kwargs):
salt '*' pkg.remove <package1>,<package2>,<package3>
salt '*' pkg.remove pkgs='["foo", "bar"]'
'''
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
try:
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
except MinionError as exc:
raise CommandExecutionError(exc)
old = list_pkgs()
targets, errors = _match([x for x in pkg_params])

View File

@ -10,6 +10,7 @@ import logging
# Import Salt libs
import salt.utils
from salt.exceptions import CommandExecutionError, MinionError
log = logging.getLogger(__name__)
@ -159,10 +160,13 @@ def install(name=None, pkgs=None, sources=None, **kwargs):
salt '*' pkg.install sources='[{"<pkg name>": "salt://pkgs/<pkg filename>"}]'
'''
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](name,
pkgs,
sources,
**kwargs)
try:
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](
name, pkgs, sources, **kwargs
)
except MinionError as exc:
raise CommandExecutionError(exc)
if pkg_params is None or len(pkg_params) == 0:
return {}
@ -202,9 +206,13 @@ def remove(name=None, pkgs=None, **kwargs):
salt '*' pkg.remove <package1>,<package2>,<package3>
salt '*' pkg.remove pkgs='["foo", "bar"]'
'''
try:
pkg_params = [x.split('--')[0] for x in
__salt__['pkg_resource.parse_targets'](name, pkgs)[0]]
except MinionError as exc:
raise CommandExecutionError(exc)
old = list_pkgs()
pkg_params = [x.split('--')[0] for x in
__salt__['pkg_resource.parse_targets'](name, pkgs)[0]]
targets = [x for x in pkg_params if x in old]
if not targets:
return {}

View File

@ -11,6 +11,7 @@ import re
# Import salt libs
import salt.utils
from salt.exceptions import CommandExecutionError, MinionError
log = logging.getLogger(__name__)
@ -272,10 +273,13 @@ def install(name=None,
{'<package>': {'old': '<old-version>',
'new': '<new-version>'}}
'''
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](name,
pkgs,
sources,
**kwargs)
try:
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](
name, pkgs, sources, **kwargs
)
except MinionError as exc:
raise CommandExecutionError(exc)
if pkg_params is None or len(pkg_params) == 0:
return {}
@ -358,7 +362,11 @@ def _uninstall(action='remove', name=None, pkgs=None, **kwargs):
remove and purge do identical things but with different pacman commands,
this function performs the common logic.
'''
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
try:
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
except MinionError as exc:
raise CommandExecutionError(exc)
old = list_pkgs()
targets = [x for x in pkg_params if x in old]
if not targets:

View File

@ -11,6 +11,7 @@ import logging
# Import salt libs
import salt.utils
import salt.utils.decorators as decorators
from salt.exceptions import CommandExecutionError, MinionError
VERSION_MATCH = re.compile(r'pkgin(?:[\s]+)([\d.]+)(?:[\s]+)(?:.*)')
log = logging.getLogger(__name__)
@ -287,10 +288,12 @@ def install(name=None, refresh=False, fromrepo=None,
salt '*' pkg.install <package name>
'''
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](name,
pkgs,
sources,
**kwargs)
try:
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](
name, pkgs, sources, **kwargs
)
except MinionError as exc:
raise CommandExecutionError(exc)
# Support old "repo" argument
repo = kwargs.get('repo', '')
@ -388,7 +391,13 @@ def remove(name=None, pkgs=None, **kwargs):
salt '*' pkg.remove <package1>,<package2>,<package3>
salt '*' pkg.remove pkgs='["foo", "bar"]'
'''
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](name, pkgs)
try:
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](
name, pkgs
)
except MinionError as exc:
raise CommandExecutionError(exc)
if not pkg_params:
return {}

View File

@ -38,6 +38,7 @@ import os
# Import salt libs
import salt.utils
from salt.exceptions import CommandExecutionError, MinionError
log = logging.getLogger(__name__)
@ -648,10 +649,12 @@ def install(name=None,
salt '*' pkg.install <extended regular expression> pcre=True
'''
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](name,
pkgs,
sources,
**kwargs)
try:
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](
name, pkgs, sources, **kwargs
)
except MinionError as exc:
raise CommandExecutionError(exc)
if pkg_params is None or len(pkg_params) == 0:
return {}
@ -814,7 +817,11 @@ def remove(name=None,
salt '*' pkg.remove <extended regular expression> pcre=True
'''
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
try:
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
except MinionError as exc:
raise CommandExecutionError(exc)
old = list_pkgs(jail=jail, chroot=chroot)
targets = [x for x in pkg_params if x in old]
if not targets:

View File

@ -8,6 +8,7 @@ import copy
# Import salt libs
import salt.utils
from salt.exceptions import CommandExecutionError, MinionError
def __virtual__():
@ -260,10 +261,13 @@ def install(name=None, refresh=False, version=None, pkgs=None, **kwargs):
if refresh:
refresh_db()
# Ignore 'sources' argument
pkg_params = __salt__['pkg_resource.parse_targets'](name,
pkgs,
**kwargs)[0]
try:
# Ignore 'sources' argument
pkg_params = __salt__['pkg_resource.parse_targets'](name,
pkgs,
**kwargs)[0]
except MinionError as exc:
raise CommandExecutionError(exc)
if pkg_params is None or len(pkg_params) == 0:
return {}
@ -313,7 +317,11 @@ def remove(name=None, pkgs=None, **kwargs):
salt '*' pkg.remove <package1>,<package2>,<package3>
salt '*' pkg.remove pkgs='["foo", "bar"]'
'''
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
try:
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
except MinionError as exc:
raise CommandExecutionError(exc)
old = list_pkgs()
targets = [x for x in pkg_params if x in old]
if not targets:

View File

@ -10,6 +10,7 @@ import logging
# Import salt libs
import salt.utils
from salt.exceptions import CommandExecutionError, MinionError
log = logging.getLogger(__name__)
@ -280,11 +281,12 @@ def install(name=None, sources=None, saltenv='base', **kwargs):
log.warning('\'refresh\' argument not implemented for solarispkg '
'module')
pkg_params, pkg_type = \
__salt__['pkg_resource.parse_targets'](name,
kwargs.get('pkgs'),
sources,
**kwargs)
try:
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](
name, kwargs.get('pkgs'), sources, **kwargs
)
except MinionError as exc:
raise CommandExecutionError(exc)
if pkg_params is None or len(pkg_params) == 0:
return {}
@ -375,7 +377,11 @@ def remove(name=None, pkgs=None, saltenv='base', **kwargs):
salt '*' pkg.remove <package1>,<package2>,<package3>
salt '*' pkg.remove pkgs='["foo", "bar"]'
'''
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
try:
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
except MinionError as exc:
raise CommandExecutionError(exc)
old = list_pkgs()
targets = [x for x in pkg_params if x in old]
if not targets:

View File

@ -11,7 +11,9 @@ import re
# Import salt libs
import salt.utils
from salt.exceptions import SaltInvocationError
from salt.exceptions import (
CommandExecutionError, MinionError, SaltInvocationError
)
log = logging.getLogger(__name__)
@ -498,10 +500,13 @@ def install(name=None,
if salt.utils.is_true(refresh):
refresh_db()
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](name,
pkgs,
sources,
**kwargs)
try:
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](
name, pkgs, sources, **kwargs
)
except MinionError as exc:
raise CommandExecutionError(exc)
if pkg_params is None or len(pkg_params) == 0:
return {}
@ -618,7 +623,11 @@ def remove(name=None, pkgs=None, **kwargs):
salt '*' pkg.remove <package1>,<package2>,<package3>
salt '*' pkg.remove pkgs='["foo", "bar"]'
'''
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
try:
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
except MinionError as exc:
raise CommandExecutionError(exc)
old = list_pkgs()
targets = [x for x in pkg_params if x in old]
if not targets:

View File

@ -10,6 +10,7 @@ import re
# Import salt libs
import salt.utils
from salt.exceptions import CommandExecutionError, MinionError
log = logging.getLogger(__name__)
@ -300,10 +301,13 @@ def install(name=None,
if salt.utils.is_true(refresh):
refresh_db()
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](name,
pkgs,
sources,
**kwargs)
try:
pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](
name, pkgs, sources, **kwargs
)
except MinionError as exc:
raise CommandExecutionError(exc)
if pkg_params is None or len(pkg_params) == 0:
return {}
@ -413,8 +417,12 @@ def _uninstall(action='remove', name=None, pkgs=None):
remove and purge do identical things but with different zypper commands,
this function performs the common logic.
'''
try:
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
except MinionError as exc:
raise CommandExecutionError(exc)
purge_arg = '-u' if action == 'purge' else ''
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
old = list_pkgs()
targets = [x for x in pkg_params if x in old]
if not targets:

View File

@ -40,7 +40,7 @@ import re
# Import salt libs
import salt.utils
from salt.exceptions import CommandExecutionError
from salt.exceptions import CommandExecutionError, MinionError
from salt.modules.pkg_resource import _repack_pkgs
if salt.utils.is_windows():
@ -455,14 +455,22 @@ def installed(
'comment': comment}
comment = []
pkg_ret = __salt__['pkg.install'](name,
refresh=refresh,
version=version,
fromrepo=fromrepo,
skip_verify=skip_verify,
pkgs=pkgs,
sources=sources,
**kwargs)
try:
pkg_ret = __salt__['pkg.install'](name,
refresh=refresh,
version=version,
fromrepo=fromrepo,
skip_verify=skip_verify,
pkgs=pkgs,
sources=sources,
**kwargs)
except CommandExecutionError as exc:
return {'name': name,
'changes': {},
'result': False,
'comment': 'An error was encountered while installing '
'package(s): {0}'.format(exc)}
if isinstance(pkg_ret, dict):
changes = pkg_ret
elif isinstance(pkg_ret, basestring):
@ -676,14 +684,21 @@ def latest(
# Build updated list of pkgs to exclude non-targeted ones
targeted_pkgs = targets.keys() if pkgs else None
# No need to refresh, if a refresh was necessary it would have been
# performed above when pkg.latest_version was run.
changes = __salt__['pkg.install'](name,
refresh=False,
fromrepo=fromrepo,
skip_verify=skip_verify,
pkgs=targeted_pkgs,
**kwargs)
try:
# No need to refresh, if a refresh was necessary it would have been
# performed above when pkg.latest_version was run.
changes = __salt__['pkg.install'](name,
refresh=false,
fromrepo=fromrepo,
skip_verify=skip_verify,
pkgs=targeted_pkgs,
**kwargs)
except CommandExecutionError as exc:
return {'name': name,
'changes': {},
'result': False,
'comment': 'An error was encountered while installing '
'package(s): {0}'.format(exc)}
if changes:
# Find failed and successful updates
@ -765,7 +780,14 @@ def _uninstall(action='remove', name=None, pkgs=None, **kwargs):
'comment': 'Invalid action {0!r}. '
'This is probably a bug.'.format(action)}
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
try:
pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]
except MinionError as exc:
return {'name': name,
'changes': {},
'result': False,
'comment': 'An error was encountered while parsing targets: '
'{0}'.format(exc)}
old = __salt__['pkg.list_pkgs'](versions_as_list=True, **kwargs)
targets = [x for x in pkg_params if x in old]
if action == 'purge':