Skip to content

Commit

Permalink
Add 'signalled' state to supervisorctl module with associated 'signal…
Browse files Browse the repository at this point in the history
…' parameter.

ansible#29847
  • Loading branch information
a-harvie committed Sep 20, 2018
1 parent 0947948 commit 4cac6fd
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/ansible/modules/web_infrastructure/supervisorctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@
description:
- The desired state of program/group.
required: true
choices: [ "present", "started", "stopped", "restarted", "absent" ]
choices: [ "present", "started", "stopped", "restarted", "absent", "signalled" ]
signal:
description:
- The signal to send to the program/group, when combined with the 'signalled' state. Required when l(state=signalled).
version_added: "2.8"
supervisorctl_path:
description:
- path to supervisorctl executable
Expand Down Expand Up @@ -86,6 +90,12 @@
username: test
password: testpass
server_url: http://localhost:9001
# Send a signal to my_app via supervisorctl
- supervisorctl:
name: my_app
state: signalled
signal: USR1
'''

import os
Expand All @@ -100,7 +110,8 @@ def main():
username=dict(required=False),
password=dict(required=False, no_log=True),
supervisorctl_path=dict(required=False, type='path'),
state=dict(required=True, choices=['present', 'started', 'restarted', 'stopped', 'absent'])
state=dict(required=True, choices=['present', 'started', 'restarted', 'stopped', 'absent', 'signalled']),
signal=dict(required=False)
)

module = AnsibleModule(argument_spec=arg_spec, supports_check_mode=True)
Expand All @@ -116,6 +127,7 @@ def main():
username = module.params.get('username')
password = module.params.get('password')
supervisorctl_path = module.params.get('supervisorctl_path')
signal = module.params.get('signal')

# we check error message for a pattern, so we need to make sure that's in C locale
module.run_command_environ_update = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C', LC_CTYPE='C')
Expand All @@ -138,6 +150,9 @@ def main():
if password:
supervisorctl_args.extend(['-p', password])

if state == 'signalled' and not signal:
module.fail_json(msg="State 'signalled' requires a 'signal' value")

def run_supervisorctl(cmd, name=None, **kwargs):
args = list(supervisorctl_args) # copy the master args
args.append(cmd)
Expand Down Expand Up @@ -236,6 +251,11 @@ def take_action_on_processes(processes, status_filter, action, expected_result):
module.fail_json(name=name, msg="ERROR (no such process)")
take_action_on_processes(processes, lambda s: s in ('RUNNING', 'STARTING'), 'stop', 'stopped')

if state == 'signalled':
if len(processes) == 0:
module.fail_json(name=name, msg="ERROR (no such process)")
take_action_on_processes(processes, lambda s: s in ('RUNNING'), "signal %s" % signal, 'signalled')


if __name__ == '__main__':
main()

0 comments on commit 4cac6fd

Please sign in to comment.