Create refresh_environment salt util

This commit is contained in:
twangboy 2018-01-31 14:29:37 -07:00
parent 967b83940c
commit 79299361c3
No known key found for this signature in database
GPG Key ID: 93FF3BDEB278C9EB
3 changed files with 34 additions and 15 deletions

View File

@ -38,7 +38,6 @@ from salt.ext.six.moves import range # pylint: disable=W0622,import-error
# Import third party libs
try:
import win32gui
import win32api
import win32con
import pywintypes
@ -48,6 +47,7 @@ except ImportError:
# Import salt libs
import salt.utils
import salt.utils.win_functions
from salt.exceptions import CommandExecutionError
PY2 = sys.version_info[0] == 2
@ -68,7 +68,7 @@ def __virtual__():
if not HAS_WINDOWS_MODULES:
return (False, 'reg execution module failed to load: '
'One of the following libraries did not load: '
+ 'win32gui, win32con, win32api')
'win32con, win32api, pywintypes')
return __virtualname__
@ -193,11 +193,7 @@ def broadcast_change():
salt '*' reg.broadcast_change
'''
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms644952(v=vs.85).aspx
_, res = win32gui.SendMessageTimeout(
win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 0,
win32con.SMTO_ABORTIFHUNG, 5000)
return not bool(res)
return salt.utils.win_functions.refresh_environment()
def list_keys(hive, key=None, use_32bit_registry=False):

View File

@ -12,18 +12,17 @@ from __future__ import absolute_import
import logging
import re
import os
import ctypes
from salt.ext.six.moves import map
# Third party libs
try:
from win32con import HWND_BROADCAST, WM_SETTINGCHANGE, SMTO_ABORTIFHUNG
HAS_WIN32 = True
except ImportError:
HAS_WIN32 = False
# Import salt libs
import salt.utils
import salt.utils.win_functions
# Settings
log = logging.getLogger(__name__)
@ -63,12 +62,7 @@ def rehash():
salt '*' win_path.rehash
'''
broadcast_message = ctypes.create_unicode_buffer('Environment')
user32 = ctypes.windll('user32', use_last_error=True)
result = user32.SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
broadcast_message, SMTO_ABORTIFHUNG,
5000, 0)
return result == 1
return salt.utils.win_functions.refresh_environment()
def get_path():

View File

@ -6,6 +6,7 @@ missing functions in other modules
from __future__ import absolute_import
import platform
import re
import ctypes
# Import Salt Libs
from salt.exceptions import CommandExecutionError
@ -17,6 +18,7 @@ try:
import win32api
import win32net
import win32security
from win32con import HWND_BROADCAST, WM_SETTINGCHANGE, SMTO_ABORTIFHUNG
HAS_WIN32 = True
except ImportError:
HAS_WIN32 = False
@ -210,3 +212,30 @@ def escape_for_cmd_exe(arg):
return meta_map[char]
return meta_re.sub(escape_meta_chars, arg)
def refresh_environment():
'''
Send a WM_SETTINGCHANGE Broadcast to Windows to refresh the Environment
variables for new processes.
.. note::
This will only affect new processes that aren't launched by services. To
apply changes to the path or registry to services, the host must be
restarted. The ``salt-minion``, if running as a service, will not see
changes to the environment until the system is restarted. See
`MSDN Documentation <https://support.microsoft.com/en-us/help/821761/changes-that-you-make-to-environment-variables-do-not-affect-services>`
CLI Example:
... code-block:: python
import salt.utils.win_functions
salt.utils.win_functions.refresh_environment()
'''
broadcast_message = ctypes.create_unicode_buffer('Environment')
user32 = ctypes.windll('user32', use_last_error=True)
result = user32.SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
broadcast_message, SMTO_ABORTIFHUNG,
5000, 0)
return result == 1