| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2012, Matt Wright <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: supervisorctl |
| 11 |
short_description: Manage the state of a program or group of programs managed by C(supervisord) |
| 12 |
description: |
| 13 |
- Manage the state of a program or group of programs managed by C(supervisord). |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
name: |
| 23 |
type: str |
| 24 |
description: |
| 25 |
- The name of the supervisord program or group to manage. |
| 26 |
- The name is taken as group name when it ends with a colon V(:). |
| 27 |
- If O(name=all), all programs and program groups are managed. |
| 28 |
required: true |
| 29 |
config: |
| 30 |
type: path |
| 31 |
description: |
| 32 |
- The supervisor configuration file path. |
| 33 |
server_url: |
| 34 |
type: str |
| 35 |
description: |
| 36 |
- URL on which supervisord server is listening. |
| 37 |
username: |
| 38 |
type: str |
| 39 |
description: |
| 40 |
- Username to use for authentication. |
| 41 |
password: |
| 42 |
type: str |
| 43 |
description: |
| 44 |
- Password to use for authentication. |
| 45 |
state: |
| 46 |
type: str |
| 47 |
description: |
| 48 |
- The desired state of program/group. |
| 49 |
required: true |
| 50 |
choices: ["present", "started", "stopped", "restarted", "absent", "signalled"] |
| 51 |
stop_before_removing: |
| 52 |
type: bool |
| 53 |
description: |
| 54 |
- Use O(stop_before_removing=true) to stop the program/group before removing it. |
| 55 |
default: false |
| 56 |
version_added: 7.5.0 |
| 57 |
signal: |
| 58 |
type: str |
| 59 |
description: |
| 60 |
- The signal to send to the program/group, when combined with the V(signalled) state. Required when l(state=signalled). |
| 61 |
supervisorctl_path: |
| 62 |
type: path |
| 63 |
description: |
| 64 |
- Path to C(supervisorctl) executable. |
| 65 |
notes: |
| 66 |
- When O(state=present), the module calls C(supervisorctl reread) then C(supervisorctl add) if the program/group does not |
| 67 |
exist. |
| 68 |
- When O(state=restarted), the module calls C(supervisorctl update) then calls C(supervisorctl restart). |
| 69 |
- When O(state=absent), the module calls C(supervisorctl reread) then C(supervisorctl remove) to remove the target program/group. |
| 70 |
If the program/group is still running, the action fails. If you want to stop the program/group before removing, use O(stop_before_removing=true). |
| 71 |
requirements: ["supervisorctl"] |
| 72 |
author: |
| 73 |
- "Matt Wright (@mattupstate)" |
| 74 |
- "Aaron Wang (@inetfuture) <[email protected]>" |
| 75 |
""" |
| 76 |
|
| 77 |
EXAMPLES = r""" |
| 78 |
- name: Manage the state of program to be in started state |
| 79 |
community.general.supervisorctl: |
| 80 |
name: my_app |
| 81 |
state: started |
| 82 |
|
| 83 |
- name: Manage the state of program group to be in started state |
| 84 |
community.general.supervisorctl: |
| 85 |
name: 'my_apps:' |
| 86 |
state: started |
| 87 |
|
| 88 |
- name: Restart my_app, reading supervisorctl configuration from a specified file |
| 89 |
community.general.supervisorctl: |
| 90 |
name: my_app |
| 91 |
state: restarted |
| 92 |
config: /var/opt/my_project/supervisord.conf |
| 93 |
|
| 94 |
- name: Restart my_app, connecting to supervisord with credentials and server URL |
| 95 |
community.general.supervisorctl: |
| 96 |
name: my_app |
| 97 |
state: restarted |
| 98 |
username: test |
| 99 |
password: testpass |
| 100 |
server_url: http://localhost:9001 |
| 101 |
|
| 102 |
- name: Send a signal to my_app via supervisorctl |
| 103 |
community.general.supervisorctl: |
| 104 |
name: my_app |
| 105 |
state: signalled |
| 106 |
signal: USR1 |
| 107 |
|
| 108 |
- name: Restart all programs and program groups |
| 109 |
community.general.supervisorctl: |
| 110 |
name: all |
| 111 |
state: restarted |
| 112 |
""" |
| 113 |
|
| 114 |
|
| 115 |
import os |
| 116 |
|
| 117 |
from ansible.module_utils.basic import AnsibleModule, is_executable |
| 118 |
|
| 119 |
|
| 120 |
def _is_active(status): |
| 121 |
return status in ("RUNNING", "STARTING") |
| 122 |
|
| 123 |
|
| 124 |
def _is_not_active(status): |
| 125 |
return not _is_active(status) |
| 126 |
|
| 127 |
|
| 128 |
def _is_strictly_running(status): |
| 129 |
return status in ("RUNNING",) |
| 130 |
|
| 131 |
|
| 132 |
def _always(_status): |
| 133 |
return True |
| 134 |
|
| 135 |
|
| 136 |
def main(): |
| 137 |
arg_spec = dict( |
| 138 |
name=dict(type="str", required=True), |
| 139 |
config=dict(type="path"), |
| 140 |
server_url=dict(type="str"), |
| 141 |
username=dict(type="str"), |
| 142 |
password=dict(type="str", no_log=True), |
| 143 |
supervisorctl_path=dict(type="path"), |
| 144 |
state=dict( |
| 145 |
type="str", required=True, choices=["present", "started", "restarted", "stopped", "absent", "signalled"] |
| 146 |
), |
| 147 |
stop_before_removing=dict(type="bool", default=False), |
| 148 |
signal=dict(type="str"), |
| 149 |
) |
| 150 |
|
| 151 |
module = AnsibleModule( |
| 152 |
argument_spec=arg_spec, |
| 153 |
supports_check_mode=True, |
| 154 |
required_if=[("state", "signalled", ["signal"])], |
| 155 |
) |
| 156 |
|
| 157 |
name = module.params["name"] |
| 158 |
is_group = False |
| 159 |
if name.endswith(":"): |
| 160 |
is_group = True |
| 161 |
name = name.rstrip(":") |
| 162 |
state = module.params["state"] |
| 163 |
stop_before_removing = module.params.get("stop_before_removing") |
| 164 |
config = module.params.get("config") |
| 165 |
server_url = module.params.get("server_url") |
| 166 |
username = module.params.get("username") |
| 167 |
password = module.params.get("password") |
| 168 |
supervisorctl_path = module.params.get("supervisorctl_path") |
| 169 |
signal = module.params.get("signal") |
| 170 |
|
| 171 |
# we check error message for a pattern, so we need to make sure that's in C locale |
| 172 |
module.run_command_environ_update = dict(LANGUAGE="C", LC_ALL="C") |
| 173 |
|
| 174 |
if supervisorctl_path: |
| 175 |
if os.path.exists(supervisorctl_path) and is_executable(supervisorctl_path): |
| 176 |
supervisorctl_args = [supervisorctl_path] |
| 177 |
else: |
| 178 |
module.fail_json( |
| 179 |
msg=f"Provided path to supervisorctl does not exist or isn't executable: {supervisorctl_path}" |
| 180 |
) |
| 181 |
else: |
| 182 |
supervisorctl_args = [module.get_bin_path("supervisorctl", True)] |
| 183 |
|
| 184 |
if config: |
| 185 |
supervisorctl_args.extend(["-c", config]) |
| 186 |
if server_url: |
| 187 |
supervisorctl_args.extend(["-s", server_url]) |
| 188 |
if username: |
| 189 |
supervisorctl_args.extend(["-u", username]) |
| 190 |
if password: |
| 191 |
supervisorctl_args.extend(["-p", password]) |
| 192 |
|
| 193 |
def run_supervisorctl(cmd, name=None, **kwargs): |
| 194 |
args = list(supervisorctl_args) # copy the master args |
| 195 |
args.append(cmd) |
| 196 |
if name: |
| 197 |
args.append(name) |
| 198 |
return module.run_command(args, **kwargs) |
| 199 |
|
| 200 |
def get_matched_processes(): |
| 201 |
matched = [] |
| 202 |
rc, out, err = run_supervisorctl("status") |
| 203 |
for line in out.splitlines(): |
| 204 |
# One status line may look like one of these two: |
| 205 |
# process not in group: |
| 206 |
# echo_date_lonely RUNNING pid 7680, uptime 13:22:18 |
| 207 |
# process in group: |
| 208 |
# echo_date_group:echo_date_00 RUNNING pid 7681, uptime 13:22:18 |
| 209 |
fields = [field for field in line.split(" ") if field != ""] |
| 210 |
process_name = fields[0] |
| 211 |
status = fields[1] |
| 212 |
|
| 213 |
if is_group: |
| 214 |
# If there is ':', this process must be in a group. |
| 215 |
if ":" in process_name: |
| 216 |
group = process_name.split(":")[0] |
| 217 |
if group != name: |
| 218 |
continue |
| 219 |
else: |
| 220 |
continue |
| 221 |
else: |
| 222 |
if process_name != name and name != "all": |
| 223 |
continue |
| 224 |
|
| 225 |
matched.append((process_name, status)) |
| 226 |
return matched |
| 227 |
|
| 228 |
def take_action_on_processes(processes, status_filter, action, expected_result, exit_module=True): |
| 229 |
to_take_action_on = [] |
| 230 |
for process_name, status in processes: |
| 231 |
if status_filter(status): |
| 232 |
to_take_action_on.append(process_name) |
| 233 |
|
| 234 |
if len(to_take_action_on) == 0: |
| 235 |
if not exit_module: |
| 236 |
return |
| 237 |
module.exit_json(changed=False, name=name, state=state) |
| 238 |
if module.check_mode: |
| 239 |
if not exit_module: |
| 240 |
return |
| 241 |
module.exit_json(changed=True) |
| 242 |
for process_name in to_take_action_on: |
| 243 |
rc, out, err = run_supervisorctl(action, process_name, check_rc=True) |
| 244 |
if f"{process_name}: {expected_result}" not in out: |
| 245 |
module.fail_json(msg=out) |
| 246 |
|
| 247 |
if exit_module: |
| 248 |
module.exit_json(changed=True, name=name, state=state, affected=to_take_action_on) |
| 249 |
|
| 250 |
if name == "all" and state in ("started", "stopped", "restarted"): |
| 251 |
if state == "restarted": |
| 252 |
run_supervisorctl("update", check_rc=True) |
| 253 |
processes = get_matched_processes() |
| 254 |
status_filters = { |
| 255 |
"started": _is_not_active, |
| 256 |
"stopped": _is_active, |
| 257 |
"restarted": _always, |
| 258 |
} |
| 259 |
to_act_on = [p for p, s in processes if status_filters[state](s)] |
| 260 |
if not to_act_on: |
| 261 |
module.exit_json(changed=False, name=name, state=state) |
| 262 |
if module.check_mode: |
| 263 |
module.exit_json(changed=True) |
| 264 |
actions = {"started": "start", "stopped": "stop", "restarted": "restart"} |
| 265 |
run_supervisorctl(actions[state], "all", check_rc=True) |
| 266 |
module.exit_json(changed=True, name=name, state=state, affected=to_act_on) |
| 267 |
|
| 268 |
if state == "restarted": |
| 269 |
rc, out, err = run_supervisorctl("update", check_rc=True) |
| 270 |
processes = get_matched_processes() |
| 271 |
if len(processes) == 0: |
| 272 |
module.fail_json(name=name, msg="ERROR (no such process)") |
| 273 |
|
| 274 |
take_action_on_processes(processes, _always, "restart", "started") |
| 275 |
|
| 276 |
processes = get_matched_processes() |
| 277 |
|
| 278 |
if state == "absent": |
| 279 |
if len(processes) == 0: |
| 280 |
module.exit_json(changed=False, name=name, state=state) |
| 281 |
|
| 282 |
if stop_before_removing: |
| 283 |
take_action_on_processes(processes, _is_active, "stop", "stopped", exit_module=False) |
| 284 |
|
| 285 |
if module.check_mode: |
| 286 |
module.exit_json(changed=True) |
| 287 |
run_supervisorctl("reread", check_rc=True) |
| 288 |
rc, out, err = run_supervisorctl("remove", name) |
| 289 |
if f"{name}: removed process group" in out: |
| 290 |
module.exit_json(changed=True, name=name, state=state) |
| 291 |
else: |
| 292 |
module.fail_json(msg=out, name=name, state=state) |
| 293 |
|
| 294 |
if state == "present": |
| 295 |
if len(processes) > 0: |
| 296 |
module.exit_json(changed=False, name=name, state=state) |
| 297 |
|
| 298 |
if module.check_mode: |
| 299 |
module.exit_json(changed=True) |
| 300 |
run_supervisorctl("reread", check_rc=True) |
| 301 |
dummy, out, dummy = run_supervisorctl("add", name) |
| 302 |
if f"{name}: added process group" in out: |
| 303 |
module.exit_json(changed=True, name=name, state=state) |
| 304 |
else: |
| 305 |
module.fail_json(msg=out, name=name, state=state) |
| 306 |
|
| 307 |
# from this point onwards, if there are no matching processes, module cannot go on. |
| 308 |
if len(processes) == 0 and name != "all": |
| 309 |
module.fail_json(name=name, msg="ERROR (no such process)") |
| 310 |
|
| 311 |
if state == "started": |
| 312 |
take_action_on_processes(processes, _is_not_active, "start", "started") |
| 313 |
|
| 314 |
if state == "stopped": |
| 315 |
take_action_on_processes(processes, _is_active, "stop", "stopped") |
| 316 |
|
| 317 |
if state == "signalled": |
| 318 |
take_action_on_processes(processes, _is_strictly_running, f"signal {signal}", "signalled") |
| 319 |
|
| 320 |
|
| 321 |
if __name__ == "__main__": |
| 322 |
main() |