2014-09-26 01:01:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, Chatham Financial <oss@chathamfinancial.com>
|
2017-07-29 07:20:36 +00:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:25 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: rabbitmq_plugin
|
2017-08-12 08:35:16 +00:00
|
|
|
short_description: Manage RabbitMQ plugins
|
2014-09-26 01:01:01 +00:00
|
|
|
description:
|
2017-08-12 08:35:16 +00:00
|
|
|
- Manage RabbitMQ plugins.
|
2014-09-26 01:01:01 +00:00
|
|
|
version_added: "1.1"
|
2017-08-12 08:35:16 +00:00
|
|
|
author:
|
|
|
|
- Chris Hoffman (@chrishoffman)
|
2014-09-26 01:01:01 +00:00
|
|
|
options:
|
|
|
|
names:
|
|
|
|
description:
|
2017-08-12 08:35:16 +00:00
|
|
|
- Comma-separated list of plugin names.
|
2014-09-26 01:01:01 +00:00
|
|
|
required: true
|
|
|
|
aliases: [name]
|
|
|
|
new_only:
|
|
|
|
description:
|
2017-08-12 08:35:16 +00:00
|
|
|
- Only enable missing plugins.
|
|
|
|
- Does not disable plugins that are not in the names list.
|
|
|
|
type: bool
|
2014-09-26 01:01:01 +00:00
|
|
|
default: "no"
|
|
|
|
state:
|
|
|
|
description:
|
2017-08-12 08:35:16 +00:00
|
|
|
- Specify if plugins are to be enabled or disabled.
|
2014-09-26 01:01:01 +00:00
|
|
|
default: enabled
|
|
|
|
choices: [enabled, disabled]
|
|
|
|
prefix:
|
|
|
|
description:
|
2017-08-12 08:35:16 +00:00
|
|
|
- Specify a custom install prefix to a Rabbit.
|
2014-09-26 01:01:01 +00:00
|
|
|
version_added: "1.3"
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2017-08-12 08:35:16 +00:00
|
|
|
- name: Enables the rabbitmq_management plugin
|
|
|
|
rabbitmq_plugin:
|
2016-12-01 13:59:53 +00:00
|
|
|
names: rabbitmq_management
|
|
|
|
state: enabled
|
2014-09-26 01:01:01 +00:00
|
|
|
'''
|
|
|
|
|
2017-08-12 08:35:16 +00:00
|
|
|
RETURN = '''
|
|
|
|
enabled:
|
|
|
|
description: list of plugins enabled during task run
|
|
|
|
returned: always
|
|
|
|
type: list
|
|
|
|
sample: ["rabbitmq_management"]
|
|
|
|
disabled:
|
|
|
|
description: list of plugins disabled during task run
|
|
|
|
returned: always
|
|
|
|
type: list
|
|
|
|
sample: ["rabbitmq_management"]
|
|
|
|
'''
|
|
|
|
|
2014-12-06 18:31:08 +00:00
|
|
|
import os
|
2017-08-10 09:57:11 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
2014-12-06 18:31:08 +00:00
|
|
|
|
2017-07-29 07:20:36 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
class RabbitMqPlugins(object):
|
2017-08-10 09:57:11 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
|
|
|
|
if module.params['prefix']:
|
2014-12-06 18:31:08 +00:00
|
|
|
if os.path.isdir(os.path.join(module.params['prefix'], 'bin')):
|
|
|
|
bin_path = os.path.join(module.params['prefix'], 'bin')
|
|
|
|
elif os.path.isdir(os.path.join(module.params['prefix'], 'sbin')):
|
|
|
|
bin_path = os.path.join(module.params['prefix'], 'sbin')
|
|
|
|
else:
|
|
|
|
# No such path exists.
|
2014-12-07 16:01:49 +00:00
|
|
|
raise Exception("No binary folder in prefix %s" %
|
2017-08-10 09:57:11 +00:00
|
|
|
module.params['prefix'])
|
2014-12-06 18:31:08 +00:00
|
|
|
|
|
|
|
self._rabbitmq_plugins = bin_path + "/rabbitmq-plugins"
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
|
|
|
self._rabbitmq_plugins = module.get_bin_path('rabbitmq-plugins', True)
|
|
|
|
|
|
|
|
def _exec(self, args, run_in_check_mode=False):
|
|
|
|
if not self.module.check_mode or (self.module.check_mode and run_in_check_mode):
|
|
|
|
cmd = [self._rabbitmq_plugins]
|
|
|
|
rc, out, err = self.module.run_command(cmd + args, check_rc=True)
|
|
|
|
return out.splitlines()
|
|
|
|
return list()
|
|
|
|
|
|
|
|
def get_all(self):
|
Ignore extra lines from Pivotal's RabbitMQ package
Pivotal's packaging of RabbitMQ shows a banner at the end of the plugin
listing talking about their official plugins. The start of the banner is
divided by a blank line so the changed plugin listing will now
break after the first empty line.
An example listing with the rabbitmq_management plugin enabled:
```
$ rabbitmq-plugins list -E -m
rabbitmq_management
Pivotal officially maintains and supports the plugins:
rabbitmq_auth_backend_ldap, rabbitmq_auth_mechanism_ssl,
rabbitmq_consistent_hash_exchange, rabbitmq_federation,
rabbitmq_federation_management, rabbitmq_jms_topic_exchange,
rabbitmq_management, rabbitmq_management_agent,
rabbitmq_mqtt, rabbitmq_shovel, rabbitmq_shovel_management,
and rabbitmq_stomp.
```
2015-04-30 15:26:33 +00:00
|
|
|
list_output = self._exec(['list', '-E', '-m'], True)
|
|
|
|
plugins = []
|
|
|
|
for plugin in list_output:
|
|
|
|
if not plugin:
|
|
|
|
break
|
|
|
|
plugins.append(plugin)
|
|
|
|
|
|
|
|
return plugins
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
def enable(self, name):
|
|
|
|
self._exec(['enable', name])
|
|
|
|
|
|
|
|
def disable(self, name):
|
|
|
|
self._exec(['disable', name])
|
|
|
|
|
Ignore extra lines from Pivotal's RabbitMQ package
Pivotal's packaging of RabbitMQ shows a banner at the end of the plugin
listing talking about their official plugins. The start of the banner is
divided by a blank line so the changed plugin listing will now
break after the first empty line.
An example listing with the rabbitmq_management plugin enabled:
```
$ rabbitmq-plugins list -E -m
rabbitmq_management
Pivotal officially maintains and supports the plugins:
rabbitmq_auth_backend_ldap, rabbitmq_auth_mechanism_ssl,
rabbitmq_consistent_hash_exchange, rabbitmq_federation,
rabbitmq_federation_management, rabbitmq_jms_topic_exchange,
rabbitmq_management, rabbitmq_management_agent,
rabbitmq_mqtt, rabbitmq_shovel, rabbitmq_shovel_management,
and rabbitmq_stomp.
```
2015-04-30 15:26:33 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def main():
|
|
|
|
arg_spec = dict(
|
|
|
|
names=dict(required=True, aliases=['name']),
|
|
|
|
new_only=dict(default='no', type='bool'),
|
|
|
|
state=dict(default='enabled', choices=['enabled', 'disabled']),
|
|
|
|
prefix=dict(required=False, default=None)
|
|
|
|
)
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=arg_spec,
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
2017-08-10 09:57:11 +00:00
|
|
|
result = dict()
|
2014-09-26 01:01:01 +00:00
|
|
|
names = module.params['names'].split(',')
|
|
|
|
new_only = module.params['new_only']
|
|
|
|
state = module.params['state']
|
|
|
|
|
|
|
|
rabbitmq_plugins = RabbitMqPlugins(module)
|
|
|
|
enabled_plugins = rabbitmq_plugins.get_all()
|
|
|
|
|
|
|
|
enabled = []
|
|
|
|
disabled = []
|
|
|
|
if state == 'enabled':
|
|
|
|
if not new_only:
|
|
|
|
for plugin in enabled_plugins:
|
|
|
|
if plugin not in names:
|
|
|
|
rabbitmq_plugins.disable(plugin)
|
|
|
|
disabled.append(plugin)
|
|
|
|
|
|
|
|
for name in names:
|
|
|
|
if name not in enabled_plugins:
|
|
|
|
rabbitmq_plugins.enable(name)
|
|
|
|
enabled.append(name)
|
|
|
|
else:
|
|
|
|
for plugin in enabled_plugins:
|
|
|
|
if plugin in names:
|
|
|
|
rabbitmq_plugins.disable(plugin)
|
|
|
|
disabled.append(plugin)
|
|
|
|
|
2017-08-10 09:57:11 +00:00
|
|
|
result['changed'] = len(enabled) > 0 or len(disabled) > 0
|
|
|
|
result['enabled'] = enabled
|
|
|
|
result['disabled'] = disabled
|
|
|
|
module.exit_json(**result)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2016-12-05 16:24:50 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|