2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 22:12:10 +02:00
|
|
|
# Copyright (c) 2013, Patrick Pelletier <pp.pelletier@gmail.com>
|
2020-03-09 10:11:07 +01:00
|
|
|
# Based on pacman (Afterburn) and pkgin (Shaun Zinck) modules
|
|
|
|
#
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: opkg
|
|
|
|
author: "Patrick Pelletier (@skinp)"
|
2023-01-12 20:43:56 +01:00
|
|
|
short_description: Package manager for OpenWrt and Openembedded/Yocto based Linux distributions
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-01-12 20:43:56 +01:00
|
|
|
- Manages ipk packages for OpenWrt and Openembedded/Yocto based Linux distributions
|
2023-02-24 09:25:20 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: none
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2021-10-14 21:14:32 +02:00
|
|
|
- Name of package(s) to install/remove.
|
2022-12-22 19:31:33 +01:00
|
|
|
- C(NAME=VERSION) syntax is also supported to install a package
|
2023-01-12 20:43:56 +01:00
|
|
|
in a certain version. See the examples. This only works on Yocto based
|
|
|
|
Linux distributions (opkg>=0.3.2) and not for OpenWrt. This is
|
|
|
|
supported since community.general 6.2.0.
|
2020-06-26 15:17:11 +02:00
|
|
|
aliases: [pkg]
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
2021-10-14 21:14:32 +02:00
|
|
|
type: list
|
|
|
|
elements: str
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
description:
|
2021-10-14 21:14:32 +02:00
|
|
|
- State of the package.
|
2020-11-13 13:41:11 +01:00
|
|
|
choices: [ 'present', 'absent', 'installed', 'removed' ]
|
2020-03-09 10:11:07 +01:00
|
|
|
default: present
|
2020-06-26 15:17:11 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
force:
|
|
|
|
description:
|
2021-10-14 21:14:32 +02:00
|
|
|
- The C(opkg --force) parameter used.
|
2023-06-20 08:14:02 +02:00
|
|
|
- Passing V("") as value and not passing any value at all have both
|
2023-05-29 18:52:29 +02:00
|
|
|
the same effect of B(not) using any C(--force-) parameter.
|
2020-03-09 10:11:07 +01:00
|
|
|
choices:
|
|
|
|
- ""
|
|
|
|
- "depends"
|
|
|
|
- "maintainer"
|
|
|
|
- "reinstall"
|
|
|
|
- "overwrite"
|
|
|
|
- "downgrade"
|
|
|
|
- "space"
|
|
|
|
- "postinstall"
|
|
|
|
- "remove"
|
|
|
|
- "checksum"
|
|
|
|
- "removal-of-dependent-packages"
|
2020-06-26 15:17:11 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
update_cache:
|
|
|
|
description:
|
2021-10-14 21:14:32 +02:00
|
|
|
- Update the package DB first.
|
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
2023-07-06 21:15:17 +02:00
|
|
|
executable:
|
|
|
|
description:
|
|
|
|
- The executable location for C(opkg).
|
|
|
|
type: path
|
|
|
|
version_added: 7.2.0
|
2020-03-09 10:11:07 +01:00
|
|
|
requirements:
|
|
|
|
- opkg
|
|
|
|
- python
|
|
|
|
'''
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Install foo
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.opkg:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: foo
|
|
|
|
state: present
|
|
|
|
|
2023-01-12 20:43:56 +01:00
|
|
|
- name: Install foo in version 1.2 (opkg>=0.3.2 on Yocto based Linux distributions)
|
2022-12-22 19:31:33 +01:00
|
|
|
community.general.opkg:
|
|
|
|
name: foo=1.2
|
|
|
|
state: present
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Update cache and install foo
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.opkg:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: foo
|
|
|
|
state: present
|
2022-08-24 19:59:13 +02:00
|
|
|
update_cache: true
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Remove foo
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.opkg:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: foo
|
|
|
|
state: absent
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Remove foo and bar
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.opkg:
|
2021-10-14 21:14:32 +02:00
|
|
|
name:
|
|
|
|
- foo
|
|
|
|
- bar
|
2020-03-09 10:11:07 +01:00
|
|
|
state: absent
|
|
|
|
|
2020-05-15 12:27:06 +02:00
|
|
|
- name: Install foo using overwrite option forcibly
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.opkg:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: foo
|
|
|
|
state: present
|
|
|
|
force: overwrite
|
|
|
|
'''
|
|
|
|
|
2023-07-06 21:15:17 +02:00
|
|
|
import os
|
2023-01-12 20:42:38 +01:00
|
|
|
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
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2023-01-12 20:42:38 +01:00
|
|
|
class Opkg(StateModuleHelper):
|
|
|
|
module = dict(
|
2020-03-09 10:11:07 +01:00
|
|
|
argument_spec=dict(
|
2021-10-14 21:14:32 +02:00
|
|
|
name=dict(aliases=["pkg"], required=True, type="list", elements="str"),
|
2020-03-09 10:11:07 +01:00
|
|
|
state=dict(default="present", choices=["present", "installed", "absent", "removed"]),
|
2023-05-29 18:52:29 +02:00
|
|
|
force=dict(choices=["", "depends", "maintainer", "reinstall", "overwrite", "downgrade", "space",
|
|
|
|
"postinstall", "remove", "checksum", "removal-of-dependent-packages"]),
|
2022-04-26 11:45:15 +02:00
|
|
|
update_cache=dict(default=False, type='bool'),
|
2023-07-06 21:15:17 +02:00
|
|
|
executable=dict(type="path"),
|
2023-01-12 20:42:38 +01:00
|
|
|
),
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
2024-04-23 22:07:29 +02:00
|
|
|
use_old_vardict = False
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-01-12 20:42:38 +01:00
|
|
|
def __init_module__(self):
|
|
|
|
self.vars.set("install_c", 0, output=False, change=True)
|
|
|
|
self.vars.set("remove_c", 0, output=False, change=True)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-01-12 20:42:38 +01:00
|
|
|
state_map = dict(
|
|
|
|
query="list-installed",
|
|
|
|
present="install",
|
|
|
|
installed="install",
|
|
|
|
absent="remove",
|
|
|
|
removed="remove",
|
|
|
|
)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-01-12 20:42:38 +01:00
|
|
|
def _force(value):
|
|
|
|
if value == "":
|
|
|
|
value = None
|
|
|
|
return cmd_runner_fmt.as_optval("--force-")(value, ctx_ignore_none=True)
|
|
|
|
|
2023-07-06 21:15:17 +02:00
|
|
|
dir, cmd = os.path.split(self.vars.executable) if self.vars.executable else (None, "opkg")
|
|
|
|
|
2023-01-12 20:42:38 +01:00
|
|
|
self.runner = CmdRunner(
|
|
|
|
self.module,
|
2023-07-06 21:15:17 +02:00
|
|
|
command=cmd,
|
2023-01-12 20:42:38 +01:00
|
|
|
arg_formats=dict(
|
|
|
|
package=cmd_runner_fmt.as_list(),
|
|
|
|
state=cmd_runner_fmt.as_map(state_map),
|
|
|
|
force=cmd_runner_fmt.as_func(_force),
|
2023-07-06 21:15:17 +02:00
|
|
|
update_cache=cmd_runner_fmt.as_bool("update"),
|
2023-01-12 20:42:38 +01:00
|
|
|
),
|
2023-07-06 21:15:17 +02:00
|
|
|
path_prefix=dir,
|
2023-01-12 20:42:38 +01:00
|
|
|
)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-03-02 07:01:15 +01:00
|
|
|
if self.vars.update_cache:
|
|
|
|
rc, dummy, dummy = self.runner("update_cache").run()
|
|
|
|
if rc != 0:
|
|
|
|
self.do_raise("could not update package db")
|
|
|
|
|
2023-01-12 20:42:38 +01:00
|
|
|
@staticmethod
|
|
|
|
def split_name_and_version(package):
|
|
|
|
""" Split the name and the version when using the NAME=VERSION syntax """
|
|
|
|
splitted = package.split('=', 1)
|
|
|
|
if len(splitted) == 1:
|
|
|
|
return splitted[0], None
|
|
|
|
else:
|
|
|
|
return splitted[0], splitted[1]
|
|
|
|
|
|
|
|
def _package_in_desired_state(self, name, want_installed, version=None):
|
|
|
|
dummy, out, dummy = self.runner("state package").run(state="query", package=name)
|
|
|
|
|
|
|
|
has_package = out.startswith(name + " - %s" % ("" if not version else (version + " ")))
|
|
|
|
return want_installed == has_package
|
|
|
|
|
|
|
|
def state_present(self):
|
|
|
|
with self.runner("state force package") as ctx:
|
|
|
|
for package in self.vars.name:
|
|
|
|
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":
|
|
|
|
ctx.run(package=package)
|
|
|
|
if not self._package_in_desired_state(pkg_name, want_installed=True, version=pkg_version):
|
|
|
|
self.do_raise("failed to install %s" % package)
|
|
|
|
self.vars.install_c += 1
|
2023-03-02 07:01:15 +01:00
|
|
|
if self.verbosity >= 4:
|
|
|
|
self.vars.run_info = ctx.run_info
|
2023-01-12 20:42:38 +01:00
|
|
|
if self.vars.install_c > 0:
|
|
|
|
self.vars.msg = "installed %s package(s)" % (self.vars.install_c)
|
|
|
|
else:
|
|
|
|
self.vars.msg = "package(s) already present"
|
|
|
|
|
|
|
|
def state_absent(self):
|
|
|
|
with self.runner("state force package") as ctx:
|
|
|
|
for package in self.vars.name:
|
|
|
|
package, dummy = self.split_name_and_version(package)
|
|
|
|
if not self._package_in_desired_state(package, want_installed=False):
|
|
|
|
ctx.run(package=package)
|
|
|
|
if not self._package_in_desired_state(package, want_installed=False):
|
|
|
|
self.do_raise("failed to remove %s" % package)
|
|
|
|
self.vars.remove_c += 1
|
2023-03-02 07:01:15 +01:00
|
|
|
if self.verbosity >= 4:
|
|
|
|
self.vars.run_info = ctx.run_info
|
2023-01-12 20:42:38 +01:00
|
|
|
if self.vars.remove_c > 0:
|
|
|
|
self.vars.msg = "removed %s package(s)" % (self.vars.remove_c)
|
|
|
|
else:
|
|
|
|
self.vars.msg = "package(s) already absent"
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-01-12 20:42:38 +01:00
|
|
|
state_installed = state_present
|
|
|
|
state_removed = state_absent
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-01-12 20:42:38 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
Opkg.execute()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|