Significantly speeds up highstate on windows by reducing the calls to query all services.

This commit is contained in:
Matthew Hopkins 2014-12-09 14:47:45 -05:00
parent e5dc9f5b28
commit 8cd1803756

View File

@ -31,19 +31,6 @@ def __virtual__():
return False
def has_powershell():
'''
Confirm if Powershell is available
CLI Example:
.. code-block:: bash
salt '*' service.has_powershell
'''
return 'powershell' in __salt__['cmd.run']('where powershell')
def get_enabled():
'''
Return the enabled services
@ -54,29 +41,7 @@ def get_enabled():
salt '*' service.get_enabled
'''
if has_powershell():
cmd = 'Get-WmiObject win32_service | where {$_.startmode -eq "Auto"} | select-object name'
lines = __salt__['cmd.run'](cmd, shell='POWERSHELL').splitlines()
return sorted([line.strip() for line in lines[3:]])
else:
ret = set()
services = []
cmd = list2cmdline(['sc', 'query', 'type=', 'service', 'state=', 'all', 'bufsize=', str(BUFFSIZE)])
lines = __salt__['cmd.run'](cmd).splitlines()
for line in lines:
if 'SERVICE_NAME:' in line:
comps = line.split(':', 1)
if not len(comps) > 1:
continue
services.append(comps[1].strip())
for service in services:
cmd2 = list2cmdline(['sc', 'qc', service, str(BUFFSIZE)])
lines = __salt__['cmd.run'](cmd2).splitlines()
for line in lines:
if 'AUTO_START' in line:
ret.add(service)
return sorted(ret)
return sorted([service for service in get_all() if enabled(service)])
def get_disabled():
@ -89,30 +54,7 @@ def get_disabled():
salt '*' service.get_disabled
'''
if has_powershell():
cmd = 'Get-WmiObject win32_service | where {$_.startmode -ne "Auto"} | select-object name'
lines = __salt__['cmd.run'](cmd, shell='POWERSHELL').splitlines()
return sorted([line.strip() for line in lines[3:]])
else:
ret = set()
services = []
cmd = list2cmdline(['sc', 'query', 'type=', 'service', 'state=', 'all', 'bufsize=', str(BUFFSIZE)])
lines = __salt__['cmd.run'](cmd).splitlines()
for line in lines:
if 'SERVICE_NAME:' in line:
comps = line.split(':', 1)
if not len(comps) > 1:
continue
services.append(comps[1].strip())
for service in services:
cmd2 = list2cmdline(['sc', 'qc', service])
lines = __salt__['cmd.run'](cmd2).splitlines()
for line in lines:
if 'DEMAND_START' in line:
ret.add(service)
elif 'DISABLED' in line:
ret.add(service)
return sorted(ret)
return sorted([service for service in get_all() if disabled(service)])
def available(name):
@ -154,7 +96,16 @@ def get_all():
salt '*' service.get_all
'''
return sorted(get_enabled() + get_disabled())
ret = set()
cmd = list2cmdline(['sc', 'query', 'type=', 'service', 'state=', 'all', 'bufsize=', str(BUFFSIZE)])
lines = __salt__['cmd.run'](cmd).splitlines()
for line in lines:
if 'SERVICE_NAME:' in line:
comps = line.split(':', 1)
if not len(comps) > 1:
continue
ret.add(comps[1].strip())
return sorted(ret)
def get_service_name(*args):
@ -347,7 +298,12 @@ def enabled(name, **kwargs):
salt '*' service.enabled <service name>
'''
return name in get_enabled()
cmd = list2cmdline(['sc', 'qc', name])
lines = __salt__['cmd.run'](cmd).splitlines()
for line in lines:
if 'AUTO_START' in line:
return True
return False
def disabled(name):
@ -360,4 +316,11 @@ def disabled(name):
salt '*' service.disabled <service name>
'''
return name in get_disabled()
cmd = list2cmdline(['sc', 'qc', name])
lines = __salt__['cmd.run'](cmd).splitlines()
for line in lines:
if 'DEMAND_START' in line:
return True
elif 'DISABLED' in line:
return True
return False