Debian Codegraph @

lib/ansible/module_utils/yumdnf.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # # Copyright: (c) 2012, Red Hat, Inc
4 # Written by Seth Vidal <skvidal at fedoraproject.org>
5 # Contributing Authors:
6 # - Ansible Core Team
7 # - Eduard Snesarev (@verm666)
8 # - Berend De Schouwer (@berenddeschouwer)
9 # - Abhijeet Kasurde (@Akasurde)
10 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
11
12 from __future__ import annotations
13
14 from abc import ABCMeta, abstractmethod
15
16 yumdnf_argument_spec = dict(
17 argument_spec=dict(
18 allow_downgrade=dict(type='bool', default=False),
19 allowerasing=dict(default=False, type="bool"),
20 autoremove=dict(type='bool', default=False),
21 best=dict(type="bool"),
22 bugfix=dict(required=False, type='bool', default=False),
23 cacheonly=dict(type='bool', default=False),
24 conf_file=dict(type='str'),
25 disable_excludes=dict(type='str', default=None),
26 disable_gpg_check=dict(type='bool', default=False),
27 disable_plugin=dict(type='list', elements='str', default=[]),
28 disablerepo=dict(type='list', elements='str', default=[]),
29 download_only=dict(type='bool', default=False),
30 download_dir=dict(type='str', default=None),
31 enable_plugin=dict(type='list', elements='str', default=[]),
32 enablerepo=dict(type='list', elements='str', default=[]),
33 exclude=dict(type='list', elements='str', default=[]),
34 installroot=dict(type='str', default="/"),
35 install_weak_deps=dict(type='bool', default=True),
36 list=dict(type='str'),
37 name=dict(type='list', elements='str', aliases=['pkg'], default=[]),
38 nobest=dict(type="bool"),
39 releasever=dict(default=None),
40 security=dict(type='bool', default=False),
41 skip_broken=dict(type='bool', default=False),
42 # removed==absent, installed==present, these are accepted as aliases
43 state=dict(type='str', default=None, choices=['absent', 'installed', 'latest', 'present', 'removed']),
44 update_cache=dict(type='bool', default=False, aliases=['expire-cache']),
45 update_only=dict(required=False, default="no", type='bool'),
46 validate_certs=dict(type='bool', default=True),
47 sslverify=dict(type='bool', default=True),
48 lock_timeout=dict(type='int', default=30),
49 ),
50 required_one_of=[['name', 'list', 'update_cache']],
51 mutually_exclusive=[['name', 'list'], ['best', 'nobest']],
52 supports_check_mode=True,
53 )
54
55
56 class YumDnf(metaclass=ABCMeta):
57 """
58 Abstract class that handles the population of instance variables that should
59 be identical between both YUM and DNF modules because of the feature parity
60 and shared argument spec
61 """
62
63 def __init__(self, module):
64
65 self.module = module
66
67 self.allow_downgrade = self.module.params['allow_downgrade']
68 self.allowerasing = self.module.params['allowerasing']
69 self.autoremove = self.module.params['autoremove']
70 self.best = self.module.params['best']
71 self.bugfix = self.module.params['bugfix']
72 self.cacheonly = self.module.params['cacheonly']
73 self.conf_file = self.module.params['conf_file']
74 self.disable_excludes = self.module.params['disable_excludes']
75 self.disable_gpg_check = self.module.params['disable_gpg_check']
76 self.disable_plugin = self.module.params['disable_plugin']
77 self.disablerepo = self.module.params.get('disablerepo', [])
78 self.download_only = self.module.params['download_only']
79 self.download_dir = self.module.params['download_dir']
80 self.enable_plugin = self.module.params['enable_plugin']
81 self.enablerepo = self.module.params.get('enablerepo', [])
82 self.exclude = self.module.params['exclude']
83 self.installroot = self.module.params['installroot']
84 self.install_weak_deps = self.module.params['install_weak_deps']
85 self.list = self.module.params['list']
86 self.names = [p.strip() for p in self.module.params['name']]
87 self.nobest = self.module.params['nobest']
88 self.releasever = self.module.params['releasever']
89 self.security = self.module.params['security']
90 self.skip_broken = self.module.params['skip_broken']
91 self.state = self.module.params['state']
92 self.update_only = self.module.params['update_only']
93 self.update_cache = self.module.params['update_cache']
94 self.validate_certs = self.module.params['validate_certs']
95 self.sslverify = self.module.params['sslverify']
96 self.lock_timeout = self.module.params['lock_timeout']
97
98 # It's possible someone passed a comma separated string since it used
99 # to be a string type, so we should handle that
100 self.names = self.listify_comma_sep_strings_in_list(self.names)
101 self.disablerepo = self.listify_comma_sep_strings_in_list(self.disablerepo)
102 self.enablerepo = self.listify_comma_sep_strings_in_list(self.enablerepo)
103 self.exclude = self.listify_comma_sep_strings_in_list(self.exclude)
104
105 # Fail if someone passed a space separated string
106 # https://github.com/ansible/ansible/issues/46301
107 for name in self.names:
108 if ' ' in name and not any(spec in name for spec in ['@', '>', '<', '=']):
109 module.fail_json(
110 msg='It appears that a space separated string of packages was passed in '
111 'as an argument. To operate on several packages, pass a comma separated '
112 'string of packages or a list of packages.'
113 )
114
115 # Sanity checking for autoremove
116 if self.state is None:
117 if self.autoremove:
118 self.state = "absent"
119 else:
120 self.state = "present"
121
122 if self.autoremove and (self.state != "absent"):
123 self.module.fail_json(
124 msg="Autoremove should be used alone or with state=absent",
125 results=[],
126 )
127
128 def listify_comma_sep_strings_in_list(self, some_list):
129 """
130 method to accept a list of strings as the parameter, find any strings
131 in that list that are comma separated, remove them from the list and add
132 their comma separated elements to the original list
133 """
134 new_list = []
135 remove_from_original_list = []
136 for element in some_list:
137 if ',' in element:
138 remove_from_original_list.append(element)
139 new_list.extend([e.strip() for e in element.split(',')])
140
141 for element in remove_from_original_list:
142 some_list.remove(element)
143
144 some_list.extend(new_list)
145
146 if some_list == [""]:
147 return []
148
149 return some_list
150
151 @abstractmethod
152 def run(self):
153 raise NotImplementedError