Merge pull request #289 from skvidal/devel

service enable=on/off
reviewable/pr18780/r1
Michael DeHaan 13 years ago
commit a0adf573a9

@ -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,6 +81,9 @@ 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)
# =========================================== # ===========================================
@ -89,37 +103,49 @@ 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: rc = 0
out = ''
# a state change command has been requested err = ''
# ===========================================
# determine if we are going to change anything
changed = False changed = False
if not running and state == "started":
changed = True
elif running and state == "stopped":
changed = True
elif state == "restarted":
changed = True
# =========================================== if enable:
# run change commands if we need to rc_enable, out_enable, err_enable = _do_enable(name, enable)
rc += rc_enable
rc = 0 out += out_enable
if changed: err += err_enable
if state in ('started', 'running'):
rc, stdout, stderr = _run("%s %s start" % (SERVICE, name)) if state:
elif state == 'stopped': # a state change command has been requested
rc, stdout, stderr = _run("%s %s stop" % (SERVICE, name))
elif state == 'restarted': # ===========================================
rc1, stdout1, stderr1 = _run("%s %s stop" % (SERVICE, name)) # determine if we are going to change anything
rc2, stdout2, stderr2 = _run("%s %s start" % (SERVICE, name))
rc = rc1 and rc2 if not running and state == "started":
stdout = stdout1 + stdout2 changed = True
stderr = stderr1 + stderr2 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:
@ -127,7 +153,7 @@ if state:
"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