saltstack/salt — Issue #390: adding enable/disable to services
Author: ewsnow
Created: 2011-12-23 05:04:12+00:00
State: open

I've found it helpful to be able to enable/disable services through salt. Here's a simple diff to show what I mean:

diff --git a/salt/modules/service.py b/salt/modules/service.py
--- a/salt/modules/service.py
+++ b/salt/modules/service.py
@@ -16,6 +16,44 @@
}

+def enable(name, level=None):
+ '''
+ Set the specified service to run at system startup
+
+ level - the run levels to enable (as a csv)
+
+ CLI Example::
+
+ salt '*' service.enable
+ '''
+ cmd = "chkconfig --add {}".format(name)
+ __salt__['cmd.retcode'](cmd)
+
+ if level is None:
+ cmd = "chkconfig {} on".format(name)
+ else:
+ cmd = "chkconfig --level {} {} on".format(level, name)
+ return not __salt__['cmd.retcode'](cmd)
+
+
+def disable(name, level=None):
+ '''
+ Set the specified service to not run at system startup
+
+ level - the run levels to disable (as a csv)
+
+ CLI Example::
+
+ salt '*' service.disable
+ '''
+ if level is None:
+ #cmd = "chkconfig --del {}".format(name)
+ cmd = "chkconfig {} off".format(name)
+ else:
+ cmd = "chkconfig --level {} {} off".format(level, name)
+ return not __salt__['cmd.retcode'](cmd)
+
+
def start(name):
'''
Start the specified service
diff --git a/salt/states/service.py b/salt/states/service.py
--- a/salt/states/service.py
+++ b/salt/states/service.py
@@ -12,6 +12,46 @@
'''

+def enabled(name):
+ '''
+ Set the service to start at system startup
+
+ name
+ The name of the init or rc script used to manage the service
+ '''
+ changes = {name: __salt__['service.enable'](name)}
+ if not changes[name]:
+ return {'name': name,
+ 'changes': {},
+ 'result': False,
+ 'comment': 'Service {0} failed to enable'.format(name)}
+
+ return {'name': name,
+ 'changes': changes,
+ 'result': True,
+ 'comment': 'Service {0} enabled'.format(name)}
+
+
+def disabled(name):
+ '''
+ Set the service to not start at system startup
+
+ name
+ The name of the init or rc script used to manage the service
+ '''
+ changes = {name: __salt__['service.disable'](name)}
+ if not changes[name]:
+ return {'name': name,
+ 'changes': {},
+ 'result': False,
+ 'comment': 'Service {0} failed to disable'.format(name)}
+
+ return {'name': name,
+ 'changes': changes,
+ 'result': True,
+ 'comment': 'Service {0} disabled'.format(name)}
+
+
def running(name, sig=None):
'''
Verify that the service is running