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-03-14 05:39:18 +01:00
|
|
|
|
2012-04-27 19:35:24 +02:00
|
|
|
SERVICE = '/sbin/service'
|
|
|
|
|
|
|
|
def _run(cmd):
|
|
|
|
''' :Return: A tuple of ``(returncode, stdout, stderr)`` resulting from executing
|
|
|
|
`cmd` with the shell. '''
|
|
|
|
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
|
|
|
|
stdout, stderr = process.communicate()
|
|
|
|
return (process.returncode, 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-03-14 05:39:18 +01:00
|
|
|
|
|
|
|
if not len(items):
|
2012-03-31 04:57:26 +02:00
|
|
|
print json.dumps(dict(failed=True, msg='this module requires arguments (-a)'))
|
2012-03-14 05:39:18 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
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:
|
|
|
|
print json.dumps(dict(failed=True, msg='expected arguments of the form name=value'))
|
|
|
|
sys.exit(1)
|
|
|
|
(name, value) = arg.split("=")
|
|
|
|
params[name] = value
|
2012-02-26 02:27:11 +01:00
|
|
|
|
|
|
|
name = params['name']
|
2012-03-15 02:49:27 +01:00
|
|
|
state = params.get('state','unknown')
|
2012-04-27 19:35:24 +02:00
|
|
|
list_ = params.get('list')
|
2012-03-15 02:49:27 +01:00
|
|
|
|
2012-03-29 01:06:26 +02:00
|
|
|
# running and started are the same
|
|
|
|
if state not in [ 'running', 'started', 'stopped', 'restarted' ]:
|
2012-03-31 04:57:26 +02:00
|
|
|
print json.dumps(dict(failed=True, msg='invalid state'))
|
2012-03-15 02:49:27 +01:00
|
|
|
sys.exit(1)
|
2012-04-27 19:35:24 +02:00
|
|
|
if list_ and list_ not in ('status',):
|
|
|
|
print json.dumps(dict(failed=True, msg='invalid argument to list'))
|
|
|
|
sys.exit(1)
|
|
|
|
|
2012-02-26 02:27:11 +01:00
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# get service status
|
|
|
|
|
2012-04-27 19:35:24 +02:00
|
|
|
rc, stdout, stderr = _run("%s %s status" % (SERVICE, name))
|
2012-02-26 02:27:11 +01:00
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# determine if we are going to change anything
|
|
|
|
|
|
|
|
running = False
|
2012-04-27 19:35:24 +02:00
|
|
|
if stdout.find("not running") != -1:
|
2012-02-26 02:27:11 +01:00
|
|
|
running = False
|
2012-04-27 19:35:24 +02:00
|
|
|
elif stdout.find("running") != -1:
|
2012-02-26 02:27:11 +01:00
|
|
|
running = True
|
2012-04-27 19:35:24 +02:00
|
|
|
elif name == 'iptables' and stdout.find("ACCEPT") != -1:
|
2012-02-26 02:27:11 +01:00
|
|
|
# iptables status command output is lame
|
|
|
|
# TODO: lookup if we can use a return code for this instead?
|
|
|
|
running = True
|
|
|
|
|
|
|
|
changed = False
|
2012-02-27 04:31:42 +01:00
|
|
|
if not running and state == "started":
|
2012-02-26 02:27:11 +01:00
|
|
|
changed = True
|
2012-02-27 04:31:42 +01:00
|
|
|
elif running and state == "stopped":
|
2012-02-26 02:27:11 +01:00
|
|
|
changed = True
|
2012-02-27 04:31:42 +01:00
|
|
|
elif state == "restarted":
|
2012-02-26 02:27:11 +01:00
|
|
|
changed = True
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# run change commands if we need to
|
|
|
|
|
|
|
|
|
|
|
|
rc = 0
|
|
|
|
if changed:
|
2012-03-29 01:06:26 +02:00
|
|
|
if state in ('started', 'running'):
|
2012-04-27 19:35:24 +02:00
|
|
|
rc, stdout, stderr = _run("%s %s start" % (SERVICE, name))
|
2012-02-27 04:31:42 +01:00
|
|
|
elif state == 'stopped':
|
2012-04-27 19:35:24 +02:00
|
|
|
rc, stdout, stderr = _run("%s %s stop" % (SERVICE, name))
|
2012-02-27 04:31:42 +01:00
|
|
|
elif state == 'restarted':
|
2012-04-27 19:35:24 +02:00
|
|
|
rc1, stdout1, stderr1 = _run("%s %s stop" % (SERVICE, name))
|
|
|
|
rc2, stdout2, stderr2 = _run("%s %s start" % (SERVICE, name))
|
|
|
|
rc = rc1 and rc2
|
|
|
|
stdout = stdout1 + stdout2
|
|
|
|
stderr = stderr1 + stderr2
|
2012-02-26 02:27:11 +01:00
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
print json.dumps({
|
2012-02-26 03:00:51 +01:00
|
|
|
"failed" : 1,
|
2012-04-27 19:35:24 +02:00
|
|
|
"rc" : rc,
|
2012-02-26 02:27:11 +01:00
|
|
|
})
|
2012-04-27 19:35:24 +02:00
|
|
|
print >> sys.stderr, stdout + stderr
|
2012-02-26 02:27:11 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2012-04-27 19:35:24 +02:00
|
|
|
|
2012-02-26 02:27:11 +01:00
|
|
|
# ===============================================
|
|
|
|
# success
|
|
|
|
|
2012-04-27 19:35:24 +02:00
|
|
|
result = {"changed": changed}
|
|
|
|
|
|
|
|
if list_ == 'status':
|
|
|
|
rc, stdout, stderr = _run("%s %s status" % (SERVICE, name))
|
|
|
|
result['status'] = stdout
|
|
|
|
|
|
|
|
print json.dumps(result)
|
2012-02-26 02:27:11 +01:00
|
|
|
|