| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2013, Patrick Pelletier <[email protected]> |
| 4 |
# Based on pacman (Afterburn) and pkgin (Shaun Zinck) modules |
| 5 |
# |
| 6 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
module: opkg |
| 13 |
author: "Patrick Pelletier (@skinp)" |
| 14 |
short_description: Package manager for OpenWrt and Openembedded/Yocto based Linux distributions |
| 15 |
description: |
| 16 |
- Manages ipk packages for OpenWrt and Openembedded/Yocto based Linux distributions. |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
support: none |
| 22 |
diff_mode: |
| 23 |
support: none |
| 24 |
options: |
| 25 |
name: |
| 26 |
description: |
| 27 |
- Name of package(s) to install/remove. |
| 28 |
- V(NAME=VERSION) syntax is also supported to install a package in a certain version. See the examples. This only works |
| 29 |
on Yocto based Linux distributions (opkg>=0.3.2) and not for OpenWrt. This is supported since community.general 6.2.0. |
| 30 |
aliases: [pkg] |
| 31 |
required: true |
| 32 |
type: list |
| 33 |
elements: str |
| 34 |
state: |
| 35 |
description: |
| 36 |
- State of the package. |
| 37 |
choices: ['present', 'absent', 'installed', 'removed'] |
| 38 |
default: present |
| 39 |
type: str |
| 40 |
force: |
| 41 |
description: |
| 42 |
- The C(opkg --force) parameter used. |
| 43 |
choices: |
| 44 |
- "depends" |
| 45 |
- "maintainer" |
| 46 |
- "reinstall" |
| 47 |
- "overwrite" |
| 48 |
- "downgrade" |
| 49 |
- "space" |
| 50 |
- "postinstall" |
| 51 |
- "remove" |
| 52 |
- "checksum" |
| 53 |
- "removal-of-dependent-packages" |
| 54 |
type: str |
| 55 |
update_cache: |
| 56 |
description: |
| 57 |
- Update the package DB first. |
| 58 |
default: false |
| 59 |
type: bool |
| 60 |
executable: |
| 61 |
description: |
| 62 |
- The executable location for C(opkg). |
| 63 |
type: path |
| 64 |
version_added: 7.2.0 |
| 65 |
requirements: |
| 66 |
- opkg |
| 67 |
- python |
| 68 |
""" |
| 69 |
|
| 70 |
EXAMPLES = r""" |
| 71 |
- name: Install foo |
| 72 |
community.general.opkg: |
| 73 |
name: foo |
| 74 |
state: present |
| 75 |
|
| 76 |
- name: Install foo in version 1.2 (opkg>=0.3.2 on Yocto based Linux distributions) |
| 77 |
community.general.opkg: |
| 78 |
name: foo=1.2 |
| 79 |
state: present |
| 80 |
|
| 81 |
- name: Update cache and install foo |
| 82 |
community.general.opkg: |
| 83 |
name: foo |
| 84 |
state: present |
| 85 |
update_cache: true |
| 86 |
|
| 87 |
- name: Remove foo |
| 88 |
community.general.opkg: |
| 89 |
name: foo |
| 90 |
state: absent |
| 91 |
|
| 92 |
- name: Remove foo and bar |
| 93 |
community.general.opkg: |
| 94 |
name: |
| 95 |
- foo |
| 96 |
- bar |
| 97 |
state: absent |
| 98 |
|
| 99 |
- name: Install foo using overwrite option forcibly |
| 100 |
community.general.opkg: |
| 101 |
name: foo |
| 102 |
state: present |
| 103 |
force: overwrite |
| 104 |
""" |
| 105 |
|
| 106 |
RETURN = r""" |
| 107 |
version: |
| 108 |
description: Version of opkg. |
| 109 |
type: str |
| 110 |
returned: always |
| 111 |
sample: "2.80.0" |
| 112 |
version_added: 10.0.0 |
| 113 |
""" |
| 114 |
|
| 115 |
import os |
| 116 |
|
| 117 |
from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt |
| 118 |
from ansible_collections.community.general.plugins.module_utils._module_helper import StateModuleHelper |
| 119 |
|
| 120 |
|
| 121 |
class Opkg(StateModuleHelper): |
| 122 |
module = dict( |
| 123 |
argument_spec=dict( |
| 124 |
name=dict(aliases=["pkg"], required=True, type="list", elements="str"), |
| 125 |
state=dict(default="present", choices=["present", "installed", "absent", "removed"]), |
| 126 |
force=dict( |
| 127 |
choices=[ |
| 128 |
"depends", |
| 129 |
"maintainer", |
| 130 |
"reinstall", |
| 131 |
"overwrite", |
| 132 |
"downgrade", |
| 133 |
"space", |
| 134 |
"postinstall", |
| 135 |
"remove", |
| 136 |
"checksum", |
| 137 |
"removal-of-dependent-packages", |
| 138 |
] |
| 139 |
), |
| 140 |
update_cache=dict(default=False, type="bool"), |
| 141 |
executable=dict(type="path"), |
| 142 |
), |
| 143 |
) |
| 144 |
|
| 145 |
def __init_module__(self): |
| 146 |
self.vars.set("install_c", 0, output=False, change=True) |
| 147 |
self.vars.set("remove_c", 0, output=False, change=True) |
| 148 |
|
| 149 |
state_map = dict( |
| 150 |
query="list-installed", |
| 151 |
present="install", |
| 152 |
installed="install", |
| 153 |
absent="remove", |
| 154 |
removed="remove", |
| 155 |
) |
| 156 |
|
| 157 |
dir, cmd = os.path.split(self.vars.executable) if self.vars.executable else (None, "opkg") |
| 158 |
|
| 159 |
self.runner = CmdRunner( |
| 160 |
self.module, |
| 161 |
command=cmd, |
| 162 |
arg_formats=dict( |
| 163 |
package=cmd_runner_fmt.as_list(), |
| 164 |
state=cmd_runner_fmt.as_map(state_map), |
| 165 |
force=cmd_runner_fmt.as_optval("--force-"), |
| 166 |
update_cache=cmd_runner_fmt.as_bool("update"), |
| 167 |
version=cmd_runner_fmt.as_fixed("--version"), |
| 168 |
), |
| 169 |
path_prefix=dir, |
| 170 |
) |
| 171 |
|
| 172 |
with self.runner("version") as ctx: |
| 173 |
rc, out, err = ctx.run() |
| 174 |
self.vars.version = out.strip().replace("opkg version ", "") |
| 175 |
|
| 176 |
if self.vars.update_cache: |
| 177 |
rc, dummy, dummy = self.runner("update_cache").run() |
| 178 |
if rc != 0: |
| 179 |
self.do_raise("could not update package db") |
| 180 |
|
| 181 |
@staticmethod |
| 182 |
def split_name_and_version(package): |
| 183 |
"""Split the name and the version when using the NAME=VERSION syntax""" |
| 184 |
splitted = package.split("=", 1) |
| 185 |
if len(splitted) == 1: |
| 186 |
return splitted[0], None |
| 187 |
else: |
| 188 |
return splitted[0], splitted[1] |
| 189 |
|
| 190 |
def _package_in_desired_state(self, name, want_installed, version=None): |
| 191 |
dummy, out, dummy = self.runner("state package").run(state="query", package=name) |
| 192 |
|
| 193 |
has_package = out.startswith(f"{name} - {'' if not version else f'{version} '}") |
| 194 |
return want_installed == has_package |
| 195 |
|
| 196 |
def state_present(self): |
| 197 |
with self.runner("state force package") as ctx: |
| 198 |
for package in self.vars.name: |
| 199 |
pkg_name, pkg_version = self.split_name_and_version(package) |
| 200 |
if ( |
| 201 |
not self._package_in_desired_state(pkg_name, want_installed=True, version=pkg_version) |
| 202 |
or self.vars.force == "reinstall" |
| 203 |
): |
| 204 |
ctx.run(package=package) |
| 205 |
self.vars.set("run_info", ctx.run_info, verbosity=4) |
| 206 |
if not self._package_in_desired_state(pkg_name, want_installed=True, version=pkg_version): |
| 207 |
self.do_raise(f"failed to install {package}") |
| 208 |
self.vars.install_c += 1 |
| 209 |
if self.vars.install_c > 0: |
| 210 |
self.vars.msg = f"installed {self.vars.install_c} package(s)" |
| 211 |
else: |
| 212 |
self.vars.msg = "package(s) already present" |
| 213 |
|
| 214 |
def state_absent(self): |
| 215 |
with self.runner("state force package") as ctx: |
| 216 |
for package in self.vars.name: |
| 217 |
package, dummy = self.split_name_and_version(package) |
| 218 |
if not self._package_in_desired_state(package, want_installed=False): |
| 219 |
ctx.run(package=package) |
| 220 |
self.vars.set("run_info", ctx.run_info, verbosity=4) |
| 221 |
if not self._package_in_desired_state(package, want_installed=False): |
| 222 |
self.do_raise(f"failed to remove {package}") |
| 223 |
self.vars.remove_c += 1 |
| 224 |
if self.vars.remove_c > 0: |
| 225 |
self.vars.msg = f"removed {self.vars.remove_c} package(s)" |
| 226 |
else: |
| 227 |
self.vars.msg = "package(s) already absent" |
| 228 |
|
| 229 |
state_installed = state_present |
| 230 |
state_removed = state_absent |
| 231 |
|
| 232 |
|
| 233 |
def main(): |
| 234 |
Opkg.execute() |
| 235 |
|
| 236 |
|
| 237 |
if __name__ == "__main__": |
| 238 |
main() |