opkg: add return value version (#9086)

* opkg: add return value version

* add changelog frag
pull/9098/head
Alexei Znamensky 2024-11-03 05:49:54 +13:00 committed by GitHub
parent 8a2ac4f1eb
commit c8410a924e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 7 deletions

View File

@ -0,0 +1,2 @@
minor_changes:
- opkg - add return value ``version`` (https://github.com/ansible-collections/community.general/pull/9086).

View File

@ -75,6 +75,7 @@ requirements:
- opkg - opkg
- python - python
''' '''
EXAMPLES = ''' EXAMPLES = '''
- name: Install foo - name: Install foo
community.general.opkg: community.general.opkg:
@ -111,6 +112,15 @@ EXAMPLES = '''
force: overwrite force: overwrite
''' '''
RETURN = """
version:
description: Version of opkg.
type: str
returned: always
sample: "2.80.0"
version_added: 10.0.0
"""
import os import os
from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt
from ansible_collections.community.general.plugins.module_utils.module_helper import StateModuleHelper from ansible_collections.community.general.plugins.module_utils.module_helper import StateModuleHelper
@ -156,10 +166,15 @@ class Opkg(StateModuleHelper):
state=cmd_runner_fmt.as_map(state_map), state=cmd_runner_fmt.as_map(state_map),
force=cmd_runner_fmt.as_func(_force), force=cmd_runner_fmt.as_func(_force),
update_cache=cmd_runner_fmt.as_bool("update"), update_cache=cmd_runner_fmt.as_bool("update"),
version=cmd_runner_fmt.as_fixed("--version"),
), ),
path_prefix=dir, path_prefix=dir,
) )
with self.runner("version") as ctx:
rc, out, err = ctx.run()
self.vars.version = out.strip().replace("opkg version ", "")
if self.vars.update_cache: if self.vars.update_cache:
rc, dummy, dummy = self.runner("update_cache").run() rc, dummy, dummy = self.runner("update_cache").run()
if rc != 0: if rc != 0:
@ -186,13 +201,12 @@ class Opkg(StateModuleHelper):
pkg_name, pkg_version = self.split_name_and_version(package) pkg_name, pkg_version = self.split_name_and_version(package)
if not self._package_in_desired_state(pkg_name, want_installed=True, version=pkg_version) or self.vars.force == "reinstall": if not self._package_in_desired_state(pkg_name, want_installed=True, version=pkg_version) or self.vars.force == "reinstall":
ctx.run(package=package) ctx.run(package=package)
self.vars.set("run_info", ctx.run_info, verbosity=4)
if not self._package_in_desired_state(pkg_name, want_installed=True, version=pkg_version): if not self._package_in_desired_state(pkg_name, want_installed=True, version=pkg_version):
self.do_raise("failed to install %s" % package) self.do_raise("failed to install %s" % package)
self.vars.install_c += 1 self.vars.install_c += 1
if self.verbosity >= 4:
self.vars.run_info = ctx.run_info
if self.vars.install_c > 0: if self.vars.install_c > 0:
self.vars.msg = "installed %s package(s)" % (self.vars.install_c) self.vars.msg = "installed %s package(s)" % self.vars.install_c
else: else:
self.vars.msg = "package(s) already present" self.vars.msg = "package(s) already present"
@ -202,13 +216,12 @@ class Opkg(StateModuleHelper):
package, dummy = self.split_name_and_version(package) package, dummy = self.split_name_and_version(package)
if not self._package_in_desired_state(package, want_installed=False): if not self._package_in_desired_state(package, want_installed=False):
ctx.run(package=package) ctx.run(package=package)
self.vars.set("run_info", ctx.run_info, verbosity=4)
if not self._package_in_desired_state(package, want_installed=False): if not self._package_in_desired_state(package, want_installed=False):
self.do_raise("failed to remove %s" % package) self.do_raise("failed to remove %s" % package)
self.vars.remove_c += 1 self.vars.remove_c += 1
if self.verbosity >= 4:
self.vars.run_info = ctx.run_info
if self.vars.remove_c > 0: if self.vars.remove_c > 0:
self.vars.msg = "removed %s package(s)" % (self.vars.remove_c) self.vars.msg = "removed %s package(s)" % self.vars.remove_c
else: else:
self.vars.msg = "package(s) already absent" self.vars.msg = "package(s) already absent"

View File

@ -12,11 +12,16 @@
msg: installed 1 package(s) msg: installed 1 package(s)
mocks: mocks:
run_command: run_command:
- command: [/testbin/opkg, list-installed, zlib-dev] - command: [/testbin/opkg, --version]
environ: &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false} environ: &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: false}
rc: 0 rc: 0
out: "" out: ""
err: "" err: ""
- command: [/testbin/opkg, list-installed, zlib-dev]
environ: *env-def
rc: 0
out: ""
err: ""
- command: [/testbin/opkg, install, zlib-dev] - command: [/testbin/opkg, install, zlib-dev]
environ: *env-def environ: *env-def
rc: 0 rc: 0
@ -42,6 +47,11 @@
msg: package(s) already present msg: package(s) already present
mocks: mocks:
run_command: run_command:
- command: [/testbin/opkg, --version]
environ: *env-def
rc: 0
out: ""
err: ""
- command: [/testbin/opkg, list-installed, zlib-dev] - command: [/testbin/opkg, list-installed, zlib-dev]
environ: *env-def environ: *env-def
rc: 0 rc: 0
@ -57,6 +67,11 @@
msg: installed 1 package(s) msg: installed 1 package(s)
mocks: mocks:
run_command: run_command:
- command: [/testbin/opkg, --version]
environ: *env-def
rc: 0
out: ""
err: ""
- command: [/testbin/opkg, list-installed, zlib-dev] - command: [/testbin/opkg, list-installed, zlib-dev]
environ: *env-def environ: *env-def
rc: 0 rc: 0
@ -85,6 +100,11 @@
msg: installed 1 package(s) msg: installed 1 package(s)
mocks: mocks:
run_command: run_command:
- command: [/testbin/opkg, --version]
environ: *env-def
rc: 0
out: ""
err: ""
- command: [/testbin/opkg, list-installed, zlib-dev] - command: [/testbin/opkg, list-installed, zlib-dev]
environ: *env-def environ: *env-def
rc: 0 rc: 0
@ -115,6 +135,11 @@
msg: installed 1 package(s) msg: installed 1 package(s)
mocks: mocks:
run_command: run_command:
- command: [/testbin/opkg, --version]
environ: *env-def
rc: 0
out: ""
err: ""
- command: [/testbin/opkg, update] - command: [/testbin/opkg, update]
environ: *env-def environ: *env-def
rc: 0 rc: 0