From 2cb0f126960ba3c493c358a5f5813ee7ed1216c3 Mon Sep 17 00:00:00 2001 From: twangboy Date: Tue, 29 Sep 2015 14:01:10 -0600 Subject: [PATCH 1/3] Removed dependency on powershell to restart salt-minion Fixes: #26629 Used net stop && net start instead of a powershell command to restart the salt-minion service. --- salt/modules/win_service.py | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/salt/modules/win_service.py b/salt/modules/win_service.py index 8e64dc4b7b..7a17313177 100644 --- a/salt/modules/win_service.py +++ b/salt/modules/win_service.py @@ -36,25 +36,6 @@ def __virtual__(): return False -def has_powershell(): - ''' - Confirm if Powershell is available - - CLI Example: - - .. code-block:: bash - - salt '*' service.has_powershell - ''' - for path in os.environ["PATH"].split(os.pathsep): - path = path.strip('"') - fullpath = os.path.join(path, "powershell.exe") - fullpath = os.path.normpath(fullpath) - if os.path.isfile(fullpath) and os.access(fullpath, os.X_OK): - return True - return False - - def get_enabled(): ''' Return the enabled services @@ -235,12 +216,10 @@ def restart(name): salt '*' service.restart ''' - if has_powershell(): - if 'salt-minion' in name: - create_win_salt_restart_task() - return execute_salt_restart_task() - cmd = 'Restart-Service {0}'.format(_cmd_quote(name)) - return not __salt__['cmd.retcode'](cmd, shell='powershell', python_shell=True) + if 'salt-minion' in name: + create_win_salt_restart_task() + return execute_salt_restart_task() + return stop(name) and start(name) @@ -254,7 +233,7 @@ def create_win_salt_restart_task(): salt '*' service.create_win_salt_restart_task() ''' - cmd = 'schtasks /RU "System" /Create /TN restart-salt-minion /TR "powershell Restart-Service salt-minion" /sc ONCE /sd 01/01/1975 /st 01:00 /F /V1 /Z' + cmd = 'schtasks /RU "System" /Create /TN restart-salt-minion /TR "cmd /C ping -n 3 127.0.0.1>nul && net stop salt-minion && net start salt-minion" /sc ONCE /sd 01/01/1975 /st 01:00 /F /V1 /Z' return __salt__['cmd.shell'](cmd) From dc8c01ed077294dc923275ec0302fe7c9726330b Mon Sep 17 00:00:00 2001 From: twangboy Date: Tue, 29 Sep 2015 15:19:19 -0600 Subject: [PATCH 2/3] Fixed some lint --- salt/modules/win_service.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/salt/modules/win_service.py b/salt/modules/win_service.py index 7a17313177..c2f41523cd 100644 --- a/salt/modules/win_service.py +++ b/salt/modules/win_service.py @@ -8,14 +8,9 @@ from __future__ import absolute_import import salt.utils import time import logging -import os from subprocess import list2cmdline from salt.ext.six.moves import zip from salt.ext.six.moves import range -try: - from shlex import quote as _cmd_quote # pylint: disable=E0611 -except ImportError: - from pipes import quote as _cmd_quote log = logging.getLogger(__name__) From 163c54505d8d0593f8428f257d2ab74ec3d15174 Mon Sep 17 00:00:00 2001 From: twangboy Date: Tue, 29 Sep 2015 15:28:23 -0600 Subject: [PATCH 3/3] Fixed tests... hopefully --- tests/unit/modules/win_service_test.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/tests/unit/modules/win_service_test.py b/tests/unit/modules/win_service_test.py index 26ef29ffce..a69b65cf03 100644 --- a/tests/unit/modules/win_service_test.py +++ b/tests/unit/modules/win_service_test.py @@ -119,21 +119,16 @@ class WinServiceTestCase(TestCase): ''' Test to restart the named service ''' - mock = MagicMock(side_effect=[True, True, False]) - with patch.object(win_service, 'has_powershell', mock): - mock_true = MagicMock(return_value=True) - with patch.object(win_service, 'create_win_salt_restart_task', + mock_true = MagicMock(return_value=True) + with patch.object(win_service, 'create_win_salt_restart_task', + mock_true): + with patch.object(win_service, 'execute_salt_restart_task', mock_true): - with patch.object(win_service, 'execute_salt_restart_task', - mock_true): - self.assertTrue(win_service.restart("salt-minion")) + self.assertTrue(win_service.restart("salt-minion")) - with patch.dict(win_service.__salt__, {'cmd.retcode': mock_true}): - self.assertFalse(win_service.restart("salt")) - - with patch.object(win_service, 'stop', mock_true): - with patch.object(win_service, 'start', mock_true): - self.assertTrue(win_service.restart("salt")) + with patch.object(win_service, 'stop', mock_true): + with patch.object(win_service, 'start', mock_true): + self.assertTrue(win_service.restart("salt")) def test_createwin_saltrestart_task(self): '''