2012-02-26 02:27:11 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
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-02-26 02:27:11 +01:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
import sys
|
|
|
|
import shlex
|
|
|
|
import subprocess
|
2012-05-03 08:34:36 +02:00
|
|
|
import os.path
|
2012-05-10 00:08:45 +02:00
|
|
|
import syslog
|
2012-02-26 02:27:11 +01:00
|
|
|
|
2012-04-27 04:43:36 +02:00
|
|
|
# TODO: switch to fail_json and other helper functions
|
|
|
|
# like other modules are using
|
|
|
|
|
2012-02-26 02:27:11 +01:00
|
|
|
# ===========================================
|
2012-03-14 05:39:18 +01:00
|
|
|
|
2012-05-03 08:34:36 +02:00
|
|
|
SERVICE = None
|
|
|
|
CHKCONFIG = None
|
|
|
|
|
|
|
|
def fail_json(d):
|
|
|
|
print json.dumps(d)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def _find_binaries():
|
|
|
|
# list of possible paths for service/chkconfig binaries
|
|
|
|
# with the most probable first
|
2012-05-03 12:36:06 +02:00
|
|
|
global CHKCONFIG
|
|
|
|
global SERVICE
|
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:
|
|
|
|
fail_json(dict(failed=True, msg='unable to find chkconfig or update-rc.d binary'))
|
|
|
|
if location.get('service', None):
|
|
|
|
SERVICE = location['service']
|
|
|
|
else:
|
|
|
|
fail_json(dict(failed=True, 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
|
|
|
|
|
|
|
|
def _get_service_status(name):
|
|
|
|
rc, status_stdout, status_stderr = _run("%s %s status" % (SERVICE, name))
|
|
|
|
status = status_stdout + status_stderr
|
|
|
|
|
|
|
|
# set the running state to None because we don't know it yet
|
|
|
|
running = None
|
|
|
|
|
|
|
|
# Check if we got upstart on the system and then the job state
|
|
|
|
if INITCTL != None:
|
|
|
|
# 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
|
|
|
|
cleaned_status_stdout = status_stdout.lower().replace(name.lower(),'')
|
|
|
|
if cleaned_status_stdout.find("stop") != -1:
|
|
|
|
running = False
|
|
|
|
elif cleaned_status_stdout.find("run") != -1 and cleaned_status_stdout.find("not") != -1:
|
|
|
|
running = False
|
|
|
|
elif cleaned_status_stdout.find("run") != -1 and cleaned_status_stdout.find("not") == -1:
|
|
|
|
running = True
|
|
|
|
elif cleaned_status_stdout.find("start") != -1 and cleaned_status_stdout.find("not") == -1:
|
|
|
|
running = True
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
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()
|
|
|
|
return (process.returncode, stdout, stderr)
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
if enable.lower() in ['on', 'true', 'yes', 'enable']:
|
|
|
|
rc, stdout, stderr = _run("%s %s %s" % (CHKCONFIG, name, valid_argument['on']))
|
|
|
|
elif enable.lower() in ['off', 'false', 'no', 'disable']:
|
|
|
|
rc, stdout, stderr = _run("%s %s %s" % (CHKCONFIG, name, valid_argument['off']))
|
2012-05-01 23:46:45 +02:00
|
|
|
|
|
|
|
return rc, stdout, stderr
|
|
|
|
|
2012-03-14 05:39:18 +01:00
|
|
|
argfile = sys.argv[1]
|
|
|
|
args = open(argfile, 'r').read()
|
2012-03-15 02:49:27 +01:00
|
|
|
items = shlex.split(args)
|
2012-05-10 00:08:45 +02:00
|
|
|
syslog.openlog('ansible-%s' % os.path.basename(__file__))
|
|
|
|
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args)
|
2012-03-14 05:39:18 +01:00
|
|
|
|
|
|
|
if not len(items):
|
2012-05-03 08:34:36 +02:00
|
|
|
fail_json(dict(failed=True, msg='this module requires arguments (-a)'))
|
2012-03-14 05:39:18 +01:00
|
|
|
|
2012-02-26 02:27:11 +01:00
|
|
|
params = {}
|
2012-04-26 15:48:50 +02:00
|
|
|
for arg in items:
|
|
|
|
if "=" not in arg:
|
2012-05-03 08:34:36 +02:00
|
|
|
fail_json(dict(failed=True, msg='expected key=value format arguments'))
|
|
|
|
|
2012-04-26 15:48:50 +02:00
|
|
|
(name, value) = arg.split("=")
|
|
|
|
params[name] = value
|
2012-02-26 02:27:11 +01:00
|
|
|
|
2012-04-27 04:43:36 +02:00
|
|
|
name = params.get('name', None)
|
|
|
|
|
|
|
|
if name is None:
|
2012-05-03 08:34:36 +02:00
|
|
|
fail_json(dict(failed=True, msg='missing name'))
|
2012-04-27 04:43:36 +02:00
|
|
|
|
|
|
|
state = params.get('state', None)
|
|
|
|
list_items = params.get('list', None)
|
2012-05-10 05:13:13 +02:00
|
|
|
enable = params.get('enabled', params.get('enable', None))
|
2012-03-15 02:49:27 +01:00
|
|
|
|
2012-03-29 01:06:26 +02:00
|
|
|
# running and started are the same
|
2012-05-15 11:28:49 +02:00
|
|
|
if state and state.lower() not in [ 'running', 'started', 'stopped', 'restarted','reloaded' ]:
|
2012-05-03 08:34:36 +02:00
|
|
|
fail_json(dict(failed=True, msg='invalid value for state'))
|
|
|
|
if list_items and list_items.lower() not in [ 'status' ]:
|
|
|
|
fail_json(dict(failed=True, msg='invalid value for list'))
|
|
|
|
if enable and enable.lower() not in [ 'on', 'off', 'true', 'false', 'yes', 'no', 'enable', 'disable' ]:
|
|
|
|
fail_json(dict(failed=True, msg='invalid value for enable'))
|
|
|
|
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# find binaries locations on minion
|
|
|
|
_find_binaries()
|
2012-05-01 23:46:45 +02:00
|
|
|
|
2012-02-26 02:27:11 +01:00
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# get service status
|
2012-06-17 15:55:26 +02:00
|
|
|
running = _get_service_status(name)
|
2012-02-26 02:27:11 +01:00
|
|
|
|
|
|
|
|
2012-05-01 23:46:45 +02:00
|
|
|
if state or enable:
|
2012-04-27 04:43:36 +02:00
|
|
|
rc = 0
|
2012-05-01 23:46:45 +02:00
|
|
|
out = ''
|
|
|
|
err = ''
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
if enable:
|
|
|
|
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:
|
|
|
|
print json.dumps({
|
|
|
|
"failed" : True,
|
|
|
|
"msg" : "failed determining the current service state => state stays unchanged",
|
|
|
|
})
|
|
|
|
print >> sys.stderr, out + err
|
|
|
|
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-05-04 07:20:51 +02:00
|
|
|
if not running and state in ("started", "running"):
|
2012-05-01 23:46:45 +02:00
|
|
|
changed = True
|
2012-05-15 11:28:49 +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:
|
|
|
|
if state in ('started', 'running'):
|
|
|
|
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:
|
|
|
|
|
|
|
|
print json.dumps({
|
|
|
|
"failed" : 1,
|
|
|
|
"rc" : rc,
|
|
|
|
})
|
2012-05-01 23:46:45 +02:00
|
|
|
print >> sys.stderr, out + err
|
2012-04-27 04:43:36 +02:00
|
|
|
sys.exit(1)
|
2012-02-26 02:27:11 +01:00
|
|
|
|
2012-04-27 19:35:24 +02:00
|
|
|
|
2012-04-27 04:43:36 +02:00
|
|
|
# ===============================================
|
|
|
|
# success
|
2012-02-26 02:27:11 +01:00
|
|
|
|
2012-04-27 04:43:36 +02:00
|
|
|
result = {"changed": changed}
|
2012-04-27 19:35:24 +02:00
|
|
|
|
|
|
|
rc, stdout, stderr = _run("%s %s status" % (SERVICE, name))
|
2012-04-27 04:43:36 +02:00
|
|
|
if list_items and list_items in [ 'status' ]:
|
|
|
|
result['status'] = stdout
|
|
|
|
print json.dumps(result)
|
|
|
|
|
2012-04-27 19:35:24 +02:00
|
|
|
|
2012-04-27 04:43:36 +02:00
|
|
|
elif list_items is not None:
|
|
|
|
|
|
|
|
# solo list=status mode, don't change anything, just return
|
|
|
|
# suitable for /usr/bin/ansible usage or API, playbooks
|
|
|
|
# not so much
|
|
|
|
|
|
|
|
print json.dumps({
|
|
|
|
"status" : status
|
|
|
|
})
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
2012-05-03 12:36:06 +02:00
|
|
|
print json.dumps(dict(failed=True, msg="expected state or list parameters"))
|
2012-04-27 04:43:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
sys.exit(0)
|
2012-02-26 02:27:11 +01:00
|
|
|
|