Skip to content

Commit

Permalink
monit - invoke run_command passing list (#3821) (#3832)
Browse files Browse the repository at this point in the history
* monit - invoke run_command passing list

* added changelog fragment

* fixed unit test

* further adjustments

* fixed handling of command_args

* better handling of command_args

(cherry picked from commit 52d4907)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
  • Loading branch information
patchback[bot] and russoz committed Dec 2, 2021
1 parent fe09516 commit fad1220
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/3821-monit-run-list.yaml
@@ -0,0 +1,2 @@
minor_changes:
- monit - calling ``run_command`` with arguments as ``list`` instead of ``str`` (https://github.com/ansible-collections/community.general/pull/3821).
13 changes: 7 additions & 6 deletions plugins/modules/monitoring/monit.py
Expand Up @@ -122,7 +122,7 @@ def monit_version(self):
return self._monit_version

def _get_monit_version(self):
rc, out, err = self.module.run_command('%s -V' % self.monit_bin_path, check_rc=True)
rc, out, err = self.module.run_command([self.monit_bin_path, '-V'], check_rc=True)
version_line = out.split('\n')[0]
raw_version = re.search(r"([0-9]+\.){1,2}([0-9]+)?", version_line).group()
return raw_version, tuple(map(int, raw_version.split('.')))
Expand All @@ -140,7 +140,7 @@ def exit_success(self, state):

@property
def command_args(self):
return "-B" if self.monit_version() > (5, 18) else ""
return ["-B"] if self.monit_version() > (5, 18) else []

def get_status(self, validate=False):
"""Return the status of the process in monit.
Expand All @@ -149,7 +149,7 @@ def get_status(self, validate=False):
"""
monit_command = "validate" if validate else "status"
check_rc = False if validate else True # 'validate' always has rc = 1
command = ' '.join([self.monit_bin_path, monit_command, self.command_args, self.process_name])
command = [self.monit_bin_path, monit_command] + self.command_args + [self.process_name]
rc, out, err = self.module.run_command(command, check_rc=check_rc)
return self._parse_status(out, err)

Expand Down Expand Up @@ -182,15 +182,16 @@ def _parse_status(self, output, err):
return status

def is_process_present(self):
rc, out, err = self.module.run_command('%s summary %s' % (self.monit_bin_path, self.command_args), check_rc=True)
command = [self.monit_bin_path, 'summary'] + self.command_args
rc, out, err = self.module.run_command(command, check_rc=True)
return bool(re.findall(r'\b%s\b' % self.process_name, out))

def is_process_running(self):
return self.get_status().is_ok

def run_command(self, command):
"""Runs a monit command, and returns the new status."""
return self.module.run_command('%s %s %s' % (self.monit_bin_path, command, self.process_name), check_rc=True)
return self.module.run_command([self.monit_bin_path, command, self.process_name], check_rc=True)

def wait_for_status_change(self, current_status):
running_status = self.get_status()
Expand Down Expand Up @@ -228,7 +229,7 @@ def wait_for_monit_to_stop_pending(self, current_status=None):
return current_status

def reload(self):
rc, out, err = self.module.run_command('%s reload' % self.monit_bin_path)
rc, out, err = self.module.run_command([self.monit_bin_path, 'reload'])
if rc != 0:
self.exit_fail('monit reload failed', stdout=out, stderr=err)
self.exit_success(state='reloaded')
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/monitoring/test_monit.py
Expand Up @@ -40,7 +40,7 @@ def test_change_state_success(self):
with self.assertRaises(AnsibleExitJson):
self.monit.stop()
self.module.fail_json.assert_not_called()
self.module.run_command.assert_called_with('monit stop processX', check_rc=True)
self.module.run_command.assert_called_with(['monit', 'stop', 'processX'], check_rc=True)

def test_change_state_fail(self):
with self.patch_status([monit.Status.OK] * 3):
Expand Down

0 comments on commit fad1220

Please sign in to comment.