From f813b23e241700bcdbb39bff11f46d2058f187b6 Mon Sep 17 00:00:00 2001 From: John Dewey Date: Tue, 19 Nov 2013 00:33:58 -0800 Subject: [PATCH 1/4] Added module to handle rabbit policies [1] Used quite a bit of the existing rabbit modules provied by @chrishoffman. [1] https://www.rabbitmq.com/parameters.html --- messaging/rabbitmq_policy | 149 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 messaging/rabbitmq_policy diff --git a/messaging/rabbitmq_policy b/messaging/rabbitmq_policy new file mode 100644 index 00000000000..f463ff1381d --- /dev/null +++ b/messaging/rabbitmq_policy @@ -0,0 +1,149 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# (c) 2013, John Dewey +# +# 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: rabbitmq_policy +short_description: Manage the state of policies in RabbitMQ +description: + - Manage the state of a virtual host in RabbitMQ +version_added: "1.0" +author: John Dewey +options: + name: + description: + - The name of the policy to manage. + required: true + default: null + vhost: + description: + - The name of the vhost to apply to. + required: false + default: / + pattern: + description: + - A regex of queues to apply the policy to. + required: true + default: null + params: + description: + - A json dict describing the policy. + required: true + default: null + priority: + description: + - The priority of the policy. + required: false + default: 0 + node: + description: + - Erlang node name of the rabbit we wish to configure. + required: false + default: rabbit + state: + description: + - The state of the policy. + default: present + choices: [present, absent] +''' + +EXAMPLES = ''' +# Ensure that the /vhost contains the HA policy. +- rabbitmq_policy: name=/ params='{"ha-mode":"all"}' policy=HA pattern='.*' +''' +class RabbitMqPolicy(object): + def __init__(self, module, name): + self._module = module + self._name = name + self._vhost = module.params['vhost'] + self._pattern = module.params['pattern'] + self._params = module.params['params'] + self._priority = module.params['priority'] + self._node = module.params['node'] + self._rabbitmqctl = module.get_bin_path('rabbitmqctl', 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._rabbitmqctl, '-q', '-n', self._node] + args.insert(1, '-p') + args.insert(2, self._vhost) + rc, out, err = self._module.run_command(cmd + args, check_rc=True) + return out.splitlines() + return list() + + def list(self): + policies = self._exec(['list_policies'], True) + + for policy in policies: + policy_name = policy.split('\t')[1] + if policy_name == self._name: + return True + return False + + def set(self): + args = ['set_policy'] + args.append(self._name) + args.append(self._pattern) + args.append(self._params) + args.append('--priority') + args.append(self._priority) + return self._exec(args) + + def clear(self): + return self._exec(['clear_policy', self._name]) + + +def main(): + arg_spec = dict( + name=dict(required=True), + vhost=dict(default='/'), + pattern=dict(required=True), + params=dict(required=True), + priority=dict(default='0'), + node=dict(default='rabbit'), + state=dict(default='present', choices=['present', 'absent']), + ) + + module = AnsibleModule( + argument_spec=arg_spec, + supports_check_mode=True + ) + + name = module.params['name'] + state = module.params['state'] + rabbitmq_policy = RabbitMqPolicy(module, name) + + changed = False + if rabbitmq_policy.list(): + if state == 'absent': + rabbitmq_policy.clear() + changed = True + else: + changed = False + elif state == 'present': + rabbitmq_policy.set() + changed = True + + module.exit_json(changed=changed, name=name, state=state) + +# this is magic, see lib/ansible/module_common.py +#<> +main() From 52b125a9da23284b45d9e15633acf0ddb12019d3 Mon Sep 17 00:00:00 2001 From: John Dewey Date: Tue, 19 Nov 2013 12:00:09 -0800 Subject: [PATCH 2/4] Cleaned up docs just a smidge Mostly added periods where missed, and corrected /vhost spacing. --- messaging/rabbitmq_policy | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/messaging/rabbitmq_policy b/messaging/rabbitmq_policy index f463ff1381d..d4e68c2d373 100644 --- a/messaging/rabbitmq_policy +++ b/messaging/rabbitmq_policy @@ -22,9 +22,9 @@ DOCUMENTATION = ''' --- module: rabbitmq_policy -short_description: Manage the state of policies in RabbitMQ +short_description: Manage the state of policies in RabbitMQ. description: - - Manage the state of a virtual host in RabbitMQ + - Manage the state of a virtual host in RabbitMQ. version_added: "1.0" author: John Dewey options: @@ -66,7 +66,7 @@ options: ''' EXAMPLES = ''' -# Ensure that the /vhost contains the HA policy. +# Ensure that the / vhost contains the HA policy. - rabbitmq_policy: name=/ params='{"ha-mode":"all"}' policy=HA pattern='.*' ''' class RabbitMqPolicy(object): From cfa588c1a3cdbf8d261a0ff1abc3e08f2a5ca411 Mon Sep 17 00:00:00 2001 From: John Dewey Date: Wed, 20 Nov 2013 17:07:08 -0800 Subject: [PATCH 3/4] The setting of params via dict or string by tags Updated per @mpdehaan suggestions. * Rather than passing in JSON, supply the policy options as a hash or string. * Bumped version_added to 1.5. --- messaging/rabbitmq_policy | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/messaging/rabbitmq_policy b/messaging/rabbitmq_policy index d4e68c2d373..26d3a3c96a9 100644 --- a/messaging/rabbitmq_policy +++ b/messaging/rabbitmq_policy @@ -25,7 +25,7 @@ module: rabbitmq_policy short_description: Manage the state of policies in RabbitMQ. description: - Manage the state of a virtual host in RabbitMQ. -version_added: "1.0" +version_added: "1.5" author: John Dewey options: name: @@ -43,9 +43,9 @@ options: - A regex of queues to apply the policy to. required: true default: null - params: + tags: description: - - A json dict describing the policy. + - A dict or string describing the policy. required: true default: null priority: @@ -66,8 +66,14 @@ options: ''' EXAMPLES = ''' -# Ensure that the / vhost contains the HA policy. -- rabbitmq_policy: name=/ params='{"ha-mode":"all"}' policy=HA pattern='.*' +- name: ensure the default vhost contains the HA policy via a dict + rabbitmq_policy: name=HA pattern='.*' + args: + tags: + "ha-mode": all + +- name: ensure the default vhost contains the HA policy + rabbitmq_policy: name=HA pattern='.*' tags="ha-mode=all" ''' class RabbitMqPolicy(object): def __init__(self, module, name): @@ -75,7 +81,7 @@ class RabbitMqPolicy(object): self._name = name self._vhost = module.params['vhost'] self._pattern = module.params['pattern'] - self._params = module.params['params'] + self._tags = module.params['tags'] self._priority = module.params['priority'] self._node = module.params['node'] self._rabbitmqctl = module.get_bin_path('rabbitmqctl', True) @@ -98,11 +104,22 @@ class RabbitMqPolicy(object): return True return False + def _dump_json(self, d): + import json + return json.dumps(d) + + def _make_params(self): + if type(self._tags) is dict: + return self._dump_json(self._tags) + else: + d = dict([x.split('=') for x in self._tags.split()]) + return self._dump_json(d) + def set(self): args = ['set_policy'] args.append(self._name) args.append(self._pattern) - args.append(self._params) + args.append(self._make_params()) args.append('--priority') args.append(self._priority) return self._exec(args) @@ -116,7 +133,7 @@ def main(): name=dict(required=True), vhost=dict(default='/'), pattern=dict(required=True), - params=dict(required=True), + tags=dict(required=True), priority=dict(default='0'), node=dict(default='rabbit'), state=dict(default='present', choices=['present', 'absent']), From 0bbb9307471f9d9f81b296c85d2f21bf835b40c0 Mon Sep 17 00:00:00 2001 From: John Dewey Date: Wed, 20 Nov 2013 20:21:04 -0800 Subject: [PATCH 4/4] Handled string/dict the ansible way --- messaging/rabbitmq_policy | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/messaging/rabbitmq_policy b/messaging/rabbitmq_policy index 26d3a3c96a9..52237f7f234 100644 --- a/messaging/rabbitmq_policy +++ b/messaging/rabbitmq_policy @@ -104,22 +104,12 @@ class RabbitMqPolicy(object): return True return False - def _dump_json(self, d): - import json - return json.dumps(d) - - def _make_params(self): - if type(self._tags) is dict: - return self._dump_json(self._tags) - else: - d = dict([x.split('=') for x in self._tags.split()]) - return self._dump_json(d) - def set(self): + import json args = ['set_policy'] args.append(self._name) args.append(self._pattern) - args.append(self._make_params()) + args.append(json.dumps(self._tags)) args.append('--priority') args.append(self._priority) return self._exec(args) @@ -133,7 +123,7 @@ def main(): name=dict(required=True), vhost=dict(default='/'), pattern=dict(required=True), - tags=dict(required=True), + tags=dict(type='dict', required=True), priority=dict(default='0'), node=dict(default='rabbit'), state=dict(default='present', choices=['present', 'absent']),