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:
parent
781dd9c459
commit
c0cfd1db45
1 changed files with 64 additions and 46 deletions
|
@ -39,7 +39,7 @@ options:
|
|||
description:
|
||||
- Desired state of a plugin.
|
||||
required: False
|
||||
choices: [present, absent]
|
||||
choices: ["present", "absent"]
|
||||
default: present
|
||||
url:
|
||||
description:
|
||||
|
@ -92,6 +92,10 @@ EXAMPLES = '''
|
|||
- elasticsearch_plugin: state=absent name="mobz/elasticsearch-head"
|
||||
'''
|
||||
|
||||
PACKAGE_STATE_MAP = dict(
|
||||
present="install",
|
||||
absent="remove"
|
||||
)
|
||||
|
||||
def parse_plugin_repo(string):
|
||||
elements = string.split("/")
|
||||
|
@ -111,11 +115,9 @@ def parse_plugin_repo(string):
|
|||
|
||||
return repo
|
||||
|
||||
|
||||
def is_plugin_present(plugin_dir, working_dir):
|
||||
return os.path.isdir(os.path.join(working_dir, plugin_dir))
|
||||
|
||||
|
||||
def parse_error(string):
|
||||
reason = "reason: "
|
||||
try:
|
||||
|
@ -123,18 +125,49 @@ def parse_error(string):
|
|||
except ValueError:
|
||||
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]
|
||||
|
||||
if version:
|
||||
name = name + '/' + version
|
||||
|
||||
if proxy_host and proxy_port:
|
||||
cmd_args.append("-DproxyHost=%s -DproxyPort=%s" % (proxy_host, proxy_port))
|
||||
|
||||
if url:
|
||||
cmd_args.append("--url %s" % url)
|
||||
|
||||
if timeout:
|
||||
cmd_args.append("--timeout %s" % timeout)
|
||||
|
||||
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 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():
|
||||
|
||||
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()),
|
||||
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"),
|
||||
|
@ -159,32 +192,17 @@ def main():
|
|||
|
||||
# skip if the state is correct
|
||||
if (present and state == "present") or (state == "absent" and not present):
|
||||
module.exit_json(changed=False, name=name)
|
||||
module.exit_json(changed=False, name=name, state=state)
|
||||
|
||||
if (version):
|
||||
name = name + '/' + version
|
||||
if state == "present":
|
||||
changed, cmd, out, err = install_plugin(module, plugin_bin, name, version, url, proxy_host, proxy_port, timeout)
|
||||
|
||||
cmd_args = [plugin_bin, package_state_map[state], name]
|
||||
elif state == "absent":
|
||||
changed, cmd, out, err = remove_plugin(module, plugin_bin, name)
|
||||
|
||||
if proxy_host and proxy_port:
|
||||
cmd_args.append("-DproxyHost=%s -DproxyPort=%s" % (proxy_host, proxy_port))
|
||||
|
||||
if url:
|
||||
cmd_args.append("--url %s" % url)
|
||||
|
||||
if timeout:
|
||||
cmd_args.append("--timeout %s" % timeout)
|
||||
|
||||
cmd = " ".join(cmd_args)
|
||||
|
||||
rc, out, err = module.run_command(cmd)
|
||||
|
||||
if rc != 0:
|
||||
reason = parse_error(out)
|
||||
module.fail_json(msg=reason)
|
||||
|
||||
module.exit_json(changed=True, cmd=cmd, name=name, state=state, url=url, timeout=timeout, stdout=out, stderr=err)
|
||||
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 *
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
Loading…
Reference in a new issue