Skip to content

Commit 2cf81c4

Browse files
committed
Merge pull request ansible#195 from ThePixelDeveloper/feature/elasticsearch
Add Elasticsearch plugin module
2 parents ff2386f + 2d2ea41 commit 2cf81c4

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

packaging/elasticsearch_plugin.py

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
import os
5+
6+
"""
7+
Ansible module to manage elasticsearch plugins
8+
(c) 2015, Mathew Davies <[email protected]>
9+
10+
This file is part of Ansible
11+
12+
Ansible is free software: you can redistribute it and/or modify
13+
it under the terms of the GNU General Public License as published by
14+
the Free Software Foundation, either version 3 of the License, or
15+
(at your option) any later version.
16+
17+
Ansible is distributed in the hope that it will be useful,
18+
but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
GNU General Public License for more details.
21+
You should have received a copy of the GNU General Public License
22+
along with Ansible. If not, see <http://www.gnu.org/licenses/>.
23+
"""
24+
25+
DOCUMENTATION = '''
26+
---
27+
module: elasticsearch_plugin
28+
short_description: Manage Elasticsearch plugins
29+
description:
30+
- Manages Elasticsearch plugins.
31+
version_added: ""
32+
author: Mathew Davies (@ThePixelDeveloper)
33+
options:
34+
name:
35+
description:
36+
- Name of the plugin to install
37+
required: True
38+
state:
39+
description:
40+
- Desired state of a plugin.
41+
required: False
42+
choices: [present, absent]
43+
default: present
44+
url:
45+
description:
46+
- Set exact URL to download the plugin from
47+
required: False
48+
default: None
49+
timeout:
50+
description:
51+
- Timeout setting: 30s, 1m, 1h...
52+
required: False
53+
default: 1m
54+
plugin_bin:
55+
description:
56+
- Location of the plugin binary
57+
required: False
58+
default: /usr/share/elasticsearch/bin/plugin
59+
plugin_dir:
60+
description:
61+
- Your configured plugin directory specified in Elasticsearch
62+
required: False
63+
default: /usr/share/elasticsearch/plugins/
64+
version:
65+
description:
66+
- Version of the plugin to be installed.
67+
If plugin exists with previous version, it will NOT be updated
68+
required: False
69+
default: None
70+
'''
71+
72+
EXAMPLES = '''
73+
# Install Elasticsearch head plugin
74+
- elasticsearch_plugin: state=present name="mobz/elasticsearch-head"
75+
76+
# Install specific version of a plugin
77+
- elasticsearch_plugin: state=present name="com.github.kzwang/elasticsearch-image" version="1.2.0"
78+
79+
# Uninstall Elasticsearch head plugin
80+
- elasticsearch_plugin: state=absent name="mobz/elasticsearch-head"
81+
'''
82+
83+
84+
def parse_plugin_repo(string):
85+
elements = string.split("/")
86+
87+
# We first consider the simplest form: pluginname
88+
repo = elements[0]
89+
90+
# We consider the form: username/pluginname
91+
if len(elements) > 1:
92+
repo = elements[1]
93+
94+
# remove elasticsearch- prefix
95+
# remove es- prefix
96+
for string in ("elasticsearch-", "es-"):
97+
if repo.startswith(string):
98+
return repo[len(string):]
99+
100+
return repo
101+
102+
103+
def is_plugin_present(plugin_dir, working_dir):
104+
return os.path.isdir(os.path.join(working_dir, plugin_dir))
105+
106+
107+
def parse_error(string):
108+
reason = "reason: "
109+
return string[string.index(reason) + len(reason):].strip()
110+
111+
112+
def main():
113+
114+
package_state_map = dict(
115+
present="--install",
116+
absent="--remove"
117+
)
118+
119+
module = AnsibleModule(
120+
argument_spec=dict(
121+
name=dict(required=True),
122+
state=dict(default="present", choices=package_state_map.keys()),
123+
url=dict(default=None),
124+
timeout=dict(default="1m"),
125+
plugin_bin=dict(default="/usr/share/elasticsearch/bin/plugin"),
126+
plugin_dir=dict(default="/usr/share/elasticsearch/plugins/"),
127+
version=dict(default=None)
128+
)
129+
)
130+
131+
plugin_bin = module.params["plugin_bin"]
132+
plugin_dir = module.params["plugin_dir"]
133+
name = module.params["name"]
134+
state = module.params["state"]
135+
url = module.params["url"]
136+
timeout = module.params["timeout"]
137+
version = module.params["version"]
138+
139+
present = is_plugin_present(parse_plugin_repo(name), plugin_dir)
140+
141+
# skip if the state is correct
142+
if (present and state == "present") or (state == "absent" and not present):
143+
module.exit_json(changed=False, name=name)
144+
145+
if (version):
146+
name = name + '/' + version
147+
148+
cmd_args = [plugin_bin, package_state_map[state], name]
149+
150+
if url:
151+
cmd_args.append("--url %s" % url)
152+
153+
if timeout:
154+
cmd_args.append("--timeout %s" % timeout)
155+
156+
cmd = " ".join(cmd_args)
157+
158+
rc, out, err = module.run_command(cmd)
159+
160+
if rc != 0:
161+
reason = parse_error(out)
162+
module.fail_json(msg=reason)
163+
164+
module.exit_json(changed=True, cmd=cmd, name=name, state=state, url=url, timeout=timeout, stdout=out, stderr=err)
165+
166+
from ansible.module_utils.basic import *
167+
168+
main()

0 commit comments

Comments
 (0)