1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

elasticsearch_plugin: rewrite module to not use unsupported parameters (#2839)

* elasticsearch_plugin: rewrite module to not use unsupported parameters (#1785)

Avoid using parameters when they are not needed (#1785)

* elasticsearch_plugin: add version only during plugin installation and parse plugin name for its removal

* elasticsearch_plugin: join command args before running it
This commit is contained in:
Thierno IB. BARRY 2016-09-27 00:18:42 +02:00 committed by Matt Clay
parent 781dd9c459
commit c0cfd1db45

View file

@ -39,7 +39,7 @@ options:
description: description:
- Desired state of a plugin. - Desired state of a plugin.
required: False required: False
choices: [present, absent] choices: ["present", "absent"]
default: present default: present
url: url:
description: description:
@ -92,6 +92,10 @@ EXAMPLES = '''
- elasticsearch_plugin: state=absent name="mobz/elasticsearch-head" - elasticsearch_plugin: state=absent name="mobz/elasticsearch-head"
''' '''
PACKAGE_STATE_MAP = dict(
present="install",
absent="remove"
)
def parse_plugin_repo(string): def parse_plugin_repo(string):
elements = string.split("/") elements = string.split("/")
@ -111,11 +115,9 @@ def parse_plugin_repo(string):
return repo return repo
def is_plugin_present(plugin_dir, working_dir): def is_plugin_present(plugin_dir, working_dir):
return os.path.isdir(os.path.join(working_dir, plugin_dir)) return os.path.isdir(os.path.join(working_dir, plugin_dir))
def parse_error(string): def parse_error(string):
reason = "reason: " reason = "reason: "
try: try:
@ -123,48 +125,11 @@ def parse_error(string):
except ValueError: except ValueError:
return string return string
def install_plugin(module, plugin_bin, plugin_name, version, url, proxy_host, proxy_port, timeout):
cmd_args = [plugin_bin, PACKAGE_STATE_MAP["present"], plugin_name]
def main(): if version:
name = name + '/' + version
package_state_map = dict(
present="install",
absent="remove"
)
module = AnsibleModule(
argument_spec=dict(
name=dict(required=True),
state=dict(default="present", choices=package_state_map.keys()),
url=dict(default=None),
timeout=dict(default="1m"),
plugin_bin=dict(default="/usr/share/elasticsearch/bin/plugin", type="path"),
plugin_dir=dict(default="/usr/share/elasticsearch/plugins/", type="path"),
proxy_host=dict(default=None),
proxy_port=dict(default=None),
version=dict(default=None)
)
)
name = module.params["name"]
state = module.params["state"]
url = module.params["url"]
timeout = module.params["timeout"]
plugin_bin = module.params["plugin_bin"]
plugin_dir = module.params["plugin_dir"]
proxy_host = module.params["proxy_host"]
proxy_port = module.params["proxy_port"]
version = module.params["version"]
present = is_plugin_present(parse_plugin_repo(name), plugin_dir)
# skip if the state is correct
if (present and state == "present") or (state == "absent" and not present):
module.exit_json(changed=False, name=name)
if (version):
name = name + '/' + version
cmd_args = [plugin_bin, package_state_map[state], name]
if proxy_host and proxy_port: if proxy_host and proxy_port:
cmd_args.append("-DproxyHost=%s -DproxyPort=%s" % (proxy_host, proxy_port)) cmd_args.append("-DproxyHost=%s -DproxyPort=%s" % (proxy_host, proxy_port))
@ -182,9 +147,62 @@ def main():
if rc != 0: if rc != 0:
reason = parse_error(out) reason = parse_error(out)
module.fail_json(msg=reason) module.fail_json(msg=reason)
return True, cmd, out, err
module.exit_json(changed=True, cmd=cmd, name=name, state=state, url=url, timeout=timeout, stdout=out, stderr=err) def remove_plugin(module, plugin_bin, plugin_name):
cmd_args = [plugin_bin, PACKAGE_STATE_MAP["absent"], parse_plugin_repo(plugin_name)]
cmd = " ".join(cmd_args)
rc, out, err = module.run_command(cmd)
if rc != 0:
reason = parse_error(out)
module.fail_json(msg=reason)
return True, cmd, out, err
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(required=True),
state=dict(default="present", choices=PACKAGE_STATE_MAP.keys()),
url=dict(default=None),
timeout=dict(default="1m"),
plugin_bin=dict(default="/usr/share/elasticsearch/bin/plugin", type="path"),
plugin_dir=dict(default="/usr/share/elasticsearch/plugins/", type="path"),
proxy_host=dict(default=None),
proxy_port=dict(default=None),
version=dict(default=None)
)
)
name = module.params["name"]
state = module.params["state"]
url = module.params["url"]
timeout = module.params["timeout"]
plugin_bin = module.params["plugin_bin"]
plugin_dir = module.params["plugin_dir"]
proxy_host = module.params["proxy_host"]
proxy_port = module.params["proxy_port"]
version = module.params["version"]
present = is_plugin_present(parse_plugin_repo(name), plugin_dir)
# skip if the state is correct
if (present and state == "present") or (state == "absent" and not present):
module.exit_json(changed=False, name=name, state=state)
if state == "present":
changed, cmd, out, err = install_plugin(module, plugin_bin, name, version, url, proxy_host, proxy_port, timeout)
elif state == "absent":
changed, cmd, out, err = remove_plugin(module, plugin_bin, name)
module.exit_json(changed=changed, cmd=cmd, name=name, state=state, url=url, timeout=timeout, stdout=out, stderr=err)
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
main() if __name__ == '__main__':
main()