Standarize zypper call to "run_all"

This commit is contained in:
Bo Maryniuk 2016-04-26 11:30:10 +02:00
parent 046ef44ca3
commit 2131ff04af

View file

@ -58,7 +58,16 @@ def __virtual__():
return __virtualname__
def _zypper(function, *args, **kwargs):
def _is_zypper_error(retcode):
'''
Return True in case the exist code indicate a zypper errror.
Otherwise False
'''
# see man zypper for existing exit codes
return int(retcode) not in [0, 100, 101, 102, 103]
def _zypper(*args, **kwargs):
'''
__salt__['cmd.run_all'] with the different environment
@ -70,16 +79,7 @@ def _zypper(function, *args, **kwargs):
kwargs['output_loglevel'] = 'trace'
kwargs['env'] = {'SALT_RUNNING': "1"}
return __salt__[function](cmd, **kwargs)
def _is_zypper_error(retcode):
'''
Return True in case the exist code indicate a zypper errror.
Otherwise False
'''
# see man zypper for existing exit codes
return int(retcode) not in [0, 100, 101, 102, 103]
return __salt__['cmd.run_all'](cmd, **kwargs)
def _zypper_check_result(result, xml=False):
@ -138,8 +138,7 @@ def list_upgrades(refresh=True):
if refresh:
refresh_db()
ret = dict()
run_data = _zypper('cmd.run_all', '-x', 'list-updates')
doc = dom.parseString(_zypper_check_result(run_data, xml=True))
doc = dom.parseString(_zypper_check_result(_zypper('-x', 'list-updates'), xml=True))
for update_node in doc.getElementsByTagName('update'):
if update_node.getAttribute('kind') == 'package':
ret[update_node.getAttribute('name')] = update_node.getAttribute('edition')
@ -238,7 +237,7 @@ def info_available(*names, **kwargs):
# Run in batches
while batch:
pkg_info.extend(re.split(r"Information for package*",
_zypper('cmd.run_stdout', 'info', '-t', 'package', *batch[:batch_size])))
_zypper_check_result(_zypper('info', '-t', 'package', *batch[:batch_size]))))
batch = batch[batch_size:]
for pkg_data in pkg_info:
@ -495,8 +494,8 @@ def del_repo(repo):
repos_cfg = _get_configured_repos()
for alias in repos_cfg.sections():
if alias == repo:
ret = _zypper('cmd.run_all', '-x', 'rr', '--loose-auth', '--loose-query', alias)
doc = dom.parseString(_zypper_check_result(ret, xml=True))
doc = dom.parseString(_zypper_check_result(_zypper('-x', 'rr',
'--loose-auth', '--loose-query', alias), xml=True))
msg = doc.getElementsByTagName('message')
if doc.getElementsByTagName('progress') and msg:
return {
@ -586,7 +585,7 @@ def mod_repo(repo, **kwargs):
)
# Add new repo
_zypper_check_result(_zypper('cmd.run_all', '-x', 'ar', url, repo,
_zypper_check_result(_zypper('-x', 'ar', url, repo,
python_shell=False, output_loglevel='trace'),
xml=True)
@ -624,7 +623,7 @@ def mod_repo(repo, **kwargs):
if cmd_opt:
cmd_opt.append(repo)
_zypper_check_result(_zypper('cmd.run_all', '-x', 'mr', *cmd_opt, python_shell=False), xml=True)
_zypper_check_result(_zypper('-x', 'mr', *cmd_opt, python_shell=False), xml=True)
# If repo nor added neither modified, error should be thrown
if not added and not cmd_opt:
@ -648,7 +647,7 @@ def refresh_db():
salt '*' pkg.refresh_db
'''
ret = {}
out = _zypper_check_result(_zypper('cmd.run_all', 'refresh', '--force'))
out = _zypper_check_result(_zypper('refresh', '--force'))
for line in out.splitlines():
if not line:
@ -798,7 +797,7 @@ def install(name=None,
while targets:
cmd = cmd_install + targets[:500]
targets = targets[500:]
for line in _zypper_check_result(_zypper('cmd.run_all', *cmd, python_shell=False)).splitlines():
for line in _zypper_check_result(_zypper(*cmd, python_shell=False)).splitlines():
match = re.match(r"^The selected package '([^']+)'.+has lower version", line)
if match:
downgrades.append(match.group(1))
@ -806,7 +805,7 @@ def install(name=None,
while downgrades:
cmd = cmd_install + ['--force'] + downgrades[:500]
downgrades = downgrades[500:]
_zypper_check_result(_zypper('cmd.run_all', *cmd, python_shell=False))
_zypper_check_result(_zypper(*cmd, python_shell=False))
__context__.pop('pkg.list_pkgs', None)
new = list_pkgs()
@ -842,7 +841,7 @@ def upgrade(refresh=True):
if refresh:
refresh_db()
old = list_pkgs()
call = _zypper('cmd.run_all', 'update', '--auto-agree-with-licenses')
call = _zypper('update', '--auto-agree-with-licenses')
if _is_zypper_error(call['retcode']):
ret['result'] = False
if 'stderr' in call:
@ -872,7 +871,7 @@ def _uninstall(name=None, pkgs=None):
return {}
while targets:
_zypper_check_result(_zypper('cmd.run_all', 'remove', *targets[:500]))
_zypper_check_result(_zypper('remove', *targets[:500]))
targets = targets[500:]
__context__.pop('pkg.list_pkgs', None)
@ -985,7 +984,7 @@ def clean_locks():
if not os.path.exists("/etc/zypp/locks"):
return out
doc = dom.parseString(_zypper_check_result(_zypper('cmd.run_all', '-x', 'cl'), xml=True))
doc = dom.parseString(_zypper_check_result(_zypper('-x', 'cl'), xml=True))
for node in doc.getElementsByTagName("message"):
text = node.childNodes[0].nodeValue.lower()
if text.startswith(LCK):
@ -1023,7 +1022,7 @@ def remove_lock(packages, **kwargs): # pylint: disable=unused-argument
missing.append(pkg)
if removed:
_zypper_check_result(_zypper('cmd.run_all', 'rl', *removed))
_zypper_check_result(_zypper('rl', *removed))
return {'removed': len(removed), 'not_found': missing}
@ -1052,7 +1051,7 @@ def add_lock(packages, **kwargs): # pylint: disable=unused-argument
added.append(pkg)
if added:
_zypper_check_result(_zypper('cmd.run_all', 'al', *added))
_zypper_check_result(_zypper('al', *added))
return {'added': len(added), 'packages': added}
@ -1185,7 +1184,7 @@ def _get_patterns(installed_only=None):
'''
patterns = {}
doc = dom.parseString(_zypper_check_result(_zypper('cmd.run_all', '--xmlout', 'se', '-t', 'pattern'), xml=True))
doc = dom.parseString(_zypper_check_result(_zypper('--xmlout', 'se', '-t', 'pattern'), xml=True))
for element in doc.getElementsByTagName('solvable'):
installed = element.getAttribute('status') == 'installed'
if (installed_only and installed) or not installed_only:
@ -1249,7 +1248,7 @@ def search(criteria, refresh=False):
if refresh:
refresh_db()
doc = dom.parseString(_zypper_check_result(_zypper('cmd.run_all', '--xmlout', 'se', criteria), xml=True))
doc = dom.parseString(_zypper_check_result(_zypper('--xmlout', 'se', criteria), xml=True))
solvables = doc.getElementsByTagName('solvable')
if not solvables:
raise CommandExecutionError('No packages found by criteria "{0}".'.format(criteria))
@ -1312,7 +1311,7 @@ def list_products(all=False, refresh=False):
if not all:
cmd.append('-i')
doc = dom.parseString(_zypper_check_result(_zypper('cmd.run_all', *cmd), xml=True))
doc = dom.parseString(_zypper_check_result(_zypper(*cmd), xml=True))
product_list = doc.getElementsByTagName('product-list')
if not product_list:
return ret # No products found
@ -1366,7 +1365,7 @@ def download(*packages, **kwargs):
if refresh:
refresh_db()
doc = dom.parseString(_zypper_check_result(_zypper('cmd.run_all', '-x', 'download', *packages), xml=True))
doc = dom.parseString(_zypper_check_result(_zypper('-x', 'download', *packages), xml=True))
pkg_ret = {}
for dld_result in doc.getElementsByTagName("download-result"):
repo = dld_result.getElementsByTagName("repository")[0]