commit
a0adf573a9
1 changed files with 58 additions and 32 deletions
40
service
40
service
|
@ -31,6 +31,7 @@ import subprocess
|
|||
# ===========================================
|
||||
|
||||
SERVICE = '/sbin/service'
|
||||
CHKCONFIG = '/sbin/chkconfig'
|
||||
|
||||
def _run(cmd):
|
||||
# returns (rc, stdout, stderr) from shell command
|
||||
|
@ -38,6 +39,15 @@ def _run(cmd):
|
|||
stdout, stderr = process.communicate()
|
||||
return (process.returncode, stdout, stderr)
|
||||
|
||||
|
||||
def _do_enable(name, enable):
|
||||
if enable.lower() in ['on', 'true', 'yes']:
|
||||
rc, stdout, stderr = _run("%s %s on" % (CHKCONFIG, name))
|
||||
elif enable.lower() in ['off', 'false', 'no']:
|
||||
rc, stdout, stderr = _run("%s %s off" % (CHKCONFIG, name))
|
||||
|
||||
return rc, stdout, stderr
|
||||
|
||||
argfile = sys.argv[1]
|
||||
args = open(argfile, 'r').read()
|
||||
items = shlex.split(args)
|
||||
|
@ -62,6 +72,7 @@ if name is None:
|
|||
|
||||
state = params.get('state', None)
|
||||
list_items = params.get('list', None)
|
||||
enable = params.get('enable', None)
|
||||
|
||||
# running and started are the same
|
||||
if state and state not in [ 'running', 'started', 'stopped', 'restarted' ]:
|
||||
|
@ -70,6 +81,9 @@ if state and state not in [ 'running', 'started', 'stopped', 'restarted' ]:
|
|||
if list_items and list_items not in [ 'status' ]:
|
||||
print json.dumps(dict(failed=True, msg='invalid value for list'))
|
||||
sys.exit(1)
|
||||
if enable and enable.lower() not in [ 'on', 'off', 'true', 'false', 'yes', 'no' ]:
|
||||
print json.dumps(dict(failed=True, msg='invalid value for enable'))
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
# ===========================================
|
||||
|
@ -89,15 +103,24 @@ elif name == 'iptables' and status_stdout.find("ACCEPT") != -1:
|
|||
# TODO: lookup if we can use a return code for this instead?
|
||||
running = True
|
||||
|
||||
if state or enable:
|
||||
rc = 0
|
||||
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
|
||||
|
||||
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
|
||||
elif running and state == "stopped":
|
||||
|
@ -108,26 +131,29 @@ if state:
|
|||
# ===========================================
|
||||
# run change commands if we need to
|
||||
|
||||
rc = 0
|
||||
if changed:
|
||||
if state in ('started', 'running'):
|
||||
rc, stdout, stderr = _run("%s %s start" % (SERVICE, name))
|
||||
rc_state, stdout, stderr = _run("%s %s start" % (SERVICE, name))
|
||||
elif state == 'stopped':
|
||||
rc, stdout, stderr = _run("%s %s stop" % (SERVICE, name))
|
||||
rc_state, stdout, stderr = _run("%s %s stop" % (SERVICE, name))
|
||||
elif state == 'restarted':
|
||||
rc1, stdout1, stderr1 = _run("%s %s stop" % (SERVICE, name))
|
||||
rc2, stdout2, stderr2 = _run("%s %s start" % (SERVICE, name))
|
||||
rc = rc1 and rc2
|
||||
rc_state = rc and rc1 and rc2
|
||||
stdout = stdout1 + stdout2
|
||||
stderr = stderr1 + stderr2
|
||||
|
||||
out += stdout
|
||||
err += stderr
|
||||
rc = rc and rc_state
|
||||
|
||||
if rc != 0:
|
||||
|
||||
print json.dumps({
|
||||
"failed" : 1,
|
||||
"rc" : rc,
|
||||
})
|
||||
print >> sys.stderr, stdout + stderr
|
||||
print >> sys.stderr, out + err
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue