From 8cd1803756dc5036832517301b591fb6f688d19b Mon Sep 17 00:00:00 2001 From: Matthew Hopkins Date: Tue, 9 Dec 2014 14:47:45 -0500 Subject: [PATCH] Significantly speeds up highstate on windows by reducing the calls to query all services. --- salt/modules/win_service.py | 89 +++++++++++-------------------------- 1 file changed, 26 insertions(+), 63 deletions(-) diff --git a/salt/modules/win_service.py b/salt/modules/win_service.py index facad982a2..831077495b 100644 --- a/salt/modules/win_service.py +++ b/salt/modules/win_service.py @@ -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 ''' - 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 ''' - 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