-
Notifications
You must be signed in to change notification settings - Fork 697
/
tdnf.py
371 lines (295 loc) · 9.92 KB
/
tdnf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (C) 2021 VMware, Inc. All Rights Reserved.
#
# GNU General Public License v3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
#
""" tdnf ansible module for Photon OS """
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
---
module: tdnf
short_description: Tiny DNF package manager
description:
- Manages rpm packages in VMware Photon OS.
version_added: "2.12.1"
options:
name:
description:
- A package name, like C(foo), or multiple packages, like C(foo, bar).
aliases:
- pkg
type: list
elements: str
state:
description:
- Indicates the desired package(s) state.
- C(present) ensures the package(s) is/are present.
- C(absent) ensures the package(s) is/are absent.
- C(latest) ensures the package(s) is/are present and the latest version(s).
- C(removed) ensures the package(s) is/are removed.
- C(installed) ensures the package(s) is/are installed.
type: str
default: present
choices: ['present', 'installed', 'absent', 'removed', 'latest']
update_cache:
description:
- Update repo metadata cache. Can be run with other steps or on it's own.
type: bool
default: 'no'
upgrade:
description:
- Upgrade all installed packages to their latest version.
type: bool
default: 'no'
enablerepo:
description:
- I(Repoid) of repositories to enable for the install/update operation.
When specifying multiple repos, separate them with a ",".
type: list
elements: str
disablerepo:
description:
- I(Repoid) of repositories to disable for the install/update operation.
When specifying multiple repos, separate them with a ",".
type: list
elements: str
conf_file:
description:
- The tdnf configuration file to use for the transaction.
type: str
disable_gpg_check:
description:
- Whether to disable the GPG checking of signatures of packages being
installed. Has an effect only if state is I(present) or I(latest).
type: bool
default: 'no'
installroot:
description:
- Specifies an alternative installroot, relative to which all packages
will be installed.
type: str
default: '/'
security_severity:
description:
- Specifies the CVSS v3 score above which to install updates for packages
type: str
releasever:
description:
- Specifies an alternative release from which all packages will be
installed.
type: str
exclude:
description:
- Package name(s) to exclude when state=present, or latest. This can be a
list or a comma separated string.
type: list
elements: str
author:
- Anish Swaminathan (@suezzelur) <anishs@vmware.com>
- Shreenidhi Shedi (@sshedi) <sshedi@vmware.com>
notes:
- '"name" and "upgrade" are mutually exclusive.'
- When used with a `loop:` each package will be processed individually, it is much more efficient to pass the list directly to the `name` option.
"""
EXAMPLES = """
# Update repositories and install "foo" package
- tdnf:
name: ['foo']
update_cache: yes
# Update repositories and install "foo" and "bar" packages
- tdnf:
name: ['foo', 'bar']
update_cache: yes
# Remove "foo" package
- tdnf:
name: ['foo']
state: absent
# Remove "foo" and "bar" packages
- tdnf:
name: ['foo', 'bar']
state: absent
# Install the package "foo"
- tdnf:
name: ['foo']
state: present
# Install the packages "foo" and "bar"
- tdnf:
name: ['foo', 'bar']
state: present
# Update repositories and update package "foo" to latest version
- tdnf:
name: ['foo']
state: latest
update_cache: yes
# Update repositories and update packages "foo" and "bar" to latest versions
- tdnf:
name: ['foo', 'bar']
state: latest
update_cache: yes
# Update all installed packages to the latest versions
- tdnf:
upgrade: yes
# Update repositories as a separate step
- tdnf:
update_cache: yes
"""
RETURN = """
stdout:
description: output from tdnf
returned: success, when needed
type: str
stderr:
description: error output from tdnf
returned: success, when needed
type: str
rc:
description: tdnf command return value
returned: 0 on success
type: int
"""
from ansible.module_utils.basic import AnsibleModule
def prep_tdnf_cmd(cmd, p_dict):
"""Prepare tdnf command based on given configs"""
if p_dict["excludelist"]:
cmd = "%s --exclude %s" % (cmd, ",".join(p_dict["excludelist"]))
if p_dict["disable_gpg_check"]:
cmd = "%s --nogpgcheck" % cmd
if p_dict["releasever"]:
cmd = "%s --releasever %s" % (cmd, p_dict["releasever"])
if p_dict["conf_file"]:
cmd = "%s -c %s" % (cmd, p_dict["conf_file"])
if p_dict["installroot"] != "/":
cmd = "%s --installroot %s" % (cmd, p_dict["installroot"])
for repo in p_dict["enablerepolist"]:
cmd = "%s --enablerepo=%s" % (cmd, repo)
for repo in p_dict["disablerepolist"]:
cmd = "%s --disablerepo=%s" % (cmd, repo)
if p_dict["security_severity"]:
cmd = "%s --sec-severity %s" % (cmd, p_dict["security_severity"])
return cmd
def exec_cmd(module, params):
"""
Run the final command
get_out is a special value from update_package_db
if it's set, we just update the db cache and exit
"""
get_out = params.get("get_out", False)
check_rc = params.get("check_rc", False)
rc, out, err = module.run_command(params["cmd"], check_rc=check_rc)
if rc:
module.fail_json(msg=params["msg_f"], stdout=out, stderr=err)
elif ("get_out" not in params and rc == 0) or get_out:
module.exit_json(changed=True, msg=params["msg_s"], stdout=out, stderr=err)
def update_package_db(module, get_out, p_dict):
"""Update tdnf cache metadata"""
cmd = "%s makecache --refresh -q" % (p_dict["tdnf"])
cmd = prep_tdnf_cmd(cmd, p_dict)
params = {
"cmd": cmd,
"msg_s": "Updated package db",
"msg_f": "Could not update package db",
"get_out": get_out,
}
exec_cmd(module, params)
def upgrade_packages(module, p_dict):
"""Upgrade all packages"""
cmd = "%s upgrade -y" % (p_dict["tdnf"])
cmd = prep_tdnf_cmd(cmd, p_dict)
params = {
"cmd": cmd,
"msg_s": "Upgraded packages",
"msg_f": "Failed to upgrade packages",
}
exec_cmd(module, params)
def install_packages(module, p_dict):
"""Install given packages"""
packages = " ".join(p_dict["pkglist"])
cmd = "%s install -y" % (p_dict["tdnf"])
cmd = prep_tdnf_cmd(cmd, p_dict)
cmd = "%s %s" % (cmd, packages)
params = {
"cmd": cmd,
"msg_s": "Installed %s package(s)" % (packages),
"msg_f": "Failed to install %s" % (packages),
}
exec_cmd(module, params)
def remove_packages(module, p_dict):
"""Erase/Uninstall packages"""
packages = " ".join(p_dict["pkglist"])
cmd = "%s erase -y %s" % (p_dict["tdnf"], packages)
params = {
"cmd": cmd,
"msg_s": "Removed %s package(s)" % (packages),
"msg_f": "Failed to remove %s package(s)" % (packages),
}
exec_cmd(module, params)
def convert_to_list(input_list):
"""Convert nested list into flat list"""
flat_list = []
if not input_list:
return flat_list
for sublist in input_list:
if not isinstance(sublist, list):
flat_list.append(sublist)
continue
for item in sublist:
flat_list.append(item)
return flat_list
def main():
"""Trigger point function"""
choices = ["present", "installed", "absent", "removed", "latest"]
module = AnsibleModule(
argument_spec=dict(
state=dict(default="present", choices=choices),
name=dict(type="list", elements="str", aliases=["pkg"]),
update_cache=dict(default=False, type="bool"),
upgrade=dict(default=False, type="bool"),
enablerepo=dict(type="list", default=[], elements="str"),
disablerepo=dict(type="list", default=[], elements="str"),
disable_gpg_check=dict(type="bool", default=False),
exclude=dict(type="list", default=[], elements="str"),
installroot=dict(type="str", default="/"),
security_severity=dict(type="str", default=None),
releasever=dict(default=None),
conf_file=dict(type="str", default=None),
),
required_one_of=[["name", "update_cache", "upgrade", "security_severity"]],
mutually_exclusive=[["name", "upgrade"], ["name", "security_severity"]],
supports_check_mode=True,
)
# Set LANG env since we parse stdout
module.run_command_environ_update = dict(
LANG="C", LC_ALL="C", LC_MESSAGES="C", LC_CTYPE="C"
)
p_dict = module.params
pkglist = convert_to_list(p_dict["name"])
enablerepolist = convert_to_list(p_dict["enablerepo"])
disablerepolist = convert_to_list(p_dict["disablerepo"])
excludelist = convert_to_list(p_dict["exclude"])
p_dict["tdnf"] = module.get_bin_path("tdnf", required=True)
p_dict["pkglist"] = pkglist
p_dict["enablerepolist"] = enablerepolist
p_dict["disablerepolist"] = disablerepolist
p_dict["excludelist"] = excludelist
# normalize the state parameter
if p_dict["state"] in ["present", "installed", "latest"]:
p_dict["state"] = "present"
if p_dict["state"] in ["absent", "removed"]:
p_dict["state"] = "absent"
if p_dict["update_cache"]:
get_out = True
for key in ["name", "upgrade", "security_severity"]:
if p_dict[key]:
get_out = False
break
update_package_db(module, get_out, p_dict)
if p_dict["upgrade"]:
upgrade_packages(module, p_dict)
if p_dict["state"] == "present":
install_packages(module, p_dict)
else:
remove_packages(module, p_dict)
if __name__ == "__main__":
main()