Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2016, Thierno IB. BARRY @barryib
4 # Sponsored by Polyconseil http://polyconseil.fr.
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: kibana_plugin
13 short_description: Manage Kibana plugins
14 description:
15 - This module can be used to manage Kibana plugins.
16 author: Thierno IB. BARRY (@barryib)
17 extends_documentation_fragment:
18 - community.general._attributes
19 attributes:
20 check_mode:
21 support: full
22 diff_mode:
23 support: none
24 options:
25 name:
26 description:
27 - Name of the plugin to install.
28 required: true
29 type: str
30 state:
31 description:
32 - Desired state of a plugin.
33 choices: ["present", "absent"]
34 default: present
35 type: str
36 url:
37 description:
38 - Set exact URL to download the plugin from.
39 - For local file, prefix its absolute path with C(file://).
40 type: str
41 timeout:
42 description:
43 - 'Timeout setting: V(30s), V(1m), V(1h) and so on.'
44 default: 1m
45 type: str
46 plugin_bin:
47 description:
48 - Location of the Kibana binary.
49 default: /opt/kibana/bin/kibana
50 type: path
51 plugin_dir:
52 description:
53 - Your configured plugin directory specified in Kibana.
54 default: /opt/kibana/installedPlugins/
55 type: path
56 version:
57 description:
58 - Version of the plugin to be installed.
59 - If the plugin is installed with in a previous version, it is B(not) updated unless O(force=true).
60 type: str
61 force:
62 description:
63 - Delete and re-install the plugin. It can be useful for plugins update.
64 type: bool
65 default: false
66 allow_root:
67 description:
68 - Whether to allow C(kibana) and C(kibana-plugin) to be run as root. Passes the C(--allow-root) flag to these commands.
69 type: bool
70 default: false
71 version_added: 2.3.0
72 """
73
74 EXAMPLES = r"""
75 - name: Install Elasticsearch head plugin
76 community.general.kibana_plugin:
77 state: present
78 name: elasticsearch/marvel
79
80 - name: Install specific version of a plugin
81 community.general.kibana_plugin:
82 state: present
83 name: elasticsearch/marvel
84 version: '2.3.3'
85
86 - name: Uninstall Elasticsearch head plugin
87 community.general.kibana_plugin:
88 state: absent
89 name: elasticsearch/marvel
90 """
91
92 RETURN = r"""
93 cmd:
94 description: The launched command during plugin management (install / remove).
95 returned: success
96 type: str
97 name:
98 description: The plugin name to install or remove.
99 returned: success
100 type: str
101 url:
102 description: The URL from where the plugin is installed from.
103 returned: success
104 type: str
105 timeout:
106 description: The timeout for plugin download.
107 returned: success
108 type: str
109 state:
110 description: The state for the managed plugin.
111 returned: success
112 type: str
113 """
114
115 import os
116
117 from ansible.module_utils.basic import AnsibleModule
118
119 from ansible_collections.community.general.plugins.module_utils._version import LooseVersion
120
121 PACKAGE_STATE_MAP = dict(present="--install", absent="--remove")
122
123
124 def parse_plugin_repo(string):
125 elements = string.split("/")
126
127 # We first consider the simplest form: pluginname
128 repo = elements[0]
129
130 # We consider the form: username/pluginname
131 if len(elements) > 1:
132 repo = elements[1]
133
134 # remove elasticsearch- prefix
135 # remove es- prefix
136 for string in ("elasticsearch-", "es-"):
137 if repo.startswith(string):
138 return repo[len(string) :]
139
140 return repo
141
142
143 def is_plugin_present(plugin_dir, working_dir):
144 return os.path.isdir(os.path.join(working_dir, plugin_dir))
145
146
147 def parse_error(string):
148 reason = "reason: "
149 try:
150 return string[string.index(reason) + len(reason) :].strip()
151 except ValueError:
152 return string
153
154
155 def install_plugin(module, plugin_bin, plugin_name, url, timeout, allow_root, kibana_version="4.6"):
156 if LooseVersion(kibana_version) > LooseVersion("4.6"):
157 kibana_plugin_bin = os.path.join(os.path.dirname(plugin_bin), "kibana-plugin")
158 cmd_args = [kibana_plugin_bin, "install"]
159 if url:
160 cmd_args.append(url)
161 else:
162 cmd_args.append(plugin_name)
163 else:
164 cmd_args = [plugin_bin, "plugin", PACKAGE_STATE_MAP["present"], plugin_name]
165
166 if url:
167 cmd_args.extend(["--url", url])
168
169 if timeout:
170 cmd_args.extend(["--timeout", timeout])
171
172 if allow_root:
173 cmd_args.append("--allow-root")
174
175 if module.check_mode:
176 return True, " ".join(cmd_args), "check mode", ""
177
178 rc, out, err = module.run_command(cmd_args)
179 if rc != 0:
180 reason = parse_error(out)
181 module.fail_json(msg=reason)
182
183 return True, " ".join(cmd_args), out, err
184
185
186 def remove_plugin(module, plugin_bin, plugin_name, allow_root, kibana_version="4.6"):
187 if LooseVersion(kibana_version) > LooseVersion("4.6"):
188 kibana_plugin_bin = os.path.join(os.path.dirname(plugin_bin), "kibana-plugin")
189 cmd_args = [kibana_plugin_bin, "remove", plugin_name]
190 else:
191 cmd_args = [plugin_bin, "plugin", PACKAGE_STATE_MAP["absent"], plugin_name]
192
193 if allow_root:
194 cmd_args.append("--allow-root")
195
196 if module.check_mode:
197 return True, " ".join(cmd_args), "check mode", ""
198
199 rc, out, err = module.run_command(cmd_args)
200 if rc != 0:
201 reason = parse_error(out)
202 module.fail_json(msg=reason)
203
204 return True, " ".join(cmd_args), out, err
205
206
207 def get_kibana_version(module, plugin_bin, allow_root):
208 cmd_args = [plugin_bin, "--version"]
209
210 if allow_root:
211 cmd_args.append("--allow-root")
212
213 rc, out, err = module.run_command(cmd_args)
214 if rc != 0:
215 module.fail_json(msg=f"Failed to get Kibana version : {err}")
216
217 return out.strip()
218
219
220 def main():
221 module = AnsibleModule(
222 argument_spec=dict(
223 name=dict(required=True),
224 state=dict(default="present", choices=list(PACKAGE_STATE_MAP.keys())),
225 url=dict(),
226 timeout=dict(default="1m"),
227 plugin_bin=dict(default="/opt/kibana/bin/kibana", type="path"),
228 plugin_dir=dict(default="/opt/kibana/installedPlugins/", type="path"),
229 version=dict(),
230 force=dict(default=False, type="bool"),
231 allow_root=dict(default=False, type="bool"),
232 ),
233 supports_check_mode=True,
234 )
235 module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"}
236
237 name = module.params["name"]
238 state = module.params["state"]
239 url = module.params["url"]
240 timeout = module.params["timeout"]
241 plugin_bin = module.params["plugin_bin"]
242 plugin_dir = module.params["plugin_dir"]
243 version = module.params["version"]
244 force = module.params["force"]
245 allow_root = module.params["allow_root"]
246
247 changed, cmd, out, err = False, "", "", ""
248
249 kibana_version = get_kibana_version(module, plugin_bin, allow_root)
250
251 present = is_plugin_present(parse_plugin_repo(name), plugin_dir)
252
253 # skip if the state is correct
254 if (present and state == "present" and not force) or (state == "absent" and not present and not force):
255 module.exit_json(changed=False, name=name, state=state)
256
257 if version:
258 name = f"{name}/{version}"
259
260 if state == "present":
261 if force:
262 remove_plugin(module, plugin_bin, name, allow_root, kibana_version)
263 changed, cmd, out, err = install_plugin(module, plugin_bin, name, url, timeout, allow_root, kibana_version)
264
265 elif state == "absent":
266 changed, cmd, out, err = remove_plugin(module, plugin_bin, name, allow_root, kibana_version)
267
268 module.exit_json(changed=changed, cmd=cmd, name=name, state=state, url=url, timeout=timeout, stdout=out, stderr=err)
269
270
271 if __name__ == "__main__":
272 main()