| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2016, Andrew Gaffney <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: openwrt_init |
| 10 |
author: |
| 11 |
- "Andrew Gaffney (@agaffney)" |
| 12 |
short_description: Manage services on OpenWrt |
| 13 |
description: |
| 14 |
- Controls OpenWrt services on remote hosts. |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.general._attributes |
| 17 |
attributes: |
| 18 |
check_mode: |
| 19 |
support: full |
| 20 |
diff_mode: |
| 21 |
support: none |
| 22 |
options: |
| 23 |
name: |
| 24 |
type: str |
| 25 |
description: |
| 26 |
- Name of the service. |
| 27 |
required: true |
| 28 |
aliases: ['service'] |
| 29 |
state: |
| 30 |
type: str |
| 31 |
description: |
| 32 |
- V(started)/V(stopped) are idempotent actions that do not run commands unless necessary. |
| 33 |
- V(restarted) always bounces the service. |
| 34 |
- V(reloaded) always reloads. |
| 35 |
choices: ['started', 'stopped', 'restarted', 'reloaded'] |
| 36 |
enabled: |
| 37 |
description: |
| 38 |
- Whether the service should start on boot. B(At least one) of O(state) and O(enabled) are required. |
| 39 |
type: bool |
| 40 |
pattern: |
| 41 |
type: str |
| 42 |
description: |
| 43 |
- If the service does not respond to the C(running) command, name a substring to look for as would be found in the output |
| 44 |
of the C(ps) command as a stand-in for a C(running) result. If the string is found, the service is assumed to be running. |
| 45 |
notes: |
| 46 |
- One option other than O(name) is required. |
| 47 |
requirements: |
| 48 |
- An OpenWrt system (with python) |
| 49 |
""" |
| 50 |
|
| 51 |
EXAMPLES = r""" |
| 52 |
- name: Start service httpd, if not running |
| 53 |
community.general.openwrt_init: |
| 54 |
state: started |
| 55 |
name: httpd |
| 56 |
|
| 57 |
- name: Stop service cron, if running |
| 58 |
community.general.openwrt_init: |
| 59 |
name: cron |
| 60 |
state: stopped |
| 61 |
|
| 62 |
- name: Reload service httpd, in all cases |
| 63 |
community.general.openwrt_init: |
| 64 |
name: httpd |
| 65 |
state: reloaded |
| 66 |
|
| 67 |
- name: Enable service httpd |
| 68 |
community.general.openwrt_init: |
| 69 |
name: httpd |
| 70 |
enabled: true |
| 71 |
""" |
| 72 |
|
| 73 |
RETURN = r""" |
| 74 |
""" |
| 75 |
|
| 76 |
import os |
| 77 |
|
| 78 |
from ansible.module_utils.basic import AnsibleModule |
| 79 |
|
| 80 |
module = None |
| 81 |
init_script = None |
| 82 |
|
| 83 |
|
| 84 |
# =============================== |
| 85 |
# Check if service is enabled |
| 86 |
def is_enabled(): |
| 87 |
rc, dummy, dummy = module.run_command([init_script, "enabled"]) |
| 88 |
return rc == 0 |
| 89 |
|
| 90 |
|
| 91 |
# =========================================== |
| 92 |
# Main control flow |
| 93 |
def main(): |
| 94 |
global module, init_script |
| 95 |
# init |
| 96 |
module = AnsibleModule( |
| 97 |
argument_spec=dict( |
| 98 |
name=dict(required=True, type="str", aliases=["service"]), |
| 99 |
state=dict(type="str", choices=["started", "stopped", "restarted", "reloaded"]), |
| 100 |
enabled=dict(type="bool"), |
| 101 |
pattern=dict(type="str"), |
| 102 |
), |
| 103 |
supports_check_mode=True, |
| 104 |
required_one_of=[("state", "enabled")], |
| 105 |
) |
| 106 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 107 |
|
| 108 |
# initialize |
| 109 |
service = module.params["name"] |
| 110 |
init_script = f"/etc/init.d/{service}" |
| 111 |
result = { |
| 112 |
"name": service, |
| 113 |
"changed": False, |
| 114 |
} |
| 115 |
# check if service exists |
| 116 |
if not os.path.exists(init_script): |
| 117 |
module.fail_json(msg=f"service {service} does not exist") |
| 118 |
|
| 119 |
# Enable/disable service startup at boot if requested |
| 120 |
if module.params["enabled"] is not None: |
| 121 |
# do we need to enable the service? |
| 122 |
enabled = is_enabled() |
| 123 |
|
| 124 |
# default to current state |
| 125 |
result["enabled"] = enabled |
| 126 |
|
| 127 |
# Change enable/disable if needed |
| 128 |
if enabled != module.params["enabled"]: |
| 129 |
result["changed"] = True |
| 130 |
action = "enable" if module.params["enabled"] else "disable" |
| 131 |
|
| 132 |
if not module.check_mode: |
| 133 |
rc, dummy, err = module.run_command([init_script, action]) |
| 134 |
# openwrt init scripts can return a non-zero exit code on a successful 'enable' |
| 135 |
# command if the init script doesn't contain a STOP value, so we ignore the exit |
| 136 |
# code and explicitly check if the service is now in the desired state |
| 137 |
if is_enabled() != module.params["enabled"]: |
| 138 |
module.fail_json(msg=f"Unable to {action} service {service}: {err}") |
| 139 |
|
| 140 |
result["enabled"] = not enabled |
| 141 |
|
| 142 |
if module.params["state"] is not None: |
| 143 |
running = False |
| 144 |
|
| 145 |
# check if service is currently running |
| 146 |
if module.params["pattern"]: |
| 147 |
# Find ps binary |
| 148 |
psbin = module.get_bin_path("ps", True) |
| 149 |
|
| 150 |
# this should be busybox ps, so we only want/need to the 'w' option |
| 151 |
rc, psout, dummy = module.run_command([psbin, "w"]) |
| 152 |
# If rc is 0, set running as appropriate |
| 153 |
if rc == 0: |
| 154 |
lines = psout.split("\n") |
| 155 |
running = any((module.params["pattern"] in line and "pattern=" not in line) for line in lines) |
| 156 |
else: |
| 157 |
rc, dummy, dummy = module.run_command([init_script, "running"]) |
| 158 |
if rc == 0: |
| 159 |
running = True |
| 160 |
|
| 161 |
# default to desired state |
| 162 |
result["state"] = module.params["state"] |
| 163 |
|
| 164 |
# determine action, if any |
| 165 |
action = None |
| 166 |
if module.params["state"] == "started": |
| 167 |
if not running: |
| 168 |
action = "start" |
| 169 |
result["changed"] = True |
| 170 |
elif module.params["state"] == "stopped": |
| 171 |
if running: |
| 172 |
action = "stop" |
| 173 |
result["changed"] = True |
| 174 |
else: |
| 175 |
action = module.params["state"][:-2] # remove 'ed' from restarted/reloaded |
| 176 |
result["state"] = "started" |
| 177 |
result["changed"] = True |
| 178 |
|
| 179 |
if action: |
| 180 |
if not module.check_mode: |
| 181 |
rc, dummy, err = module.run_command([init_script, action]) |
| 182 |
if rc != 0: |
| 183 |
module.fail_json(msg=f"Unable to {action} service {service}: {err}") |
| 184 |
|
| 185 |
module.exit_json(**result) |
| 186 |
|
| 187 |
|
| 188 |
if __name__ == "__main__": |
| 189 |
main() |