Debian Codegraph @

ansible_collections/community/general/plugins/modules/urpmi.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2013, Philippe Makowski
4 # Written by Philippe Makowski <[email protected]>
5 # Based on apt module written by Matthew Williams <[email protected]>
6
7 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
8 # SPDX-License-Identifier: GPL-3.0-or-later
9
10 from __future__ import annotations
11
12 DOCUMENTATION = r"""
13 module: urpmi
14 short_description: Urpmi manager
15 description:
16 - Manages packages with C(urpmi) (such as for Mageia or Mandriva).
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 - A list of package names to install, upgrade or remove.
28 required: true
29 aliases: [package, pkg]
30 type: list
31 elements: str
32 state:
33 description:
34 - Indicates the desired package state.
35 choices: [absent, present, installed, removed]
36 default: present
37 type: str
38 update_cache:
39 description:
40 - Update the package database first C(urpmi.update -a).
41 type: bool
42 default: false
43 no_recommends:
44 description:
45 - Corresponds to the C(--no-recommends) option for C(urpmi).
46 type: bool
47 default: true
48 force:
49 description:
50 - Assume "yes" is the answer to any question urpmi has to ask. Corresponds to the C(--force) option for C(urpmi).
51 type: bool
52 default: true
53 root:
54 description:
55 - Specifies an alternative install root, relative to which all packages are installed. Corresponds to the C(--root)
56 option for C(urpmi).
57 aliases: [installroot]
58 type: str
59 author:
60 - Philippe Makowski (@pmakowski)
61 """
62
63 EXAMPLES = r"""
64 - name: Install package foo
65 community.general.urpmi:
66 pkg: foo
67 state: present
68
69 - name: Remove package foo
70 community.general.urpmi:
71 pkg: foo
72 state: absent
73
74 - name: Remove packages foo and bar
75 community.general.urpmi:
76 pkg: foo,bar
77 state: absent
78
79 - name: Update the package database (urpmi.update -a -q) and install bar (bar will be the updated if a newer version exists)
80 - community.general.urpmi:
81 name: bar
82 state: present
83 update_cache: true
84 """
85
86
87 from ansible.module_utils.basic import AnsibleModule
88
89
90 def query_package(module, name, root):
91 # rpm -q returns 0 if the package is installed,
92 # 1 if it is not installed
93 rpm_path = module.get_bin_path("rpm", True)
94 cmd = [rpm_path, "-q", name] + root_option(root)
95 rc, stdout, stderr = module.run_command(cmd, check_rc=False)
96 return rc == 0
97
98
99 def query_package_provides(module, name, root):
100 # rpm -q returns 0 if the package is installed,
101 # 1 if it is not installed
102 rpm_path = module.get_bin_path("rpm", True)
103 cmd = [rpm_path, "-q", "--whatprovides", name] + root_option(root)
104 rc, stdout, stderr = module.run_command(cmd, check_rc=False)
105 return rc == 0
106
107
108 def update_package_db(module):
109 urpmiupdate_path = module.get_bin_path("urpmi.update", True)
110 cmd = [urpmiupdate_path, "-a", "-q"]
111 rc, stdout, stderr = module.run_command(cmd, check_rc=False)
112 if rc != 0:
113 module.fail_json(msg="could not update package db")
114
115
116 def remove_packages(module, packages, root):
117 remove_c = 0
118 # Using a for loop in case of error, we can report the package that failed
119 for package in packages:
120 # Query the package first, to see if we even need to remove
121 if not query_package(module, package, root):
122 continue
123
124 urpme_path = module.get_bin_path("urpme", True)
125 cmd = [urpme_path, "--auto"] + root_option(root) + [package]
126 rc, stdout, stderr = module.run_command(cmd, check_rc=False)
127
128 if rc != 0:
129 module.fail_json(msg=f"failed to remove {package}")
130
131 remove_c += 1
132
133 if remove_c > 0:
134 module.exit_json(changed=True, msg=f"removed {remove_c} package(s)")
135
136 module.exit_json(changed=False, msg="package(s) already absent")
137
138
139 def install_packages(module, pkgspec, root, force=True, no_recommends=True):
140 packages = ""
141 for package in pkgspec:
142 if not query_package_provides(module, package, root):
143 packages += f"'{package}' "
144
145 if len(packages) != 0:
146 if no_recommends:
147 no_recommends_yes = ["--no-recommends"]
148 else:
149 no_recommends_yes = []
150
151 if force:
152 force_yes = ["--force"]
153 else:
154 force_yes = []
155
156 urpmi_path = module.get_bin_path("urpmi", True)
157 cmd = [urpmi_path, "--auto"] + force_yes + ["--quiet"] + no_recommends_yes + root_option(root) + packages
158
159 rc, out, err = module.run_command(cmd)
160
161 for package in pkgspec:
162 if not query_package_provides(module, package, root):
163 module.fail_json(msg=f"'urpmi {package}' failed: {err}")
164
165 # urpmi always have 0 for exit code if --force is used
166 if rc:
167 module.fail_json(msg=f"'urpmi {packages}' failed: {err}")
168 else:
169 module.exit_json(changed=True, msg=f"{packages} present(s)")
170 else:
171 module.exit_json(changed=False)
172
173
174 def root_option(root):
175 if root:
176 return [f"--root={root}"]
177 else:
178 return []
179
180
181 def main():
182 module = AnsibleModule(
183 argument_spec=dict(
184 state=dict(type="str", default="present", choices=["absent", "installed", "present", "removed"]),
185 update_cache=dict(type="bool", default=False),
186 force=dict(type="bool", default=True),
187 no_recommends=dict(type="bool", default=True),
188 name=dict(type="list", elements="str", required=True, aliases=["package", "pkg"]),
189 root=dict(type="str", aliases=["installroot"]),
190 ),
191 )
192
193 p = module.params
194
195 if p["update_cache"]:
196 update_package_db(module)
197
198 if p["state"] in ["installed", "present"]:
199 install_packages(module, p["name"], p["root"], p["force"], p["no_recommends"])
200
201 elif p["state"] in ["removed", "absent"]:
202 remove_packages(module, p["name"], p["root"])
203
204
205 if __name__ == "__main__":
206 main()