Skip to content

Commit

Permalink
Add support for name, pkgs and diff_attr parameters to zypperpkg.upgr…
Browse files Browse the repository at this point in the history
…ade()/yumpkg.upgrade() - backport 3004 (#538)

* Migrate zypper.upgrade tests to pytest

(cherry picked from commit ecce005b543f66198c7ac118966254dd3d60682f)

* Add names and pkgs parameters to zypper.upgrade

Fixes saltstack/salt#62030

(cherry picked from commit 19ebb40dc4538c983721a8746a201b7f1300c2f7)

* Don't turn attr="all" into a list

pkg_resource.format_pkg_list expects its `attr` argument to be either a
list of attributes or the string "all" to indicate all available
attributes should be used for formatting.

Fixes: saltstack/salt#62032
(cherry picked from commit 05482da89b91442235d3cc2889e59ac3722a7fae)

* Add diff_attr parameter to zypper/yum upgrade

diff_attr works just like it does for pkg.install. Having the
option to return additional attributes can remove the need for a
follow-up list_pkgs call.

Fixes: saltstack/salt#62031
(cherry picked from commit 20ffffe3be6c7d94e9cc3338a57bbf5014f33d93)
  • Loading branch information
agraul authored and meaksh committed Dec 29, 2022
1 parent 2cd450a commit fa99a01
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 490 deletions.
1 change: 1 addition & 0 deletions changelog/62030.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix inconsitency regarding name and pkgs parameters between zypperpkg.upgrade() and yumpkg.upgrade()
1 change: 1 addition & 0 deletions changelog/62031.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `diff_attr` parameter to pkg.upgrade() (zypper/yum).
1 change: 1 addition & 0 deletions changelog/62032.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix attr=all handling in pkg.list_pkgs() (yum/zypper).
7 changes: 4 additions & 3 deletions salt/modules/yumpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ def list_pkgs(versions_as_list=False, **kwargs):
return {}

attr = kwargs.get("attr")
if attr is not None:
if attr is not None and attr != "all":
attr = salt.utils.args.split_input(attr)

contextkey = "pkg.list_pkgs"
Expand Down Expand Up @@ -1844,6 +1844,7 @@ def upgrade(
normalize=True,
minimal=False,
obsoletes=True,
diff_attr=None,
**kwargs
):
"""
Expand Down Expand Up @@ -2000,7 +2001,7 @@ def upgrade(
if salt.utils.data.is_true(refresh):
refresh_db(**kwargs)

old = list_pkgs()
old = list_pkgs(attr=diff_attr)

targets = []
if name or pkgs:
Expand Down Expand Up @@ -2032,7 +2033,7 @@ def upgrade(
cmd.extend(targets)
result = _call_yum(cmd)
__context__.pop("pkg.list_pkgs", None)
new = list_pkgs()
new = list_pkgs(attr=diff_attr)
ret = salt.utils.data.compare_dicts(old, new)

if result["retcode"] != 0:
Expand Down
76 changes: 73 additions & 3 deletions salt/modules/zypperpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ def list_pkgs(versions_as_list=False, root=None, includes=None, **kwargs):
return {}

attr = kwargs.get("attr")
if attr is not None:
if attr is not None and attr != "all":
attr = salt.utils.args.split_input(attr)

includes = includes if includes else []
Expand Down Expand Up @@ -1791,6 +1791,8 @@ def install(


def upgrade(
name=None,
pkgs=None,
refresh=True,
dryrun=False,
dist_upgrade=False,
Expand All @@ -1800,6 +1802,7 @@ def upgrade(
skip_verify=False,
no_recommends=False,
root=None,
diff_attr=None,
**kwargs
): # pylint: disable=unused-argument
"""
Expand All @@ -1819,6 +1822,27 @@ def upgrade(
Run a full system upgrade, a zypper upgrade
name
The name of the package to be installed. Note that this parameter is
ignored if ``pkgs`` is passed or if ``dryrun`` is set to True.
CLI Example:
.. code-block:: bash
salt '*' pkg.install name=<package name>
pkgs
A list of packages to install from a software repository. Must be
passed as a python list. Note that this parameter is ignored if
``dryrun`` is set to True.
CLI Examples:
.. code-block:: bash
salt '*' pkg.install pkgs='["foo", "bar"]'
refresh
force a refresh if set to True (default).
If set to False it depends on zypper if a refresh is
Expand Down Expand Up @@ -1850,18 +1874,52 @@ def upgrade(
root
Operate on a different root directory.
diff_attr:
If a list of package attributes is specified, returned value will
contain them, eg.::
{'<package>': {
'old': {
'version': '<old-version>',
'arch': '<old-arch>'},
'new': {
'version': '<new-version>',
'arch': '<new-arch>'}}}
Valid attributes are: ``epoch``, ``version``, ``release``, ``arch``,
``install_date``, ``install_date_time_t``.
If ``all`` is specified, all valid attributes will be returned.
Returns a dictionary containing the changes:
.. code-block:: python
{'<package>': {'old': '<old-version>',
'new': '<new-version>'}}
If an attribute list is specified in ``diff_attr``, the dict will also contain
any specified attribute, eg.::
.. code-block:: python
{'<package>': {
'old': {
'version': '<old-version>',
'arch': '<old-arch>'},
'new': {
'version': '<new-version>',
'arch': '<new-arch>'}}}
CLI Example:
.. code-block:: bash
salt '*' pkg.upgrade
salt '*' pkg.upgrade name=mypackage
salt '*' pkg.upgrade pkgs='["package1", "package2"]'
salt '*' pkg.upgrade dist_upgrade=True fromrepo='["MyRepoName"]' novendorchange=True
salt '*' pkg.upgrade dist_upgrade=True dryrun=True
"""
Expand Down Expand Up @@ -1897,12 +1955,24 @@ def upgrade(
allowvendorchange, novendorchange
).noraise.call(*cmd_update + ["--debug-solver"])

old = list_pkgs(root=root)
if not dist_upgrade:
if name or pkgs:
try:
(pkg_params, _) = __salt__["pkg_resource.parse_targets"](
name=name, pkgs=pkgs, sources=None, **kwargs
)
if pkg_params:
cmd_update.extend(pkg_params.keys())

except MinionError as exc:
raise CommandExecutionError(exc)

old = list_pkgs(root=root, attr=diff_attr)
__zypper__(systemd_scope=_systemd_scope(), root=root).allow_vendor_change(
allowvendorchange, novendorchange
).noraise.call(*cmd_update)
_clean_cache()
new = list_pkgs(root=root)
new = list_pkgs(root=root, attr=diff_attr)
ret = salt.utils.data.compare_dicts(old, new)

if __zypper__.exit_code not in __zypper__.SUCCESS_EXIT_CODES:
Expand Down
Loading

0 comments on commit fa99a01

Please sign in to comment.