| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2021, Damian Dabrowski <[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 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: rabbitmq_upgrade |
| 13 |
short_description: Execute rabbitmq-upgrade commands |
| 14 |
description: |
| 15 |
- Allows to execute rabbitmq-upgrade commands |
| 16 |
author: "Damian Dabrowski (@damiandabrowski5)" |
| 17 |
version_added: '1.1.0' |
| 18 |
options: |
| 19 |
action: |
| 20 |
description: |
| 21 |
- Specify action to be executed. |
| 22 |
type: str |
| 23 |
required: true |
| 24 |
choices: ['await_online_quorum_plus_one','await_online_synchronized_mirror','post_upgrade','drain','revive'] |
| 25 |
node: |
| 26 |
description: |
| 27 |
- Erlang node name of the target rabbit node. |
| 28 |
type: str |
| 29 |
required: false |
| 30 |
default: rabbit |
| 31 |
''' |
| 32 |
|
| 33 |
EXAMPLES = r''' |
| 34 |
- name: Drain 'rabbit@node-1' node (in other words, put it into maintenance mode) |
| 35 |
community.rabbitmq.rabbitmq_upgrade: |
| 36 |
action: drain |
| 37 |
node: rabbit@node-1 |
| 38 |
''' |
| 39 |
|
| 40 |
import json |
| 41 |
from ansible.module_utils.basic import AnsibleModule |
| 42 |
|
| 43 |
|
| 44 |
class RabbitMqUpgrade(object): |
| 45 |
|
| 46 |
def __init__(self, module, action, node, result): |
| 47 |
self.module = module |
| 48 |
self.action = action |
| 49 |
self.node = node |
| 50 |
self.result = result |
| 51 |
|
| 52 |
def _exec(self, binary, args, force_exec_in_check_mode=False): |
| 53 |
if not self.module.check_mode or (self.module.check_mode and force_exec_in_check_mode): |
| 54 |
cmd = [self.module.get_bin_path(binary, True)] |
| 55 |
rc, out, err = self.module.run_command(cmd + args, check_rc=True) |
| 56 |
return out.splitlines() |
| 57 |
return list() |
| 58 |
|
| 59 |
def is_node_under_maintenance(self): |
| 60 |
cmd = self._exec('rabbitmq-diagnostics', |
| 61 |
['--formatter', 'json', 'status', '-n', self.node], True) |
| 62 |
node_status = json.loads("".join(cmd)) |
| 63 |
maint_enabled = node_status['is_under_maintenance'] |
| 64 |
if maint_enabled: |
| 65 |
return True |
| 66 |
return False |
| 67 |
|
| 68 |
def is_maint_flag_enabled(self): |
| 69 |
feature_flags = self._exec('rabbitmqctl', ['list_feature_flags', '-q'], True) |
| 70 |
for param_item in feature_flags: |
| 71 |
name, state = param_item.split('\t') |
| 72 |
if name == 'maintenance_mode_status' and state == 'enabled': |
| 73 |
return True |
| 74 |
return False |
| 75 |
|
| 76 |
def drain(self): |
| 77 |
if not self.is_maint_flag_enabled(): |
| 78 |
self.module.fail_json(msg='maintenance_mode_status feature_flag is disabled.') |
| 79 |
if not self.is_node_under_maintenance(): |
| 80 |
self._exec('rabbitmq-upgrade', ['drain', '-n', self.node]) |
| 81 |
self.result['changed'] = True |
| 82 |
|
| 83 |
def revive(self): |
| 84 |
if not self.is_maint_flag_enabled(): |
| 85 |
self.module.fail_json(msg='maintenance_mode_status feature_flag is disabled.') |
| 86 |
if self.is_node_under_maintenance(): |
| 87 |
self._exec('rabbitmq-upgrade', ['revive', '-n', self.node]) |
| 88 |
self.result['changed'] = True |
| 89 |
|
| 90 |
def await_online_quorum_plus_one(self): |
| 91 |
self._exec('rabbitmq-upgrade', ['await_online_quorum_plus_one']) |
| 92 |
self.result['changed'] = True |
| 93 |
|
| 94 |
def await_online_synchronized_mirror(self): |
| 95 |
self._exec('rabbitmq-upgrade', ['await_online_synchronized_mirror']) |
| 96 |
self.result['changed'] = True |
| 97 |
|
| 98 |
def post_upgrade(self): |
| 99 |
self._exec('rabbitmq-upgrade', ['post_upgrade']) |
| 100 |
self.result['changed'] = True |
| 101 |
|
| 102 |
|
| 103 |
def main(): |
| 104 |
arg_spec = dict( |
| 105 |
action=dict( |
| 106 |
choices=['await_online_quorum_plus_one', 'await_online_synchronized_mirror', 'post_upgrade', 'drain', 'revive'], |
| 107 |
required=True), |
| 108 |
node=dict(type='str', default='rabbit') |
| 109 |
) |
| 110 |
module = AnsibleModule( |
| 111 |
argument_spec=arg_spec, |
| 112 |
supports_check_mode=True |
| 113 |
) |
| 114 |
|
| 115 |
action = module.params['action'] |
| 116 |
node = module.params['node'] |
| 117 |
result = dict(changed=False) |
| 118 |
|
| 119 |
rabbitmq_upgrade = RabbitMqUpgrade(module, action, node, result) |
| 120 |
|
| 121 |
getattr(rabbitmq_upgrade, action)() |
| 122 |
|
| 123 |
module.exit_json(**result) |
| 124 |
|
| 125 |
|
| 126 |
if __name__ == '__main__': |
| 127 |
main() |