From 4e140bb80e15b85da3ff724f30c5d4342e0dd544 Mon Sep 17 00:00:00 2001 From: Mathew Davies Date: Sun, 11 Jan 2015 16:42:45 +0000 Subject: [PATCH 1/8] Add Elasticsearch plugin module --- packaging/elasticsearch_plugin.py | 160 ++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 packaging/elasticsearch_plugin.py diff --git a/packaging/elasticsearch_plugin.py b/packaging/elasticsearch_plugin.py new file mode 100644 index 00000000000..38303686e8d --- /dev/null +++ b/packaging/elasticsearch_plugin.py @@ -0,0 +1,160 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import os + +from ansible.module_utils.basic import * + +""" +Ansible module to manage elasticsearch plugins +(c) 2015, Mathew Davies + +This file is part of Ansible + +Ansible is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Ansible is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. +You should have received a copy of the GNU General Public License +along with Ansible. If not, see . +""" + +DOCUMENTATION = ''' +--- +module: elasticsearch_plugin +short_description: Manage Elasticsearch plugins +description: + - Manages Elasticsearch plugins. +version_added: "" +author: Mathew Davies (@ThePixelDeveloper) +options: + name: + description: + - Name of the plugin to install + required: True + state: + description: + - Desired state of a plugin. + required: False + choices: [present, absent] + default: present + url: + description: + - Set exact URL to download the plugin from + required: False + timeout: + description: + - Timeout setting: 30s, 1m, 1h... (1m by default) + required: False + plugin_bin: + description: + - Location of the plugin binary + required: False + default: /usr/share/elasticsearch/bin/plugin + plugin_dir: + description: + - Your configured plugin directory specified in Elasticsearch + required: False + default: /usr/share/elasticsearch/plugins/ + version: + description: + - Version of the plugin to be installed. +''' + +EXAMPLES = ''' +# Install Elasticsearch head plugin +- elasticsearch_plugin: state=present name="mobz/elasticsearch-head" +''' + + +def parse_plugin_repo(string): + elements = string.split("/") + + # We first consider the simplest form: pluginname + repo = elements[0] + + # We consider the form: username/pluginname + if len(elements) > 1: + repo = elements[1] + + # remove elasticsearch- prefix + # remove es- prefix + for string in ("elasticsearch-", "es-"): + if repo.startswith(string): + return repo[len(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: " + return string[string.index(reason) + len(reason):].strip() + + +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()), + url=dict(default=None), + timeout=dict(default="1m"), + plugin_bin=dict(default="/usr/share/elasticsearch/bin/plugin"), + plugin_dir=dict(default="/usr/share/elasticsearch/plugins/"), + version=dict(default=None) + ) + ) + + plugin_bin = module.params["plugin_bin"] + plugin_dir = module.params["plugin_dir"] + name = module.params["name"] + state = module.params["state"] + url = module.params["url"] + timeout = module.params["timeout"] + version = module.params["version"] + + present = is_plugin_present(parse_plugin_repo(name), plugin_dir) + + print state + + # 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 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) + +if __name__ == "__main__": + main() From ebbe84b2d6fda94f3d40c7bbbd95ba48bfddb65d Mon Sep 17 00:00:00 2001 From: Mathew Davies Date: Thu, 16 Jul 2015 20:38:58 +0100 Subject: [PATCH 2/8] Document defaults --- packaging/elasticsearch_plugin.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packaging/elasticsearch_plugin.py b/packaging/elasticsearch_plugin.py index 38303686e8d..818ebb00484 100644 --- a/packaging/elasticsearch_plugin.py +++ b/packaging/elasticsearch_plugin.py @@ -47,6 +47,7 @@ options: description: - Set exact URL to download the plugin from required: False + default: None timeout: description: - Timeout setting: 30s, 1m, 1h... (1m by default) @@ -64,6 +65,7 @@ options: version: description: - Version of the plugin to be installed. + default: None ''' EXAMPLES = ''' From 93e59297f0f71e85d18482aa2abe1733720c5d66 Mon Sep 17 00:00:00 2001 From: Mathew Davies Date: Thu, 16 Jul 2015 20:55:50 +0100 Subject: [PATCH 3/8] Remove debugging line --- packaging/elasticsearch_plugin.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/packaging/elasticsearch_plugin.py b/packaging/elasticsearch_plugin.py index 818ebb00484..3c0d6124e82 100644 --- a/packaging/elasticsearch_plugin.py +++ b/packaging/elasticsearch_plugin.py @@ -130,8 +130,6 @@ def main(): version = module.params["version"] present = is_plugin_present(parse_plugin_repo(name), plugin_dir) - - print state # skip if the state is correct if (present and state == "present") or (state == "absent" and not present): From 045f0908e2002a98e8b50200530a32872dcb87d5 Mon Sep 17 00:00:00 2001 From: Mathew Davies Date: Thu, 16 Jul 2015 20:56:05 +0100 Subject: [PATCH 4/8] Add required property to version documentation --- packaging/elasticsearch_plugin.py | 1 + 1 file changed, 1 insertion(+) diff --git a/packaging/elasticsearch_plugin.py b/packaging/elasticsearch_plugin.py index 3c0d6124e82..15821eb6adc 100644 --- a/packaging/elasticsearch_plugin.py +++ b/packaging/elasticsearch_plugin.py @@ -65,6 +65,7 @@ options: version: description: - Version of the plugin to be installed. + required: False default: None ''' From 394053ff2bab918d905a44ba11704aa0ebf39124 Mon Sep 17 00:00:00 2001 From: Mathew Davies Date: Thu, 16 Jul 2015 20:56:45 +0100 Subject: [PATCH 5/8] Add default documentation for timeout --- packaging/elasticsearch_plugin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packaging/elasticsearch_plugin.py b/packaging/elasticsearch_plugin.py index 15821eb6adc..4838d478ef4 100644 --- a/packaging/elasticsearch_plugin.py +++ b/packaging/elasticsearch_plugin.py @@ -50,8 +50,9 @@ options: default: None timeout: description: - - Timeout setting: 30s, 1m, 1h... (1m by default) + - Timeout setting: 30s, 1m, 1h... required: False + default: 1m plugin_bin: description: - Location of the plugin binary From 6fa1809ec4007faf623b955e8a811e87aa87c3b9 Mon Sep 17 00:00:00 2001 From: Mathew Davies Date: Thu, 16 Jul 2015 21:00:15 +0100 Subject: [PATCH 6/8] Move ansible util import to the bottom of the module --- packaging/elasticsearch_plugin.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packaging/elasticsearch_plugin.py b/packaging/elasticsearch_plugin.py index 4838d478ef4..34b028accaf 100644 --- a/packaging/elasticsearch_plugin.py +++ b/packaging/elasticsearch_plugin.py @@ -3,8 +3,6 @@ import os -from ansible.module_utils.basic import * - """ Ansible module to manage elasticsearch plugins (c) 2015, Mathew Davies @@ -158,5 +156,6 @@ def main(): module.exit_json(changed=True, cmd=cmd, name=name, state=state, url=url, timeout=timeout, stdout=out, stderr=err) -if __name__ == "__main__": - main() +from ansible.module_utils.basic import * + +main() From fb42f6effcbed7980ff9c1db9a5f85ffa3d1183e Mon Sep 17 00:00:00 2001 From: Mathew Davies Date: Thu, 16 Jul 2015 21:12:42 +0100 Subject: [PATCH 7/8] Note that the plugin can't be updated once installed --- packaging/elasticsearch_plugin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packaging/elasticsearch_plugin.py b/packaging/elasticsearch_plugin.py index 34b028accaf..c263388b9e6 100644 --- a/packaging/elasticsearch_plugin.py +++ b/packaging/elasticsearch_plugin.py @@ -63,7 +63,8 @@ options: default: /usr/share/elasticsearch/plugins/ version: description: - - Version of the plugin to be installed. + - Version of the plugin to be installed. + If plugin exists with previous version, it will NOT be updated required: False default: None ''' From 2d2ea412aeef207af26906bd78a30b29486dbfd9 Mon Sep 17 00:00:00 2001 From: Mathew Davies Date: Thu, 16 Jul 2015 21:15:15 +0100 Subject: [PATCH 8/8] Add more examples --- packaging/elasticsearch_plugin.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packaging/elasticsearch_plugin.py b/packaging/elasticsearch_plugin.py index c263388b9e6..f1053144e12 100644 --- a/packaging/elasticsearch_plugin.py +++ b/packaging/elasticsearch_plugin.py @@ -72,6 +72,12 @@ options: EXAMPLES = ''' # Install Elasticsearch head plugin - elasticsearch_plugin: state=present name="mobz/elasticsearch-head" + +# Install specific version of a plugin +- elasticsearch_plugin: state=present name="com.github.kzwang/elasticsearch-image" version="1.2.0" + +# Uninstall Elasticsearch head plugin +- elasticsearch_plugin: state=absent name="mobz/elasticsearch-head" '''