require version >= 5.21.0

Prior to this version the status output was different
This commit is contained in:
Simon Kelly 2020-10-15 08:47:27 +02:00
parent 89dbb918a0
commit 8e8ea4eacf
2 changed files with 13 additions and 24 deletions

View file

@ -54,6 +54,8 @@ STATE_COMMAND_MAP = {
'restarted': 'restart' 'restarted': 'restart'
} }
MIN_VERSION = (5, 21)
class Monit(object): class Monit(object):
def __init__(self, module, monit_bin_path, service_name, timeout): def __init__(self, module, monit_bin_path, service_name, timeout):
@ -74,26 +76,12 @@ class Monit(object):
self._monit_version = int(version[0]), int(version[1]) self._monit_version = int(version[0]), int(version[1])
return self._monit_version return self._monit_version
def is_version_higher_than_5_18(self): def check_version(self):
return self.monit_version() > (5, 18) if self.monit_version() < MIN_VERSION:
min_version = '.'.join(str(v) for v in MIN_VERSION)
@property self.module.fail_json(msg='Monit version not compatible with module. Install version >= %s' % min_version)
def summary_command(self):
return 'summary -B' if self.is_version_higher_than_5_18() else 'summary'
def parse(self, parts): def parse(self, parts):
if self.is_version_higher_than_5_18():
return self.parse_current(parts)
else:
return self.parse_older_versions(parts)
def parse_older_versions(self, parts):
if len(parts) > 2 and parts[0].lower() == 'process' and parts[1] == "'%s'" % self.process_name:
return ' '.join(parts[2:]).lower()
else:
return ''
def parse_current(self, parts):
if len(parts) > 2 and parts[2].lower() == 'process' and parts[0] == self.process_name: if len(parts) > 2 and parts[2].lower() == 'process' and parts[0] == self.process_name:
return ''.join(parts[1]).lower() return ''.join(parts[1]).lower()
else: else:
@ -101,7 +89,7 @@ class Monit(object):
def get_status(self): def get_status(self):
"""Return the status of the process in monit, or the empty string if not present.""" """Return the status of the process in monit, or the empty string if not present."""
rc, out, err = self.module.run_command('%s %s' % (self.monit_bin_path, self.summary_command), check_rc=True) rc, out, err = self.module.run_command('%s %s' % (self.monit_bin_path, 'summary -B'), check_rc=True)
for line in out.split('\n'): for line in out.split('\n'):
# Sample output lines: # Sample output lines:
# Process 'name' Running # Process 'name' Running
@ -193,6 +181,7 @@ def main():
timeout = module.params['timeout'] timeout = module.params['timeout']
monit = Monit(module, module.get_bin_path('monit', True), name, timeout) monit = Monit(module, module.get_bin_path('monit', True), name, timeout)
monit.check_version()
def exit_if_check_mode(): def exit_if_check_mode():
if module.check_mode: if module.check_mode:

View file

@ -25,15 +25,15 @@ class MonitTest(unittest.TestCase):
self.module = MagicMock() self.module = MagicMock()
self.module.exit_json.side_effect = AnsibleExitJson(Exception) self.module.exit_json.side_effect = AnsibleExitJson(Exception)
self.monit = monit.Monit(self.module, 'monit', 'processX', 1) self.monit = monit.Monit(self.module, 'monit', 'processX', 1)
self.version_patch = mock.patch.object(self.monit, "is_version_higher_than_5_18", return_value=True)
self.version_patch.start()
def tearDown(self):
self.version_patch.stop()
def patch_status(self, side_effect): def patch_status(self, side_effect):
return mock.patch.object(self.monit, 'get_status', side_effect=side_effect) return mock.patch.object(self.monit, 'get_status', side_effect=side_effect)
def test_min_version(self):
with mock.patch.object(self.monit, 'monit_version', return_value=(5, 20)):
self.monit.check_version()
self.module.fail_json.assert_called()
def test_change_state_success(self): def test_change_state_success(self):
with self.patch_status(['not monitored']), self.assertRaises(AnsibleExitJson): with self.patch_status(['not monitored']), self.assertRaises(AnsibleExitJson):
self.monit.stop() self.monit.stop()