Debian Codegraph @

ansible_collections/kubernetes/core/plugins/modules/helm_plugin.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2020, Ansible Project
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10
11 DOCUMENTATION = r"""
12 ---
13 module: helm_plugin
14 short_description: Manage Helm plugins
15 version_added: 1.0.0
16 author:
17 - Abhijeet Kasurde (@Akasurde)
18 requirements:
19 - "helm >= 3.0.0 (https://github.com/helm/helm/releases)"
20 description:
21 - Manages Helm plugins.
22 options:
23 #Helm options
24 state:
25 description:
26 - If C(state=present) the Helm plugin will be installed.
27 - If C(state=latest) the Helm plugin will be updated. Added in version 2.3.0.
28 - If C(state=absent) the Helm plugin will be removed.
29 choices: [ absent, present, latest ]
30 default: present
31 type: str
32 plugin_name:
33 description:
34 - Name of Helm plugin.
35 - Required only if C(state=absent) or C(state=latest).
36 type: str
37 plugin_path:
38 description:
39 - Plugin path to a plugin on your local file system or a url of a remote VCS repo.
40 - If plugin path from file system is provided, make sure that tar is present on remote
41 machine and not on Ansible controller.
42 - Required only if C(state=present).
43 type: str
44 plugin_version:
45 description:
46 - Plugin version to install. If this is not specified, the latest version is installed.
47 - Ignored when C(state=absent) or C(state=latest).
48 required: false
49 type: str
50 version_added: 2.3.0
51 verify:
52 description:
53 - Verify the plugin signature before installing.
54 - This option requires helm version >= 4.0.0
55 - Used with I(state=present).
56 type: bool
57 default: true
58 version_added: 6.4.0
59 extends_documentation_fragment:
60 - kubernetes.core.helm_common_options
61 """
62
63 EXAMPLES = r"""
64 - name: Install Helm env plugin
65 kubernetes.core.helm_plugin:
66 plugin_path: https://github.com/adamreese/helm-env
67 state: present
68
69 - name: Install Helm plugin from local filesystem
70 kubernetes.core.helm_plugin:
71 plugin_path: https://domain/path/to/plugin.tar.gz
72 state: present
73
74 - name: Remove Helm env plugin
75 kubernetes.core.helm_plugin:
76 plugin_name: env
77 state: absent
78
79 - name: Install Helm plugin with a specific version
80 kubernetes.core.helm_plugin:
81 plugin_version: 2.0.1
82 plugin_path: https://domain/path/to/plugin.tar.gz
83 state: present
84
85 - name: Update Helm plugin
86 kubernetes.core.helm_plugin:
87 plugin_name: secrets
88 state: latest
89 """
90
91 RETURN = r"""
92 stdout:
93 type: str
94 description: Full `helm` command stdout, in case you want to display it or examine the event log
95 returned: always
96 sample: ''
97 stderr:
98 type: str
99 description: Full `helm` command stderr, in case you want to display it or examine the event log
100 returned: always
101 sample: ''
102 command:
103 type: str
104 description: Full `helm` command built by this module, in case you want to re-run the command outside the module or debug a problem.
105 returned: always
106 sample: helm plugin list ...
107 msg:
108 type: str
109 description: Info about successful command
110 returned: always
111 sample: "Plugin installed successfully"
112 rc:
113 type: int
114 description: Helm plugin command return code
115 returned: always
116 sample: 1
117 """
118
119 import copy
120
121 from ansible_collections.kubernetes.core.plugins.module_utils.helm import (
122 AnsibleHelmModule,
123 parse_helm_plugin_list,
124 )
125 from ansible_collections.kubernetes.core.plugins.module_utils.helm_args_common import (
126 HELM_AUTH_ARG_SPEC,
127 HELM_AUTH_MUTUALLY_EXCLUSIVE,
128 )
129 from ansible_collections.kubernetes.core.plugins.module_utils.version import (
130 LooseVersion,
131 )
132
133
134 def argument_spec():
135 arg_spec = copy.deepcopy(HELM_AUTH_ARG_SPEC)
136 arg_spec.update(
137 dict(
138 plugin_path=dict(
139 type="str",
140 ),
141 plugin_name=dict(
142 type="str",
143 ),
144 plugin_version=dict(
145 type="str",
146 ),
147 state=dict(
148 type="str",
149 default="present",
150 choices=["present", "absent", "latest"],
151 ),
152 verify=dict(
153 type="bool",
154 default=True,
155 ),
156 )
157 )
158 return arg_spec
159
160
161 def mutually_exclusive():
162 mutually_ex = copy.deepcopy(HELM_AUTH_MUTUALLY_EXCLUSIVE)
163 mutually_ex.append(("plugin_name", "plugin_path"))
164 return mutually_ex
165
166
167 def main():
168 module = AnsibleHelmModule(
169 argument_spec=argument_spec(),
170 supports_check_mode=True,
171 required_if=[
172 ("state", "present", ("plugin_path",)),
173 ("state", "absent", ("plugin_name",)),
174 ("state", "latest", ("plugin_name",)),
175 ],
176 mutually_exclusive=mutually_exclusive(),
177 )
178
179 # Validate helm version >= 3.0.0
180 module.validate_helm_version()
181
182 state = module.params.get("state")
183
184 helm_cmd_common = module.get_helm_binary() + " plugin"
185
186 if state == "present":
187 helm_cmd_common += " install %s" % module.params.get("plugin_path")
188 plugin_version = module.params.get("plugin_version")
189 verify = module.params.get("verify")
190 if plugin_version is not None:
191 helm_cmd_common += " --version=%s" % plugin_version
192 if not verify:
193 helm_version = module.get_helm_version()
194 if LooseVersion(helm_version) < LooseVersion("4.0.0"):
195 module.warn(
196 "verify parameter requires helm >= 4.0.0, current version is {0}".format(
197 helm_version
198 )
199 )
200 else:
201 helm_cmd_common += " --verify=false"
202 if not module.check_mode:
203 rc, out, err = module.run_helm_command(
204 helm_cmd_common, fails_on_error=False
205 )
206 else:
207 rc, out, err = (0, "", "")
208
209 if rc == 1 and "plugin already exists" in err:
210 module.exit_json(
211 failed=False,
212 changed=False,
213 msg="Plugin already exists",
214 command=helm_cmd_common,
215 stdout=out,
216 stderr=err,
217 rc=rc,
218 )
219 elif rc == 0:
220 module.exit_json(
221 failed=False,
222 changed=True,
223 msg="Plugin installed successfully",
224 command=helm_cmd_common,
225 stdout=out,
226 stderr=err,
227 rc=rc,
228 )
229 else:
230 module.fail_json(
231 msg="Failure when executing Helm command.",
232 command=helm_cmd_common,
233 stdout=out,
234 stderr=err,
235 rc=rc,
236 )
237 elif state == "absent":
238 plugin_name = module.params.get("plugin_name")
239 rc, output, err, command = module.get_helm_plugin_list()
240 plugins = parse_helm_plugin_list(output=output.splitlines())
241
242 if not plugins:
243 module.exit_json(
244 failed=False,
245 changed=False,
246 msg="Plugin not found or is already uninstalled",
247 command=command,
248 stdout=output,
249 stderr=err,
250 rc=rc,
251 )
252
253 if all(plugin["name"] != plugin_name for plugin in plugins):
254 module.exit_json(
255 failed=False,
256 changed=False,
257 msg="Plugin not found or is already uninstalled",
258 command=command,
259 stdout=output,
260 stderr=err,
261 rc=rc,
262 )
263
264 helm_uninstall_cmd = "%s uninstall %s" % (helm_cmd_common, plugin_name)
265 if not module.check_mode:
266 rc, out, err = module.run_helm_command(
267 helm_uninstall_cmd, fails_on_error=False
268 )
269 else:
270 rc, out, err = (0, "", "")
271
272 if rc == 0:
273 module.exit_json(
274 changed=True,
275 msg="Plugin uninstalled successfully",
276 command=helm_uninstall_cmd,
277 stdout=out,
278 stderr=err,
279 rc=rc,
280 )
281 module.fail_json(
282 msg="Failed to get Helm plugin uninstall",
283 command=helm_uninstall_cmd,
284 stdout=out,
285 stderr=err,
286 rc=rc,
287 )
288 elif state == "latest":
289 plugin_name = module.params.get("plugin_name")
290 rc, output, err, command = module.get_helm_plugin_list()
291 plugins = parse_helm_plugin_list(output=output.splitlines())
292
293 if not plugins:
294 module.exit_json(
295 failed=False,
296 changed=False,
297 msg="Plugin not found",
298 command=command,
299 stdout=output,
300 stderr=err,
301 rc=rc,
302 )
303
304 if all(plugin["name"] != plugin_name for plugin in plugins):
305 module.exit_json(
306 failed=False,
307 changed=False,
308 msg="Plugin not found",
309 command=command,
310 stdout=output,
311 stderr=err,
312 rc=rc,
313 )
314
315 helm_update_cmd = "%s update %s" % (helm_cmd_common, plugin_name)
316 if not module.check_mode:
317 rc, out, err = module.run_helm_command(
318 helm_update_cmd, fails_on_error=False
319 )
320 else:
321 rc, out, err = (0, "", "")
322
323 if rc == 0:
324 module.exit_json(
325 changed=True,
326 msg="Plugin updated successfully",
327 command=helm_update_cmd,
328 stdout=out,
329 stderr=err,
330 rc=rc,
331 )
332 module.fail_json(
333 msg="Failed to get Helm plugin update",
334 command=helm_update_cmd,
335 stdout=out,
336 stderr=err,
337 rc=rc,
338 )
339
340
341 if __name__ == "__main__":
342 main()