Skip to content

Commit

Permalink
supervisorctl: stop process before removing it (#7284)
Browse files Browse the repository at this point in the history
* supervisorctl: stop process before removing it

* Update supervisorctl.py

Removes blanks

* adds fragment

* introduces stop_before_removing parameter and fix deleting after stopping

* reduce line length

* Apply suggestions from code review

Co-authored-by: Felix Fontein <felix@fontein.de>

* miss some exit

* fixing review

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit b88b045)
  • Loading branch information
DeLoWaN authored and patchback[bot] committed Sep 28, 2023
1 parent 9254110 commit 5050455
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- supervisorctl - allow to stop matching running processes before removing them with ``stop_before_removing=true`` (https://github.com/ansible-collections/community.general/pull/7284).
22 changes: 20 additions & 2 deletions plugins/modules/supervisorctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@
- The desired state of program/group.
required: true
choices: [ "present", "started", "stopped", "restarted", "absent", "signalled" ]
stop_before_removing:
type: bool
description:
- Use O(stop_before_removing=true) to stop the program/group before removing it
required: false
default: false
version_added: 7.5.0
signal:
type: str
description:
Expand All @@ -65,6 +72,7 @@
- When O(state=present), the module will call C(supervisorctl reread) then C(supervisorctl add) if the program/group does not exist.
- When O(state=restarted), the module will call C(supervisorctl update) then call C(supervisorctl restart).
- When O(state=absent), the module will call C(supervisorctl reread) then C(supervisorctl remove) to remove the target program/group.
If the program/group is still running, the action will fail. If you want to stop the program/group before removing, use O(stop_before_removing=true).
requirements: [ "supervisorctl" ]
author:
- "Matt Wright (@mattupstate)"
Expand Down Expand Up @@ -121,6 +129,7 @@ def main():
password=dict(type='str', no_log=True),
supervisorctl_path=dict(type='path'),
state=dict(type='str', required=True, choices=['present', 'started', 'restarted', 'stopped', 'absent', 'signalled']),
stop_before_removing=dict(type='bool', default=False),
signal=dict(type='str'),
)

Expand All @@ -136,6 +145,7 @@ def main():
is_group = True
name = name.rstrip(':')
state = module.params['state']
stop_before_removing = module.params.get('stop_before_removing')
config = module.params.get('config')
server_url = module.params.get('server_url')
username = module.params.get('username')
Expand Down Expand Up @@ -199,22 +209,27 @@ def get_matched_processes():
matched.append((process_name, status))
return matched

def take_action_on_processes(processes, status_filter, action, expected_result):
def take_action_on_processes(processes, status_filter, action, expected_result, exit_module=True):
to_take_action_on = []
for process_name, status in processes:
if status_filter(status):
to_take_action_on.append(process_name)

if len(to_take_action_on) == 0:
if not exit_module:
return
module.exit_json(changed=False, name=name, state=state)
if module.check_mode:
if not exit_module:
return
module.exit_json(changed=True)
for process_name in to_take_action_on:
rc, out, err = run_supervisorctl(action, process_name, check_rc=True)
if '%s: %s' % (process_name, expected_result) not in out:
module.fail_json(msg=out)

module.exit_json(changed=True, name=name, state=state, affected=to_take_action_on)
if exit_module:
module.exit_json(changed=True, name=name, state=state, affected=to_take_action_on)

if state == 'restarted':
rc, out, err = run_supervisorctl('update', check_rc=True)
Expand All @@ -230,6 +245,9 @@ def take_action_on_processes(processes, status_filter, action, expected_result):
if len(processes) == 0:
module.exit_json(changed=False, name=name, state=state)

if stop_before_removing:
take_action_on_processes(processes, lambda s: s in ('RUNNING', 'STARTING'), 'stop', 'stopped', exit_module=False)

if module.check_mode:
module.exit_json(changed=True)
run_supervisorctl('reread', check_rc=True)
Expand Down

0 comments on commit 5050455

Please sign in to comment.