| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Hiroyuki Matsuo <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = r''' |
| 12 |
--- |
| 13 |
module: rabbitmq_vhost_limits |
| 14 |
author: Hiroyuki Matsuo (@h-matsuo) |
| 15 |
|
| 16 |
short_description: Manage the state of virtual host limits in RabbitMQ |
| 17 |
description: |
| 18 |
- This module sets/clears certain limits on a virtual host. |
| 19 |
- The configurable limits are I(max_connections) and I(max-queues). |
| 20 |
|
| 21 |
options: |
| 22 |
max_connections: |
| 23 |
description: |
| 24 |
- Max number of concurrent client connections. |
| 25 |
- Negative value means "no limit". |
| 26 |
- Ignored when the I(state) is C(absent). |
| 27 |
type: int |
| 28 |
default: -1 |
| 29 |
max_queues: |
| 30 |
description: |
| 31 |
- Max number of queues. |
| 32 |
- Negative value means "no limit". |
| 33 |
- Ignored when the I(state) is C(absent). |
| 34 |
type: int |
| 35 |
default: -1 |
| 36 |
node: |
| 37 |
description: |
| 38 |
- Name of the RabbitMQ Erlang node to manage. |
| 39 |
type: str |
| 40 |
state: |
| 41 |
description: |
| 42 |
- Specify whether the limits are to be set or cleared. |
| 43 |
- If set to C(absent), the limits of both I(max_connections) and I(max-queues) will be cleared. |
| 44 |
type: str |
| 45 |
default: present |
| 46 |
choices: [present, absent] |
| 47 |
vhost: |
| 48 |
description: |
| 49 |
- Name of the virtual host to manage. |
| 50 |
type: str |
| 51 |
default: / |
| 52 |
''' |
| 53 |
|
| 54 |
EXAMPLES = r''' |
| 55 |
- name: Limit both of the max number of connections and queues on the vhost '/'. |
| 56 |
community.rabbitmq.rabbitmq_vhost_limits: |
| 57 |
vhost: / |
| 58 |
max_connections: 64 |
| 59 |
max_queues: 256 |
| 60 |
state: present |
| 61 |
|
| 62 |
- name: |- |
| 63 |
Limit the max number of connections on the vhost '/'. |
| 64 |
This task implicitly clears the max number of queues limit using default value: -1. |
| 65 |
community.rabbitmq.rabbitmq_vhost_limits: |
| 66 |
vhost: / |
| 67 |
max_connections: 64 |
| 68 |
state: present |
| 69 |
|
| 70 |
- name: Clear the limits on the vhost '/'. |
| 71 |
community.rabbitmq.rabbitmq_vhost_limits: |
| 72 |
vhost: / |
| 73 |
state: absent |
| 74 |
''' |
| 75 |
|
| 76 |
RETURN = r''' # ''' |
| 77 |
|
| 78 |
|
| 79 |
import json |
| 80 |
from ansible.module_utils.basic import AnsibleModule |
| 81 |
|
| 82 |
|
| 83 |
class RabbitMqVhostLimits(object): |
| 84 |
def __init__(self, module): |
| 85 |
self._module = module |
| 86 |
self._max_connections = module.params['max_connections'] |
| 87 |
self._max_queues = module.params['max_queues'] |
| 88 |
self._node = module.params['node'] |
| 89 |
self._state = module.params['state'] |
| 90 |
self._vhost = module.params['vhost'] |
| 91 |
self._rabbitmqctl = module.get_bin_path('rabbitmqctl', True) |
| 92 |
|
| 93 |
def _exec(self, args): |
| 94 |
cmd = [self._rabbitmqctl, '-q', '-p', self._vhost] |
| 95 |
if self._node is not None: |
| 96 |
cmd.extend(['-n', self._node]) |
| 97 |
rc, out, err = self._module.run_command(cmd + args, check_rc=True) |
| 98 |
return dict(rc=rc, out=out.splitlines(), err=err.splitlines()) |
| 99 |
|
| 100 |
def list(self): |
| 101 |
exec_result = self._exec(['list_vhost_limits']) |
| 102 |
vhost_limits = exec_result['out'][0] |
| 103 |
max_connections = None |
| 104 |
max_queues = None |
| 105 |
if vhost_limits: |
| 106 |
vhost_limits = json.loads(vhost_limits) |
| 107 |
if 'max-connections' in vhost_limits: |
| 108 |
max_connections = vhost_limits['max-connections'] |
| 109 |
if 'max-queues' in vhost_limits: |
| 110 |
max_queues = vhost_limits['max-queues'] |
| 111 |
return dict( |
| 112 |
max_connections=max_connections, |
| 113 |
max_queues=max_queues |
| 114 |
) |
| 115 |
|
| 116 |
def set(self): |
| 117 |
if self._module.check_mode: |
| 118 |
return |
| 119 |
json_str = '{{"max-connections": {0}, "max-queues": {1}}}'.format(self._max_connections, self._max_queues) |
| 120 |
self._exec(['set_vhost_limits', json_str]) |
| 121 |
|
| 122 |
def clear(self): |
| 123 |
if self._module.check_mode: |
| 124 |
return |
| 125 |
self._exec(['clear_vhost_limits']) |
| 126 |
|
| 127 |
|
| 128 |
def main(): |
| 129 |
arg_spec = dict( |
| 130 |
max_connections=dict(default=-1, type='int'), |
| 131 |
max_queues=dict(default=-1, type='int'), |
| 132 |
node=dict(default=None, type='str'), |
| 133 |
state=dict(default='present', choices=['present', 'absent'], type='str'), |
| 134 |
vhost=dict(default='/', type='str') |
| 135 |
) |
| 136 |
|
| 137 |
module = AnsibleModule( |
| 138 |
argument_spec=arg_spec, |
| 139 |
supports_check_mode=True |
| 140 |
) |
| 141 |
|
| 142 |
max_connections = module.params['max_connections'] |
| 143 |
max_queues = module.params['max_queues'] |
| 144 |
node = module.params['node'] |
| 145 |
state = module.params['state'] |
| 146 |
vhost = module.params['vhost'] |
| 147 |
|
| 148 |
module_result = dict(changed=False) |
| 149 |
rabbitmq_vhost_limits = RabbitMqVhostLimits(module) |
| 150 |
current_status = rabbitmq_vhost_limits.list() |
| 151 |
|
| 152 |
if state == 'present': |
| 153 |
wanted_status = dict( |
| 154 |
max_connections=max_connections, |
| 155 |
max_queues=max_queues |
| 156 |
) |
| 157 |
else: # state == 'absent' |
| 158 |
wanted_status = dict( |
| 159 |
max_connections=None, |
| 160 |
max_queues=None |
| 161 |
) |
| 162 |
|
| 163 |
if current_status != wanted_status: |
| 164 |
module_result['changed'] = True |
| 165 |
if state == 'present': |
| 166 |
rabbitmq_vhost_limits.set() |
| 167 |
else: # state == 'absent' |
| 168 |
rabbitmq_vhost_limits.clear() |
| 169 |
|
| 170 |
module.exit_json(**module_result) |
| 171 |
|
| 172 |
|
| 173 |
if __name__ == '__main__': |
| 174 |
main() |