From cb586184cb25008033c0339aeb98b271f56acd01 Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Tue, 15 Dec 2015 14:29:23 -0600 Subject: [PATCH] pkgng.py: raise exception if changes fail This uses the new 'info' param for the CommandExecutionError exception to allow us to report partial changes when there are errors. --- salt/modules/pkgng.py | 71 ++++++++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/salt/modules/pkgng.py b/salt/modules/pkgng.py index 54e22fe6a1..1851a7f52f 100644 --- a/salt/modules/pkgng.py +++ b/salt/modules/pkgng.py @@ -794,11 +794,29 @@ def install(name=None, # pkg add doesn't have a dryrun mode, so echo out what will be run return ' '.join(cmd) - __salt__['cmd.run'](cmd, output_loglevel='trace', python_shell=False) + out = __salt__['cmd.run_all']( + cmd, + output_loglevel='trace', + python_shell=False + ) + + if out['retcode'] != 0 and out['stderr']: + errors = [out['stderr']] + else: + errors = [] + __context__.pop(_contextkey(jail, chroot), None) __context__.pop(_contextkey(jail, chroot, prefix='pkg.origin'), None) new = list_pkgs(jail=jail, chroot=chroot) - return salt.utils.compare_dicts(old, new) + ret = salt.utils.compare_dicts(old, new) + + if errors: + raise CommandExecutionError( + 'Problem encountered installing package(s)', + info={'errors': errors, 'changes': ret} + ) + + return ret def remove(name=None, @@ -945,11 +963,30 @@ def remove(name=None, if opts: cmd.append('-' + opts) cmd.extend(targets) - __salt__['cmd.run'](cmd, output_loglevel='trace', python_shell=False) + + out = __salt__['cmd.run_all']( + cmd, + output_loglevel='trace', + python_shell=False + ) + + if out['retcode'] != 0 and out['stderr']: + errors = [out['stderr']] + else: + errors = [] + __context__.pop(_contextkey(jail, chroot), None) __context__.pop(_contextkey(jail, chroot, prefix='pkg.origin'), None) new = list_pkgs(jail=jail, chroot=chroot, with_origin=True) - return salt.utils.compare_dicts(old, new) + ret = salt.utils.compare_dicts(old, new) + + if errors: + raise CommandExecutionError( + 'Problem encountered removing package(s)', + info={'errors': errors, 'changes': ret} + ) + + return ret # Support pkg.delete to remove packages, since this is the CLI usage delete = salt.utils.alias_function(remove, 'delete') @@ -1050,21 +1087,21 @@ def upgrade(*names, **kwargs): cmd.extend(names) old = list_pkgs() - call = __salt__['cmd.run_all']( - cmd, - output_loglevel='trace', - python_shell=False - ) + call = __salt__['cmd.run_all'](cmd, + output_loglevel='trace', + python_shell=False, + redirect_stderr=True) + if call['retcode'] != 0: ret['result'] = False - if 'stderr' in call: - ret['comment'] += call['stderr'] - if 'stdout' in call: - ret['comment'] += call['stdout'] - else: - __context__.pop('pkg.list_pkgs', None) - new = list_pkgs() - ret['changes'] = salt.utils.compare_dicts(old, new) + if call['stdout']: + ret['comment'] = call['stdout'] + + __context__.pop(_contextkey(jail, chroot), None) + __context__.pop(_contextkey(jail, chroot, prefix='pkg.origin'), None) + new = list_pkgs() + ret['changes'] = salt.utils.compare_dicts(old, new) + return ret