Skip to content

Commit

Permalink
Merge pull request #31305 from mcalmer/zypper-non-interactive-everywhere
Browse files Browse the repository at this point in the history
call zypper with option --non-interactive everywhere
  • Loading branch information
Nicole Thomas committed Feb 19, 2016
2 parents 98a14f8 + 75e7767 commit d067e77
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions salt/modules/zypper.py
Expand Up @@ -56,6 +56,20 @@ def __virtual__():
return __virtualname__


def _zypper(*opts):
'''
Return zypper command with default options as a list.
opts
additional options for zypper command
'''
cmd = ['zypper', '--non-interactive']
cmd.extend(opts)

return cmd


def list_upgrades(refresh=True):
'''
List all available package upgrades on this system
Expand All @@ -70,7 +84,7 @@ def list_upgrades(refresh=True):
refresh_db()
ret = {}
call = __salt__['cmd.run_all'](
'zypper list-updates', output_loglevel='trace'
_zypper('list-updates'), output_loglevel='trace'
)
if call['retcode'] != 0:
comment = ''
Expand Down Expand Up @@ -184,7 +198,7 @@ def info_available(*names, **kwargs):

# Run in batches
while batch:
cmd = 'zypper info -t package {0}'.format(' '.join(batch[:batch_size]))
cmd = _zypper('info', '-t', 'package', *batch[:batch_size])
pkg_info.extend(re.split(r"Information for package*", __salt__['cmd.run_stdout'](cmd, output_loglevel='trace')))
batch = batch[batch_size:]

Expand Down Expand Up @@ -493,7 +507,7 @@ def del_repo(repo):
repos_cfg = _get_configured_repos()
for alias in repos_cfg.sections():
if alias == repo:
cmd = ('zypper -x --non-interactive rr --loose-auth --loose-query {0}'.format(alias))
cmd = _zypper('-x', 'rr', '--loose-auth', '--loose-query', alias)
doc = dom.parseString(__salt__['cmd.run'](cmd, output_loglevel='trace'))
msg = doc.getElementsByTagName('message')
if doc.getElementsByTagName('progress') and msg:
Expand Down Expand Up @@ -582,8 +596,8 @@ def mod_repo(repo, **kwargs):
try:
# Try to parse the output and find the error,
# but this not always working (depends on Zypper version)
doc = dom.parseString(__salt__['cmd.run'](('zypper -x ar {0} \'{1}\''.format(url, repo)),
output_loglevel='trace'))
doc = dom.parseString(__salt__['cmd.run'](
_zypper('-x', 'ar', url, repo), output_loglevel='trace'))
except Exception:
# No XML out available, but it is still unknown the state of the result.
pass
Expand Down Expand Up @@ -628,7 +642,8 @@ def mod_repo(repo, **kwargs):
cmd_opt.append("--name='{0}'".format(kwargs.get('humanname')))

if cmd_opt:
__salt__['cmd.run'](('zypper -x mr {0} \'{1}\''.format(' '.join(cmd_opt), repo)),
cmd_opt.append(repo)
__salt__['cmd.run'](_zypper('-x', 'mr', *cmd_opt),
output_loglevel='trace')

# If repo nor added neither modified, error should be thrown
Expand All @@ -651,7 +666,7 @@ def refresh_db():
salt '*' pkg.refresh_db
'''
cmd = 'zypper refresh'
cmd = _zypper('refresh')
ret = {}
call = __salt__['cmd.run_all'](cmd, output_loglevel='trace')
if call['retcode'] != 0:
Expand Down Expand Up @@ -798,7 +813,7 @@ def install(name=None,
log.info('Targeting repo {0!r}'.format(fromrepo))
else:
fromrepoopt = ''
cmd_install = ['zypper', '--non-interactive']
cmd_install = _zypper()
if not refresh:
cmd_install.append('--no-refresh')
cmd_install += ['install', '--name', '--auto-agree-with-licenses']
Expand Down Expand Up @@ -854,7 +869,7 @@ def upgrade(refresh=True):
if salt.utils.is_true(refresh):
refresh_db()
old = list_pkgs()
cmd = 'zypper --non-interactive update --auto-agree-with-licenses'
cmd = _zypper('update', '--auto-agree-with-licenses')
call = __salt__['cmd.run_all'](cmd, output_loglevel='trace')
if call['retcode'] != 0:
ret['result'] = False
Expand Down Expand Up @@ -885,10 +900,7 @@ def _uninstall(action='remove', name=None, pkgs=None):
if not targets:
return {}
while targets:
cmd = (
'zypper --non-interactive remove {0} {1}'
.format(purge_arg, ' '.join(targets[:500]))
)
cmd = _zypper('remove', purge_arg, *targets[:500])
__salt__['cmd.run'](cmd, output_loglevel='trace')
targets = targets[500:]
__context__.pop('pkg.list_pkgs', None)
Expand Down Expand Up @@ -1002,7 +1014,7 @@ def clean_locks():
if not os.path.exists("/etc/zypp/locks"):
return out

doc = dom.parseString(__salt__['cmd.run']('zypper --non-interactive -x cl', output_loglevel='trace'))
doc = dom.parseString(__salt__['cmd.run'](_zypper('-x', 'cl'), output_loglevel='trace'))
for node in doc.getElementsByTagName("message"):
text = node.childNodes[0].nodeValue.lower()
if text.startswith(LCK):
Expand Down Expand Up @@ -1040,8 +1052,7 @@ def remove_lock(packages, **kwargs): # pylint: disable=unused-argument
missing.append(pkg)

if removed:
__salt__['cmd.run'](('zypper --non-interactive rl {0}'.format(' '.join(removed))),
output_loglevel='trace')
__salt__['cmd.run'](_zypper('rl', *removed), output_loglevel='trace')

return {'removed': len(removed), 'not_found': missing}

Expand Down Expand Up @@ -1070,8 +1081,7 @@ def add_lock(packages, **kwargs): # pylint: disable=unused-argument
added.append(pkg)

if added:
__salt__['cmd.run'](('zypper --non-interactive al {0}'.format(' '.join(added))),
output_loglevel='trace')
__salt__['cmd.run'](_zypper('al', *added), output_loglevel='trace')

return {'added': len(added), 'packages': added}

Expand Down Expand Up @@ -1203,7 +1213,7 @@ def _get_patterns(installed_only=None):
List all known patterns in repos.
'''
patterns = {}
doc = dom.parseString(__salt__['cmd.run'](('zypper --xmlout se -t pattern'),
doc = dom.parseString(__salt__['cmd.run'](_zypper('--xmlout', 'se', '-t', 'pattern'),
output_loglevel='trace'))
for element in doc.getElementsByTagName('solvable'):
installed = element.getAttribute('status') == 'installed'
Expand Down Expand Up @@ -1252,7 +1262,7 @@ def search(criteria):
salt '*' pkg.search <criteria>
'''
doc = dom.parseString(__salt__['cmd.run'](('zypper --xmlout se {0}'.format(criteria)),
doc = dom.parseString(__salt__['cmd.run'](_zypper('--xmlout', 'se', criteria),
output_loglevel='trace'))
solvables = doc.getElementsByTagName('solvable')
if not solvables:
Expand Down Expand Up @@ -1301,8 +1311,10 @@ def list_products(all=False):
'''
ret = list()
OEM_PATH = "/var/lib/suseRegister/OEM"
doc = dom.parseString(__salt__['cmd.run'](("zypper -x products{0}".format(not all and ' -i' or '')),
output_loglevel='trace'))
cmd = _zypper('-x', 'products')
if not all:
cmd.append('-i')
doc = dom.parseString(__salt__['cmd.run'](cmd, output_loglevel='trace'))
for prd in doc.getElementsByTagName('product-list')[0].getElementsByTagName('product'):
p_nfo = dict()
for k_p_nfo, v_p_nfo in prd.attributes.items():
Expand Down Expand Up @@ -1341,8 +1353,7 @@ def download(*packages):
raise CommandExecutionError("No packages has been specified.")

doc = dom.parseString(__salt__['cmd.run'](
('zypper -x --non-interactive download {0}'.format(' '.join(packages))),
output_loglevel='trace'))
_zypper('-x', 'download', *packages), output_loglevel='trace'))
pkg_ret = {}
for dld_result in doc.getElementsByTagName("download-result"):
repo = dld_result.getElementsByTagName("repository")[0]
Expand Down

0 comments on commit d067e77

Please sign in to comment.