new service module handles enable=on/off for chkconfigging it on

pull/289/head
Seth Vidal 13 years ago
parent cfb7c94c49
commit dc30b01c87

@ -31,6 +31,7 @@ import subprocess
# =========================================== # ===========================================
SERVICE = '/sbin/service' SERVICE = '/sbin/service'
CHKCONFIG = '/sbin/chkconfig'
def _run(cmd): def _run(cmd):
# returns (rc, stdout, stderr) from shell command # returns (rc, stdout, stderr) from shell command
@ -38,6 +39,15 @@ def _run(cmd):
stdout, stderr = process.communicate() stdout, stderr = process.communicate()
return (process.returncode, stdout, stderr) 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] argfile = sys.argv[1]
args = open(argfile, 'r').read() args = open(argfile, 'r').read()
items = shlex.split(args) items = shlex.split(args)
@ -62,6 +72,7 @@ if name is None:
state = params.get('state', None) state = params.get('state', None)
list_items = params.get('list', None) list_items = params.get('list', None)
enable = params.get('enable', None)
# running and started are the same # running and started are the same
if state and state not in [ 'running', 'started', 'stopped', 'restarted' ]: if state and state not in [ 'running', 'started', 'stopped', 'restarted' ]:
@ -70,7 +81,10 @@ if state and state not in [ 'running', 'started', 'stopped', 'restarted' ]:
if list_items and list_items not in [ 'status' ]: if list_items and list_items not in [ 'status' ]:
print json.dumps(dict(failed=True, msg='invalid value for list')) print json.dumps(dict(failed=True, msg='invalid value for list'))
sys.exit(1) 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)
# =========================================== # ===========================================
# get service status # get service status
@ -89,45 +103,57 @@ elif name == 'iptables' and status_stdout.find("ACCEPT") != -1:
# 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
if state or 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":
changed = True
elif state == "restarted":
changed = True
# ===========================================
# run change commands if we need to
rc = 0 rc = 0
if changed: out = ''
if state in ('started', 'running'): err = ''
rc, stdout, stderr = _run("%s %s start" % (SERVICE, name)) changed = False
elif state == 'stopped':
rc, stdout, stderr = _run("%s %s stop" % (SERVICE, name)) if enable:
elif state == 'restarted': rc_enable, out_enable, err_enable = _do_enable(name, enable)
rc1, stdout1, stderr1 = _run("%s %s stop" % (SERVICE, name)) rc += rc_enable
rc2, stdout2, stderr2 = _run("%s %s start" % (SERVICE, name)) out += out_enable
rc = rc1 and rc2 err += err_enable
stdout = stdout1 + stdout2
stderr = stderr1 + stderr2 if state:
# a state change command has been requested
# ===========================================
# determine if we are going to change anything
if not running and state == "started":
changed = True
elif running and state == "stopped":
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))
elif state == 'restarted':
rc1, stdout1, stderr1 = _run("%s %s stop" % (SERVICE, name))
rc2, stdout2, stderr2 = _run("%s %s start" % (SERVICE, name))
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: if rc != 0:
print json.dumps({ print json.dumps({
"failed" : 1, "failed" : 1,
"rc" : rc, "rc" : rc,
}) })
print >> sys.stderr, stdout + stderr print >> sys.stderr, out + err
sys.exit(1) sys.exit(1)

Loading…
Cancel
Save