1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

supervisorctl: stop process before removing it (#7284)

* 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>
This commit is contained in:
Damien Gustave 2023-09-28 21:04:09 +02:00 committed by GitHub
parent fbebcbada5
commit b88b04593f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View file

@ -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).

View file

@ -53,6 +53,13 @@ options:
- 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:
@ -65,6 +72,7 @@ notes:
- 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)"
@ -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'),
)
@ -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')
@ -199,22 +209,27 @@ def main():
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)
@ -230,6 +245,9 @@ def main():
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)