~canonical-ci-engineering/ubuntu-ci-services-itself/ansible

« back to all changes in this revision

Viewing changes to library/rabbitmq_plugin

  • Committer: Package Import Robot
  • Author(s): Janos Guljas
  • Date: 2013-04-06 23:27:08 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130406232708-6rn7w7mprfbjpmmk
Tags: 1.1+dfsg-1
* New upstream release.
* Update patches disable-google-analytics.patch and 
  remove-external-image.patch to apply cleanly.
* Add remove-external-footer-image.patch to remove link on external resource.
* Add remove-external-training-references.patch:
  Training advertise contains links to external resources that may not be
  available or may be used for tracking users activity without their
  knowledge by the third-party.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# (c) 2013, Chatham Financial <oss@chathamfinancial.com>
 
5
#
 
6
# This file is part of Ansible
 
7
#
 
8
# Ansible is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation, either version 3 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# Ansible is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
 
20
 
 
21
DOCUMENTATION = '''
 
22
---
 
23
module: rabbitmq_plugin
 
24
short_description: Adds or removes users to RabbitMQ
 
25
description:
 
26
  - Enables or disables RabbitMQ plugins
 
27
version_added: "1.1"
 
28
author: Chris Hoffman
 
29
options:
 
30
  names:
 
31
    description:
 
32
      - Comma-separated list of plugin names
 
33
    required: true
 
34
    default: null
 
35
    aliases: [name]
 
36
  new_only:
 
37
    description:
 
38
      - Only enable missing plugins
 
39
      - Does not disable plugins that are not in the names list
 
40
    required: false
 
41
    default: "no"
 
42
    choices: [ "yes", "no" ]
 
43
  state:
 
44
    description:
 
45
      - Specify if pluginss are to be enabled or disabled
 
46
    required: false
 
47
    default: enabled
 
48
    choices: [enabled, disabled]
 
49
examples:
 
50
  - code: rabbitmq_plugin names=rabbitmq_management state=enabled
 
51
    description: Enables the rabbitmq_management plugin
 
52
'''
 
53
 
 
54
class RabbitMqPlugins(object):
 
55
    def __init__(self, module):
 
56
        self.module = module
 
57
        self._rabbitmq_plugins = module.get_bin_path('rabbitmq-plugins', True)
 
58
 
 
59
    def _exec(self, args, run_in_check_mode=False):
 
60
        if not self.module.check_mode or (self.module.check_mode and run_in_check_mode):
 
61
            cmd = [self._rabbitmq_plugins]
 
62
            rc, out, err = self.module.run_command(cmd + args, check_rc=True)
 
63
            return out.splitlines()
 
64
        return list()
 
65
 
 
66
    def get_all(self):
 
67
        return self._exec(['list', '-E', '-m'], True)
 
68
 
 
69
    def enable(self, name):
 
70
        self._exec(['enable', name])
 
71
 
 
72
    def disable(self, name):
 
73
        self._exec(['disable', name])
 
74
 
 
75
def main():
 
76
    arg_spec = dict(
 
77
        names=dict(required=True, aliases=['name']),
 
78
        new_only=dict(default='no', type='bool'),
 
79
        state=dict(default='enabled', choices=['enabled', 'disabled'])
 
80
    )
 
81
    module = AnsibleModule(
 
82
        argument_spec=arg_spec,
 
83
        supports_check_mode=True
 
84
    )
 
85
 
 
86
    names = module.params['names'].split(',')
 
87
    new_only = module.params['new_only']
 
88
    state = module.params['state']
 
89
 
 
90
    rabbitmq_plugins = RabbitMqPlugins(module)
 
91
    enabled_plugins = rabbitmq_plugins.get_all()
 
92
 
 
93
    enabled = []
 
94
    disabled = []
 
95
    if state == 'enabled':
 
96
        if not new_only:
 
97
            for plugin in enabled_plugins:
 
98
                if plugin not in names:
 
99
                    rabbitmq_plugins.disable(plugin)
 
100
                    disabled.append(plugin)
 
101
 
 
102
        for name in names:
 
103
            if name not in enabled_plugins:
 
104
                rabbitmq_plugins.enable(name)
 
105
                enabled.append(name)
 
106
    else:
 
107
        for plugin in enabled_plugins:
 
108
            if plugin in names:
 
109
                rabbitmq_plugins.disable(plugin)
 
110
                disabled.append(plugin)
 
111
 
 
112
    changed = len(enabled) > 0 or len(disabled) > 0
 
113
    module.exit_json(changed=changed, enabled=enabled, disabled=disabled)
 
114
 
 
115
# this is magic, see lib/ansible/module_common.py
 
116
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
 
117
main()