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-08-17 22:55:18 +02:00
|
|
|
import platform
|
|
|
|
|
2012-05-03 08:34:36 +02:00
|
|
|
SERVICE = None
|
|
|
|
CHKCONFIG = None
|
2012-07-28 22:48:04 +02:00
|
|
|
INITCTL = None
|
2012-08-17 22:55:18 +02:00
|
|
|
PS_OPTIONS = 'auxww'
|
2012-05-03 08:34:36 +02:00
|
|
|
|
2012-07-30 11:18:24 +02:00
|
|
|
def _find_binaries(m):
|
2012-05-03 08:34:36 +02:00
|
|
|
# list of possible paths for service/chkconfig binaries
|
|
|
|
# with the most probable first
|
2012-05-03 12:36:06 +02:00
|
|
|
global SERVICE
|
2012-07-28 22:48:04 +02:00
|
|
|
global CHKCONFIG
|
2012-06-17 15:55:26 +02:00
|
|
|
global INITCTL
|
2012-05-03 08:34:36 +02:00
|
|
|
paths = ['/sbin', '/usr/sbin', '/bin', '/usr/bin']
|
2012-06-17 15:55:26 +02:00
|
|
|
binaries = [ 'service', 'chkconfig', 'update-rc.d', 'initctl']
|
2012-05-03 08:34:36 +02:00
|
|
|
location = dict()
|
|
|
|
|
|
|
|
for binary in binaries:
|
|
|
|
location[binary] = None
|
|
|
|
|
2012-05-03 12:36:06 +02:00
|
|
|
for binary in binaries:
|
|
|
|
for path in paths:
|
2012-05-03 08:34:36 +02:00
|
|
|
if os.path.exists(path + '/' + binary):
|
|
|
|
location[binary] = path + '/' + binary
|
|
|
|
break
|
|
|
|
|
|
|
|
if location.get('chkconfig', None):
|
|
|
|
CHKCONFIG = location['chkconfig']
|
|
|
|
elif location.get('update-rc.d', None):
|
|
|
|
CHKCONFIG = location['update-rc.d']
|
|
|
|
else:
|
2012-07-30 11:18:24 +02:00
|
|
|
m.fail_json(msg='unable to find chkconfig or update-rc.d binary')
|
2012-05-03 08:34:36 +02:00
|
|
|
if location.get('service', None):
|
|
|
|
SERVICE = location['service']
|
|
|
|
else:
|
2012-07-30 11:18:24 +02:00
|
|
|
m.fail_json(msg='unable to find service binary')
|
2012-06-17 15:55:26 +02:00
|
|
|
if location.get('initctl', None):
|
|
|
|
INITCTL = location['initctl']
|
|
|
|
else:
|
|
|
|
INITCTL = None
|
|
|
|
|
2012-08-17 22:55:18 +02:00
|
|
|
def _get_service_status(name, pattern):
|
2012-06-17 15:55:26 +02:00
|
|
|
rc, status_stdout, status_stderr = _run("%s %s status" % (SERVICE, name))
|
|
|
|
|
|
|
|
# set the running state to None because we don't know it yet
|
|
|
|
running = None
|
|
|
|
|
2012-08-17 22:55:18 +02:00
|
|
|
# If pattern is provided, search for that
|
|
|
|
# before checking initctl, service output, and other tricks
|
|
|
|
if pattern is not None:
|
2012-08-18 02:26:22 +02:00
|
|
|
|
2012-08-17 22:55:18 +02:00
|
|
|
psbin = '/bin/ps'
|
|
|
|
if not os.path.exists(psbin):
|
|
|
|
if os.path.exists('/usr/bin/ps'):
|
|
|
|
psbin = '/usr/bin/ps'
|
|
|
|
else:
|
|
|
|
psbin = None
|
2012-08-18 02:26:22 +02:00
|
|
|
|
2012-08-17 22:55:18 +02:00
|
|
|
if psbin is not None:
|
|
|
|
(rc, psout, pserr) = _run('%s %s' % (psbin, PS_OPTIONS))
|
|
|
|
# If rc is 0, set running as appropriate
|
|
|
|
# If ps command fails, fall back to other means.
|
|
|
|
if rc == 0:
|
2012-08-18 02:26:22 +02:00
|
|
|
running = False
|
|
|
|
lines = psout.split("\n")
|
|
|
|
for line in lines:
|
|
|
|
if pattern in line and not "pattern=" in line:
|
|
|
|
# so as to not confuse ./hacking/test-module
|
|
|
|
running = True
|
|
|
|
break
|
2012-08-17 22:55:18 +02:00
|
|
|
|
2012-06-17 15:55:26 +02:00
|
|
|
# Check if we got upstart on the system and then the job state
|
2012-08-17 22:55:18 +02:00
|
|
|
if INITCTL != None and running is None:
|
2012-06-17 15:55:26 +02:00
|
|
|
# check the job status by upstart response
|
|
|
|
initctl_rc, initctl_status_stdout, initctl_status_stderr = _run("%s status %s" % (INITCTL, name))
|
|
|
|
if initctl_status_stdout.find("stop/waiting") != -1:
|
|
|
|
running = False
|
|
|
|
elif initctl_status_stdout.find("start/running") != -1:
|
|
|
|
running = True
|
|
|
|
|
|
|
|
# if the job status is still not known check it by response code
|
|
|
|
if running == None:
|
|
|
|
if rc == 3:
|
|
|
|
running = False
|
|
|
|
elif rc == 0:
|
|
|
|
running = True
|
|
|
|
|
|
|
|
# if the job status is still not known check it by status output keywords
|
|
|
|
if running == None:
|
|
|
|
# first tranform the status output that could irritate keyword matching
|
2012-07-19 19:15:09 +02:00
|
|
|
cleanout = status_stdout.lower().replace(name.lower(), '')
|
|
|
|
if "stop" in cleanout:
|
2012-06-17 15:55:26 +02:00
|
|
|
running = False
|
2012-07-19 19:15:09 +02:00
|
|
|
elif "run" in cleanout and "not" in cleanout:
|
2012-06-17 15:55:26 +02:00
|
|
|
running = False
|
2012-07-19 19:15:09 +02:00
|
|
|
elif "run" in cleanout and "not" not in cleanout:
|
2012-06-17 15:55:26 +02:00
|
|
|
running = True
|
2012-07-19 19:15:09 +02:00
|
|
|
elif "start" in cleanout and "not" not in cleanout:
|
2012-06-17 15:55:26 +02:00
|
|
|
running = True
|
2012-07-19 19:15:09 +02:00
|
|
|
elif 'could not access pid file' in cleanout:
|
|
|
|
running = False
|
|
|
|
elif 'is dead and pid file exists' in cleanout:
|
2012-07-10 22:13:39 +02:00
|
|
|
running = False
|
2012-06-17 15:55:26 +02:00
|
|
|
|
|
|
|
# if the job status is still not known check it by special conditions
|
|
|
|
if running == None:
|
|
|
|
if name == 'iptables' and status_stdout.find("ACCEPT") != -1:
|
|
|
|
# iptables status command output is lame
|
|
|
|
# TODO: lookup if we can use a return code for this instead?
|
|
|
|
running = True
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-06-17 15:55:26 +02:00
|
|
|
return running
|
2012-05-03 12:36:06 +02:00
|
|
|
|
2012-04-27 19:35:24 +02:00
|
|
|
def _run(cmd):
|
2012-04-27 04:43:36 +02:00
|
|
|
# returns (rc, stdout, stderr) from shell command
|
2012-04-27 19:35:24 +02:00
|
|
|
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
|
|
|
|
stdout, stderr = process.communicate()
|
2012-08-07 02:07:02 +02:00
|
|
|
return (process.returncode, stdout, stderr)
|
2012-04-27 19:35:24 +02:00
|
|
|
|
2012-05-01 23:46:45 +02:00
|
|
|
|
|
|
|
def _do_enable(name, enable):
|
2012-05-03 08:34:36 +02:00
|
|
|
# we change argument depending on real binary used
|
|
|
|
# update-rc.d wants enable/disable while
|
|
|
|
# chkconfig wants on/off
|
|
|
|
valid_argument = dict({'on' : 'on', 'off' : 'off'})
|
|
|
|
|
|
|
|
if CHKCONFIG.endswith("update-rc.d"):
|
|
|
|
valid_argument['on'] = "enable"
|
|
|
|
valid_argument['off'] = "disable"
|
|
|
|
|
2012-07-28 22:48:04 +02:00
|
|
|
if enable is not None:
|
|
|
|
if enable:
|
|
|
|
rc, stdout, stderr = _run("%s %s %s" % (CHKCONFIG, name, valid_argument['on']))
|
|
|
|
else:
|
|
|
|
rc, stdout, stderr = _run("%s %s %s" % (CHKCONFIG, name, valid_argument['off']))
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-05-01 23:46:45 +02:00
|
|
|
return rc, stdout, stderr
|
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']),
|
2012-08-17 22:55:18 +02:00
|
|
|
pattern = dict(required=False, default=None),
|
2012-08-01 23:44:26 +02:00
|
|
|
enabled = dict(choices=BOOLEANS)
|
2012-07-30 11:18:24 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2012-07-28 22:48:04 +02:00
|
|
|
name = module.params['name']
|
|
|
|
state = module.params['state']
|
2012-08-17 22:55:18 +02:00
|
|
|
pattern = module.params['pattern']
|
2012-08-01 23:44:26 +02:00
|
|
|
enable = module.boolean(module.params.get('enabled', None))
|
2012-07-30 11:18:24 +02:00
|
|
|
|
2012-08-17 22:55:18 +02:00
|
|
|
# Set PS options here if 'ps auxww' will not work on
|
|
|
|
# target platform
|
|
|
|
if platform.system() == 'SunOS':
|
|
|
|
global PS_OPTIONS
|
|
|
|
PS_OPTIONS = '-ef'
|
|
|
|
|
2012-07-30 11:18:24 +02:00
|
|
|
# ===========================================
|
|
|
|
# find binaries locations on minion
|
|
|
|
_find_binaries(module)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-30 11:18:24 +02:00
|
|
|
# ===========================================
|
|
|
|
# get service status
|
2012-08-17 22:55:18 +02:00
|
|
|
running = _get_service_status(name, pattern)
|
2012-05-03 08:34:36 +02:00
|
|
|
|
2012-07-30 11:18:24 +02:00
|
|
|
# ===========================================
|
|
|
|
# Some common variables
|
|
|
|
changed = False
|
2012-04-27 04:43:36 +02:00
|
|
|
rc = 0
|
2012-05-01 23:46:45 +02:00
|
|
|
err = ''
|
2012-07-30 11:18:24 +02:00
|
|
|
out = ''
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-08-01 23:44:26 +02:00
|
|
|
if module.params['enabled']:
|
2012-05-01 23:46:45 +02:00
|
|
|
rc_enable, out_enable, err_enable = _do_enable(name, enable)
|
|
|
|
rc += rc_enable
|
|
|
|
out += out_enable
|
|
|
|
err += err_enable
|
|
|
|
|
2012-06-17 15:55:26 +02:00
|
|
|
if state and running == None:
|
2012-07-30 11:18:24 +02:00
|
|
|
module.fail_json(msg="failed determining the current service state => state stays unchanged", changed=False)
|
|
|
|
|
2012-06-17 15:55:26 +02:00
|
|
|
elif state:
|
2012-05-01 23:46:45 +02:00
|
|
|
# a state change command has been requested
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# determine if we are going to change anything
|
2012-07-30 11:18:24 +02:00
|
|
|
if not running and state in ["started", "running"]:
|
2012-05-01 23:46:45 +02:00
|
|
|
changed = True
|
2012-07-30 11:18:24 +02:00
|
|
|
elif running and state in ["stopped","reloaded"]:
|
2012-05-01 23:46:45 +02:00
|
|
|
changed = True
|
|
|
|
elif state == "restarted":
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# run change commands if we need to
|
|
|
|
if changed:
|
2012-07-30 11:18:24 +02:00
|
|
|
if state in ['started', 'running']:
|
2012-05-01 23:46:45 +02:00
|
|
|
rc_state, stdout, stderr = _run("%s %s start" % (SERVICE, name))
|
|
|
|
elif state == 'stopped':
|
|
|
|
rc_state, stdout, stderr = _run("%s %s stop" % (SERVICE, name))
|
2012-05-15 11:28:49 +02:00
|
|
|
elif state == 'reloaded':
|
|
|
|
rc_state, stdout, stderr = _run("%s %s reload" % (SERVICE, name))
|
2012-05-01 23:46:45 +02:00
|
|
|
elif state == 'restarted':
|
|
|
|
rc1, stdout1, stderr1 = _run("%s %s stop" % (SERVICE, name))
|
|
|
|
rc2, stdout2, stderr2 = _run("%s %s start" % (SERVICE, name))
|
2012-05-04 07:20:51 +02:00
|
|
|
rc_state = rc + rc1 + rc2
|
2012-05-01 23:46:45 +02:00
|
|
|
stdout = stdout1 + stdout2
|
|
|
|
stderr = stderr1 + stderr2
|
2012-05-03 12:36:06 +02:00
|
|
|
|
2012-05-01 23:46:45 +02:00
|
|
|
out += stdout
|
|
|
|
err += stderr
|
2012-05-04 07:20:51 +02:00
|
|
|
rc = rc + rc_state
|
2012-05-03 12:36:06 +02:00
|
|
|
|
2012-04-27 04:43:36 +02:00
|
|
|
if rc != 0:
|
2012-07-30 11:18:24 +02:00
|
|
|
module.fail_json(msg=err)
|
2012-02-26 02:27:11 +01:00
|
|
|
|
2012-04-27 04:43:36 +02:00
|
|
|
result = {"changed": changed}
|
2012-08-01 23:58:32 +02:00
|
|
|
if module.params['enabled']:
|
|
|
|
result['enabled'] = module.params['enabled']
|
|
|
|
if state:
|
|
|
|
result['state'] = state
|
2012-04-27 19:35:24 +02:00
|
|
|
rc, stdout, stderr = _run("%s %s status" % (SERVICE, name))
|
2012-08-11 18:35:58 +02:00
|
|
|
module.exit_json(**result)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
2012-04-27 04:43:36 +02:00
|
|
|
|
2012-07-30 11:18:24 +02:00
|
|
|
main()
|