| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: (c) 2012, Michael DeHaan <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
--- |
| 11 |
module: service |
| 12 |
version_added: "0.1" |
| 13 |
short_description: Manage services |
| 14 |
description: |
| 15 |
- Controls services on remote hosts. Supported init systems include BSD init, |
| 16 |
OpenRC, SysV, Solaris SMF, systemd, upstart. |
| 17 |
- This module acts as a proxy to the underlying service manager module. While all arguments will be passed to the |
| 18 |
underlying module, not all modules support the same arguments. This documentation only covers the minimum intersection |
| 19 |
of module arguments that all service manager modules support. |
| 20 |
- This module is a proxy for multiple more specific service manager modules |
| 21 |
(such as M(ansible.builtin.systemd) and M(ansible.builtin.sysvinit)). |
| 22 |
This allows management of a heterogeneous environment of machines without creating a specific task for |
| 23 |
each service manager. The module to be executed is determined by the O(use) option, which defaults to the |
| 24 |
service manager discovered by M(ansible.builtin.setup). If M(ansible.builtin.setup) was not yet run, this module may run it. |
| 25 |
- For Windows targets, use the M(ansible.windows.win_service) module instead. |
| 26 |
options: |
| 27 |
name: |
| 28 |
description: |
| 29 |
- Name of the service. |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
state: |
| 33 |
description: |
| 34 |
- V(started)/V(stopped) are idempotent actions that will not run |
| 35 |
commands unless necessary. |
| 36 |
- V(restarted) will always bounce the service. |
| 37 |
- V(reloaded) will always reload. |
| 38 |
- At least one of O(state) and O(enabled) are required. |
| 39 |
- Note that V(reloaded) will start the service if it is not already started, |
| 40 |
even if your chosen init system wouldn't normally. |
| 41 |
type: str |
| 42 |
choices: [ reloaded, restarted, started, stopped ] |
| 43 |
sleep: |
| 44 |
description: |
| 45 |
- If the service is being V(restarted) then sleep this many seconds |
| 46 |
between the stop and start command. |
| 47 |
- This helps to work around badly-behaving init scripts that exit immediately |
| 48 |
after signaling a process to stop. |
| 49 |
- Not all service managers support sleep, i.e when using systemd this setting will be ignored. |
| 50 |
type: int |
| 51 |
version_added: "1.3" |
| 52 |
pattern: |
| 53 |
description: |
| 54 |
- If the service does not respond to the status command, name a |
| 55 |
substring to look for as would be found in the output of the C(ps) |
| 56 |
command as a stand-in for a status result. |
| 57 |
- If the string is found, the service will be assumed to be started. |
| 58 |
- While using remote hosts with systemd this setting will be ignored. |
| 59 |
type: str |
| 60 |
version_added: "0.7" |
| 61 |
enabled: |
| 62 |
description: |
| 63 |
- Whether the service should start on boot. |
| 64 |
- At least one of O(state) and O(enabled) are required. |
| 65 |
type: bool |
| 66 |
runlevel: |
| 67 |
description: |
| 68 |
- For OpenRC init scripts (e.g. Gentoo) only. |
| 69 |
- The runlevel that this service belongs to. |
| 70 |
- While using remote hosts with systemd this setting will be ignored. |
| 71 |
type: str |
| 72 |
default: default |
| 73 |
arguments: |
| 74 |
description: |
| 75 |
- Additional arguments provided on the command line. |
| 76 |
- While using remote hosts with systemd this setting will be ignored. |
| 77 |
type: str |
| 78 |
default: '' |
| 79 |
aliases: [ args ] |
| 80 |
use: |
| 81 |
description: |
| 82 |
- The service module actually uses system specific modules, normally through auto detection, this setting can force a specific module. |
| 83 |
- Normally it uses the value of the C(ansible_service_mgr) fact and falls back to the C(ansible.legacy.service) module when none matching is found. |
| 84 |
- The 'old service module' still uses autodetection and in no way does it correspond to the C(service) command. |
| 85 |
type: str |
| 86 |
default: auto |
| 87 |
version_added: 2.2 |
| 88 |
extends_documentation_fragment: |
| 89 |
- action_common_attributes |
| 90 |
- action_common_attributes.flow |
| 91 |
attributes: |
| 92 |
action: |
| 93 |
support: full |
| 94 |
async: |
| 95 |
support: full |
| 96 |
bypass_host_loop: |
| 97 |
support: none |
| 98 |
check_mode: |
| 99 |
details: support depends on the underlying plugin invoked |
| 100 |
support: N/A |
| 101 |
diff_mode: |
| 102 |
details: support depends on the underlying plugin invoked |
| 103 |
support: N/A |
| 104 |
platform: |
| 105 |
details: The support depends on the availability for the specific plugin for each platform and if fact gathering is able to detect it |
| 106 |
platforms: all |
| 107 |
notes: |
| 108 |
- For AIX, group subsystem names can be used. |
| 109 |
- The C(service) command line utility is not part of any service manager system but a convenience. |
| 110 |
It does not have a standard implementation across systems, and this action cannot use it directly. |
| 111 |
Though it might be used if found in certain circumstances, the detected system service manager is normally preferred. |
| 112 |
seealso: |
| 113 |
- module: ansible.windows.win_service |
| 114 |
author: |
| 115 |
- Ansible Core Team |
| 116 |
- Michael DeHaan |
| 117 |
""" |
| 118 |
|
| 119 |
EXAMPLES = r""" |
| 120 |
- name: Start service httpd, if not started |
| 121 |
ansible.builtin.service: |
| 122 |
name: httpd |
| 123 |
state: started |
| 124 |
|
| 125 |
- name: Stop service httpd, if started |
| 126 |
ansible.builtin.service: |
| 127 |
name: httpd |
| 128 |
state: stopped |
| 129 |
|
| 130 |
- name: Restart service httpd, in all cases |
| 131 |
ansible.builtin.service: |
| 132 |
name: httpd |
| 133 |
state: restarted |
| 134 |
|
| 135 |
- name: Reload service httpd, in all cases |
| 136 |
ansible.builtin.service: |
| 137 |
name: httpd |
| 138 |
state: reloaded |
| 139 |
|
| 140 |
- name: Enable service httpd, and not touch the state |
| 141 |
ansible.builtin.service: |
| 142 |
name: httpd |
| 143 |
enabled: yes |
| 144 |
|
| 145 |
- name: Start service foo, based on running process /usr/bin/foo |
| 146 |
ansible.builtin.service: |
| 147 |
name: foo |
| 148 |
pattern: /usr/bin/foo |
| 149 |
state: started |
| 150 |
|
| 151 |
- name: Restart network service for interface eth0 |
| 152 |
ansible.builtin.service: |
| 153 |
name: network |
| 154 |
state: restarted |
| 155 |
args: eth0 |
| 156 |
""" |
| 157 |
|
| 158 |
RETURN = r"""#""" |
| 159 |
|
| 160 |
import glob |
| 161 |
import json |
| 162 |
import os |
| 163 |
import platform |
| 164 |
import re |
| 165 |
import select |
| 166 |
import shlex |
| 167 |
import subprocess |
| 168 |
import tempfile |
| 169 |
import time |
| 170 |
|
| 171 |
# The distutils module is not shipped with SUNWPython on Solaris. |
| 172 |
# It's in the SUNWPython-devel package which also contains development files |
| 173 |
# that don't belong on production boxes. Since our Solaris code doesn't |
| 174 |
# depend on LooseVersion, do not import it on Solaris. |
| 175 |
if platform.system() != 'SunOS': |
| 176 |
from ansible.module_utils.compat.version import LooseVersion |
| 177 |
|
| 178 |
from ansible.module_utils.common.text.converters import to_bytes, to_text |
| 179 |
from ansible.module_utils.basic import AnsibleModule |
| 180 |
from ansible.module_utils.common.locale import get_best_parsable_locale |
| 181 |
from ansible.module_utils.common.sys_info import get_platform_subclass |
| 182 |
from ansible.module_utils.service import fail_if_missing, is_systemd_managed |
| 183 |
|
| 184 |
|
| 185 |
class Service(object): |
| 186 |
""" |
| 187 |
This is the generic Service manipulation class that is subclassed |
| 188 |
based on platform. |
| 189 |
|
| 190 |
A subclass should override the following action methods:- |
| 191 |
- get_service_tools |
| 192 |
- service_enable |
| 193 |
- get_service_status |
| 194 |
- service_control |
| 195 |
|
| 196 |
All subclasses MUST define platform and distribution (which may be None). |
| 197 |
""" |
| 198 |
|
| 199 |
platform = 'Generic' |
| 200 |
distribution = None |
| 201 |
|
| 202 |
def __new__(cls, *args, **kwargs): |
| 203 |
new_cls = get_platform_subclass(Service) |
| 204 |
return super(cls, new_cls).__new__(new_cls) |
| 205 |
|
| 206 |
def __init__(self, module): |
| 207 |
self.module = module |
| 208 |
self.name = module.params['name'] |
| 209 |
self.state = module.params['state'] |
| 210 |
self.sleep = module.params['sleep'] |
| 211 |
self.pattern = module.params['pattern'] |
| 212 |
self.enable = module.params['enabled'] |
| 213 |
self.runlevel = module.params['runlevel'] |
| 214 |
self.changed = False |
| 215 |
self.running = None |
| 216 |
self.crashed = None |
| 217 |
self.action = None |
| 218 |
self.svc_cmd = None |
| 219 |
self.svc_initscript = None |
| 220 |
self.svc_initctl = None |
| 221 |
self.enable_cmd = None |
| 222 |
self.arguments = module.params.get('arguments', '') |
| 223 |
self.rcconf_file = None |
| 224 |
self.rcconf_key = None |
| 225 |
self.rcconf_value = None |
| 226 |
self.svc_change = False |
| 227 |
|
| 228 |
# =========================================== |
| 229 |
# Platform specific methods (must be replaced by subclass). |
| 230 |
|
| 231 |
def get_service_tools(self): |
| 232 |
self.module.fail_json(msg="get_service_tools not implemented on target platform") |
| 233 |
|
| 234 |
def service_enable(self): |
| 235 |
self.module.fail_json(msg="service_enable not implemented on target platform") |
| 236 |
|
| 237 |
def get_service_status(self): |
| 238 |
self.module.fail_json(msg="get_service_status not implemented on target platform") |
| 239 |
|
| 240 |
def service_control(self): |
| 241 |
self.module.fail_json(msg="service_control not implemented on target platform") |
| 242 |
|
| 243 |
# =========================================== |
| 244 |
# Generic methods that should be used on all platforms. |
| 245 |
|
| 246 |
def execute_command(self, cmd, daemonize=False): |
| 247 |
|
| 248 |
locale = get_best_parsable_locale(self.module) |
| 249 |
lang_env = dict(LANG=locale, LC_ALL=locale, LC_MESSAGES=locale) |
| 250 |
|
| 251 |
# Most things don't need to be daemonized |
| 252 |
if not daemonize: |
| 253 |
# chkconfig localizes messages and we're screen scraping so make |
| 254 |
# sure we use the C locale |
| 255 |
return self.module.run_command(cmd, environ_update=lang_env) |
| 256 |
|
| 257 |
# This is complex because daemonization is hard for people. |
| 258 |
# What we do is daemonize a part of this module, the daemon runs the |
| 259 |
# command, picks up the return code and output, and returns it to the |
| 260 |
# main process. |
| 261 |
pipe = os.pipe() |
| 262 |
pid = os.fork() |
| 263 |
if pid == 0: |
| 264 |
os.close(pipe[0]) |
| 265 |
# Set stdin/stdout/stderr to /dev/null |
| 266 |
fd = os.open(os.devnull, os.O_RDWR) |
| 267 |
if fd != 0: |
| 268 |
os.dup2(fd, 0) |
| 269 |
if fd != 1: |
| 270 |
os.dup2(fd, 1) |
| 271 |
if fd != 2: |
| 272 |
os.dup2(fd, 2) |
| 273 |
if fd not in (0, 1, 2): |
| 274 |
os.close(fd) |
| 275 |
|
| 276 |
# Make us a daemon. Yes, that's all it takes. |
| 277 |
pid = os.fork() |
| 278 |
if pid > 0: |
| 279 |
os._exit(0) |
| 280 |
os.setsid() |
| 281 |
os.chdir("/") |
| 282 |
pid = os.fork() |
| 283 |
if pid > 0: |
| 284 |
os._exit(0) |
| 285 |
|
| 286 |
# Start the command |
| 287 |
cmd = to_text(cmd, errors='surrogate_or_strict') |
| 288 |
cmd = [to_bytes(c, errors='surrogate_or_strict') for c in shlex.split(cmd)] |
| 289 |
# In either of the above cases, pass a list of byte strings to Popen |
| 290 |
|
| 291 |
# chkconfig localizes messages and we're screen scraping so make |
| 292 |
# sure we use the C locale |
| 293 |
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=lang_env, preexec_fn=lambda: os.close(pipe[1])) |
| 294 |
stdout = b"" |
| 295 |
stderr = b"" |
| 296 |
fds = [p.stdout, p.stderr] |
| 297 |
# Wait for all output, or until the main process is dead and its output is done. |
| 298 |
while fds: |
| 299 |
rfd, wfd, efd = select.select(fds, [], fds, 1) |
| 300 |
if not (rfd + wfd + efd) and p.poll() is not None: |
| 301 |
break |
| 302 |
if p.stdout in rfd: |
| 303 |
dat = os.read(p.stdout.fileno(), 4096) |
| 304 |
if not dat: |
| 305 |
fds.remove(p.stdout) |
| 306 |
stdout += dat |
| 307 |
if p.stderr in rfd: |
| 308 |
dat = os.read(p.stderr.fileno(), 4096) |
| 309 |
if not dat: |
| 310 |
fds.remove(p.stderr) |
| 311 |
stderr += dat |
| 312 |
p.wait() |
| 313 |
# Return a JSON blob to parent |
| 314 |
blob = json.dumps([p.returncode, to_text(stdout), to_text(stderr)]) |
| 315 |
os.write(pipe[1], to_bytes(blob, errors='surrogate_or_strict')) |
| 316 |
os.close(pipe[1]) |
| 317 |
os._exit(0) |
| 318 |
elif pid == -1: |
| 319 |
self.module.fail_json(msg="unable to fork") |
| 320 |
else: |
| 321 |
os.close(pipe[1]) |
| 322 |
os.waitpid(pid, 0) |
| 323 |
# Wait for data from daemon process and process it. |
| 324 |
data = b"" |
| 325 |
while True: |
| 326 |
rfd, wfd, efd = select.select([pipe[0]], [], [pipe[0]]) |
| 327 |
if pipe[0] in rfd: |
| 328 |
dat = os.read(pipe[0], 4096) |
| 329 |
if not dat: |
| 330 |
break |
| 331 |
data += dat |
| 332 |
return json.loads(to_text(data, errors='surrogate_or_strict')) |
| 333 |
|
| 334 |
def check_ps(self): |
| 335 |
# Set ps flags |
| 336 |
if platform.system() == 'SunOS': |
| 337 |
psflags = '-ef' |
| 338 |
else: |
| 339 |
psflags = 'auxww' |
| 340 |
|
| 341 |
# Find ps binary |
| 342 |
psbin = self.module.get_bin_path('ps', True) |
| 343 |
|
| 344 |
(rc, psout, pserr) = self.execute_command('%s %s' % (psbin, psflags)) |
| 345 |
# If rc is 0, set running as appropriate |
| 346 |
if rc == 0: |
| 347 |
self.running = False |
| 348 |
lines = psout.split("\n") |
| 349 |
for line in lines: |
| 350 |
if self.pattern in line and "pattern=" not in line: |
| 351 |
# so as to not confuse ./hacking/test-module.py |
| 352 |
self.running = True |
| 353 |
break |
| 354 |
|
| 355 |
def check_service_changed(self): |
| 356 |
if self.state and self.running is None: |
| 357 |
self.module.fail_json(msg="failed determining service state, possible typo of service name?") |
| 358 |
# Find out if state has changed |
| 359 |
if not self.running and self.state in ["reloaded", "started"]: |
| 360 |
self.svc_change = True |
| 361 |
elif self.running and self.state in ["reloaded", "stopped"]: |
| 362 |
self.svc_change = True |
| 363 |
elif self.state == "restarted": |
| 364 |
self.svc_change = True |
| 365 |
if self.module.check_mode and self.svc_change: |
| 366 |
self.module.exit_json(changed=True, msg='service state changed') |
| 367 |
|
| 368 |
def modify_service_state(self): |
| 369 |
|
| 370 |
# Only do something if state will change |
| 371 |
if self.svc_change: |
| 372 |
# Control service |
| 373 |
if self.state in ['started']: |
| 374 |
self.action = "start" |
| 375 |
elif not self.running and self.state == 'reloaded': |
| 376 |
self.action = "start" |
| 377 |
elif self.state == 'stopped': |
| 378 |
self.action = "stop" |
| 379 |
elif self.state == 'reloaded': |
| 380 |
self.action = "reload" |
| 381 |
elif self.state == 'restarted': |
| 382 |
self.action = "restart" |
| 383 |
|
| 384 |
if self.module.check_mode: |
| 385 |
self.module.exit_json(changed=True, msg='changing service state') |
| 386 |
|
| 387 |
return self.service_control() |
| 388 |
|
| 389 |
else: |
| 390 |
# If nothing needs to change just say all is well |
| 391 |
rc = 0 |
| 392 |
err = '' |
| 393 |
out = '' |
| 394 |
return rc, out, err |
| 395 |
|
| 396 |
def service_enable_rcconf(self): |
| 397 |
if self.rcconf_file is None or self.rcconf_key is None or self.rcconf_value is None: |
| 398 |
self.module.fail_json(msg="service_enable_rcconf() requires rcconf_file, rcconf_key and rcconf_value") |
| 399 |
|
| 400 |
self.changed = None |
| 401 |
entry = '%s="%s"\n' % (self.rcconf_key, self.rcconf_value) |
| 402 |
with open(self.rcconf_file, "r") as RCFILE: |
| 403 |
new_rc_conf = [] |
| 404 |
|
| 405 |
# Build a list containing the possibly modified file. |
| 406 |
for rcline in RCFILE: |
| 407 |
# Parse line removing whitespaces, quotes, etc. |
| 408 |
rcarray = shlex.split(rcline, comments=True) |
| 409 |
if len(rcarray) >= 1 and '=' in rcarray[0]: |
| 410 |
(key, value) = rcarray[0].split("=", 1) |
| 411 |
if key == self.rcconf_key: |
| 412 |
if value.upper() == self.rcconf_value: |
| 413 |
# Since the proper entry already exists we can stop iterating. |
| 414 |
self.changed = False |
| 415 |
break |
| 416 |
else: |
| 417 |
# We found the key but the value is wrong, replace with new entry. |
| 418 |
rcline = entry |
| 419 |
self.changed = True |
| 420 |
|
| 421 |
# Add line to the list. |
| 422 |
new_rc_conf.append(rcline.strip() + '\n') |
| 423 |
|
| 424 |
# If we did not see any trace of our entry we need to add it. |
| 425 |
if self.changed is None: |
| 426 |
new_rc_conf.append(entry) |
| 427 |
self.changed = True |
| 428 |
|
| 429 |
if self.changed is True: |
| 430 |
|
| 431 |
if self.module.check_mode: |
| 432 |
self.module.exit_json(changed=True, msg="changing service enablement") |
| 433 |
|
| 434 |
# Create a temporary file next to the current rc.conf (so we stay on the same filesystem). |
| 435 |
# This way the replacement operation is atomic. |
| 436 |
rcconf_dir = os.path.dirname(self.rcconf_file) |
| 437 |
rcconf_base = os.path.basename(self.rcconf_file) |
| 438 |
(TMP_RCCONF, tmp_rcconf_file) = tempfile.mkstemp(dir=rcconf_dir, prefix="%s-" % rcconf_base) |
| 439 |
|
| 440 |
# Write out the contents of the list into our temporary file. |
| 441 |
for rcline in new_rc_conf: |
| 442 |
os.write(TMP_RCCONF, rcline.encode()) |
| 443 |
|
| 444 |
# Close temporary file. |
| 445 |
os.close(TMP_RCCONF) |
| 446 |
|
| 447 |
# Replace previous rc.conf. |
| 448 |
self.module.atomic_move(tmp_rcconf_file, self.rcconf_file) |
| 449 |
|
| 450 |
|
| 451 |
class LinuxService(Service): |
| 452 |
""" |
| 453 |
This is the Linux Service manipulation class - it is currently supporting |
| 454 |
a mixture of binaries and init scripts for controlling services started at |
| 455 |
boot, as well as for controlling the current state. |
| 456 |
""" |
| 457 |
|
| 458 |
platform = 'Linux' |
| 459 |
distribution = None |
| 460 |
|
| 461 |
def get_service_tools(self): |
| 462 |
|
| 463 |
paths = ['/sbin', '/usr/sbin', '/bin', '/usr/bin'] |
| 464 |
binaries = ['service', 'chkconfig', 'update-rc.d', 'rc-service', 'rc-update', 'initctl', 'systemctl', 'start', 'stop', 'restart', 'insserv'] |
| 465 |
initpaths = ['/etc/init.d'] |
| 466 |
location = dict() |
| 467 |
|
| 468 |
for binary in binaries: |
| 469 |
location[binary] = self.module.get_bin_path(binary, opt_dirs=paths) |
| 470 |
|
| 471 |
for initdir in initpaths: |
| 472 |
initscript = "%s/%s" % (initdir, self.name) |
| 473 |
if os.path.isfile(initscript): |
| 474 |
self.svc_initscript = initscript |
| 475 |
|
| 476 |
def check_systemd(): |
| 477 |
|
| 478 |
# tools must be installed |
| 479 |
if location.get('systemctl', False): |
| 480 |
return is_systemd_managed(self.module) |
| 481 |
return False |
| 482 |
|
| 483 |
# Locate a tool to enable/disable a service |
| 484 |
if check_systemd(): |
| 485 |
# service is managed by systemd |
| 486 |
self.__systemd_unit = self.name |
| 487 |
self.svc_cmd = location['systemctl'] |
| 488 |
self.enable_cmd = location['systemctl'] |
| 489 |
|
| 490 |
elif location.get('initctl', False) and os.path.exists("/etc/init/%s.conf" % self.name): |
| 491 |
# service is managed by upstart |
| 492 |
self.enable_cmd = location['initctl'] |
| 493 |
# set the upstart version based on the output of 'initctl version' |
| 494 |
self.upstart_version = LooseVersion('0.0.0') |
| 495 |
try: |
| 496 |
version_re = re.compile(r'\(upstart (.*)\)') |
| 497 |
rc, stdout, stderr = self.module.run_command('%s version' % location['initctl']) |
| 498 |
if rc == 0: |
| 499 |
res = version_re.search(stdout) |
| 500 |
if res: |
| 501 |
self.upstart_version = LooseVersion(res.groups()[0]) |
| 502 |
except Exception: |
| 503 |
pass # we'll use the default of 0.0.0 |
| 504 |
|
| 505 |
self.svc_cmd = location['initctl'] |
| 506 |
|
| 507 |
elif location.get('rc-service', False): |
| 508 |
# service is managed by OpenRC |
| 509 |
self.svc_cmd = location['rc-service'] |
| 510 |
self.enable_cmd = location['rc-update'] |
| 511 |
return # already have service start/stop tool too! |
| 512 |
|
| 513 |
elif self.svc_initscript: |
| 514 |
# service is managed by with SysV init scripts |
| 515 |
if location.get('update-rc.d', False): |
| 516 |
# and uses update-rc.d |
| 517 |
self.enable_cmd = location['update-rc.d'] |
| 518 |
elif location.get('insserv', None): |
| 519 |
# and uses insserv |
| 520 |
self.enable_cmd = location['insserv'] |
| 521 |
elif location.get('chkconfig', False): |
| 522 |
# and uses chkconfig |
| 523 |
self.enable_cmd = location['chkconfig'] |
| 524 |
|
| 525 |
if self.enable_cmd is None: |
| 526 |
fail_if_missing(self.module, False, self.name, msg='host') |
| 527 |
|
| 528 |
# If no service control tool selected yet, try to see if 'service' is available |
| 529 |
if self.svc_cmd is None and location.get('service', False): |
| 530 |
self.svc_cmd = location['service'] |
| 531 |
|
| 532 |
# couldn't find anything yet |
| 533 |
if self.svc_cmd is None and not self.svc_initscript: |
| 534 |
self.module.fail_json(msg='cannot find \'service\' binary or init script for service, possible typo in service name?, aborting') |
| 535 |
|
| 536 |
if location.get('initctl', False): |
| 537 |
self.svc_initctl = location['initctl'] |
| 538 |
|
| 539 |
def get_systemd_service_enabled(self): |
| 540 |
def sysv_exists(name): |
| 541 |
script = '/etc/init.d/' + name |
| 542 |
return os.access(script, os.X_OK) |
| 543 |
|
| 544 |
def sysv_is_enabled(name): |
| 545 |
return bool(glob.glob('/etc/rc?.d/S??' + name)) |
| 546 |
|
| 547 |
service_name = self.__systemd_unit |
| 548 |
(rc, out, err) = self.execute_command("%s is-enabled %s" % (self.enable_cmd, service_name,)) |
| 549 |
if rc == 0: |
| 550 |
return True |
| 551 |
elif out.startswith('disabled'): |
| 552 |
return False |
| 553 |
elif sysv_exists(service_name): |
| 554 |
return sysv_is_enabled(service_name) |
| 555 |
else: |
| 556 |
return False |
| 557 |
|
| 558 |
def get_systemd_status_dict(self): |
| 559 |
|
| 560 |
# Check status first as show will not fail if service does not exist |
| 561 |
(rc, out, err) = self.execute_command("%s show '%s'" % (self.enable_cmd, self.__systemd_unit,)) |
| 562 |
if rc != 0: |
| 563 |
self.module.fail_json(msg='failure %d running systemctl show for %r: %s' % (rc, self.__systemd_unit, err)) |
| 564 |
elif 'LoadState=not-found' in out: |
| 565 |
self.module.fail_json(msg='systemd could not find the requested service "%r": %s' % (self.__systemd_unit, err)) |
| 566 |
|
| 567 |
key = None |
| 568 |
value_buffer = [] |
| 569 |
status_dict = {} |
| 570 |
for line in out.splitlines(): |
| 571 |
if '=' in line: |
| 572 |
if not key: |
| 573 |
key, value = line.split('=', 1) |
| 574 |
# systemd fields that are shell commands can be multi-line |
| 575 |
# We take a value that begins with a "{" as the start of |
| 576 |
# a shell command and a line that ends with "}" as the end of |
| 577 |
# the command |
| 578 |
if value.lstrip().startswith('{'): |
| 579 |
if value.rstrip().endswith('}'): |
| 580 |
status_dict[key] = value |
| 581 |
key = None |
| 582 |
else: |
| 583 |
value_buffer.append(value) |
| 584 |
else: |
| 585 |
status_dict[key] = value |
| 586 |
key = None |
| 587 |
else: |
| 588 |
if line.rstrip().endswith('}'): |
| 589 |
status_dict[key] = '\n'.join(value_buffer) |
| 590 |
key = None |
| 591 |
else: |
| 592 |
value_buffer.append(value) |
| 593 |
else: |
| 594 |
value_buffer.append(value) |
| 595 |
|
| 596 |
return status_dict |
| 597 |
|
| 598 |
def get_systemd_service_status(self): |
| 599 |
d = self.get_systemd_status_dict() |
| 600 |
if d.get('ActiveState') == 'active': |
| 601 |
# run-once services (for which a single successful exit indicates |
| 602 |
# that they are running as designed) should not be restarted here. |
| 603 |
# Thus, we are not checking d['SubState']. |
| 604 |
self.running = True |
| 605 |
self.crashed = False |
| 606 |
elif d.get('ActiveState') == 'failed': |
| 607 |
self.running = False |
| 608 |
self.crashed = True |
| 609 |
elif d.get('ActiveState') is None: |
| 610 |
self.module.fail_json(msg='No ActiveState value in systemctl show output for %r' % (self.__systemd_unit,)) |
| 611 |
else: |
| 612 |
self.running = False |
| 613 |
self.crashed = False |
| 614 |
return self.running |
| 615 |
|
| 616 |
def get_service_status(self): |
| 617 |
if self.svc_cmd and self.svc_cmd.endswith('systemctl'): |
| 618 |
return self.get_systemd_service_status() |
| 619 |
|
| 620 |
self.action = "status" |
| 621 |
rc, status_stdout, status_stderr = self.service_control() |
| 622 |
|
| 623 |
# if we have decided the service is managed by upstart, we check for some additional output... |
| 624 |
if self.svc_initctl and self.running is None: |
| 625 |
# check the job status by upstart response |
| 626 |
initctl_rc, initctl_status_stdout, initctl_status_stderr = self.execute_command("%s status %s %s" % (self.svc_initctl, self.name, self.arguments)) |
| 627 |
if "stop/waiting" in initctl_status_stdout: |
| 628 |
self.running = False |
| 629 |
elif "start/running" in initctl_status_stdout: |
| 630 |
self.running = True |
| 631 |
|
| 632 |
if self.svc_cmd and self.svc_cmd.endswith("rc-service") and self.running is None: |
| 633 |
openrc_rc, openrc_status_stdout, openrc_status_stderr = self.execute_command("%s %s status" % (self.svc_cmd, self.name)) |
| 634 |
self.running = "started" in openrc_status_stdout |
| 635 |
self.crashed = "crashed" in openrc_status_stderr |
| 636 |
|
| 637 |
# Prefer a non-zero return code. For reference, see: |
| 638 |
# http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html |
| 639 |
if self.running is None and rc in [1, 2, 3, 4, 69]: |
| 640 |
self.running = False |
| 641 |
|
| 642 |
# if the job status is still not known check it by status output keywords |
| 643 |
# Only check keywords if there's only one line of output (some init |
| 644 |
# scripts will output verbosely in case of error and those can emit |
| 645 |
# keywords that are picked up as false positives |
| 646 |
if self.running is None and status_stdout.count('\n') <= 1: |
| 647 |
# first transform the status output that could irritate keyword matching |
| 648 |
cleanout = status_stdout.lower().replace(self.name.lower(), '') |
| 649 |
if "stop" in cleanout: |
| 650 |
self.running = False |
| 651 |
elif "run" in cleanout: |
| 652 |
self.running = not ("not " in cleanout) |
| 653 |
elif "start" in cleanout and "not " not in cleanout: |
| 654 |
self.running = True |
| 655 |
elif 'could not access pid file' in cleanout: |
| 656 |
self.running = False |
| 657 |
elif 'is dead and pid file exists' in cleanout: |
| 658 |
self.running = False |
| 659 |
elif 'dead but subsys locked' in cleanout: |
| 660 |
self.running = False |
| 661 |
elif 'dead but pid file exists' in cleanout: |
| 662 |
self.running = False |
| 663 |
|
| 664 |
# if the job status is still not known and we got a zero for the |
| 665 |
# return code, assume here that the service is running |
| 666 |
if self.running is None and rc == 0: |
| 667 |
self.running = True |
| 668 |
|
| 669 |
# if the job status is still not known check it by special conditions |
| 670 |
if self.running is None: |
| 671 |
if self.name == 'iptables' and "ACCEPT" in status_stdout: |
| 672 |
# iptables status command output is lame |
| 673 |
# TODO: lookup if we can use a return code for this instead? |
| 674 |
self.running = True |
| 675 |
|
| 676 |
return self.running |
| 677 |
|
| 678 |
def service_enable(self): |
| 679 |
|
| 680 |
if self.enable_cmd is None: |
| 681 |
self.module.fail_json(msg='cannot detect command to enable service %s, typo or init system potentially unknown' % self.name) |
| 682 |
|
| 683 |
self.changed = True |
| 684 |
action = None |
| 685 |
|
| 686 |
# |
| 687 |
# Upstart's initctl |
| 688 |
# |
| 689 |
if self.enable_cmd.endswith("initctl"): |
| 690 |
def write_to_override_file(file_name, file_contents, ): |
| 691 |
with open(file_name, 'w') as override_file: |
| 692 |
override_file.write(file_contents) |
| 693 |
|
| 694 |
initpath = '/etc/init' |
| 695 |
if self.upstart_version >= LooseVersion('0.6.7'): |
| 696 |
manreg = re.compile(r'^manual\s*$', re.M | re.I) |
| 697 |
config_line = 'manual\n' |
| 698 |
else: |
| 699 |
manreg = re.compile(r'^start on manual\s*$', re.M | re.I) |
| 700 |
config_line = 'start on manual\n' |
| 701 |
conf_file_name = "%s/%s.conf" % (initpath, self.name) |
| 702 |
override_file_name = "%s/%s.override" % (initpath, self.name) |
| 703 |
|
| 704 |
# Check to see if files contain the manual line in .conf and fail if True |
| 705 |
with open(conf_file_name) as conf_file_fh: |
| 706 |
conf_file_content = conf_file_fh.read() |
| 707 |
if manreg.search(conf_file_content): |
| 708 |
self.module.fail_json(msg="manual stanza not supported in a .conf file") |
| 709 |
|
| 710 |
self.changed = False |
| 711 |
if os.path.exists(override_file_name): |
| 712 |
with open(override_file_name) as override_fh: |
| 713 |
override_file_contents = override_fh.read() |
| 714 |
# Remove manual stanza if present and service enabled |
| 715 |
if self.enable and manreg.search(override_file_contents): |
| 716 |
self.changed = True |
| 717 |
override_state = manreg.sub('', override_file_contents) |
| 718 |
# Add manual stanza if not present and service disabled |
| 719 |
elif not (self.enable) and not (manreg.search(override_file_contents)): |
| 720 |
self.changed = True |
| 721 |
override_state = '\n'.join((override_file_contents, config_line)) |
| 722 |
# service already in desired state |
| 723 |
else: |
| 724 |
pass |
| 725 |
# Add file with manual stanza if service disabled |
| 726 |
elif not (self.enable): |
| 727 |
self.changed = True |
| 728 |
override_state = config_line |
| 729 |
else: |
| 730 |
# service already in desired state |
| 731 |
pass |
| 732 |
|
| 733 |
if self.module.check_mode: |
| 734 |
self.module.exit_json(changed=self.changed) |
| 735 |
|
| 736 |
# The initctl method of enabling and disabling services is much |
| 737 |
# different than for the other service methods. So actually |
| 738 |
# committing the change is done in this conditional and then we |
| 739 |
# skip the boilerplate at the bottom of the method |
| 740 |
if self.changed: |
| 741 |
try: |
| 742 |
write_to_override_file(override_file_name, override_state) |
| 743 |
except Exception: |
| 744 |
self.module.fail_json(msg='Could not modify override file') |
| 745 |
|
| 746 |
return |
| 747 |
|
| 748 |
# |
| 749 |
# SysV's chkconfig |
| 750 |
# |
| 751 |
if self.enable_cmd.endswith("chkconfig"): |
| 752 |
if self.enable: |
| 753 |
action = 'on' |
| 754 |
else: |
| 755 |
action = 'off' |
| 756 |
|
| 757 |
(rc, out, err) = self.execute_command("%s --list %s" % (self.enable_cmd, self.name)) |
| 758 |
if 'chkconfig --add %s' % self.name in err: |
| 759 |
self.execute_command("%s --add %s" % (self.enable_cmd, self.name)) |
| 760 |
(rc, out, err) = self.execute_command("%s --list %s" % (self.enable_cmd, self.name)) |
| 761 |
if self.name not in out: |
| 762 |
self.module.fail_json(msg="service %s does not support chkconfig" % self.name) |
| 763 |
# TODO: look back on why this is here |
| 764 |
# state = out.split()[-1] |
| 765 |
|
| 766 |
# Check if we're already in the correct state |
| 767 |
if "3:%s" % action in out and "5:%s" % action in out: |
| 768 |
self.changed = False |
| 769 |
return |
| 770 |
|
| 771 |
# |
| 772 |
# Systemd's systemctl |
| 773 |
# |
| 774 |
if self.enable_cmd.endswith("systemctl"): |
| 775 |
if self.enable: |
| 776 |
action = 'enable' |
| 777 |
else: |
| 778 |
action = 'disable' |
| 779 |
|
| 780 |
# Check if we're already in the correct state |
| 781 |
service_enabled = self.get_systemd_service_enabled() |
| 782 |
|
| 783 |
# self.changed should already be true |
| 784 |
if self.enable == service_enabled: |
| 785 |
self.changed = False |
| 786 |
return |
| 787 |
|
| 788 |
# |
| 789 |
# OpenRC's rc-update |
| 790 |
# |
| 791 |
if self.enable_cmd.endswith("rc-update"): |
| 792 |
if self.enable: |
| 793 |
action = 'add' |
| 794 |
else: |
| 795 |
action = 'delete' |
| 796 |
|
| 797 |
(rc, out, err) = self.execute_command("%s show" % self.enable_cmd) |
| 798 |
for line in out.splitlines(): |
| 799 |
service_name, runlevels = line.split('|') |
| 800 |
service_name = service_name.strip() |
| 801 |
if service_name != self.name: |
| 802 |
continue |
| 803 |
runlevels = re.split(r'\s+', runlevels) |
| 804 |
# service already enabled for the runlevel |
| 805 |
if self.enable and self.runlevel in runlevels: |
| 806 |
self.changed = False |
| 807 |
# service already disabled for the runlevel |
| 808 |
elif not self.enable and self.runlevel not in runlevels: |
| 809 |
self.changed = False |
| 810 |
break |
| 811 |
else: |
| 812 |
# service already disabled altogether |
| 813 |
if not self.enable: |
| 814 |
self.changed = False |
| 815 |
|
| 816 |
if not self.changed: |
| 817 |
return |
| 818 |
|
| 819 |
# |
| 820 |
# update-rc.d style |
| 821 |
# |
| 822 |
if self.enable_cmd.endswith("update-rc.d"): |
| 823 |
|
| 824 |
enabled = False |
| 825 |
slinks = glob.glob('/etc/rc?.d/S??' + self.name) |
| 826 |
if slinks: |
| 827 |
enabled = True |
| 828 |
|
| 829 |
if self.enable != enabled: |
| 830 |
self.changed = True |
| 831 |
|
| 832 |
if self.enable: |
| 833 |
action = 'enable' |
| 834 |
klinks = glob.glob('/etc/rc?.d/K??' + self.name) |
| 835 |
if not klinks: |
| 836 |
if not self.module.check_mode: |
| 837 |
(rc, out, err) = self.execute_command("%s %s defaults" % (self.enable_cmd, self.name)) |
| 838 |
if rc != 0: |
| 839 |
if err: |
| 840 |
self.module.fail_json(msg=err) |
| 841 |
else: |
| 842 |
self.module.fail_json(msg=out) % (self.enable_cmd, self.name, action) |
| 843 |
else: |
| 844 |
action = 'disable' |
| 845 |
|
| 846 |
if not self.module.check_mode: |
| 847 |
(rc, out, err) = self.execute_command("%s %s %s" % (self.enable_cmd, self.name, action)) |
| 848 |
if rc != 0: |
| 849 |
if err: |
| 850 |
self.module.fail_json(msg=err) |
| 851 |
else: |
| 852 |
self.module.fail_json(msg=out) % (self.enable_cmd, self.name, action) |
| 853 |
else: |
| 854 |
self.changed = False |
| 855 |
|
| 856 |
return |
| 857 |
|
| 858 |
# |
| 859 |
# insserv (Debian <=7, SLES, others) |
| 860 |
# |
| 861 |
if self.enable_cmd.endswith("insserv"): |
| 862 |
if self.enable: |
| 863 |
(rc, out, err) = self.execute_command("%s -n -v %s" % (self.enable_cmd, self.name)) |
| 864 |
else: |
| 865 |
(rc, out, err) = self.execute_command("%s -n -r -v %s" % (self.enable_cmd, self.name)) |
| 866 |
|
| 867 |
self.changed = False |
| 868 |
for line in err.splitlines(): |
| 869 |
if self.enable and line.find('enable service') != -1: |
| 870 |
self.changed = True |
| 871 |
break |
| 872 |
if not self.enable and line.find('remove service') != -1: |
| 873 |
self.changed = True |
| 874 |
break |
| 875 |
|
| 876 |
if self.module.check_mode: |
| 877 |
self.module.exit_json(changed=self.changed) |
| 878 |
|
| 879 |
if not self.changed: |
| 880 |
return |
| 881 |
|
| 882 |
if self.enable: |
| 883 |
(rc, out, err) = self.execute_command("%s %s" % (self.enable_cmd, self.name)) |
| 884 |
if (rc != 0) or (err != ''): |
| 885 |
self.module.fail_json(msg=("Failed to install service. rc: %s, out: %s, err: %s" % (rc, out, err))) |
| 886 |
return (rc, out, err) |
| 887 |
else: |
| 888 |
(rc, out, err) = self.execute_command("%s -r %s" % (self.enable_cmd, self.name)) |
| 889 |
if (rc != 0) or (err != ''): |
| 890 |
self.module.fail_json(msg=("Failed to remove service. rc: %s, out: %s, err: %s" % (rc, out, err))) |
| 891 |
return (rc, out, err) |
| 892 |
|
| 893 |
# |
| 894 |
# If we've gotten to the end, the service needs to be updated |
| 895 |
# |
| 896 |
self.changed = True |
| 897 |
|
| 898 |
# we change argument order depending on real binary used: |
| 899 |
# rc-update and systemctl need the argument order reversed |
| 900 |
|
| 901 |
if self.enable_cmd.endswith("rc-update"): |
| 902 |
args = (self.enable_cmd, action, self.name + " " + self.runlevel) |
| 903 |
elif self.enable_cmd.endswith("systemctl"): |
| 904 |
args = (self.enable_cmd, action, self.__systemd_unit) |
| 905 |
else: |
| 906 |
args = (self.enable_cmd, self.name, action) |
| 907 |
|
| 908 |
if self.module.check_mode: |
| 909 |
self.module.exit_json(changed=self.changed) |
| 910 |
|
| 911 |
(rc, out, err) = self.execute_command("%s %s %s" % args) |
| 912 |
if rc != 0: |
| 913 |
if err: |
| 914 |
self.module.fail_json(msg="Error when trying to %s %s: rc=%s %s" % (action, self.name, rc, err)) |
| 915 |
else: |
| 916 |
self.module.fail_json(msg="Failure for %s %s: rc=%s %s" % (action, self.name, rc, out)) |
| 917 |
|
| 918 |
return (rc, out, err) |
| 919 |
|
| 920 |
def service_control(self): |
| 921 |
|
| 922 |
# Decide what command to run |
| 923 |
svc_cmd = '' |
| 924 |
arguments = self.arguments |
| 925 |
if self.svc_cmd: |
| 926 |
if not self.svc_cmd.endswith("systemctl"): |
| 927 |
if self.svc_cmd.endswith("initctl"): |
| 928 |
# initctl commands take the form <cmd> <action> <name> |
| 929 |
svc_cmd = self.svc_cmd |
| 930 |
arguments = "%s %s" % (self.name, arguments) |
| 931 |
else: |
| 932 |
# SysV and OpenRC take the form <cmd> <name> <action> |
| 933 |
svc_cmd = "%s %s" % (self.svc_cmd, self.name) |
| 934 |
else: |
| 935 |
# systemd commands take the form <cmd> <action> <name> |
| 936 |
svc_cmd = self.svc_cmd |
| 937 |
arguments = "%s %s" % (self.__systemd_unit, arguments) |
| 938 |
elif self.svc_cmd is None and self.svc_initscript: |
| 939 |
# upstart |
| 940 |
svc_cmd = "%s" % self.svc_initscript |
| 941 |
|
| 942 |
# In OpenRC, if a service crashed, we need to reset its status to |
| 943 |
# stopped with the zap command, before we can start it back. |
| 944 |
if self.svc_cmd and self.svc_cmd.endswith('rc-service') and self.action == 'start' and self.crashed: |
| 945 |
self.execute_command("%s zap" % svc_cmd, daemonize=True) |
| 946 |
|
| 947 |
if self.action != "restart": |
| 948 |
if svc_cmd != '': |
| 949 |
# upstart or systemd or OpenRC |
| 950 |
rc_state, stdout, stderr = self.execute_command("%s %s %s" % (svc_cmd, self.action, arguments), daemonize=True) |
| 951 |
else: |
| 952 |
# SysV |
| 953 |
rc_state, stdout, stderr = self.execute_command("%s %s %s" % (self.action, self.name, arguments), daemonize=True) |
| 954 |
elif self.svc_cmd and self.svc_cmd.endswith('rc-service'): |
| 955 |
# All services in OpenRC support restart. |
| 956 |
rc_state, stdout, stderr = self.execute_command("%s %s %s" % (svc_cmd, self.action, arguments), daemonize=True) |
| 957 |
else: |
| 958 |
# In other systems, not all services support restart. Do it the hard way. |
| 959 |
if svc_cmd != '': |
| 960 |
# upstart or systemd |
| 961 |
rc1, stdout1, stderr1 = self.execute_command("%s %s %s" % (svc_cmd, 'stop', arguments), daemonize=True) |
| 962 |
else: |
| 963 |
# SysV |
| 964 |
rc1, stdout1, stderr1 = self.execute_command("%s %s %s" % ('stop', self.name, arguments), daemonize=True) |
| 965 |
|
| 966 |
if self.sleep: |
| 967 |
time.sleep(self.sleep) |
| 968 |
|
| 969 |
if svc_cmd != '': |
| 970 |
# upstart or systemd |
| 971 |
rc2, stdout2, stderr2 = self.execute_command("%s %s %s" % (svc_cmd, 'start', arguments), daemonize=True) |
| 972 |
else: |
| 973 |
# SysV |
| 974 |
rc2, stdout2, stderr2 = self.execute_command("%s %s %s" % ('start', self.name, arguments), daemonize=True) |
| 975 |
|
| 976 |
# merge return information |
| 977 |
if rc1 != 0 and rc2 == 0: |
| 978 |
rc_state = rc2 |
| 979 |
stdout = stdout2 |
| 980 |
stderr = stderr2 |
| 981 |
else: |
| 982 |
rc_state = rc1 + rc2 |
| 983 |
stdout = stdout1 + stdout2 |
| 984 |
stderr = stderr1 + stderr2 |
| 985 |
|
| 986 |
return (rc_state, stdout, stderr) |
| 987 |
|
| 988 |
|
| 989 |
class HurdService(LinuxService): |
| 990 |
""" |
| 991 |
This is the GNU Hurd Service manipulation class - it uses SysV init scripts |
| 992 |
and should be broadly compatible with the Linux Service manipulation class. |
| 993 |
""" |
| 994 |
|
| 995 |
platform = 'GNU' |
| 996 |
distribution = None |
| 997 |
|
| 998 |
|
| 999 |
class FreeBsdService(Service): |
| 1000 |
""" |
| 1001 |
This is the FreeBSD Service manipulation class - it uses the /etc/rc.conf |
| 1002 |
file for controlling services started at boot and the 'service' binary to |
| 1003 |
check status and perform direct service manipulation. |
| 1004 |
""" |
| 1005 |
|
| 1006 |
platform = 'FreeBSD' |
| 1007 |
distribution = None |
| 1008 |
|
| 1009 |
def get_service_tools(self): |
| 1010 |
self.svc_cmd = self.module.get_bin_path('service', True) |
| 1011 |
if not self.svc_cmd: |
| 1012 |
self.module.fail_json(msg='unable to find service binary') |
| 1013 |
|
| 1014 |
self.sysrc_cmd = self.module.get_bin_path('sysrc') |
| 1015 |
|
| 1016 |
def get_service_status(self): |
| 1017 |
rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.svc_cmd, self.arguments, self.name, 'onestatus')) |
| 1018 |
if self.name == "pf": |
| 1019 |
self.running = "Enabled" in stdout |
| 1020 |
else: |
| 1021 |
if rc == 1: |
| 1022 |
self.running = False |
| 1023 |
elif rc == 0: |
| 1024 |
self.running = True |
| 1025 |
|
| 1026 |
def service_enable(self): |
| 1027 |
if self.enable: |
| 1028 |
self.rcconf_value = "YES" |
| 1029 |
else: |
| 1030 |
self.rcconf_value = "NO" |
| 1031 |
|
| 1032 |
rcfiles = ['/etc/rc.conf', '/etc/rc.conf.local', '/usr/local/etc/rc.conf'] |
| 1033 |
for rcfile in rcfiles: |
| 1034 |
if os.path.isfile(rcfile): |
| 1035 |
self.rcconf_file = rcfile |
| 1036 |
|
| 1037 |
rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.svc_cmd, self.arguments, self.name, 'rcvar')) |
| 1038 |
try: |
| 1039 |
rcvars = shlex.split(stdout, comments=True) |
| 1040 |
except Exception: |
| 1041 |
# TODO: add a warning to the output with the failure |
| 1042 |
pass |
| 1043 |
|
| 1044 |
if not rcvars: |
| 1045 |
self.module.fail_json(msg="unable to determine rcvar", stdout=stdout, stderr=stderr) |
| 1046 |
|
| 1047 |
# In rare cases, i.e. sendmail, rcvar can return several key=value pairs |
| 1048 |
# Usually there is just one, however. In other rare cases, i.e. uwsgi, |
| 1049 |
# rcvar can return extra uncommented data that is not at all related to |
| 1050 |
# the rcvar. We will just take the first key=value pair we come across |
| 1051 |
# and hope for the best. |
| 1052 |
for rcvar in rcvars: |
| 1053 |
if '=' in rcvar: |
| 1054 |
self.rcconf_key, default_rcconf_value = rcvar.split('=', 1) |
| 1055 |
break |
| 1056 |
|
| 1057 |
if self.rcconf_key is None: |
| 1058 |
self.module.fail_json(msg="unable to determine rcvar", stdout=stdout, stderr=stderr) |
| 1059 |
|
| 1060 |
if self.sysrc_cmd: # FreeBSD >= 9.2 |
| 1061 |
|
| 1062 |
rc, current_rcconf_value, stderr = self.execute_command("%s -n %s" % (self.sysrc_cmd, self.rcconf_key)) |
| 1063 |
# it can happen that rcvar is not set (case of a system coming from the ports collection) |
| 1064 |
# so we will fallback on the default |
| 1065 |
if rc != 0: |
| 1066 |
current_rcconf_value = default_rcconf_value |
| 1067 |
|
| 1068 |
if current_rcconf_value.strip().upper() != self.rcconf_value: |
| 1069 |
|
| 1070 |
self.changed = True |
| 1071 |
|
| 1072 |
if self.module.check_mode: |
| 1073 |
self.module.exit_json(changed=True, msg="changing service enablement") |
| 1074 |
|
| 1075 |
rc, change_stdout, change_stderr = self.execute_command("%s %s=\"%s\"" % (self.sysrc_cmd, self.rcconf_key, self.rcconf_value)) |
| 1076 |
if rc != 0: |
| 1077 |
self.module.fail_json(msg="unable to set rcvar using sysrc", stdout=change_stdout, stderr=change_stderr) |
| 1078 |
|
| 1079 |
# sysrc does not exit with code 1 on permission error => validate successful change using service(8) |
| 1080 |
rc, check_stdout, check_stderr = self.execute_command("%s %s %s" % (self.svc_cmd, self.name, "enabled")) |
| 1081 |
if self.enable != (rc == 0): # rc = 0 indicates enabled service, rc = 1 indicates disabled service |
| 1082 |
self.module.fail_json(msg="unable to set rcvar: sysrc did not change value", stdout=change_stdout, stderr=change_stderr) |
| 1083 |
|
| 1084 |
else: |
| 1085 |
self.changed = False |
| 1086 |
|
| 1087 |
else: # Legacy (FreeBSD < 9.2) |
| 1088 |
try: |
| 1089 |
return self.service_enable_rcconf() |
| 1090 |
except Exception: |
| 1091 |
self.module.fail_json(msg='unable to set rcvar') |
| 1092 |
|
| 1093 |
def service_control(self): |
| 1094 |
|
| 1095 |
if self.action == "start": |
| 1096 |
self.action = "onestart" |
| 1097 |
if self.action == "stop": |
| 1098 |
self.action = "onestop" |
| 1099 |
if self.action == "reload": |
| 1100 |
self.action = "onereload" |
| 1101 |
|
| 1102 |
ret = self.execute_command("%s %s %s %s" % (self.svc_cmd, self.arguments, self.name, self.action)) |
| 1103 |
|
| 1104 |
if self.sleep: |
| 1105 |
time.sleep(self.sleep) |
| 1106 |
|
| 1107 |
return ret |
| 1108 |
|
| 1109 |
|
| 1110 |
class DragonFlyBsdService(FreeBsdService): |
| 1111 |
""" |
| 1112 |
This is the DragonFly BSD Service manipulation class - it uses the /etc/rc.conf |
| 1113 |
file for controlling services started at boot and the 'service' binary to |
| 1114 |
check status and perform direct service manipulation. |
| 1115 |
""" |
| 1116 |
|
| 1117 |
platform = 'DragonFly' |
| 1118 |
distribution = None |
| 1119 |
|
| 1120 |
def service_enable(self): |
| 1121 |
if self.enable: |
| 1122 |
self.rcconf_value = "YES" |
| 1123 |
else: |
| 1124 |
self.rcconf_value = "NO" |
| 1125 |
|
| 1126 |
rcfiles = ['/etc/rc.conf'] # Overkill? |
| 1127 |
for rcfile in rcfiles: |
| 1128 |
if os.path.isfile(rcfile): |
| 1129 |
self.rcconf_file = rcfile |
| 1130 |
|
| 1131 |
self.rcconf_key = "%s" % self.name.replace("-", "_") |
| 1132 |
|
| 1133 |
return self.service_enable_rcconf() |
| 1134 |
|
| 1135 |
|
| 1136 |
class OpenBsdService(Service): |
| 1137 |
""" |
| 1138 |
This is the OpenBSD Service manipulation class - it uses rcctl(8) or |
| 1139 |
/etc/rc.d scripts for service control. Enabling a service is |
| 1140 |
only supported if rcctl is present. |
| 1141 |
""" |
| 1142 |
|
| 1143 |
platform = 'OpenBSD' |
| 1144 |
distribution = None |
| 1145 |
|
| 1146 |
def get_service_tools(self): |
| 1147 |
self.enable_cmd = self.module.get_bin_path('rcctl') |
| 1148 |
|
| 1149 |
if self.enable_cmd: |
| 1150 |
self.svc_cmd = self.enable_cmd |
| 1151 |
else: |
| 1152 |
rcdir = '/etc/rc.d' |
| 1153 |
|
| 1154 |
rc_script = "%s/%s" % (rcdir, self.name) |
| 1155 |
if os.path.isfile(rc_script): |
| 1156 |
self.svc_cmd = rc_script |
| 1157 |
|
| 1158 |
if not self.svc_cmd: |
| 1159 |
self.module.fail_json(msg='unable to find svc_cmd') |
| 1160 |
|
| 1161 |
def get_service_status(self): |
| 1162 |
if self.enable_cmd: |
| 1163 |
rc, stdout, stderr = self.execute_command("%s %s %s" % (self.svc_cmd, 'check', self.name)) |
| 1164 |
else: |
| 1165 |
rc, stdout, stderr = self.execute_command("%s %s" % (self.svc_cmd, 'check')) |
| 1166 |
|
| 1167 |
if stderr: |
| 1168 |
self.module.fail_json(msg=stderr) |
| 1169 |
|
| 1170 |
if rc == 1: |
| 1171 |
self.running = False |
| 1172 |
elif rc == 0: |
| 1173 |
self.running = True |
| 1174 |
|
| 1175 |
def service_control(self): |
| 1176 |
if self.enable_cmd: |
| 1177 |
return self.execute_command("%s -f %s %s" % (self.svc_cmd, self.action, self.name), daemonize=True) |
| 1178 |
else: |
| 1179 |
return self.execute_command("%s -f %s" % (self.svc_cmd, self.action)) |
| 1180 |
|
| 1181 |
def service_enable(self): |
| 1182 |
|
| 1183 |
if not self.enable_cmd: |
| 1184 |
return super(OpenBsdService, self).service_enable() |
| 1185 |
|
| 1186 |
rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.enable_cmd, 'get', self.name, 'status')) |
| 1187 |
|
| 1188 |
status_action = None |
| 1189 |
if self.enable: |
| 1190 |
if rc != 0: |
| 1191 |
status_action = "on" |
| 1192 |
elif self.enable is not None: |
| 1193 |
# should be explicit False at this point |
| 1194 |
if rc != 1: |
| 1195 |
status_action = "off" |
| 1196 |
|
| 1197 |
if status_action is not None: |
| 1198 |
self.changed = True |
| 1199 |
if not self.module.check_mode: |
| 1200 |
rc, stdout, stderr = self.execute_command("%s set %s status %s" % (self.enable_cmd, self.name, status_action)) |
| 1201 |
|
| 1202 |
if rc != 0: |
| 1203 |
if stderr: |
| 1204 |
self.module.fail_json(msg=stderr) |
| 1205 |
else: |
| 1206 |
self.module.fail_json(msg="rcctl failed to modify service status") |
| 1207 |
|
| 1208 |
|
| 1209 |
class NetBsdService(Service): |
| 1210 |
""" |
| 1211 |
This is the NetBSD Service manipulation class - it uses the /etc/rc.conf |
| 1212 |
file for controlling services started at boot, check status and perform |
| 1213 |
direct service manipulation. Init scripts in /etc/rc.d are used for |
| 1214 |
controlling services (start/stop) as well as for controlling the current |
| 1215 |
state. |
| 1216 |
""" |
| 1217 |
|
| 1218 |
platform = 'NetBSD' |
| 1219 |
distribution = None |
| 1220 |
|
| 1221 |
def get_service_tools(self): |
| 1222 |
initpaths = ['/etc/rc.d'] # better: $rc_directories - how to get in here? Run: sh -c '. /etc/rc.conf ; echo $rc_directories' |
| 1223 |
|
| 1224 |
for initdir in initpaths: |
| 1225 |
initscript = "%s/%s" % (initdir, self.name) |
| 1226 |
if os.path.isfile(initscript): |
| 1227 |
self.svc_initscript = initscript |
| 1228 |
|
| 1229 |
if not self.svc_initscript: |
| 1230 |
self.module.fail_json(msg='unable to find rc.d script') |
| 1231 |
|
| 1232 |
def service_enable(self): |
| 1233 |
if self.enable: |
| 1234 |
self.rcconf_value = "YES" |
| 1235 |
else: |
| 1236 |
self.rcconf_value = "NO" |
| 1237 |
|
| 1238 |
rcfiles = ['/etc/rc.conf'] # Overkill? |
| 1239 |
for rcfile in rcfiles: |
| 1240 |
if os.path.isfile(rcfile): |
| 1241 |
self.rcconf_file = rcfile |
| 1242 |
|
| 1243 |
self.rcconf_key = "%s" % self.name.replace("-", "_") |
| 1244 |
|
| 1245 |
return self.service_enable_rcconf() |
| 1246 |
|
| 1247 |
def get_service_status(self): |
| 1248 |
self.svc_cmd = "%s" % self.svc_initscript |
| 1249 |
rc, stdout, stderr = self.execute_command("%s %s" % (self.svc_cmd, 'onestatus')) |
| 1250 |
if rc == 1: |
| 1251 |
self.running = False |
| 1252 |
elif rc == 0: |
| 1253 |
self.running = True |
| 1254 |
|
| 1255 |
def service_control(self): |
| 1256 |
if self.action == "start": |
| 1257 |
self.action = "onestart" |
| 1258 |
if self.action == "stop": |
| 1259 |
self.action = "onestop" |
| 1260 |
|
| 1261 |
self.svc_cmd = "%s" % self.svc_initscript |
| 1262 |
return self.execute_command("%s %s" % (self.svc_cmd, self.action), daemonize=True) |
| 1263 |
|
| 1264 |
|
| 1265 |
class SunOSService(Service): |
| 1266 |
""" |
| 1267 |
This is the SunOS Service manipulation class - it uses the svcadm |
| 1268 |
command for controlling services, and svcs command for checking status. |
| 1269 |
It also tries to be smart about taking the service out of maintenance |
| 1270 |
state if necessary. |
| 1271 |
""" |
| 1272 |
platform = 'SunOS' |
| 1273 |
distribution = None |
| 1274 |
|
| 1275 |
def get_service_tools(self): |
| 1276 |
self.svcs_cmd = self.module.get_bin_path('svcs', True) |
| 1277 |
|
| 1278 |
if not self.svcs_cmd: |
| 1279 |
self.module.fail_json(msg='unable to find svcs binary') |
| 1280 |
|
| 1281 |
self.svcadm_cmd = self.module.get_bin_path('svcadm', True) |
| 1282 |
|
| 1283 |
if not self.svcadm_cmd: |
| 1284 |
self.module.fail_json(msg='unable to find svcadm binary') |
| 1285 |
|
| 1286 |
if self.svcadm_supports_sync(): |
| 1287 |
self.svcadm_sync = '-s' |
| 1288 |
else: |
| 1289 |
self.svcadm_sync = '' |
| 1290 |
|
| 1291 |
def svcadm_supports_sync(self): |
| 1292 |
# Support for synchronous restart/refresh is only supported on |
| 1293 |
# Oracle Solaris >= 11.2 |
| 1294 |
for line in open('/etc/release', 'r').readlines(): |
| 1295 |
m = re.match(r'\s+Oracle Solaris (\d+)\.(\d+).*', line.rstrip()) |
| 1296 |
if m and m.groups() >= ('11', '2'): |
| 1297 |
return True |
| 1298 |
|
| 1299 |
def get_service_status(self): |
| 1300 |
status = self.get_sunos_svcs_status() |
| 1301 |
# Only 'online' is considered properly running. Everything else is off |
| 1302 |
# or has some sort of problem. |
| 1303 |
if status == 'online': |
| 1304 |
self.running = True |
| 1305 |
else: |
| 1306 |
self.running = False |
| 1307 |
|
| 1308 |
def get_sunos_svcs_status(self): |
| 1309 |
rc, stdout, stderr = self.execute_command("%s %s" % (self.svcs_cmd, self.name)) |
| 1310 |
if rc == 1: |
| 1311 |
if stderr: |
| 1312 |
self.module.fail_json(msg=stderr) |
| 1313 |
else: |
| 1314 |
self.module.fail_json(msg=stdout) |
| 1315 |
|
| 1316 |
lines = stdout.rstrip("\n").split("\n") |
| 1317 |
status = lines[-1].split(" ")[0] |
| 1318 |
# status is one of: online, offline, degraded, disabled, maintenance, uninitialized |
| 1319 |
# see man svcs(1) |
| 1320 |
return status |
| 1321 |
|
| 1322 |
def service_enable(self): |
| 1323 |
# Get current service enablement status |
| 1324 |
rc, stdout, stderr = self.execute_command("%s -l %s" % (self.svcs_cmd, self.name)) |
| 1325 |
|
| 1326 |
if rc != 0: |
| 1327 |
if stderr: |
| 1328 |
self.module.fail_json(msg=stderr) |
| 1329 |
else: |
| 1330 |
self.module.fail_json(msg=stdout) |
| 1331 |
|
| 1332 |
enabled = False |
| 1333 |
temporary = False |
| 1334 |
|
| 1335 |
# look for enabled line, which could be one of: |
| 1336 |
# enabled true (temporary) |
| 1337 |
# enabled false (temporary) |
| 1338 |
# enabled true |
| 1339 |
# enabled false |
| 1340 |
for line in stdout.split("\n"): |
| 1341 |
if line.startswith("enabled"): |
| 1342 |
if "true" in line: |
| 1343 |
enabled = True |
| 1344 |
if "temporary" in line: |
| 1345 |
temporary = True |
| 1346 |
|
| 1347 |
startup_enabled = (enabled and not temporary) or (not enabled and temporary) |
| 1348 |
|
| 1349 |
if self.enable and startup_enabled: |
| 1350 |
return |
| 1351 |
elif (not self.enable) and (not startup_enabled): |
| 1352 |
return |
| 1353 |
|
| 1354 |
if not self.module.check_mode: |
| 1355 |
# Mark service as started or stopped (this will have the side effect of |
| 1356 |
# actually stopping or starting the service) |
| 1357 |
if self.enable: |
| 1358 |
subcmd = "enable -rs" |
| 1359 |
else: |
| 1360 |
subcmd = "disable -s" |
| 1361 |
|
| 1362 |
rc, stdout, stderr = self.execute_command("%s %s %s" % (self.svcadm_cmd, subcmd, self.name)) |
| 1363 |
|
| 1364 |
if rc != 0: |
| 1365 |
if stderr: |
| 1366 |
self.module.fail_json(msg=stderr) |
| 1367 |
else: |
| 1368 |
self.module.fail_json(msg=stdout) |
| 1369 |
|
| 1370 |
self.changed = True |
| 1371 |
|
| 1372 |
def service_control(self): |
| 1373 |
status = self.get_sunos_svcs_status() |
| 1374 |
|
| 1375 |
# if starting or reloading, clear maintenance states |
| 1376 |
if self.action in ['start', 'reload', 'restart'] and status in ['maintenance', 'degraded']: |
| 1377 |
rc, stdout, stderr = self.execute_command("%s clear %s" % (self.svcadm_cmd, self.name)) |
| 1378 |
if rc != 0: |
| 1379 |
return rc, stdout, stderr |
| 1380 |
status = self.get_sunos_svcs_status() |
| 1381 |
|
| 1382 |
if status in ['maintenance', 'degraded']: |
| 1383 |
self.module.fail_json(msg="Failed to bring service out of %s status." % status) |
| 1384 |
|
| 1385 |
if self.action == 'start': |
| 1386 |
subcmd = "enable -rst" |
| 1387 |
elif self.action == 'stop': |
| 1388 |
subcmd = "disable -st" |
| 1389 |
elif self.action == 'reload': |
| 1390 |
subcmd = "refresh %s" % (self.svcadm_sync) |
| 1391 |
elif self.action == 'restart' and status == 'online': |
| 1392 |
subcmd = "restart %s" % (self.svcadm_sync) |
| 1393 |
elif self.action == 'restart' and status != 'online': |
| 1394 |
subcmd = "enable -rst" |
| 1395 |
|
| 1396 |
return self.execute_command("%s %s %s" % (self.svcadm_cmd, subcmd, self.name)) |
| 1397 |
|
| 1398 |
|
| 1399 |
class AIX(Service): |
| 1400 |
""" |
| 1401 |
This is the AIX Service (SRC) manipulation class - it uses lssrc, startsrc, stopsrc |
| 1402 |
and refresh for service control. Enabling a service is currently not supported. |
| 1403 |
Would require to add an entry in the /etc/inittab file (mkitab, chitab and rmitab |
| 1404 |
commands) |
| 1405 |
""" |
| 1406 |
|
| 1407 |
platform = 'AIX' |
| 1408 |
distribution = None |
| 1409 |
|
| 1410 |
def get_service_tools(self): |
| 1411 |
self.lssrc_cmd = self.module.get_bin_path('lssrc', True) |
| 1412 |
|
| 1413 |
if not self.lssrc_cmd: |
| 1414 |
self.module.fail_json(msg='unable to find lssrc binary') |
| 1415 |
|
| 1416 |
self.startsrc_cmd = self.module.get_bin_path('startsrc', True) |
| 1417 |
|
| 1418 |
if not self.startsrc_cmd: |
| 1419 |
self.module.fail_json(msg='unable to find startsrc binary') |
| 1420 |
|
| 1421 |
self.stopsrc_cmd = self.module.get_bin_path('stopsrc', True) |
| 1422 |
|
| 1423 |
if not self.stopsrc_cmd: |
| 1424 |
self.module.fail_json(msg='unable to find stopsrc binary') |
| 1425 |
|
| 1426 |
self.refresh_cmd = self.module.get_bin_path('refresh', True) |
| 1427 |
|
| 1428 |
if not self.refresh_cmd: |
| 1429 |
self.module.fail_json(msg='unable to find refresh binary') |
| 1430 |
|
| 1431 |
def get_service_status(self): |
| 1432 |
status = self.get_aix_src_status() |
| 1433 |
# Only 'active' is considered properly running. Everything else is off |
| 1434 |
# or has some sort of problem. |
| 1435 |
if status == 'active': |
| 1436 |
self.running = True |
| 1437 |
else: |
| 1438 |
self.running = False |
| 1439 |
|
| 1440 |
def get_aix_src_status(self): |
| 1441 |
# Check subsystem status |
| 1442 |
rc, stdout, stderr = self.execute_command("%s -s %s" % (self.lssrc_cmd, self.name)) |
| 1443 |
if rc == 1: |
| 1444 |
# If check for subsystem is not ok, check if service name is a |
| 1445 |
# group subsystem |
| 1446 |
rc, stdout, stderr = self.execute_command("%s -g %s" % (self.lssrc_cmd, self.name)) |
| 1447 |
if rc == 1: |
| 1448 |
if stderr: |
| 1449 |
self.module.fail_json(msg=stderr) |
| 1450 |
else: |
| 1451 |
self.module.fail_json(msg=stdout) |
| 1452 |
else: |
| 1453 |
# Check all subsystem status, if one subsystem is not active |
| 1454 |
# the group is considered not active. |
| 1455 |
lines = stdout.splitlines() |
| 1456 |
for state in lines[1:]: |
| 1457 |
if state.split()[-1].strip() != "active": |
| 1458 |
status = state.split()[-1].strip() |
| 1459 |
break |
| 1460 |
else: |
| 1461 |
status = "active" |
| 1462 |
|
| 1463 |
# status is one of: active, inoperative |
| 1464 |
return status |
| 1465 |
else: |
| 1466 |
lines = stdout.rstrip("\n").split("\n") |
| 1467 |
status = lines[-1].split(" ")[-1] |
| 1468 |
|
| 1469 |
# status is one of: active, inoperative |
| 1470 |
return status |
| 1471 |
|
| 1472 |
def service_control(self): |
| 1473 |
|
| 1474 |
# Check if service name is a subsystem of a group subsystem |
| 1475 |
rc, stdout, stderr = self.execute_command("%s -a" % (self.lssrc_cmd)) |
| 1476 |
if rc == 1: |
| 1477 |
if stderr: |
| 1478 |
self.module.fail_json(msg=stderr) |
| 1479 |
else: |
| 1480 |
self.module.fail_json(msg=stdout) |
| 1481 |
else: |
| 1482 |
lines = stdout.splitlines() |
| 1483 |
subsystems = [] |
| 1484 |
groups = [] |
| 1485 |
for line in lines[1:]: |
| 1486 |
subsystem = line.split()[0].strip() |
| 1487 |
group = line.split()[1].strip() |
| 1488 |
subsystems.append(subsystem) |
| 1489 |
if group: |
| 1490 |
groups.append(group) |
| 1491 |
|
| 1492 |
# Define if service name parameter: |
| 1493 |
# -s subsystem or -g group subsystem |
| 1494 |
if self.name in subsystems: |
| 1495 |
srccmd_parameter = "-s" |
| 1496 |
elif self.name in groups: |
| 1497 |
srccmd_parameter = "-g" |
| 1498 |
|
| 1499 |
if self.action == 'start': |
| 1500 |
srccmd = self.startsrc_cmd |
| 1501 |
elif self.action == 'stop': |
| 1502 |
srccmd = self.stopsrc_cmd |
| 1503 |
elif self.action == 'reload': |
| 1504 |
srccmd = self.refresh_cmd |
| 1505 |
elif self.action == 'restart': |
| 1506 |
self.execute_command("%s %s %s" % (self.stopsrc_cmd, srccmd_parameter, self.name)) |
| 1507 |
if self.sleep: |
| 1508 |
time.sleep(self.sleep) |
| 1509 |
srccmd = self.startsrc_cmd |
| 1510 |
|
| 1511 |
if self.arguments and self.action in ('start', 'restart'): |
| 1512 |
return self.execute_command("%s -a \"%s\" %s %s" % (srccmd, self.arguments, srccmd_parameter, self.name)) |
| 1513 |
else: |
| 1514 |
return self.execute_command("%s %s %s" % (srccmd, srccmd_parameter, self.name)) |
| 1515 |
|
| 1516 |
|
| 1517 |
# =========================================== |
| 1518 |
# Main control flow |
| 1519 |
|
| 1520 |
def main(): |
| 1521 |
module = AnsibleModule( |
| 1522 |
argument_spec=dict( |
| 1523 |
name=dict(type='str', required=True), |
| 1524 |
state=dict(type='str', choices=['started', 'stopped', 'reloaded', 'restarted']), |
| 1525 |
sleep=dict(type='int'), |
| 1526 |
pattern=dict(type='str'), |
| 1527 |
enabled=dict(type='bool'), |
| 1528 |
runlevel=dict(type='str', default='default'), |
| 1529 |
arguments=dict(type='str', default='', aliases=['args']), |
| 1530 |
), |
| 1531 |
supports_check_mode=True, |
| 1532 |
required_one_of=[['state', 'enabled']], |
| 1533 |
) |
| 1534 |
|
| 1535 |
service = Service(module) |
| 1536 |
|
| 1537 |
module.debug('Service instantiated - platform %s' % service.platform) |
| 1538 |
if service.distribution: |
| 1539 |
module.debug('Service instantiated - distribution %s' % service.distribution) |
| 1540 |
|
| 1541 |
rc = 0 |
| 1542 |
out = '' |
| 1543 |
err = '' |
| 1544 |
result = {} |
| 1545 |
result['name'] = service.name |
| 1546 |
|
| 1547 |
# Find service management tools |
| 1548 |
service.get_service_tools() |
| 1549 |
|
| 1550 |
# Enable/disable service startup at boot if requested |
| 1551 |
if service.module.params['enabled'] is not None: |
| 1552 |
# FIXME: ideally this should detect if we need to toggle the enablement state, though |
| 1553 |
# it's unlikely the changed handler would need to fire in this case so it's a minor thing. |
| 1554 |
service.service_enable() |
| 1555 |
result['enabled'] = service.enable |
| 1556 |
|
| 1557 |
if module.params['state'] is None: |
| 1558 |
# Not changing the running state, so bail out now. |
| 1559 |
result['changed'] = service.changed |
| 1560 |
module.exit_json(**result) |
| 1561 |
|
| 1562 |
result['state'] = service.state |
| 1563 |
|
| 1564 |
# Collect service status |
| 1565 |
if service.pattern: |
| 1566 |
service.check_ps() |
| 1567 |
else: |
| 1568 |
service.get_service_status() |
| 1569 |
|
| 1570 |
# Calculate if request will change service state |
| 1571 |
service.check_service_changed() |
| 1572 |
|
| 1573 |
# Modify service state if necessary |
| 1574 |
(rc, out, err) = service.modify_service_state() |
| 1575 |
|
| 1576 |
if rc != 0: |
| 1577 |
if err and "Job is already running" in err: |
| 1578 |
# upstart got confused, one such possibility is MySQL on Ubuntu 12.04 |
| 1579 |
# where status may report it has no start/stop links and we could |
| 1580 |
# not get accurate status |
| 1581 |
pass |
| 1582 |
else: |
| 1583 |
if err: |
| 1584 |
module.fail_json(msg=err) |
| 1585 |
else: |
| 1586 |
module.fail_json(msg=out) |
| 1587 |
|
| 1588 |
result['changed'] = service.changed | service.svc_change |
| 1589 |
if service.module.params['enabled'] is not None: |
| 1590 |
result['enabled'] = service.module.params['enabled'] |
| 1591 |
|
| 1592 |
if not service.module.params['state']: |
| 1593 |
status = service.get_service_status() |
| 1594 |
if status is None: |
| 1595 |
result['state'] = 'absent' |
| 1596 |
elif status is False: |
| 1597 |
result['state'] = 'started' |
| 1598 |
else: |
| 1599 |
result['state'] = 'stopped' |
| 1600 |
else: |
| 1601 |
# as we may have just bounced the service the service command may not |
| 1602 |
# report accurate state at this moment so just show what we ran |
| 1603 |
if service.module.params['state'] in ['reloaded', 'restarted', 'started']: |
| 1604 |
result['state'] = 'started' |
| 1605 |
else: |
| 1606 |
result['state'] = 'stopped' |
| 1607 |
|
| 1608 |
module.exit_json(**result) |
| 1609 |
|
| 1610 |
|
| 1611 |
if __name__ == '__main__': |
| 1612 |
main() |