add _zypper_check_result() to raise and error or return stdout

This commit is contained in:
Michael Calmer 2016-02-25 12:26:15 +01:00
parent a6785ef7a9
commit 7c5d5a2b7a

View File

@ -26,6 +26,7 @@ except ImportError:
# pylint: enable=import-error,redefined-builtin,no-name-in-module # pylint: enable=import-error,redefined-builtin,no-name-in-module
from xml.dom import minidom as dom from xml.dom import minidom as dom
from xml.parsers.expat import ExpatError
# Import salt libs # Import salt libs
import salt.utils import salt.utils
@ -79,6 +80,44 @@ def _is_zypper_error(retcode):
return not int(retcode) in [0, 100, 101, 102, 103] return not int(retcode) in [0, 100, 101, 102, 103]
def _zypper_check_result(result, xml=False):
'''
Check the result of a zypper command. In case of an error, it raise
a CommandExecutionError. Otherwise it returns stdout string of the
command.
result
The result of a zypper command called with cmd.run_all
xml
Set to True if zypper command was called with --xmlout.
In this case it try to read an error message out of the XML
stream. Default is False.
'''
if _is_zypper_error(result['retcode']):
msg = list()
if not xml:
msg.append(result['stderr'] and result['stderr'] or "")
else:
try:
doc = dom.parseString(result['stdout'])
except ExpatError as err:
log.error(err)
doc = None
if doc:
msg_nodes = doc.getElementsByTagName('message')
for node in msg_nodes:
if node.getAttribute('type') == 'error':
msg.append(node.childNodes[0].nodeValue)
elif result['stderr'].strip():
msg.append(result['stderr'].strip())
raise CommandExecutionError("zypper command failed: {0}".format(
msg and os.linesep.join(msg) or "Check zypper logs"))
return result['stdout']
def list_upgrades(refresh=True): def list_upgrades(refresh=True):
''' '''
List all available package upgrades on this system List all available package upgrades on this system