|
|
@ -25,18 +25,19 @@ import sys
|
|
|
|
import shlex
|
|
|
|
import shlex
|
|
|
|
import subprocess
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: switch to fail_json and other helper functions
|
|
|
|
|
|
|
|
# like other modules are using
|
|
|
|
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# ===========================================
|
|
|
|
|
|
|
|
|
|
|
|
SERVICE = '/sbin/service'
|
|
|
|
SERVICE = '/sbin/service'
|
|
|
|
|
|
|
|
|
|
|
|
def _run(cmd):
|
|
|
|
def _run(cmd):
|
|
|
|
''' :Return: A tuple of ``(returncode, stdout, stderr)`` resulting from executing
|
|
|
|
# returns (rc, stdout, stderr) from shell command
|
|
|
|
`cmd` with the shell. '''
|
|
|
|
|
|
|
|
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
|
|
|
|
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
|
|
|
|
stdout, stderr = process.communicate()
|
|
|
|
stdout, stderr = process.communicate()
|
|
|
|
return (process.returncode, stdout, stderr)
|
|
|
|
return (process.returncode, stdout, stderr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
argfile = sys.argv[1]
|
|
|
|
argfile = sys.argv[1]
|
|
|
|
args = open(argfile, 'r').read()
|
|
|
|
args = open(argfile, 'r').read()
|
|
|
|
items = shlex.split(args)
|
|
|
|
items = shlex.split(args)
|
|
|
@ -48,56 +49,67 @@ if not len(items):
|
|
|
|
params = {}
|
|
|
|
params = {}
|
|
|
|
for arg in items:
|
|
|
|
for arg in items:
|
|
|
|
if "=" not in arg:
|
|
|
|
if "=" not in arg:
|
|
|
|
print json.dumps(dict(failed=True, msg='expected arguments of the form name=value'))
|
|
|
|
print json.dumps(dict(failed=True, msg='expected key=value format arguments'))
|
|
|
|
sys.exit(1)
|
|
|
|
sys.exit(1)
|
|
|
|
(name, value) = arg.split("=")
|
|
|
|
(name, value) = arg.split("=")
|
|
|
|
params[name] = value
|
|
|
|
params[name] = value
|
|
|
|
|
|
|
|
|
|
|
|
name = params['name']
|
|
|
|
name = params.get('name', None)
|
|
|
|
state = params.get('state','unknown')
|
|
|
|
|
|
|
|
list_ = params.get('list')
|
|
|
|
if name is None:
|
|
|
|
|
|
|
|
print json.dumps(dict(failed=True, msg='missing name'))
|
|
|
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
state = params.get('state', None)
|
|
|
|
|
|
|
|
list_items = params.get('list', None)
|
|
|
|
|
|
|
|
|
|
|
|
# running and started are the same
|
|
|
|
# running and started are the same
|
|
|
|
if state not in [ 'running', 'started', 'stopped', 'restarted' ]:
|
|
|
|
if state and state not in [ 'running', 'started', 'stopped', 'restarted' ]:
|
|
|
|
print json.dumps(dict(failed=True, msg='invalid state'))
|
|
|
|
print json.dumps(dict(failed=True, msg='invalid value for state'))
|
|
|
|
sys.exit(1)
|
|
|
|
sys.exit(1)
|
|
|
|
if list_ and list_ not in ('status',):
|
|
|
|
if list_items and list_items not in [ 'status' ]:
|
|
|
|
print json.dumps(dict(failed=True, msg='invalid argument to list'))
|
|
|
|
print json.dumps(dict(failed=True, msg='invalid value for list'))
|
|
|
|
sys.exit(1)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# ===========================================
|
|
|
|
# get service status
|
|
|
|
# get service status
|
|
|
|
|
|
|
|
|
|
|
|
rc, stdout, stderr = _run("%s %s status" % (SERVICE, name))
|
|
|
|
rc, status_stdout, status_stderr = _run("%s %s status" % (SERVICE, name))
|
|
|
|
|
|
|
|
status = status_stdout + status_stderr
|
|
|
|
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
|
|
|
|
# determine if we are going to change anything
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
running = False
|
|
|
|
running = False
|
|
|
|
if stdout.find("not running") != -1:
|
|
|
|
if status_stdout.find("not running") != -1:
|
|
|
|
running = False
|
|
|
|
running = False
|
|
|
|
elif stdout.find("running") != -1:
|
|
|
|
elif status_stdout.find("running") != -1:
|
|
|
|
running = True
|
|
|
|
running = True
|
|
|
|
elif name == 'iptables' and stdout.find("ACCEPT") != -1:
|
|
|
|
elif name == 'iptables' and status_stdout.find("ACCEPT") != -1:
|
|
|
|
# iptables status command output is lame
|
|
|
|
# iptables status command output is lame
|
|
|
|
# TODO: lookup if we can use a return code for this instead?
|
|
|
|
# TODO: lookup if we can use a return code for this instead?
|
|
|
|
running = True
|
|
|
|
running = True
|
|
|
|
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
if not running and state == "started":
|
|
|
|
if state:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# a state change command has been requested
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
|
|
|
|
# determine if we are going to change anything
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
if not running and state == "started":
|
|
|
|
changed = True
|
|
|
|
changed = True
|
|
|
|
elif running and state == "stopped":
|
|
|
|
elif running and state == "stopped":
|
|
|
|
changed = True
|
|
|
|
changed = True
|
|
|
|
elif state == "restarted":
|
|
|
|
elif state == "restarted":
|
|
|
|
changed = True
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# ===========================================
|
|
|
|
# run change commands if we need to
|
|
|
|
# run change commands if we need to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rc = 0
|
|
|
|
rc = 0
|
|
|
|
if changed:
|
|
|
|
if changed:
|
|
|
|
if state in ('started', 'running'):
|
|
|
|
if state in ('started', 'running'):
|
|
|
|
rc, stdout, stderr = _run("%s %s start" % (SERVICE, name))
|
|
|
|
rc, stdout, stderr = _run("%s %s start" % (SERVICE, name))
|
|
|
|
elif state == 'stopped':
|
|
|
|
elif state == 'stopped':
|
|
|
@ -109,7 +121,8 @@ if changed:
|
|
|
|
stdout = stdout1 + stdout2
|
|
|
|
stdout = stdout1 + stdout2
|
|
|
|
stderr = stderr1 + stderr2
|
|
|
|
stderr = stderr1 + stderr2
|
|
|
|
|
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
if rc != 0:
|
|
|
|
|
|
|
|
|
|
|
|
print json.dumps({
|
|
|
|
print json.dumps({
|
|
|
|
"failed" : 1,
|
|
|
|
"failed" : 1,
|
|
|
|
"rc" : rc,
|
|
|
|
"rc" : rc,
|
|
|
@ -118,14 +131,31 @@ if rc != 0:
|
|
|
|
sys.exit(1)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ===============================================
|
|
|
|
# ===============================================
|
|
|
|
# success
|
|
|
|
# success
|
|
|
|
|
|
|
|
|
|
|
|
result = {"changed": changed}
|
|
|
|
result = {"changed": changed}
|
|
|
|
|
|
|
|
|
|
|
|
if list_ == 'status':
|
|
|
|
|
|
|
|
rc, stdout, stderr = _run("%s %s status" % (SERVICE, name))
|
|
|
|
rc, stdout, stderr = _run("%s %s status" % (SERVICE, name))
|
|
|
|
|
|
|
|
if list_items and list_items in [ 'status' ]:
|
|
|
|
result['status'] = stdout
|
|
|
|
result['status'] = stdout
|
|
|
|
|
|
|
|
print json.dumps(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print json.dumps(dict(failed=True, msg="expected state or list parameters"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print json.dumps(result)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|