Merge pull request #27522 from twangboy/fix_26629

Removed dependency on powershell to restart salt-minion
This commit is contained in:
Mike Place 2015-09-30 10:19:29 -06:00
commit fd11e0cd95
2 changed files with 13 additions and 44 deletions

View File

@ -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__)
@ -36,25 +31,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 +211,10 @@ def restart(name):
salt '*' service.restart <service name>
'''
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 +228,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)

View File

@ -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):
'''