Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f83e5ca
monit: triggering CI tests
russoz Oct 12, 2025
014f599
monit: add verbosity
russoz Oct 13, 2025
7253753
provisional fix for unit test
russoz Oct 13, 2025
9e7540b
add -vv to status run
russoz Oct 13, 2025
fea2650
add debug task to check for SIGTERM
russoz Oct 13, 2025
bc7d125
add debug task to check for SIGTERM
russoz Oct 13, 2025
b4ec74b
add 5 second sleep before state change
russoz Oct 13, 2025
1e12a31
remove debugging task
russoz Oct 13, 2025
122e644
remove extra verbosity
russoz Oct 13, 2025
fbb7e36
revert module code
russoz Oct 13, 2025
872f38e
make it 10 seconds
russoz Oct 13, 2025
ffbc775
remove unstable
russoz Oct 13, 2025
b1915a2
increase sleep time to 20s
russoz Oct 13, 2025
a564186
pausing for 30s +/- 10s (random)
russoz Oct 13, 2025
acfbd4d
pausing for 30s +/- 20s (random)
russoz Oct 13, 2025
51fa2db
add mode 0700 to monitrc file
russoz Oct 13, 2025
e6bd101
monitrc was not really being templated
russoz Oct 13, 2025
c26a2e2
more debugging
russoz Oct 14, 2025
d6183c7
sanity
russoz Oct 14, 2025
4c013bc
more debugging
russoz Oct 14, 2025
833a97e
more debugging
russoz Oct 14, 2025
9e91384
more debugging
russoz Oct 14, 2025
b67d951
more debugging
russoz Oct 14, 2025
2d27ffa
more debugging: AA
russoz Oct 14, 2025
75e7ab0
more debugging: BB+CC
russoz Oct 14, 2025
8893623
more debugging: DD
russoz Oct 14, 2025
5452292
more debugging: EE
russoz Oct 14, 2025
b9eff21
more debugging: FF
russoz Oct 14, 2025
4821ca6
more debugging: FF (# EE)
russoz Oct 14, 2025
6d846db
more debugging: GG
russoz Oct 14, 2025
5d30b32
more debugging: HH
russoz Oct 14, 2025
5e68347
more debugging: II1-3 (# HH)
russoz Oct 14, 2025
a2559d7
more debugging: II1 (values)
russoz Oct 14, 2025
b9140c2
more debugging: JJ (# II1)
russoz Oct 14, 2025
31b5ad8
more debugging: II1A (values)
russoz Oct 14, 2025
46a81e7
more debugging: add 1s sleep before running monit status/validate
russoz Oct 14, 2025
419aae5
more debugging: add 1s sleep before running monit status/validate (#I…
russoz Oct 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions plugins/modules/monit.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,11 @@ 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

time.sleep(1)
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)
# raise Exception('HH')
return self._parse_status(out, err)

def _parse_status(self, output, err):
Expand All @@ -171,9 +174,12 @@ def _parse_status(self, output, err):
if ' - ' not in status_val:
status_val = status_val.replace(' ', '_')
try:
# raise Exception(f'II1A {getattr(Status, status_val)=}')
# raise Exception(f'II1 {status_val=} {dir(Status)=}')
return getattr(Status, status_val)
except AttributeError:
self.module.warn("Unknown monit status '%s', treating as execution failed" % status_val)
raise Exception('II2')
return Status.EXECUTION_FAILED
else:
status_val, substatus = status_val.split(' - ')
Expand All @@ -185,6 +191,7 @@ def _parse_status(self, output, err):

if state == 'pending':
status = status.pending()
raise Exception('II3')
return status

def is_process_present(self):
Expand All @@ -201,9 +208,11 @@ def run_command(self, command):

def wait_for_status_change(self, current_status):
running_status = self.get_status()
raise Exception('JJ')
if running_status.value != current_status.value or current_status.value == StatusValue.EXECUTION_FAILED:
return running_status

raise Exception('GG')
loop_count = 0
while running_status.value == current_status.value:
if loop_count >= self._status_change_retry_count:
Expand All @@ -213,6 +222,7 @@ def wait_for_status_change(self, current_status):
time.sleep(0.5)
validate = loop_count % 2 == 0 # force recheck of status every second try
running_status = self.get_status(validate)
raise Exception('FF')
return running_status

def wait_for_monit_to_stop_pending(self, current_status=None):
Expand Down Expand Up @@ -255,17 +265,28 @@ def present(self):
def change_state(self, state, expected_status, invert_expected=None):
current_status = self.get_status()
self.run_command(STATE_COMMAND_MAP[state])
# if state == 'stopped':
# raise Exception('EE')
status = self.wait_for_status_change(current_status)
if state == 'stopped':
raise Exception('DD')
status = self.wait_for_monit_to_stop_pending(status)
status_match = status.value == expected_status.value
if state == 'stopped':
raise Exception('CC')
if invert_expected:
status_match = not status_match
if state == 'stopped':
raise Exception('BB')
if status_match:
self.exit_success(state=state)
if state == 'stopped':
raise Exception('AA')
self.exit_fail('%s process not %s' % (self.process_name, state), status)

def stop(self):
self.change_state('stopped', Status.NOT_MONITORED)
raise Exception('INSIDE-AFTER: Failed to stop process')

def unmonitor(self):
self.change_state('unmonitored', Status.NOT_MONITORED)
Expand Down Expand Up @@ -323,6 +344,7 @@ def exit_if_check_mode():
if running and state == 'stopped':
exit_if_check_mode()
monit.stop()
raise Exception('OUTSIDE-AFTER: Failed to stop process')

if running and state == 'unmonitored':
exit_if_check_mode()
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/targets/monit/aliases
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ skip/macos
skip/freebsd
skip/aix
skip/rhel # FIXME
unstable # TODO: the tests fail a lot; 'unstable' only requires them to pass when the module itself has been modified
# unstable # TODO: the tests fail a lot; 'unstable' only requires them to pass when the module itself has been modified
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{#
Copyright (c) Ansible Project
GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
SPDX-License-Identifier: GPL-3.0-or-later
#}
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

set daemon 2
set logfile /var/log/monit.log
Expand Down
10 changes: 4 additions & 6 deletions tests/integration/targets/monit/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@

- name: monit config
become: true
template:
src: "monitrc.j2"
copy:
src: "monitrc"
dest: "{{ monitrc }}"
mode: '0700'

- name: copy process file
become: true
Expand All @@ -65,12 +66,9 @@

- name: install dependencies
pip:
name: "{{ item }}"
name: python-daemon
virtualenv: "{{ process_venv }}"
extra_args: "-c {{ remote_constraints }}"
loop:
- setuptools==44
- python-daemon

- name: restart monit
become: true
Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/monit/tasks/test_state.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
name: httpd_echo
state: "{{ state }}"
register: result
failed_when: result.rc | default(0) is not in [0, -15]

- name: check that state changed
assert:
Expand Down
Loading