Skip to content

Commit

Permalink
Merge pull request saltstack#50773 from isbm/isbm-zypper-locks-to-holds
Browse files Browse the repository at this point in the history
zypper's locks to holds
  • Loading branch information
Mike Place authored Dec 10, 2018
2 parents 71fe054 + a0e6d29 commit c4110b4
Showing 1 changed file with 95 additions and 1 deletion.
96 changes: 95 additions & 1 deletion salt/modules/zypper.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import salt.utils.pkg.rpm
import salt.utils.stringutils
import salt.utils.systemd
import salt.utils.versions
from salt.utils.versions import LooseVersion
import salt.utils.environment
from salt.exceptions import CommandExecutionError, MinionError, SaltInvocationError
Expand Down Expand Up @@ -1845,7 +1846,7 @@ def clean_locks(root=None):
return out


def remove_lock(packages, root=None, **kwargs): # pylint: disable=unused-argument
def unhold(name=None, pkgs=None, **kwargs):
'''
Remove specified package lock.
Expand All @@ -1860,7 +1861,51 @@ def remove_lock(packages, root=None, **kwargs): # pylint: disable=unused-argume
salt '*' pkg.remove_lock <package1>,<package2>,<package3>
salt '*' pkg.remove_lock pkgs='["foo", "bar"]'
'''
ret = {}
root = kwargs.get('root')
if (not name and not pkgs) or (name and pkgs):
raise CommandExecutionError('Name or packages must be specified.')
elif name:
pkgs = [name]

locks = list_locks(root)
try:
pkgs = list(__salt__['pkg_resource.parse_targets'](pkgs)[0].keys())
except MinionError as exc:
raise CommandExecutionError(exc)

removed = []
missing = []
for pkg in pkgs:
if locks.get(pkg):
removed.append(pkg)
ret[pkg]['comment'] = 'Package {0} is no longer held.'.format(pkg)
else:
missing.append(pkg)
ret[pkg]['comment'] = 'Package {0} unable to be unheld.'.format(pkg)

if removed:
__zypper__(root=root).call('rl', *removed)

return ret


def remove_lock(packages, root=None, **kwargs): # pylint: disable=unused-argument
'''
Remove specified package lock.
root
operate on a different root directory.
CLI Example:
.. code-block:: bash
salt '*' pkg.remove_lock <package name>
salt '*' pkg.remove_lock <package1>,<package2>,<package3>
salt '*' pkg.remove_lock pkgs='["foo", "bar"]'
'''
salt.utils.versions.warn_until('Sodium', 'This function is deprecated. Please use unhold() instead.')
locks = list_locks(root)
try:
packages = list(__salt__['pkg_resource.parse_targets'](packages)[0].keys())
Expand All @@ -1881,6 +1926,54 @@ def remove_lock(packages, root=None, **kwargs): # pylint: disable=unused-argume
return {'removed': len(removed), 'not_found': missing}


def hold(name=None, pkgs=None, **kwargs):
'''
Add a package lock. Specify packages to lock by exact name.
root
operate on a different root directory.
CLI Example:
.. code-block:: bash
salt '*' pkg.add_lock <package name>
salt '*' pkg.add_lock <package1>,<package2>,<package3>
salt '*' pkg.add_lock pkgs='["foo", "bar"]'
:param name:
:param pkgs:
:param kwargs:
:return:
'''
ret = {}
root = kwargs.get('root')
if (not name and not pkgs) or (name and pkgs):
raise CommandExecutionError('Name or packages must be specified.')
elif name:
pkgs = [name]

locks = list_locks(root=root)
added = []
try:
pkgs = list(__salt__['pkg_resource.parse_targets'](pkgs)[0].keys())
except MinionError as exc:
raise CommandExecutionError(exc)

for pkg in pkgs:
ret[pkg] = {'name': pkg, 'changes': {}, 'result': False, 'comment': ''}
if not locks.get(pkg):
added.append(pkg)
ret[pkg]['comment'] = 'Package {0} is now being held.'.format(pkg)
else:
ret[pkg]['comment'] = 'Package {0} is already set to be held.'.format(pkg)

if added:
__zypper__(root=root).call('al', *added)

return ret


def add_lock(packages, root=None, **kwargs): # pylint: disable=unused-argument
'''
Add a package lock. Specify packages to lock by exact name.
Expand All @@ -1896,6 +1989,7 @@ def add_lock(packages, root=None, **kwargs): # pylint: disable=unused-argument
salt '*' pkg.add_lock <package1>,<package2>,<package3>
salt '*' pkg.add_lock pkgs='["foo", "bar"]'
'''
salt.utils.versions.warn_until('Sodium', 'This function is deprecated. Please use hold() instead.')
locks = list_locks(root)
added = []
try:
Expand Down

0 comments on commit c4110b4

Please sign in to comment.