From e2907850b5d3ee1c68b72361e601581d2e21e864 Mon Sep 17 00:00:00 2001 From: Nahum Shalman Date: Mon, 24 Feb 2014 17:33:01 -0500 Subject: [PATCH] SmartOS/Solaris: fix SMF service management fixes saltstack/salt#10702 The functions previously named get_enabled and get_disabled actually only queried what services were running or not This caused breakages where a state that both requested "enabled" and "running" could cause salt to start the service which would make it appear in the list of enabled services, but not actually get enabled and thus not actually come back up on reboot Those functions have been renamed to get_running and get_stopped An accurate enabled function has been written which queries the true property that determines if a service will come up at boot and disabled returns its negation. New get_enabled and get_disabled functions have also been written which accurately return the list of enabled or disabled services Those functions return full FMRIs (rather than the much shorter 'SVC' column) because that's how SMF actually works internally. --- salt/modules/smf.py | 76 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 11 deletions(-) diff --git a/salt/modules/smf.py b/salt/modules/smf.py index c2d24ef8443c..e3c0142c5f46 100644 --- a/salt/modules/smf.py +++ b/salt/modules/smf.py @@ -24,18 +24,34 @@ def __virtual__(): return False -def get_enabled(): +def _get_enabled_disabled(enabled_prop="true"): ''' - Return the enabled services + DRY: Get all service FMRIs and their enabled property + ''' + ret = set() + cmd = '/usr/bin/svcprop -c -p general/enabled "*"' + lines = __salt__['cmd.run_stdout'](cmd).splitlines() + for line in lines: + comps = line.split() + if not comps: + continue + if comps[2] == enabled_prop: + ret.add(comps[0].split("/:properties")[0]) + return sorted(ret) + + +def get_running(): + ''' + Return the running services CLI Example: .. code-block:: bash - salt '*' service.get_enabled + salt '*' service.get_running ''' ret = set() - cmd = 'svcs -H -o SVC,STATE -s SVC' + cmd = '/usr/bin/svcs -H -o SVC,STATE -s SVC' lines = __salt__['cmd.run'](cmd).splitlines() for line in lines: comps = line.split() @@ -46,18 +62,18 @@ def get_enabled(): return sorted(ret) -def get_disabled(): +def get_stopped(): ''' - Return the disabled services + Return the stopped services CLI Example: .. code-block:: bash - salt '*' service.get_disabled + salt '*' service.get_stopped ''' ret = set() - cmd = 'svcs -aH -o SVC,STATE -s SVC' + cmd = '/usr/bin/svcs -aH -o SVC,STATE -s SVC' lines = __salt__['cmd.run'](cmd).splitlines() for line in lines: comps = line.split() @@ -108,7 +124,7 @@ def get_all(): salt '*' service.get_all ''' ret = set() - cmd = 'svcs -aH -o SVC,STATE -s SVC' + cmd = '/usr/bin/svcs -aH -o SVC,STATE -s SVC' lines = __salt__['cmd.run'](cmd).splitlines() for line in lines: comps = line.split() @@ -249,7 +265,17 @@ def enabled(name): salt '*' service.enabled ''' - return name in get_enabled() + # The property that reveals whether a service is enabled + # can only be queried using the full FMRI + # We extract the FMRI and then do the query + fmri_cmd = '/usr/bin/svcs -H -o FMRI {0}'.format(name) + fmri = __salt__['cmd.run'](fmri_cmd) + cmd = '/usr/sbin/svccfg -s {0} listprop general/enabled'.format(fmri) + comps = __salt__['cmd.run'](cmd).split() + if comps[2] == 'true': + return True + else: + return False def disabled(name): @@ -262,4 +288,32 @@ def disabled(name): salt '*' service.disabled ''' - return name in get_disabled() + return not enabled(name) + + +def get_enabled(): + ''' + Return the enabled services + + CLI Example: + + .. code-block:: bash + + salt '*' service.get_enabled + ''' + # Note that this returns the full FMRI + return _get_enabled_disabled("true") + + +def get_disabled(): + ''' + Return the disabled services + + CLI Example: + + .. code-block:: bash + + salt '*' service.get_disabled + ''' + # Note that this returns the full FMRI + return _get_enabled_disabled("false")