2012-02-26 02:27:11 +01:00
|
|
|
#!/usr/bin/python
|
2012-08-03 03:29:10 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-02-26 02:27:11 +01:00
|
|
|
|
2012-02-29 01:08:09 +01:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-09-30 08:50:25 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: service
|
|
|
|
author: Michael DeHaan
|
2013-03-30 20:44:34 +01:00
|
|
|
version_added: "0.1"
|
2012-09-30 08:50:25 +02:00
|
|
|
short_description: Manage services.
|
|
|
|
description:
|
|
|
|
- Controls services on remote hosts.
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
required: true
|
2012-10-08 18:46:16 +02:00
|
|
|
description:
|
2012-10-01 09:18:54 +02:00
|
|
|
- Name of the service.
|
2012-09-30 08:50:25 +02:00
|
|
|
state:
|
|
|
|
required: false
|
2012-10-13 00:14:09 +02:00
|
|
|
choices: [ started, stopped, restarted, reloaded ]
|
2012-10-08 18:46:16 +02:00
|
|
|
description:
|
2013-01-24 00:32:39 +01:00
|
|
|
- C(started)/C(stopped) are idempotent actions that will not run
|
|
|
|
commands unless necessary. C(restarted) will always bounce the
|
2014-04-07 16:37:15 +02:00
|
|
|
service. C(reloaded) will always reload. B(At least one of state
|
|
|
|
and enabled are required.)
|
2013-07-03 17:08:07 +02:00
|
|
|
sleep:
|
|
|
|
required: false
|
|
|
|
version_added: "1.3"
|
|
|
|
description:
|
|
|
|
- If the service is being C(restarted) then sleep this many seconds
|
|
|
|
between the stop and start command. This helps to workaround badly
|
|
|
|
behaving init scripts that exit immediately after signaling a process
|
|
|
|
to stop.
|
2012-09-30 08:50:25 +02:00
|
|
|
pattern:
|
|
|
|
required: false
|
2012-10-01 09:18:54 +02:00
|
|
|
version_added: "0.7"
|
2012-10-08 18:46:16 +02:00
|
|
|
description:
|
2012-10-01 09:18:54 +02:00
|
|
|
- If the service does not respond to the status command, name a
|
|
|
|
substring to look for as would be found in the output of the I(ps)
|
|
|
|
command as a stand-in for a status result. If the string is found,
|
2012-10-08 18:46:16 +02:00
|
|
|
the service will be assumed to be running.
|
2012-09-30 08:50:25 +02:00
|
|
|
enabled:
|
|
|
|
required: false
|
2012-10-01 09:18:54 +02:00
|
|
|
choices: [ "yes", "no" ]
|
2012-10-08 18:46:16 +02:00
|
|
|
description:
|
2014-04-07 16:37:15 +02:00
|
|
|
- Whether the service should start on boot. B(At least one of state and
|
|
|
|
enabled are required.)
|
2013-04-04 21:00:47 +02:00
|
|
|
|
2013-06-17 05:29:48 +02:00
|
|
|
runlevel:
|
|
|
|
required: false
|
2013-06-19 02:19:29 +02:00
|
|
|
default: 'default'
|
2013-06-17 05:29:48 +02:00
|
|
|
description:
|
2013-06-20 04:37:09 +02:00
|
|
|
- "For OpenRC init scripts (ex: Gentoo) only. The runlevel that this service belongs to."
|
2012-11-15 18:31:40 +01:00
|
|
|
arguments:
|
|
|
|
description:
|
|
|
|
- Additional arguments provided on the command line
|
|
|
|
aliases: [ 'args' ]
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Example action to start service httpd, if not running
|
|
|
|
- service: name=httpd state=started
|
|
|
|
|
|
|
|
# Example action to stop service httpd, if running
|
|
|
|
- service: name=httpd state=stopped
|
|
|
|
|
|
|
|
# Example action to restart service httpd, in all cases
|
|
|
|
- service: name=httpd state=restarted
|
|
|
|
|
|
|
|
# Example action to reload service httpd, in all cases
|
|
|
|
- service: name=httpd state=reloaded
|
|
|
|
|
|
|
|
# Example action to enable service httpd, and not touch the running state
|
|
|
|
- service: name=httpd enabled=yes
|
|
|
|
|
|
|
|
# Example action to start service foo, based on running process /usr/bin/foo
|
|
|
|
- service: name=foo pattern=/usr/bin/foo state=started
|
|
|
|
|
|
|
|
# Example action to restart network service for interface eth0
|
|
|
|
- service: name=network state=restarted args=eth0
|
2012-09-30 08:50:25 +02:00
|
|
|
'''
|
|
|
|
|
2012-08-17 22:55:18 +02:00
|
|
|
import platform
|
2012-10-31 03:06:11 +01:00
|
|
|
import os
|
2013-06-17 05:29:48 +02:00
|
|
|
import re
|
2012-11-24 22:52:18 +01:00
|
|
|
import tempfile
|
2012-11-25 03:24:49 +01:00
|
|
|
import shlex
|
2013-01-22 19:33:39 +01:00
|
|
|
import select
|
2013-07-03 17:08:07 +02:00
|
|
|
import time
|
2013-10-04 16:30:39 +02:00
|
|
|
import string
|
2012-08-17 22:55:18 +02:00
|
|
|
|
2014-08-25 20:41:39 +02:00
|
|
|
from distutils.version import LooseVersion
|
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
class Service(object):
|
|
|
|
"""
|
|
|
|
This is the generic Service manipulation class that is subclassed
|
|
|
|
based on platform.
|
|
|
|
|
|
|
|
A subclass should override the following action methods:-
|
|
|
|
- get_service_tools
|
|
|
|
- service_enable
|
|
|
|
- get_service_status
|
|
|
|
- service_control
|
|
|
|
|
|
|
|
All subclasses MUST define platform and distribution (which may be None).
|
|
|
|
"""
|
|
|
|
|
|
|
|
platform = 'Generic'
|
|
|
|
distribution = None
|
|
|
|
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
return load_platform_subclass(Service, args, kwargs)
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
self.name = module.params['name']
|
|
|
|
self.state = module.params['state']
|
2013-07-03 17:08:07 +02:00
|
|
|
self.sleep = module.params['sleep']
|
2012-11-23 21:03:17 +01:00
|
|
|
self.pattern = module.params['pattern']
|
2013-02-23 19:59:52 +01:00
|
|
|
self.enable = module.params['enabled']
|
2013-06-17 05:29:48 +02:00
|
|
|
self.runlevel = module.params['runlevel']
|
2012-11-23 21:03:17 +01:00
|
|
|
self.changed = False
|
|
|
|
self.running = None
|
2013-06-17 05:29:48 +02:00
|
|
|
self.crashed = None
|
2012-11-23 21:03:17 +01:00
|
|
|
self.action = None
|
|
|
|
self.svc_cmd = None
|
|
|
|
self.svc_initscript = None
|
|
|
|
self.svc_initctl = None
|
|
|
|
self.enable_cmd = None
|
|
|
|
self.arguments = module.params.get('arguments', '')
|
2012-11-24 22:52:18 +01:00
|
|
|
self.rcconf_file = None
|
|
|
|
self.rcconf_key = None
|
|
|
|
self.rcconf_value = None
|
2013-09-13 22:04:37 +02:00
|
|
|
self.svc_change = False
|
2012-11-23 21:03:17 +01:00
|
|
|
|
|
|
|
# select whether we dump additional debug info through syslog
|
|
|
|
self.syslogging = False
|
|
|
|
|
2013-02-17 23:45:44 +01:00
|
|
|
# ===========================================
|
|
|
|
# Platform specific methods (must be replaced by subclass).
|
2012-11-23 21:03:17 +01:00
|
|
|
|
|
|
|
def get_service_tools(self):
|
|
|
|
self.module.fail_json(msg="get_service_tools not implemented on target platform")
|
|
|
|
|
|
|
|
def service_enable(self):
|
|
|
|
self.module.fail_json(msg="service_enable not implemented on target platform")
|
|
|
|
|
|
|
|
def get_service_status(self):
|
|
|
|
self.module.fail_json(msg="get_service_status not implemented on target platform")
|
|
|
|
|
|
|
|
def service_control(self):
|
|
|
|
self.module.fail_json(msg="service_control not implemented on target platform")
|
|
|
|
|
2013-02-17 23:45:44 +01:00
|
|
|
# ===========================================
|
|
|
|
# Generic methods that should be used on all platforms.
|
2012-11-23 21:03:17 +01:00
|
|
|
|
2013-01-22 19:33:39 +01:00
|
|
|
def execute_command(self, cmd, daemonize=False):
|
2012-11-23 21:03:17 +01:00
|
|
|
if self.syslogging:
|
|
|
|
syslog.openlog('ansible-%s' % os.path.basename(__file__))
|
2013-01-22 19:33:39 +01:00
|
|
|
syslog.syslog(syslog.LOG_NOTICE, 'Command %s, daemonize %r' % (cmd, daemonize))
|
|
|
|
|
|
|
|
# Most things don't need to be daemonized
|
|
|
|
if not daemonize:
|
|
|
|
return self.module.run_command(cmd)
|
|
|
|
|
|
|
|
# This is complex because daemonization is hard for people.
|
|
|
|
# What we do is daemonize a part of this module, the daemon runs the
|
|
|
|
# command, picks up the return code and output, and returns it to the
|
|
|
|
# main process.
|
|
|
|
pipe = os.pipe()
|
|
|
|
pid = os.fork()
|
|
|
|
if pid == 0:
|
|
|
|
os.close(pipe[0])
|
|
|
|
# Set stdin/stdout/stderr to /dev/null
|
|
|
|
fd = os.open(os.devnull, os.O_RDWR)
|
|
|
|
if fd != 0:
|
|
|
|
os.dup2(fd, 0)
|
|
|
|
if fd != 1:
|
|
|
|
os.dup2(fd, 1)
|
|
|
|
if fd != 2:
|
|
|
|
os.dup2(fd, 2)
|
|
|
|
if fd not in (0, 1, 2):
|
|
|
|
os.close(fd)
|
|
|
|
|
|
|
|
# Make us a daemon. Yes, that's all it takes.
|
|
|
|
pid = os.fork()
|
|
|
|
if pid > 0:
|
|
|
|
os._exit(0)
|
|
|
|
os.setsid()
|
|
|
|
os.chdir("/")
|
|
|
|
pid = os.fork()
|
|
|
|
if pid > 0:
|
|
|
|
os._exit(0)
|
|
|
|
|
|
|
|
# Start the command
|
2014-03-10 22:11:24 +01:00
|
|
|
if isinstance(cmd, basestring):
|
|
|
|
cmd = shlex.split(cmd)
|
|
|
|
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=lambda: os.close(pipe[1]))
|
2013-01-22 19:33:39 +01:00
|
|
|
stdout = ""
|
|
|
|
stderr = ""
|
|
|
|
fds = [p.stdout, p.stderr]
|
|
|
|
# Wait for all output, or until the main process is dead and its output is done.
|
|
|
|
while fds:
|
|
|
|
rfd, wfd, efd = select.select(fds, [], fds, 1)
|
|
|
|
if not (rfd + wfd + efd) and p.poll() is not None:
|
|
|
|
break
|
|
|
|
if p.stdout in rfd:
|
|
|
|
dat = os.read(p.stdout.fileno(), 4096)
|
|
|
|
if not dat:
|
|
|
|
fds.remove(p.stdout)
|
2013-02-06 16:52:04 +01:00
|
|
|
stdout += dat
|
2013-01-22 19:33:39 +01:00
|
|
|
if p.stderr in rfd:
|
|
|
|
dat = os.read(p.stderr.fileno(), 4096)
|
|
|
|
if not dat:
|
|
|
|
fds.remove(p.stderr)
|
|
|
|
stderr += dat
|
|
|
|
p.wait()
|
|
|
|
# Return a JSON blob to parent
|
|
|
|
os.write(pipe[1], json.dumps([p.returncode, stdout, stderr]))
|
|
|
|
os.close(pipe[1])
|
|
|
|
os._exit(0)
|
|
|
|
elif pid == -1:
|
|
|
|
self.module.fail_json(msg="unable to fork")
|
|
|
|
else:
|
|
|
|
os.close(pipe[1])
|
|
|
|
os.waitpid(pid, 0)
|
|
|
|
# Wait for data from daemon process and process it.
|
|
|
|
data = ""
|
|
|
|
while True:
|
|
|
|
rfd, wfd, efd = select.select([pipe[0]], [], [pipe[0]])
|
|
|
|
if pipe[0] in rfd:
|
|
|
|
dat = os.read(pipe[0], 4096)
|
|
|
|
if not dat:
|
|
|
|
break
|
|
|
|
data += dat
|
|
|
|
return json.loads(data)
|
2012-11-23 21:03:17 +01:00
|
|
|
|
|
|
|
def check_ps(self):
|
|
|
|
# Set ps flags
|
|
|
|
if platform.system() == 'SunOS':
|
|
|
|
psflags = '-ef'
|
|
|
|
else:
|
|
|
|
psflags = 'auxww'
|
|
|
|
|
|
|
|
# Find ps binary
|
|
|
|
psbin = self.module.get_bin_path('ps', True)
|
|
|
|
|
|
|
|
(rc, psout, pserr) = self.execute_command('%s %s' % (psbin, psflags))
|
|
|
|
# If rc is 0, set running as appropriate
|
|
|
|
if rc == 0:
|
|
|
|
self.running = False
|
|
|
|
lines = psout.split("\n")
|
|
|
|
for line in lines:
|
|
|
|
if self.pattern in line and not "pattern=" in line:
|
|
|
|
# so as to not confuse ./hacking/test-module
|
|
|
|
self.running = True
|
|
|
|
break
|
|
|
|
|
|
|
|
def check_service_changed(self):
|
2013-02-10 00:49:54 +01:00
|
|
|
if self.state and self.running is None:
|
2013-02-17 18:11:53 +01:00
|
|
|
self.module.fail_json(msg="failed determining service state, possible typo of service name?")
|
2012-11-23 21:03:17 +01:00
|
|
|
# Find out if state has changed
|
2013-10-25 21:30:02 +02:00
|
|
|
if not self.running and self.state in ["started", "running", "reloaded"]:
|
2013-09-13 22:04:37 +02:00
|
|
|
self.svc_change = True
|
2012-11-23 21:03:17 +01:00
|
|
|
elif self.running and self.state in ["stopped","reloaded"]:
|
2013-09-13 22:04:37 +02:00
|
|
|
self.svc_change = True
|
2012-11-23 21:03:17 +01:00
|
|
|
elif self.state == "restarted":
|
2013-09-13 22:04:37 +02:00
|
|
|
self.svc_change = True
|
|
|
|
if self.module.check_mode and self.svc_change:
|
2013-02-17 23:45:44 +01:00
|
|
|
self.module.exit_json(changed=True, msg='service state changed')
|
2012-11-23 21:03:17 +01:00
|
|
|
|
|
|
|
def modify_service_state(self):
|
2013-02-17 23:45:44 +01:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
# Only do something if state will change
|
2013-09-13 22:04:37 +02:00
|
|
|
if self.svc_change:
|
2012-11-23 21:03:17 +01:00
|
|
|
# Control service
|
|
|
|
if self.state in ['started', 'running']:
|
|
|
|
self.action = "start"
|
2013-10-25 21:30:02 +02:00
|
|
|
elif not self.running and self.state == 'reloaded':
|
|
|
|
self.action = "start"
|
2012-11-23 21:03:17 +01:00
|
|
|
elif self.state == 'stopped':
|
|
|
|
self.action = "stop"
|
|
|
|
elif self.state == 'reloaded':
|
|
|
|
self.action = "reload"
|
|
|
|
elif self.state == 'restarted':
|
|
|
|
self.action = "restart"
|
|
|
|
|
2013-02-17 23:45:44 +01:00
|
|
|
if self.module.check_mode:
|
|
|
|
self.module.exit_json(changed=True, msg='changing service state')
|
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
return self.service_control()
|
|
|
|
|
|
|
|
else:
|
|
|
|
# If nothing needs to change just say all is well
|
|
|
|
rc = 0
|
|
|
|
err = ''
|
|
|
|
out = ''
|
|
|
|
return rc, out, err
|
|
|
|
|
2012-11-24 22:52:18 +01:00
|
|
|
def service_enable_rcconf(self):
|
|
|
|
if self.rcconf_file is None or self.rcconf_key is None or self.rcconf_value is None:
|
|
|
|
self.module.fail_json(msg="service_enable_rcconf() requires rcconf_file, rcconf_key and rcconf_value")
|
|
|
|
|
2014-08-28 00:52:23 +02:00
|
|
|
self.changed = None
|
2012-11-24 22:52:18 +01:00
|
|
|
entry = '%s="%s"\n' % (self.rcconf_key, self.rcconf_value)
|
|
|
|
RCFILE = open(self.rcconf_file, "r")
|
|
|
|
new_rc_conf = []
|
|
|
|
|
2013-02-28 13:06:50 +01:00
|
|
|
# Build a list containing the possibly modified file.
|
2012-11-24 22:52:18 +01:00
|
|
|
for rcline in RCFILE:
|
2012-11-25 03:24:49 +01:00
|
|
|
# Parse line removing whitespaces, quotes, etc.
|
|
|
|
rcarray = shlex.split(rcline, comments=True)
|
|
|
|
if len(rcarray) >= 1 and '=' in rcarray[0]:
|
|
|
|
(key, value) = rcarray[0].split("=", 1)
|
2012-11-24 22:52:18 +01:00
|
|
|
if key == self.rcconf_key:
|
2014-01-28 18:15:47 +01:00
|
|
|
if value.upper() == self.rcconf_value:
|
2012-11-24 22:52:18 +01:00
|
|
|
# Since the proper entry already exists we can stop iterating.
|
2014-08-28 00:52:23 +02:00
|
|
|
self.changed = False
|
2012-11-24 22:52:18 +01:00
|
|
|
break
|
2013-02-28 13:06:50 +01:00
|
|
|
else:
|
2012-11-24 22:52:18 +01:00
|
|
|
# We found the key but the value is wrong, replace with new entry.
|
|
|
|
rcline = entry
|
2014-08-28 00:52:23 +02:00
|
|
|
self.changed = True
|
2012-11-24 22:52:18 +01:00
|
|
|
|
|
|
|
# Add line to the list.
|
|
|
|
new_rc_conf.append(rcline)
|
|
|
|
|
|
|
|
# We are done with reading the current rc.conf, close it.
|
|
|
|
RCFILE.close()
|
|
|
|
|
|
|
|
# If we did not see any trace of our entry we need to add it.
|
2014-08-28 00:52:23 +02:00
|
|
|
if self.changed is None:
|
2012-11-24 22:52:18 +01:00
|
|
|
new_rc_conf.append(entry)
|
2014-08-28 00:52:23 +02:00
|
|
|
self.changed = True
|
2012-11-24 22:52:18 +01:00
|
|
|
|
2014-08-28 00:52:23 +02:00
|
|
|
if self.changed is True:
|
2013-02-17 23:45:44 +01:00
|
|
|
|
|
|
|
if self.module.check_mode:
|
|
|
|
self.module.exit_json(changed=True, msg="changing service enablement")
|
|
|
|
|
2012-11-24 22:52:18 +01:00
|
|
|
# Create a temporary file next to the current rc.conf (so we stay on the same filesystem).
|
|
|
|
# This way the replacement operation is atomic.
|
|
|
|
rcconf_dir = os.path.dirname(self.rcconf_file)
|
|
|
|
rcconf_base = os.path.basename(self.rcconf_file)
|
|
|
|
(TMP_RCCONF, tmp_rcconf_file) = tempfile.mkstemp(dir=rcconf_dir, prefix="%s-" % rcconf_base)
|
|
|
|
|
|
|
|
# Write out the contents of the list into our temporary file.
|
|
|
|
for rcline in new_rc_conf:
|
|
|
|
os.write(TMP_RCCONF, rcline)
|
|
|
|
|
|
|
|
# Close temporary file.
|
|
|
|
os.close(TMP_RCCONF)
|
|
|
|
|
|
|
|
# Replace previous rc.conf.
|
2013-04-24 03:52:23 +02:00
|
|
|
self.module.atomic_move(tmp_rcconf_file, self.rcconf_file)
|
2012-11-24 22:52:18 +01:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
# ===========================================
|
|
|
|
# Subclass: Linux
|
|
|
|
|
|
|
|
class LinuxService(Service):
|
|
|
|
"""
|
|
|
|
This is the Linux Service manipulation class - it is currently supporting
|
|
|
|
a mixture of binaries and init scripts for controlling services started at
|
|
|
|
boot, as well as for controlling the current state.
|
|
|
|
"""
|
|
|
|
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = None
|
|
|
|
|
|
|
|
def get_service_tools(self):
|
2013-02-24 02:31:29 +01:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
paths = [ '/sbin', '/usr/sbin', '/bin', '/usr/bin' ]
|
2013-06-17 05:29:48 +02:00
|
|
|
binaries = [ 'service', 'chkconfig', 'update-rc.d', 'rc-service', 'rc-update', 'initctl', 'systemctl', 'start', 'stop', 'restart' ]
|
2012-11-23 21:03:17 +01:00
|
|
|
initpaths = [ '/etc/init.d' ]
|
|
|
|
location = dict()
|
|
|
|
|
|
|
|
for binary in binaries:
|
|
|
|
location[binary] = self.module.get_bin_path(binary)
|
|
|
|
|
2013-09-10 18:15:30 +02:00
|
|
|
def check_systemd(name):
|
|
|
|
# verify service is managed by systemd
|
|
|
|
if not location.get('systemctl', None):
|
|
|
|
return False
|
|
|
|
|
2013-09-11 22:48:36 +02:00
|
|
|
# default to .service if the unit type is not specified
|
|
|
|
if name.find('.') > 0:
|
|
|
|
unit_name, unit_type = name.rsplit('.', 1)
|
|
|
|
if unit_type not in ("service", "socket", "device", "mount", "automount",
|
|
|
|
"swap", "target", "path", "timer", "snapshot"):
|
|
|
|
name = "%s.service" % name
|
|
|
|
else:
|
|
|
|
name = "%s.service" % name
|
|
|
|
|
2013-09-10 18:15:30 +02:00
|
|
|
rc, out, err = self.execute_command("%s list-unit-files" % (location['systemctl']))
|
|
|
|
|
|
|
|
# adjust the service name to account for template service unit files
|
|
|
|
index = name.find('@')
|
|
|
|
if index != -1:
|
2014-03-08 01:52:47 +01:00
|
|
|
template_name = name[:index+1]
|
|
|
|
else:
|
|
|
|
template_name = name
|
2013-09-10 18:15:30 +02:00
|
|
|
|
2013-09-11 22:48:36 +02:00
|
|
|
self.__systemd_unit = None
|
2013-09-10 18:15:30 +02:00
|
|
|
for line in out.splitlines():
|
2014-03-08 01:52:47 +01:00
|
|
|
if line.startswith(template_name):
|
2013-09-11 22:48:36 +02:00
|
|
|
self.__systemd_unit = name
|
2013-09-10 18:15:30 +02:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
# Locate a tool for enable options
|
2013-02-24 02:31:29 +01:00
|
|
|
if location.get('chkconfig', None) and os.path.exists("/etc/init.d/%s" % self.name):
|
2013-09-11 02:27:54 +02:00
|
|
|
if check_systemd(self.name):
|
|
|
|
# service is managed by systemd
|
|
|
|
self.enable_cmd = location['systemctl']
|
|
|
|
else:
|
|
|
|
# we are using a standard SysV service
|
|
|
|
self.enable_cmd = location['chkconfig']
|
2013-09-10 18:15:30 +02:00
|
|
|
elif location.get('update-rc.d', None):
|
|
|
|
if check_systemd(self.name):
|
|
|
|
# service is managed by systemd
|
|
|
|
self.enable_cmd = location['systemctl']
|
2013-12-23 09:54:16 +01:00
|
|
|
elif location['initctl'] and os.path.exists("/etc/init/%s.conf" % self.name):
|
2013-09-10 18:15:30 +02:00
|
|
|
# service is managed by upstart
|
2013-09-30 21:50:47 +02:00
|
|
|
self.enable_cmd = location['initctl']
|
2013-12-23 09:54:16 +01:00
|
|
|
elif location['update-rc.d'] and os.path.exists("/etc/init.d/%s" % self.name):
|
2013-09-10 18:15:30 +02:00
|
|
|
# service is managed by with SysV init scripts, but with update-rc.d
|
|
|
|
self.enable_cmd = location['update-rc.d']
|
2013-09-10 18:31:13 +02:00
|
|
|
else:
|
2013-09-11 02:27:54 +02:00
|
|
|
self.module.fail_json(msg="service not found: %s" % self.name)
|
2013-06-17 05:29:48 +02:00
|
|
|
elif location.get('rc-service', None) and not location.get('systemctl', None):
|
|
|
|
# service is managed by OpenRC
|
|
|
|
self.svc_cmd = location['rc-service']
|
|
|
|
self.enable_cmd = location['rc-update']
|
|
|
|
return
|
2013-09-10 18:15:30 +02:00
|
|
|
elif check_systemd(self.name):
|
|
|
|
# service is managed by systemd
|
|
|
|
self.enable_cmd = location['systemctl']
|
2014-08-25 20:41:39 +02:00
|
|
|
elif location['initctl'] and os.path.exists("/etc/init/%s.conf" % self.name):
|
|
|
|
# service is managed by upstart
|
|
|
|
self.enable_cmd = location['initctl']
|
|
|
|
|
|
|
|
# if this service is managed via upstart, get the current upstart version
|
|
|
|
if self.enable_cmd == location['initctl']:
|
|
|
|
# default the upstart version to something we can compare against
|
|
|
|
self.upstart_version = LooseVersion('0.0.0')
|
|
|
|
try:
|
|
|
|
# set the upstart version based on the output of 'initctl version'
|
|
|
|
version_re = re.compile(r'\(upstart (.*)\)')
|
|
|
|
rc,stdout,stderr = self.module.run_command('initctl version')
|
|
|
|
if rc == 0:
|
|
|
|
res = version_re.search(stdout)
|
|
|
|
if res:
|
|
|
|
self.upstart_version = LooseVersion(res.groups()[0])
|
|
|
|
except:
|
|
|
|
# we'll use the default of 0.0.0 since we couldn't
|
|
|
|
# detect the current upstart version above
|
|
|
|
pass
|
2013-02-28 13:06:50 +01:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
# Locate a tool for runtime service management (start, stop etc.)
|
2013-02-24 02:31:29 +01:00
|
|
|
if location.get('service', None) and os.path.exists("/etc/init.d/%s" % self.name):
|
|
|
|
# SysV init script
|
2012-11-23 21:03:17 +01:00
|
|
|
self.svc_cmd = location['service']
|
2013-02-24 02:31:29 +01:00
|
|
|
elif location.get('start', None) and os.path.exists("/etc/init/%s.conf" % self.name):
|
|
|
|
# upstart -- rather than being managed by one command, start/stop/restart are actual commands
|
|
|
|
self.svc_cmd = ''
|
2012-11-23 21:03:17 +01:00
|
|
|
else:
|
2013-02-24 02:31:29 +01:00
|
|
|
# still a SysV init script, but /sbin/service isn't installed
|
2012-11-23 21:03:17 +01:00
|
|
|
for initdir in initpaths:
|
|
|
|
initscript = "%s/%s" % (initdir,self.name)
|
|
|
|
if os.path.isfile(initscript):
|
|
|
|
self.svc_initscript = initscript
|
|
|
|
|
2013-02-24 17:50:39 +01:00
|
|
|
# couldn't find anything yet, assume systemd
|
2013-05-11 12:22:07 +02:00
|
|
|
if self.svc_cmd is None and self.svc_initscript is None:
|
2013-02-24 17:50:39 +01:00
|
|
|
if location.get('systemctl'):
|
|
|
|
self.svc_cmd = location['systemctl']
|
|
|
|
|
2013-02-24 02:31:29 +01:00
|
|
|
if self.svc_cmd is None and not self.svc_initscript:
|
2013-05-31 21:23:24 +02:00
|
|
|
self.module.fail_json(msg='cannot find \'service\' binary or init script for service, possible typo in service name?, aborting')
|
2012-11-23 21:03:17 +01:00
|
|
|
|
|
|
|
if location.get('initctl', None):
|
|
|
|
self.svc_initctl = location['initctl']
|
|
|
|
|
2014-03-10 07:05:48 +01:00
|
|
|
def get_systemd_status_dict(self):
|
|
|
|
(rc, out, err) = self.execute_command("%s show %s" % (self.enable_cmd, self.__systemd_unit,))
|
|
|
|
if rc != 0:
|
2014-06-09 21:03:51 +02:00
|
|
|
self.module.fail_json(msg='failure %d running systemctl show for %r: %s' % (rc, self.__systemd_unit, err))
|
2014-03-10 07:05:48 +01:00
|
|
|
return dict(line.split('=', 1) for line in out.splitlines())
|
|
|
|
|
2014-03-10 06:09:08 +01:00
|
|
|
def get_systemd_service_status(self):
|
2014-03-10 07:05:48 +01:00
|
|
|
d = self.get_systemd_status_dict()
|
|
|
|
if d.get('ActiveState') == 'active':
|
2014-03-10 06:09:08 +01:00
|
|
|
# run-once services (for which a single successful exit indicates
|
|
|
|
# that they are running as designed) should not be restarted here.
|
|
|
|
# Thus, we are not checking d['SubState'].
|
|
|
|
self.running = True
|
|
|
|
self.crashed = False
|
2014-03-10 07:05:48 +01:00
|
|
|
elif d.get('ActiveState') == 'failed':
|
2014-03-10 06:09:08 +01:00
|
|
|
self.running = False
|
|
|
|
self.crashed = True
|
2014-03-10 07:05:48 +01:00
|
|
|
elif d.get('ActiveState') is None:
|
|
|
|
self.module.fail_json(msg='No ActiveState value in systemctl show output for %r' % (self.__systemd_unit,))
|
2014-03-10 06:09:08 +01:00
|
|
|
else:
|
|
|
|
self.running = False
|
|
|
|
self.crashed = False
|
|
|
|
return self.running
|
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
def get_service_status(self):
|
2014-03-10 06:09:08 +01:00
|
|
|
if self.svc_cmd and self.svc_cmd.endswith('systemctl'):
|
|
|
|
return self.get_systemd_service_status()
|
|
|
|
|
2013-01-14 12:37:55 +01:00
|
|
|
self.action = "status"
|
|
|
|
rc, status_stdout, status_stderr = self.service_control()
|
2012-11-23 21:03:17 +01:00
|
|
|
|
2013-02-24 02:31:29 +01:00
|
|
|
# if we have decided the service is managed by upstart, we check for some additional output...
|
2013-02-10 00:49:54 +01:00
|
|
|
if self.svc_initctl and self.running is None:
|
2012-11-23 21:03:17 +01:00
|
|
|
# check the job status by upstart response
|
|
|
|
initctl_rc, initctl_status_stdout, initctl_status_stderr = self.execute_command("%s status %s" % (self.svc_initctl, self.name))
|
2014-01-23 16:02:17 +01:00
|
|
|
if "stop/waiting" in initctl_status_stdout:
|
2012-11-23 21:03:17 +01:00
|
|
|
self.running = False
|
2014-01-23 16:02:17 +01:00
|
|
|
elif "start/running" in initctl_status_stdout:
|
2012-11-23 21:03:17 +01:00
|
|
|
self.running = True
|
|
|
|
|
2013-08-08 14:11:11 +02:00
|
|
|
if self.svc_cmd and self.svc_cmd.endswith("rc-service") and self.running is None:
|
2013-06-17 05:29:48 +02:00
|
|
|
openrc_rc, openrc_status_stdout, openrc_status_stderr = self.execute_command("%s %s status" % (self.svc_cmd, self.name))
|
|
|
|
self.running = "started" in openrc_status_stdout
|
|
|
|
self.crashed = "crashed" in openrc_status_stderr
|
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
# if the job status is still not known check it by status output keywords
|
2014-09-22 20:53:07 +02:00
|
|
|
# Only check keywords if there's only one line of output (some init
|
|
|
|
# scripts will output verbosely in case of error and those can emit
|
|
|
|
# keywords that are picked up as false positives
|
|
|
|
if self.running is None and status_stdout.count('\n') <= 1:
|
2014-04-29 16:41:05 +02:00
|
|
|
# first transform the status output that could irritate keyword matching
|
2012-11-23 21:03:17 +01:00
|
|
|
cleanout = status_stdout.lower().replace(self.name.lower(), '')
|
|
|
|
if "stop" in cleanout:
|
|
|
|
self.running = False
|
|
|
|
elif "run" in cleanout and "not" in cleanout:
|
|
|
|
self.running = False
|
|
|
|
elif "run" in cleanout and "not" not in cleanout:
|
|
|
|
self.running = True
|
|
|
|
elif "start" in cleanout and "not" not in cleanout:
|
|
|
|
self.running = True
|
|
|
|
elif 'could not access pid file' in cleanout:
|
|
|
|
self.running = False
|
|
|
|
elif 'is dead and pid file exists' in cleanout:
|
|
|
|
self.running = False
|
|
|
|
elif 'dead but subsys locked' in cleanout:
|
|
|
|
self.running = False
|
|
|
|
elif 'dead but pid file exists' in cleanout:
|
|
|
|
self.running = False
|
|
|
|
|
2014-09-11 19:10:19 +02:00
|
|
|
# if the job status is still not known check it by response code
|
|
|
|
# For reference, see:
|
|
|
|
# http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
|
|
|
|
if self.running is None:
|
|
|
|
if rc in [1, 2, 3, 4, 69]:
|
|
|
|
self.running = False
|
|
|
|
elif rc == 0:
|
|
|
|
self.running = True
|
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
# if the job status is still not known check it by special conditions
|
2013-02-10 00:49:54 +01:00
|
|
|
if self.running is None:
|
2014-01-23 16:02:17 +01:00
|
|
|
if self.name == 'iptables' and "ACCEPT" in status_stdout:
|
2012-11-23 21:03:17 +01:00
|
|
|
# iptables status command output is lame
|
|
|
|
# TODO: lookup if we can use a return code for this instead?
|
|
|
|
self.running = True
|
|
|
|
|
2013-02-17 18:11:53 +01:00
|
|
|
return self.running
|
|
|
|
|
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
def service_enable(self):
|
2013-02-24 02:31:29 +01:00
|
|
|
|
|
|
|
if self.enable_cmd is None:
|
2014-04-19 19:01:46 +02:00
|
|
|
self.module.fail_json(msg='cannot detect command to enable service %s, typo or init system potentially unknown' % self.name)
|
2013-02-24 02:31:29 +01:00
|
|
|
|
2014-09-17 01:12:09 +02:00
|
|
|
self.changed = True
|
|
|
|
action = None
|
|
|
|
|
2013-02-24 17:50:39 +01:00
|
|
|
# FIXME: we use chkconfig or systemctl
|
|
|
|
# to decide whether to run the command here but need something
|
|
|
|
# similar for upstart
|
2013-02-24 16:32:56 +01:00
|
|
|
|
2014-09-17 01:12:09 +02:00
|
|
|
#
|
|
|
|
# Upstart's initctl
|
|
|
|
#
|
2013-09-30 21:50:47 +02:00
|
|
|
if self.enable_cmd.endswith("initctl"):
|
|
|
|
def write_to_override_file(file_name, file_contents, ):
|
|
|
|
override_file = open(file_name, 'w')
|
|
|
|
override_file.write(file_contents)
|
|
|
|
override_file.close()
|
|
|
|
|
|
|
|
initpath = '/etc/init'
|
2014-08-25 20:41:39 +02:00
|
|
|
if self.upstart_version >= LooseVersion('0.6.7'):
|
|
|
|
manreg = re.compile('^manual\s*$', re.M | re.I)
|
|
|
|
config_line = 'manual\n'
|
|
|
|
else:
|
|
|
|
manreg = re.compile('^start on manual\s*$', re.M | re.I)
|
|
|
|
config_line = 'start on manual\n'
|
2013-09-30 21:50:47 +02:00
|
|
|
conf_file_name = "%s/%s.conf" % (initpath, self.name)
|
|
|
|
override_file_name = "%s/%s.override" % (initpath, self.name)
|
|
|
|
|
|
|
|
# Check to see if files contain the manual line in .conf and fail if True
|
|
|
|
if manreg.search(open(conf_file_name).read()):
|
|
|
|
self.module.fail_json(msg="manual stanza not supported in a .conf file")
|
|
|
|
|
2014-09-17 01:12:09 +02:00
|
|
|
self.changed = False
|
2013-09-30 21:50:47 +02:00
|
|
|
if os.path.exists(override_file_name):
|
|
|
|
override_file_contents = open(override_file_name).read()
|
|
|
|
# Remove manual stanza if present and service enabled
|
|
|
|
if self.enable and manreg.search(override_file_contents):
|
2014-09-17 01:12:09 +02:00
|
|
|
self.changed = True
|
|
|
|
override_state = manreg.sub('', override_file_contents)
|
2013-09-30 21:50:47 +02:00
|
|
|
# Add manual stanza if not present and service disabled
|
|
|
|
elif not (self.enable) and not (manreg.search(override_file_contents)):
|
2014-09-17 01:12:09 +02:00
|
|
|
self.changed = True
|
|
|
|
override_state = '\n'.join((override_file_contents, config_line))
|
|
|
|
# service already in desired state
|
2013-09-30 21:50:47 +02:00
|
|
|
else:
|
2014-09-17 01:12:09 +02:00
|
|
|
pass
|
2013-09-30 21:50:47 +02:00
|
|
|
# Add file with manual stanza if service disabled
|
|
|
|
elif not (self.enable):
|
2014-09-17 01:12:09 +02:00
|
|
|
self.changed = True
|
|
|
|
override_state = config_line
|
2013-09-30 21:50:47 +02:00
|
|
|
else:
|
2014-09-17 01:12:09 +02:00
|
|
|
# service already in desired state
|
|
|
|
pass
|
2014-09-17 01:12:09 +02:00
|
|
|
|
2014-09-17 01:12:09 +02:00
|
|
|
if self.module.check_mode:
|
|
|
|
self.module.exit_json(changed=self.changed)
|
|
|
|
|
|
|
|
# The initctl method of enabling and disabling services is much
|
|
|
|
# different than for the other service methods. So actually
|
|
|
|
# committing the change is done in this conditional and then we
|
|
|
|
# skip the boilerplate at the bottom of the method
|
|
|
|
if self.changed:
|
2014-09-18 21:39:54 +02:00
|
|
|
try:
|
|
|
|
write_to_override_file(override_file_name, override_state)
|
|
|
|
except:
|
|
|
|
self.module.fail_json(msg='Could not modify override file')
|
|
|
|
|
2014-09-17 01:12:09 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
#
|
|
|
|
# SysV's chkconfig
|
|
|
|
#
|
2013-02-24 16:32:56 +01:00
|
|
|
if self.enable_cmd.endswith("chkconfig"):
|
2014-09-17 01:12:09 +02:00
|
|
|
if self.enable:
|
|
|
|
action = 'on'
|
|
|
|
else:
|
|
|
|
action = 'off'
|
|
|
|
|
2013-02-24 16:32:56 +01:00
|
|
|
(rc, out, err) = self.execute_command("%s --list %s" % (self.enable_cmd, self.name))
|
2013-03-18 09:13:41 +01:00
|
|
|
if 'chkconfig --add %s' % self.name in err:
|
|
|
|
self.execute_command("%s --add %s" % (self.enable_cmd, self.name))
|
|
|
|
(rc, out, err) = self.execute_command("%s --list %s" % (self.enable_cmd, self.name))
|
2013-02-24 16:32:56 +01:00
|
|
|
if not self.name in out:
|
2014-02-14 07:47:14 +01:00
|
|
|
self.module.fail_json(msg="service %s does not support chkconfig" % self.name)
|
2013-02-24 16:32:56 +01:00
|
|
|
state = out.split()[-1]
|
2014-09-17 01:12:09 +02:00
|
|
|
|
|
|
|
# Check if we're already in the correct state
|
|
|
|
if "3:%s" % action in out and "5:%s" % action in out:
|
2014-09-21 14:55:31 +02:00
|
|
|
self.changed = False
|
2013-02-24 16:32:56 +01:00
|
|
|
return
|
|
|
|
|
2014-09-17 01:12:09 +02:00
|
|
|
#
|
|
|
|
# Systemd's systemctl
|
|
|
|
#
|
2013-02-24 17:50:39 +01:00
|
|
|
if self.enable_cmd.endswith("systemctl"):
|
2014-09-17 01:12:09 +02:00
|
|
|
if self.enable:
|
|
|
|
action = 'enable'
|
|
|
|
else:
|
|
|
|
action = 'disable'
|
|
|
|
|
|
|
|
# Check if we're already in the correct state
|
2014-03-10 07:05:48 +01:00
|
|
|
d = self.get_systemd_status_dict()
|
2013-03-24 04:39:55 +01:00
|
|
|
if "UnitFileState" in d:
|
|
|
|
if self.enable and d["UnitFileState"] == "enabled":
|
2014-09-17 01:12:09 +02:00
|
|
|
self.changed = False
|
2013-03-24 04:39:55 +01:00
|
|
|
elif not self.enable and d["UnitFileState"] == "disabled":
|
2014-09-17 01:12:09 +02:00
|
|
|
self.changed = False
|
2013-03-24 04:39:55 +01:00
|
|
|
elif not self.enable:
|
2014-09-17 01:12:09 +02:00
|
|
|
self.changed = False
|
|
|
|
|
|
|
|
if not self.changed:
|
2013-02-24 17:50:39 +01:00
|
|
|
return
|
|
|
|
|
2014-09-17 01:12:09 +02:00
|
|
|
#
|
|
|
|
# OpenRC's rc-update
|
|
|
|
#
|
2013-06-17 05:29:48 +02:00
|
|
|
if self.enable_cmd.endswith("rc-update"):
|
2014-09-17 01:12:09 +02:00
|
|
|
if self.enable:
|
|
|
|
action = 'add'
|
|
|
|
else:
|
|
|
|
action = 'delete'
|
|
|
|
|
2013-06-17 05:29:48 +02:00
|
|
|
(rc, out, err) = self.execute_command("%s show" % self.enable_cmd)
|
|
|
|
for line in out.splitlines():
|
|
|
|
service_name, runlevels = line.split('|')
|
|
|
|
service_name = service_name.strip()
|
|
|
|
if service_name != self.name:
|
|
|
|
continue
|
|
|
|
runlevels = re.split(r'\s+', runlevels)
|
|
|
|
# service already enabled for the runlevel
|
|
|
|
if self.enable and self.runlevel in runlevels:
|
2014-09-17 01:12:09 +02:00
|
|
|
self.changed = False
|
2013-06-17 05:29:48 +02:00
|
|
|
# service already disabled for the runlevel
|
|
|
|
elif not self.enable and self.runlevel not in runlevels:
|
2014-09-17 01:12:09 +02:00
|
|
|
self.changed = False
|
2013-06-17 05:29:48 +02:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
# service already disabled altogether
|
|
|
|
if not self.enable:
|
2014-09-17 01:12:09 +02:00
|
|
|
self.changed = False
|
|
|
|
|
|
|
|
if not self.changed:
|
|
|
|
return
|
2013-06-17 05:29:48 +02:00
|
|
|
|
2014-09-18 21:39:54 +02:00
|
|
|
#
|
|
|
|
# update-rc.d style
|
|
|
|
#
|
2013-06-22 11:43:38 +02:00
|
|
|
if self.enable_cmd.endswith("update-rc.d"):
|
2013-06-23 09:19:27 +02:00
|
|
|
if self.enable:
|
|
|
|
action = 'enable'
|
|
|
|
else:
|
|
|
|
action = 'disable'
|
2013-09-11 05:19:15 +02:00
|
|
|
|
2014-09-17 01:12:09 +02:00
|
|
|
if self.enable:
|
|
|
|
# make sure the init.d symlinks are created
|
|
|
|
# otherwise enable might not work
|
|
|
|
(rc, out, err) = self.execute_command("%s %s defaults" \
|
|
|
|
% (self.enable_cmd, self.name))
|
|
|
|
if rc != 0:
|
2014-09-18 21:39:54 +02:00
|
|
|
if err:
|
|
|
|
self.module.fail_json(msg=err)
|
|
|
|
else:
|
|
|
|
self.module.fail_json(msg=out)
|
2014-09-17 01:12:09 +02:00
|
|
|
|
2013-06-22 11:43:38 +02:00
|
|
|
(rc, out, err) = self.execute_command("%s -n %s %s" \
|
|
|
|
% (self.enable_cmd, self.name, action))
|
|
|
|
self.changed = False
|
|
|
|
for line in out.splitlines():
|
|
|
|
if line.startswith('rename'):
|
|
|
|
self.changed = True
|
|
|
|
break
|
2014-01-23 16:02:17 +01:00
|
|
|
elif self.enable and 'do not exist' in line:
|
2013-09-11 05:19:15 +02:00
|
|
|
self.changed = True
|
|
|
|
break
|
2014-01-23 16:02:17 +01:00
|
|
|
elif not self.enable and 'already exist' in line:
|
2013-09-11 05:19:15 +02:00
|
|
|
self.changed = True
|
|
|
|
break
|
2013-06-22 11:43:38 +02:00
|
|
|
|
2013-10-02 13:53:10 +02:00
|
|
|
# Debian compatibility
|
|
|
|
for line in err.splitlines():
|
2014-01-23 16:02:17 +01:00
|
|
|
if self.enable and 'no runlevel symlinks to modify' in line:
|
2013-10-02 13:53:10 +02:00
|
|
|
self.changed = True
|
|
|
|
break
|
|
|
|
|
2013-06-22 11:43:38 +02:00
|
|
|
if not self.changed:
|
|
|
|
return
|
|
|
|
|
2014-09-18 21:39:54 +02:00
|
|
|
#
|
2014-09-17 01:12:09 +02:00
|
|
|
# If we've gotten to the end, the service needs to be updated
|
2014-09-18 21:39:54 +02:00
|
|
|
#
|
2014-09-17 01:12:09 +02:00
|
|
|
self.changed = True
|
2013-09-11 05:19:15 +02:00
|
|
|
|
2014-09-17 01:12:09 +02:00
|
|
|
# we change argument order depending on real binary used:
|
|
|
|
# rc-update and systemctl need the argument order reversed
|
2012-11-23 21:03:17 +01:00
|
|
|
|
2013-06-22 11:43:38 +02:00
|
|
|
if self.enable_cmd.endswith("rc-update"):
|
2014-09-17 01:12:09 +02:00
|
|
|
args = (self.enable_cmd, action, self.name + " " + self.runlevel)
|
2012-11-23 21:03:17 +01:00
|
|
|
elif self.enable_cmd.endswith("systemctl"):
|
2014-09-17 01:12:09 +02:00
|
|
|
args = (self.enable_cmd, action, self.__systemd_unit)
|
2012-11-23 21:03:17 +01:00
|
|
|
else:
|
2014-09-17 01:12:09 +02:00
|
|
|
args = (self.enable_cmd, self.name, action)
|
2013-02-17 23:45:44 +01:00
|
|
|
|
2014-09-17 01:12:09 +02:00
|
|
|
if self.module.check_mode:
|
|
|
|
self.module.exit_json(changed=self.changed)
|
2013-02-17 23:45:44 +01:00
|
|
|
|
2014-09-18 21:39:54 +02:00
|
|
|
(rc, out, err) = self.execute_command("%s %s %s" % args)
|
|
|
|
if rc != 0:
|
|
|
|
if err:
|
|
|
|
self.module.fail_json(msg=err)
|
|
|
|
else:
|
|
|
|
self.module.fail_json(msg=out)
|
|
|
|
|
|
|
|
return (rc, out, err)
|
2012-11-23 21:03:17 +01:00
|
|
|
|
2013-02-24 02:31:29 +01:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
def service_control(self):
|
|
|
|
|
|
|
|
# Decide what command to run
|
2013-02-24 02:31:29 +01:00
|
|
|
svc_cmd = ''
|
2013-03-20 16:50:36 +01:00
|
|
|
arguments = self.arguments
|
2012-11-23 21:03:17 +01:00
|
|
|
if self.svc_cmd:
|
2013-02-24 17:50:39 +01:00
|
|
|
if not self.svc_cmd.endswith("systemctl"):
|
2013-06-17 05:29:48 +02:00
|
|
|
# SysV and OpenRC take the form <cmd> <name> <action>
|
2013-02-24 17:50:39 +01:00
|
|
|
svc_cmd = "%s %s" % (self.svc_cmd, self.name)
|
|
|
|
else:
|
|
|
|
# systemd commands take the form <cmd> <action> <name>
|
|
|
|
svc_cmd = self.svc_cmd
|
2013-09-11 22:48:36 +02:00
|
|
|
arguments = "%s %s" % (self.__systemd_unit, arguments)
|
2013-02-28 13:06:50 +01:00
|
|
|
elif self.svc_initscript:
|
2013-02-24 02:31:29 +01:00
|
|
|
# upstart
|
2012-11-23 21:03:17 +01:00
|
|
|
svc_cmd = "%s" % self.svc_initscript
|
|
|
|
|
2013-06-17 05:29:48 +02:00
|
|
|
# In OpenRC, if a service crashed, we need to reset its status to
|
|
|
|
# stopped with the zap command, before we can start it back.
|
2013-08-08 14:11:11 +02:00
|
|
|
if self.svc_cmd and self.svc_cmd.endswith('rc-service') and self.action == 'start' and self.crashed:
|
2013-06-17 05:29:48 +02:00
|
|
|
self.execute_command("%s zap" % svc_cmd, daemonize=True)
|
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
if self.action is not "restart":
|
2013-02-24 02:31:29 +01:00
|
|
|
if svc_cmd != '':
|
2013-06-17 05:29:48 +02:00
|
|
|
# upstart or systemd or OpenRC
|
2013-03-20 16:50:36 +01:00
|
|
|
rc_state, stdout, stderr = self.execute_command("%s %s %s" % (svc_cmd, self.action, arguments), daemonize=True)
|
2013-02-24 02:31:29 +01:00
|
|
|
else:
|
2013-03-20 16:50:36 +01:00
|
|
|
# SysV
|
|
|
|
rc_state, stdout, stderr = self.execute_command("%s %s %s" % (self.action, self.name, arguments), daemonize=True)
|
2013-09-13 16:26:11 +02:00
|
|
|
elif self.svc_cmd and self.svc_cmd.endswith('rc-service'):
|
2013-06-17 05:29:48 +02:00
|
|
|
# All services in OpenRC support restart.
|
|
|
|
rc_state, stdout, stderr = self.execute_command("%s %s %s" % (svc_cmd, self.action, arguments), daemonize=True)
|
2012-11-23 21:03:17 +01:00
|
|
|
else:
|
2013-06-17 05:29:48 +02:00
|
|
|
# In other systems, not all services support restart. Do it the hard way.
|
2013-02-24 02:31:29 +01:00
|
|
|
if svc_cmd != '':
|
2013-03-20 16:50:36 +01:00
|
|
|
# upstart or systemd
|
|
|
|
rc1, stdout1, stderr1 = self.execute_command("%s %s %s" % (svc_cmd, 'stop', arguments), daemonize=True)
|
2013-02-24 02:31:29 +01:00
|
|
|
else:
|
2013-03-20 16:50:36 +01:00
|
|
|
# SysV
|
|
|
|
rc1, stdout1, stderr1 = self.execute_command("%s %s %s" % ('stop', self.name, arguments), daemonize=True)
|
2013-02-28 13:06:50 +01:00
|
|
|
|
2013-07-03 17:08:07 +02:00
|
|
|
if self.sleep:
|
|
|
|
time.sleep(self.sleep)
|
|
|
|
|
2013-02-24 02:31:29 +01:00
|
|
|
if svc_cmd != '':
|
2013-03-20 16:50:36 +01:00
|
|
|
# upstart or systemd
|
|
|
|
rc2, stdout2, stderr2 = self.execute_command("%s %s %s" % (svc_cmd, 'start', arguments), daemonize=True)
|
2013-02-24 02:31:29 +01:00
|
|
|
else:
|
2013-03-20 16:50:36 +01:00
|
|
|
# SysV
|
|
|
|
rc2, stdout2, stderr2 = self.execute_command("%s %s %s" % ('start', self.name, arguments), daemonize=True)
|
2013-02-24 02:31:29 +01:00
|
|
|
|
|
|
|
# merge return information
|
2012-11-23 21:03:17 +01:00
|
|
|
if rc1 != 0 and rc2 == 0:
|
2012-11-25 00:23:29 +01:00
|
|
|
rc_state = rc2
|
2012-11-23 21:03:17 +01:00
|
|
|
stdout = stdout2
|
|
|
|
stderr = stderr2
|
2012-08-17 22:55:18 +02:00
|
|
|
else:
|
2012-11-25 00:23:29 +01:00
|
|
|
rc_state = rc1 + rc2
|
2012-11-23 21:03:17 +01:00
|
|
|
stdout = stdout1 + stdout2
|
|
|
|
stderr = stderr1 + stderr2
|
|
|
|
|
|
|
|
return(rc_state, stdout, stderr)
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Subclass: FreeBSD
|
|
|
|
|
|
|
|
class FreeBsdService(Service):
|
|
|
|
"""
|
|
|
|
This is the FreeBSD Service manipulation class - it uses the /etc/rc.conf
|
|
|
|
file for controlling services started at boot and the 'service' binary to
|
|
|
|
check status and perform direct service manipulation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
platform = 'FreeBSD'
|
|
|
|
distribution = None
|
|
|
|
|
|
|
|
def get_service_tools(self):
|
|
|
|
self.svc_cmd = self.module.get_bin_path('service', True)
|
|
|
|
|
|
|
|
if not self.svc_cmd:
|
|
|
|
self.module.fail_json(msg='unable to find service binary')
|
|
|
|
|
|
|
|
def get_service_status(self):
|
2013-04-23 19:40:50 +02:00
|
|
|
rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.svc_cmd, self.name, 'onestatus', self.arguments))
|
2012-11-23 21:03:17 +01:00
|
|
|
if rc == 1:
|
|
|
|
self.running = False
|
2012-06-17 15:55:26 +02:00
|
|
|
elif rc == 0:
|
2012-11-23 21:03:17 +01:00
|
|
|
self.running = True
|
|
|
|
|
|
|
|
def service_enable(self):
|
|
|
|
if self.enable:
|
2012-11-24 22:52:18 +01:00
|
|
|
self.rcconf_value = "YES"
|
2012-11-23 21:03:17 +01:00
|
|
|
else:
|
2012-11-24 22:52:18 +01:00
|
|
|
self.rcconf_value = "NO"
|
2012-11-23 21:03:17 +01:00
|
|
|
|
2014-01-08 20:38:49 +01:00
|
|
|
rcfiles = [ '/etc/rc.conf','/etc/rc.conf.local', '/usr/local/etc/rc.conf' ]
|
2012-11-23 21:03:17 +01:00
|
|
|
for rcfile in rcfiles:
|
|
|
|
if os.path.isfile(rcfile):
|
2012-11-24 22:52:18 +01:00
|
|
|
self.rcconf_file = rcfile
|
2013-02-28 13:06:50 +01:00
|
|
|
|
2013-07-03 13:52:23 +02:00
|
|
|
rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.svc_cmd, self.name, 'rcvar', self.arguments))
|
2013-11-12 06:02:30 +01:00
|
|
|
cmd = "%s %s %s %s" % (self.svc_cmd, self.name, 'rcvar', self.arguments)
|
2013-07-03 13:52:23 +02:00
|
|
|
rcvars = shlex.split(stdout, comments=True)
|
2013-11-12 06:02:30 +01:00
|
|
|
|
2013-07-03 13:52:23 +02:00
|
|
|
if not rcvars:
|
2013-11-12 06:02:30 +01:00
|
|
|
self.module.fail_json(msg="unable to determine rcvar", stdout=stdout, stderr=stderr)
|
2013-07-03 13:52:23 +02:00
|
|
|
|
|
|
|
# In rare cases, i.e. sendmail, rcvar can return several key=value pairs
|
2013-10-21 22:20:54 +02:00
|
|
|
# Usually there is just one, however. In other rare cases, i.e. uwsgi,
|
|
|
|
# rcvar can return extra uncommented data that is not at all related to
|
|
|
|
# the rcvar. We will just take the first key=value pair we come across
|
|
|
|
# and hope for the best.
|
|
|
|
for rcvar in rcvars:
|
|
|
|
if '=' in rcvar:
|
|
|
|
self.rcconf_key = rcvar.split('=')[0]
|
|
|
|
break
|
|
|
|
|
|
|
|
if self.rcconf_key is None:
|
2013-11-12 06:02:30 +01:00
|
|
|
self.module.fail_json(msg="unable to determine rcvar", stdout=stdout, stderr=stderr)
|
2012-11-24 22:52:18 +01:00
|
|
|
|
2014-09-18 21:39:54 +02:00
|
|
|
try:
|
|
|
|
return self.service_enable_rcconf()
|
|
|
|
except:
|
|
|
|
self.module.fail_json(msg='unable to set rcvar')
|
2012-10-31 03:06:11 +01:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
def service_control(self):
|
2013-02-17 23:45:44 +01:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
if self.action is "start":
|
|
|
|
self.action = "onestart"
|
|
|
|
if self.action is "stop":
|
|
|
|
self.action = "onestop"
|
|
|
|
if self.action is "reload":
|
|
|
|
self.action = "onereload"
|
|
|
|
|
2013-04-23 19:40:50 +02:00
|
|
|
return self.execute_command("%s %s %s %s" % (self.svc_cmd, self.name, self.action, self.arguments))
|
2012-11-23 21:03:17 +01:00
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Subclass: OpenBSD
|
|
|
|
|
|
|
|
class OpenBsdService(Service):
|
|
|
|
"""
|
|
|
|
This is the OpenBSD Service manipulation class - it uses /etc/rc.d for
|
|
|
|
service control. Enabling a service is currently not supported because the
|
|
|
|
<service>_flags variable is not boolean, you should supply a rc.conf.local
|
|
|
|
file in some other way.
|
|
|
|
"""
|
|
|
|
|
|
|
|
platform = 'OpenBSD'
|
|
|
|
distribution = None
|
|
|
|
|
|
|
|
def get_service_tools(self):
|
|
|
|
rcdir = '/etc/rc.d'
|
|
|
|
|
|
|
|
rc_script = "%s/%s" % (rcdir, self.name)
|
|
|
|
if os.path.isfile(rc_script):
|
|
|
|
self.svc_cmd = rc_script
|
|
|
|
|
|
|
|
if not self.svc_cmd:
|
|
|
|
self.module.fail_json(msg='unable to find rc.d script')
|
|
|
|
|
|
|
|
def get_service_status(self):
|
|
|
|
rc, stdout, stderr = self.execute_command("%s %s" % (self.svc_cmd, 'check'))
|
|
|
|
if rc == 1:
|
|
|
|
self.running = False
|
|
|
|
elif rc == 0:
|
|
|
|
self.running = True
|
2012-05-03 08:34:36 +02:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
def service_control(self):
|
|
|
|
return self.execute_command("%s %s" % (self.svc_cmd, self.action))
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2013-02-13 22:07:31 +01:00
|
|
|
# ===========================================
|
|
|
|
# Subclass: NetBSD
|
|
|
|
|
|
|
|
class NetBsdService(Service):
|
|
|
|
"""
|
|
|
|
This is the NetBSD Service manipulation class - it uses the /etc/rc.conf
|
|
|
|
file for controlling services started at boot, check status and perform
|
|
|
|
direct service manipulation. Init scripts in /etc/rcd are used for
|
|
|
|
controlling services (start/stop) as well as for controlling the current
|
|
|
|
state.
|
|
|
|
"""
|
|
|
|
|
|
|
|
platform = 'NetBSD'
|
|
|
|
distribution = None
|
|
|
|
|
|
|
|
def get_service_tools(self):
|
|
|
|
initpaths = [ '/etc/rc.d' ] # better: $rc_directories - how to get in here? Run: sh -c '. /etc/rc.conf ; echo $rc_directories'
|
|
|
|
|
|
|
|
for initdir in initpaths:
|
2013-02-18 01:48:02 +01:00
|
|
|
initscript = "%s/%s" % (initdir,self.name)
|
|
|
|
if os.path.isfile(initscript):
|
|
|
|
self.svc_initscript = initscript
|
2013-02-13 22:07:31 +01:00
|
|
|
|
|
|
|
if not self.svc_initscript:
|
|
|
|
self.module.fail_json(msg='unable to find rc.d script')
|
|
|
|
|
|
|
|
def service_enable(self):
|
|
|
|
if self.enable:
|
|
|
|
self.rcconf_value = "YES"
|
|
|
|
else:
|
|
|
|
self.rcconf_value = "NO"
|
|
|
|
|
|
|
|
rcfiles = [ '/etc/rc.conf' ] # Overkill?
|
|
|
|
for rcfile in rcfiles:
|
|
|
|
if os.path.isfile(rcfile):
|
|
|
|
self.rcconf_file = rcfile
|
2013-02-28 13:06:50 +01:00
|
|
|
|
2013-10-19 19:02:57 +02:00
|
|
|
self.rcconf_key = "%s" % string.replace(self.name,"-","_")
|
2013-02-13 22:07:31 +01:00
|
|
|
|
|
|
|
return self.service_enable_rcconf()
|
|
|
|
|
|
|
|
def get_service_status(self):
|
|
|
|
self.svc_cmd = "%s" % self.svc_initscript
|
|
|
|
rc, stdout, stderr = self.execute_command("%s %s" % (self.svc_cmd, 'onestatus'))
|
|
|
|
if rc == 1:
|
|
|
|
self.running = False
|
|
|
|
elif rc == 0:
|
|
|
|
self.running = True
|
|
|
|
|
|
|
|
def service_control(self):
|
|
|
|
if self.action is "start":
|
|
|
|
self.action = "onestart"
|
|
|
|
if self.action is "stop":
|
|
|
|
self.action = "onestop"
|
|
|
|
|
|
|
|
self.svc_cmd = "%s" % self.svc_initscript
|
2013-02-27 13:26:12 +01:00
|
|
|
return self.execute_command("%s %s" % (self.svc_cmd, self.action), daemonize=True)
|
2013-02-13 22:07:31 +01:00
|
|
|
|
2013-03-23 22:02:54 +01:00
|
|
|
# ===========================================
|
|
|
|
# Subclass: SunOS
|
|
|
|
class SunOSService(Service):
|
|
|
|
"""
|
|
|
|
This is the SunOS Service manipulation class - it uses the svcadm
|
|
|
|
command for controlling services, and svcs command for checking status.
|
|
|
|
It also tries to be smart about taking the service out of maintenance
|
|
|
|
state if necessary.
|
|
|
|
"""
|
|
|
|
platform = 'SunOS'
|
|
|
|
distribution = None
|
|
|
|
|
|
|
|
def get_service_tools(self):
|
|
|
|
self.svcs_cmd = self.module.get_bin_path('svcs', True)
|
|
|
|
|
|
|
|
if not self.svcs_cmd:
|
|
|
|
self.module.fail_json(msg='unable to find svcs binary')
|
|
|
|
|
|
|
|
self.svcadm_cmd = self.module.get_bin_path('svcadm', True)
|
2013-04-04 21:00:47 +02:00
|
|
|
|
2013-03-23 22:02:54 +01:00
|
|
|
if not self.svcadm_cmd:
|
|
|
|
self.module.fail_json(msg='unable to find svcadm binary')
|
|
|
|
|
|
|
|
def get_service_status(self):
|
|
|
|
status = self.get_sunos_svcs_status()
|
|
|
|
# Only 'online' is considered properly running. Everything else is off
|
|
|
|
# or has some sort of problem.
|
|
|
|
if status == 'online':
|
|
|
|
self.running = True
|
|
|
|
else:
|
|
|
|
self.running = False
|
|
|
|
|
|
|
|
def get_sunos_svcs_status(self):
|
|
|
|
rc, stdout, stderr = self.execute_command("%s %s" % (self.svcs_cmd, self.name))
|
|
|
|
if rc == 1:
|
|
|
|
if stderr:
|
|
|
|
self.module.fail_json(msg=stderr)
|
|
|
|
else:
|
|
|
|
self.module.fail_json(msg=stdout)
|
|
|
|
|
|
|
|
lines = stdout.rstrip("\n").split("\n")
|
|
|
|
status = lines[-1].split(" ")[0]
|
|
|
|
# status is one of: online, offline, degraded, disabled, maintenance, uninitialized
|
|
|
|
# see man svcs(1)
|
|
|
|
return status
|
|
|
|
|
|
|
|
def service_enable(self):
|
|
|
|
# Get current service enablement status
|
|
|
|
rc, stdout, stderr = self.execute_command("%s -l %s" % (self.svcs_cmd, self.name))
|
|
|
|
|
|
|
|
if rc != 0:
|
2013-03-24 19:47:56 +01:00
|
|
|
if stderr:
|
2013-03-23 22:02:54 +01:00
|
|
|
self.module.fail_json(msg=stderr)
|
|
|
|
else:
|
|
|
|
self.module.fail_json(msg=stdout)
|
|
|
|
|
|
|
|
enabled = False
|
|
|
|
temporary = False
|
|
|
|
|
|
|
|
# look for enabled line, which could be one of:
|
|
|
|
# enabled true (temporary)
|
|
|
|
# enabled false (temporary)
|
|
|
|
# enabled true
|
|
|
|
# enabled false
|
|
|
|
for line in stdout.split("\n"):
|
2014-03-27 19:30:09 +01:00
|
|
|
if line.startswith("enabled"):
|
2014-01-23 16:02:17 +01:00
|
|
|
if "true" in line:
|
2013-03-23 22:02:54 +01:00
|
|
|
enabled = True
|
2014-01-23 16:02:17 +01:00
|
|
|
if "temporary" in line:
|
2013-03-23 22:02:54 +01:00
|
|
|
temporary = True
|
2013-04-04 21:00:47 +02:00
|
|
|
|
2013-03-23 22:02:54 +01:00
|
|
|
startup_enabled = (enabled and not temporary) or (not enabled and temporary)
|
2013-04-04 21:00:47 +02:00
|
|
|
|
2013-03-23 22:02:54 +01:00
|
|
|
if self.enable and startup_enabled:
|
|
|
|
return
|
|
|
|
elif (not self.enable) and (not startup_enabled):
|
|
|
|
return
|
|
|
|
|
|
|
|
# Mark service as started or stopped (this will have the side effect of
|
|
|
|
# actually stopping or starting the service)
|
|
|
|
if self.enable:
|
|
|
|
subcmd = "enable -rs"
|
|
|
|
else:
|
|
|
|
subcmd = "disable -s"
|
|
|
|
|
|
|
|
rc, stdout, stderr = self.execute_command("%s %s %s" % (self.svcadm_cmd, subcmd, self.name))
|
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
if stderr:
|
|
|
|
self.module.fail_json(msg=stderr)
|
|
|
|
else:
|
|
|
|
self.module.fail_json(msg=stdout)
|
|
|
|
|
|
|
|
self.changed = True
|
2013-04-04 21:00:47 +02:00
|
|
|
|
|
|
|
|
2013-03-23 22:02:54 +01:00
|
|
|
def service_control(self):
|
|
|
|
status = self.get_sunos_svcs_status()
|
|
|
|
|
|
|
|
# if starting or reloading, clear maintenace states
|
|
|
|
if self.action in ['start', 'reload', 'restart'] and status in ['maintenance', 'degraded']:
|
|
|
|
rc, stdout, stderr = self.execute_command("%s clear %s" % (self.svcadm_cmd, self.name))
|
|
|
|
if rc != 0:
|
|
|
|
return rc, stdout, stderr
|
|
|
|
status = self.get_sunos_svcs_status()
|
|
|
|
|
|
|
|
if status in ['maintenance', 'degraded']:
|
|
|
|
self.module.fail_json(msg="Failed to bring service out of %s status." % status)
|
|
|
|
|
|
|
|
if self.action == 'start':
|
|
|
|
subcmd = "enable -rst"
|
|
|
|
elif self.action == 'stop':
|
|
|
|
subcmd = "disable -st"
|
|
|
|
elif self.action == 'reload':
|
|
|
|
subcmd = "refresh"
|
|
|
|
elif self.action == 'restart' and status == 'online':
|
|
|
|
subcmd = "restart"
|
|
|
|
elif self.action == 'restart' and status != 'online':
|
|
|
|
subcmd = "enable -rst"
|
2013-04-04 21:00:47 +02:00
|
|
|
|
2013-03-23 22:02:54 +01:00
|
|
|
return self.execute_command("%s %s %s" % (self.svcadm_cmd, subcmd, self.name))
|
|
|
|
|
2013-05-08 15:10:22 +02:00
|
|
|
# ===========================================
|
|
|
|
# Subclass: AIX
|
|
|
|
|
|
|
|
class AIX(Service):
|
|
|
|
"""
|
|
|
|
This is the AIX Service (SRC) manipulation class - it uses lssrc, startsrc, stopsrc
|
|
|
|
and refresh for service control. Enabling a service is currently not supported.
|
|
|
|
Would require to add an entry in the /etc/inittab file (mkitab, chitab and rmitab
|
|
|
|
commands)
|
|
|
|
"""
|
|
|
|
|
|
|
|
platform = 'AIX'
|
|
|
|
distribution = None
|
|
|
|
|
|
|
|
def get_service_tools(self):
|
|
|
|
self.lssrc_cmd = self.module.get_bin_path('lssrc', True)
|
|
|
|
|
|
|
|
if not self.lssrc_cmd:
|
|
|
|
self.module.fail_json(msg='unable to find lssrc binary')
|
|
|
|
|
|
|
|
self.startsrc_cmd = self.module.get_bin_path('startsrc', True)
|
|
|
|
|
|
|
|
if not self.startsrc_cmd:
|
|
|
|
self.module.fail_json(msg='unable to find startsrc binary')
|
|
|
|
|
|
|
|
self.stopsrc_cmd = self.module.get_bin_path('stopsrc', True)
|
|
|
|
|
|
|
|
if not self.stopsrc_cmd:
|
|
|
|
self.module.fail_json(msg='unable to find stopsrc binary')
|
|
|
|
|
|
|
|
self.refresh_cmd = self.module.get_bin_path('refresh', True)
|
|
|
|
|
|
|
|
if not self.refresh_cmd:
|
|
|
|
self.module.fail_json(msg='unable to find refresh binary')
|
|
|
|
|
|
|
|
|
|
|
|
def get_service_status(self):
|
|
|
|
status = self.get_aix_src_status()
|
|
|
|
# Only 'active' is considered properly running. Everything else is off
|
|
|
|
# or has some sort of problem.
|
|
|
|
if status == 'active':
|
|
|
|
self.running = True
|
|
|
|
else:
|
|
|
|
self.running = False
|
|
|
|
|
|
|
|
def get_aix_src_status(self):
|
|
|
|
rc, stdout, stderr = self.execute_command("%s -s %s" % (self.lssrc_cmd, self.name))
|
|
|
|
if rc == 1:
|
|
|
|
if stderr:
|
|
|
|
self.module.fail_json(msg=stderr)
|
|
|
|
else:
|
|
|
|
self.module.fail_json(msg=stdout)
|
|
|
|
|
|
|
|
lines = stdout.rstrip("\n").split("\n")
|
|
|
|
status = lines[-1].split(" ")[-1]
|
|
|
|
# status is one of: active, inoperative
|
|
|
|
return status
|
|
|
|
|
|
|
|
def service_control(self):
|
|
|
|
if self.action == 'start':
|
|
|
|
srccmd = self.startsrc_cmd
|
|
|
|
elif self.action == 'stop':
|
|
|
|
srccmd = self.stopsrc_cmd
|
|
|
|
elif self.action == 'reload':
|
|
|
|
srccmd = self.refresh_cmd
|
|
|
|
elif self.action == 'restart':
|
|
|
|
self.execute_command("%s -s %s" % (self.stopsrc_cmd, self.name))
|
|
|
|
srccmd = self.startsrc_cmd
|
|
|
|
|
|
|
|
if self.arguments and self.action == 'start':
|
|
|
|
return self.execute_command("%s -a \"%s\" -s %s" % (srccmd, self.arguments, self.name))
|
|
|
|
else:
|
|
|
|
return self.execute_command("%s -s %s" % (srccmd, self.name))
|
|
|
|
|
2013-02-13 22:07:31 +01:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
# ===========================================
|
|
|
|
# Main control flow
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-30 11:18:24 +02:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
name = dict(required=True),
|
2012-08-01 23:44:26 +02:00
|
|
|
state = dict(choices=['running', 'started', 'stopped', 'restarted', 'reloaded']),
|
2013-07-03 17:08:07 +02:00
|
|
|
sleep = dict(required=False, type='int', default=None),
|
2012-08-17 22:55:18 +02:00
|
|
|
pattern = dict(required=False, default=None),
|
2013-10-11 14:45:13 +02:00
|
|
|
enabled = dict(type='bool'),
|
2013-06-17 05:29:48 +02:00
|
|
|
runlevel = dict(required=False, default='default'),
|
2012-11-21 16:11:20 +01:00
|
|
|
arguments = dict(aliases=['args'], default=''),
|
2013-02-17 23:45:44 +01:00
|
|
|
),
|
|
|
|
supports_check_mode=True
|
2012-07-30 11:18:24 +02:00
|
|
|
)
|
2013-04-04 21:00:47 +02:00
|
|
|
if module.params['state'] is None and module.params['enabled'] is None:
|
|
|
|
module.fail_json(msg="Neither 'state' nor 'enabled' set")
|
2012-07-30 11:18:24 +02:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
service = Service(module)
|
|
|
|
|
|
|
|
if service.syslogging:
|
|
|
|
syslog.openlog('ansible-%s' % os.path.basename(__file__))
|
|
|
|
syslog.syslog(syslog.LOG_NOTICE, 'Service instantiated - platform %s' % service.platform)
|
|
|
|
if service.distribution:
|
|
|
|
syslog.syslog(syslog.LOG_NOTICE, 'Service instantiated - distribution %s' % service.distribution)
|
|
|
|
|
2012-04-27 04:43:36 +02:00
|
|
|
rc = 0
|
2012-07-30 11:18:24 +02:00
|
|
|
out = ''
|
2012-11-23 21:03:17 +01:00
|
|
|
err = ''
|
|
|
|
result = {}
|
|
|
|
result['name'] = service.name
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
# Find service management tools
|
|
|
|
service.get_service_tools()
|
|
|
|
|
|
|
|
# Enable/disable service startup at boot if requested
|
2013-03-08 06:07:54 +01:00
|
|
|
if service.module.params['enabled'] is not None:
|
2013-02-17 18:13:26 +01:00
|
|
|
# FIXME: ideally this should detect if we need to toggle the enablement state, though
|
|
|
|
# it's unlikely the changed handler would need to fire in this case so it's a minor thing.
|
2012-11-23 21:03:17 +01:00
|
|
|
service.service_enable()
|
2013-04-04 21:00:47 +02:00
|
|
|
result['enabled'] = service.enable
|
|
|
|
|
|
|
|
if module.params['state'] is None:
|
|
|
|
# Not changing the running state, so bail out now.
|
|
|
|
result['changed'] = service.changed
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
result['state'] = service.state
|
2012-11-23 21:03:17 +01:00
|
|
|
|
|
|
|
# Collect service status
|
|
|
|
if service.pattern:
|
|
|
|
service.check_ps()
|
2013-05-23 17:39:26 +02:00
|
|
|
else:
|
|
|
|
service.get_service_status()
|
2012-11-23 21:03:17 +01:00
|
|
|
|
|
|
|
# Calculate if request will change service state
|
|
|
|
service.check_service_changed()
|
|
|
|
|
|
|
|
# Modify service state if necessary
|
|
|
|
(rc, out, err) = service.modify_service_state()
|
2012-05-03 12:36:06 +02:00
|
|
|
|
2012-04-27 04:43:36 +02:00
|
|
|
if rc != 0:
|
2014-09-19 21:01:46 +02:00
|
|
|
if err and "Job is already running" in err:
|
2013-04-28 15:29:31 +02:00
|
|
|
# upstart got confused, one such possibility is MySQL on Ubuntu 12.04
|
|
|
|
# where status may report it has no start/stop links and we could
|
|
|
|
# not get accurate status
|
|
|
|
pass
|
2012-11-23 21:03:17 +01:00
|
|
|
else:
|
2013-04-28 15:29:31 +02:00
|
|
|
if err:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg=out)
|
2012-02-26 02:27:11 +01:00
|
|
|
|
2013-09-19 22:00:25 +02:00
|
|
|
result['changed'] = service.changed | service.svc_change
|
2013-03-08 06:07:54 +01:00
|
|
|
if service.module.params['enabled'] is not None:
|
2012-11-23 21:03:17 +01:00
|
|
|
result['enabled'] = service.module.params['enabled']
|
2013-02-17 18:11:53 +01:00
|
|
|
|
2013-02-28 13:06:50 +01:00
|
|
|
if not service.module.params['state']:
|
2013-02-17 18:11:53 +01:00
|
|
|
status = service.get_service_status()
|
|
|
|
if status is None:
|
2013-02-28 13:06:50 +01:00
|
|
|
result['state'] = 'absent'
|
2013-02-17 18:11:53 +01:00
|
|
|
elif status is False:
|
|
|
|
result['state'] = 'started'
|
|
|
|
else:
|
|
|
|
result['state'] = 'stopped'
|
|
|
|
else:
|
|
|
|
# as we may have just bounced the service the service command may not
|
|
|
|
# report accurate state at this moment so just show what we ran
|
2013-09-03 16:49:19 +02:00
|
|
|
if service.module.params['state'] in ['started','restarted','running','reloaded']:
|
2013-02-17 18:11:53 +01:00
|
|
|
result['state'] = 'started'
|
|
|
|
else:
|
|
|
|
result['state'] = 'stopped'
|
2012-10-31 03:06:11 +01:00
|
|
|
|
2012-08-11 18:35:58 +02:00
|
|
|
module.exit_json(**result)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2013-10-31 21:52:37 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2012-07-30 11:18:24 +02:00
|
|
|
main()
|