2013-02-09 18:28:41 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-03-01 01:32:44 +00:00
|
|
|
# (c) 2013, Chatham Financial <oss@chathamfinancial.com>
|
2013-02-09 18:28:41 +00:00
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: rabbitmq_vhost
|
|
|
|
short_description: Manage the state of a virtual host in RabbitMQ
|
|
|
|
description:
|
|
|
|
- Manage the state of a virtual host in RabbitMQ
|
2013-03-30 19:44:34 +00:00
|
|
|
version_added: "1.1"
|
|
|
|
author: Chris Hoffman
|
2013-02-09 18:28:41 +00:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The name of the vhost to manage
|
|
|
|
required: true
|
|
|
|
default: null
|
2013-02-10 22:08:33 +00:00
|
|
|
aliases: [vhost]
|
2013-05-07 16:20:26 +00:00
|
|
|
node:
|
|
|
|
description:
|
|
|
|
- erlang node name of the rabbit we wish to configure
|
|
|
|
required: false
|
|
|
|
default: rabbit
|
2013-02-10 22:01:20 +00:00
|
|
|
tracing:
|
|
|
|
description:
|
2013-03-30 19:53:28 +00:00
|
|
|
- Enable/disable tracing for a vhost
|
2013-03-12 12:18:12 +00:00
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
2013-02-10 22:08:33 +00:00
|
|
|
aliases: [trace]
|
2013-02-09 18:28:41 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- The state of vhost
|
2013-02-10 22:01:20 +00:00
|
|
|
default: present
|
|
|
|
choices: [present, absent]
|
2013-02-09 18:28:41 +00:00
|
|
|
examples:
|
2013-02-10 22:01:20 +00:00
|
|
|
- code: 'rabbitmq_vhost: name=/test state=present'
|
2013-02-09 18:28:41 +00:00
|
|
|
description: Ensure that the vhost /test exists.
|
|
|
|
'''
|
|
|
|
|
2013-02-10 22:01:20 +00:00
|
|
|
class RabbitMqVhost(object):
|
2013-05-07 16:20:26 +00:00
|
|
|
def __init__(self, module, name, tracing, node):
|
2013-02-10 22:01:20 +00:00
|
|
|
self.module = module
|
|
|
|
self.name = name
|
|
|
|
self.tracing = tracing
|
2013-05-08 14:34:51 +00:00
|
|
|
self.node = node
|
2013-02-10 22:01:20 +00:00
|
|
|
|
|
|
|
self._tracing = False
|
|
|
|
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):
|
2013-05-26 13:51:50 +00:00
|
|
|
cmd = [self._rabbitmqctl, '-q', '-n', self.node]
|
2013-02-10 22:01:20 +00:00
|
|
|
rc, out, err = self.module.run_command(cmd + args, check_rc=True)
|
|
|
|
return out.splitlines()
|
|
|
|
return list()
|
|
|
|
|
|
|
|
def get(self):
|
|
|
|
vhosts = self._exec(['list_vhosts', 'name', 'tracing'], True)
|
|
|
|
|
|
|
|
for vhost in vhosts:
|
|
|
|
name, tracing = vhost.split('\t')
|
|
|
|
if name == self.name:
|
|
|
|
self._tracing = self.module.boolean(tracing)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def add(self):
|
|
|
|
return self._exec(['add_vhost', self.name])
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
return self._exec(['delete_vhost', self.name])
|
|
|
|
|
|
|
|
def set_tracing(self):
|
|
|
|
if self.tracing != self._tracing:
|
|
|
|
if self.tracing:
|
|
|
|
self._enable_tracing()
|
|
|
|
else:
|
|
|
|
self._disable_tracing()
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _enable_tracing(self):
|
|
|
|
return self._exec(['trace_on', '-p', self.name])
|
|
|
|
|
|
|
|
def _disable_tracing(self):
|
|
|
|
return self._exec(['trace_off', '-p', self.name])
|
|
|
|
|
2013-02-09 18:28:41 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
arg_spec = dict(
|
2013-02-10 22:08:33 +00:00
|
|
|
name=dict(required=True, aliases=['vhost']),
|
2013-02-23 21:56:45 +00:00
|
|
|
tracing=dict(default='off', aliases=['trace'], type='bool'),
|
2013-05-07 16:20:26 +00:00
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
|
|
|
node=dict(default='rabbit'),
|
2013-02-09 18:28:41 +00:00
|
|
|
)
|
|
|
|
|
2013-02-10 22:01:20 +00:00
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=arg_spec,
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
2013-02-09 18:28:41 +00:00
|
|
|
|
|
|
|
name = module.params['name']
|
2013-02-23 18:59:52 +00:00
|
|
|
tracing = module.params['tracing']
|
2013-02-09 18:28:41 +00:00
|
|
|
state = module.params['state']
|
2013-05-07 16:20:26 +00:00
|
|
|
node = module.params['node']
|
2013-02-09 18:28:41 +00:00
|
|
|
|
2013-05-07 16:20:26 +00:00
|
|
|
rabbitmq_vhost = RabbitMqVhost(module, name, tracing, node)
|
2013-02-09 18:28:41 +00:00
|
|
|
|
2013-02-10 22:01:20 +00:00
|
|
|
changed = False
|
|
|
|
if rabbitmq_vhost.get():
|
|
|
|
if state == 'absent':
|
|
|
|
rabbitmq_vhost.delete()
|
|
|
|
changed = True
|
2013-02-09 18:28:41 +00:00
|
|
|
else:
|
2013-02-10 22:01:20 +00:00
|
|
|
if rabbitmq_vhost.set_tracing():
|
|
|
|
changed = True
|
|
|
|
elif state == 'present':
|
|
|
|
rabbitmq_vhost.add()
|
|
|
|
rabbitmq_vhost.set_tracing()
|
|
|
|
changed = True
|
2013-02-09 18:28:41 +00:00
|
|
|
|
2013-02-10 22:01:20 +00:00
|
|
|
module.exit_json(changed=changed, name=name, state=state)
|
2013-02-09 18:28:41 +00:00
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
main()
|