New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix dunder virtual to check for Remote Administration Tools #31292
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4bdae47
Added checks for Windows 2008 R2
twangboy 3270a28
Add check for server manager module
twangboy 13cd57a
Remove repeating Import ServerManager command
twangboy a0be431
Fix cmd_quote error
twangboy 89b47ab
Update return documentation for install/remove
twangboy File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,24 +16,43 @@ | |
|
||
log = logging.getLogger(__name__) | ||
|
||
__virtualname__ = 'win_servermanager' | ||
|
||
|
||
def __virtual__(): | ||
''' | ||
Load only on windows | ||
Load only on windows with servermanager module | ||
''' | ||
if salt.utils.is_windows(): | ||
return 'win_servermanager' | ||
return False | ||
if not salt.utils.is_windows(): | ||
return (False, 'Failed to load win_servermanager module:\n' | ||
'Only available on Windows systems.') | ||
|
||
if not _check_server_manager(): | ||
return (False, 'Failed to load win_servermanager module:\n' | ||
'ServerManager module not available.\n' | ||
'May need to install Remote Server Administration Tools.') | ||
|
||
return __virtualname__ | ||
|
||
|
||
def _srvmgr(func): | ||
def _check_server_manager(): | ||
''' | ||
Execute a function from the ServerManager PS module and return the STDOUT | ||
See if ServerManager module will import | ||
|
||
Returns: True if import is successful, otherwise returns False | ||
''' | ||
return __salt__['cmd.run']( | ||
'Import-Module ServerManager ; {0}'.format(func), | ||
shell='powershell', | ||
python_shell=True) | ||
return not __salt__['cmd.retcode']('Import-Module ServerManager', | ||
shell='powershell', | ||
python_shell=True) | ||
|
||
|
||
def _pshell(func): | ||
''' | ||
Execute a powershell command and return the STDOUT | ||
''' | ||
return __salt__['cmd.run']('{0}'.format(func), | ||
shell='powershell', | ||
python_shell=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question here with python_shell being set to True |
||
|
||
|
||
def _parse_powershell_list(lst): | ||
|
@@ -59,34 +78,43 @@ def list_available(): | |
''' | ||
List available features to install | ||
|
||
:return: A list of available features | ||
:rtype: list | ||
|
||
CLI Example: | ||
|
||
.. code-block:: bash | ||
|
||
salt '*' win_servermanager.list_available | ||
''' | ||
return _srvmgr('Get-WindowsFeature -erroraction silentlycontinue -warningaction silentlycontinue') | ||
return _pshell('Get-WindowsFeature -erroraction silentlycontinue ' | ||
'-warningaction silentlycontinue') | ||
|
||
|
||
def list_installed(): | ||
''' | ||
List installed features. Supported on Windows Server 2008 and Windows 8 and | ||
newer. | ||
|
||
:return: A list of installed features | ||
:rtype: list | ||
|
||
CLI Example: | ||
|
||
.. code-block:: bash | ||
|
||
salt '*' win_servermanager.list_installed | ||
''' | ||
ret = {} | ||
names = _srvmgr('Get-WindowsFeature -erroraction silentlycontinue -warningaction silentlycontinue | Select DisplayName,Name') | ||
names = _pshell('Get-WindowsFeature -erroraction silentlycontinue ' | ||
'-warningaction silentlycontinue | Select DisplayName,Name') | ||
for line in names.splitlines()[2:]: | ||
splt = line.split() | ||
name = splt.pop(-1) | ||
display_name = ' '.join(splt) | ||
ret[name] = display_name | ||
state = _srvmgr('Get-WindowsFeature -erroraction silentlycontinue -warningaction silentlycontinue | Select Installed,Name') | ||
state = _pshell('Get-WindowsFeature -erroraction silentlycontinue ' | ||
'-warningaction silentlycontinue | Select Installed,Name') | ||
for line in state.splitlines()[2:]: | ||
splt = line.split() | ||
if splt[0] == 'False' and splt[1] in ret: | ||
|
@@ -100,12 +128,20 @@ def install(feature, recurse=False): | |
''' | ||
Install a feature | ||
|
||
Note: | ||
Some features requires reboot after un/installation, if so until the server is restarted | ||
Other features can not be installed ! | ||
.. note:: | ||
Some features require reboot after un/installation, if so until the | ||
server is restarted other features can not be installed! | ||
|
||
.. note:: | ||
Some features take a long time to complete un/installation, set -t with | ||
a long timeout | ||
|
||
:param str feature: The name of the feature to install | ||
|
||
:param bool recurse: Install all sub-features | ||
|
||
Note: | ||
Some features takes a long time to complete un/installation, set -t with a long timeout | ||
:return: A dictionary containing the results of the install | ||
:rtype: dict | ||
|
||
CLI Example: | ||
|
||
|
@@ -117,8 +153,10 @@ def install(feature, recurse=False): | |
sub = '' | ||
if recurse: | ||
sub = '-IncludeAllSubFeature' | ||
out = _srvmgr('Add-WindowsFeature -Name {0} {1} -erroraction silentlycontinue -warningaction silentlycontinue | format-list'.format( | ||
_cmd_quote(feature), sub)) | ||
out = _pshell('Add-WindowsFeature -Name {0} {1} ' | ||
'-erroraction silentlycontinue ' | ||
'-warningaction silentlycontinue ' | ||
'| format-list'.format(_cmd_quote(feature), sub)) | ||
return _parse_powershell_list(out) | ||
|
||
|
||
|
@@ -133,12 +171,19 @@ def remove(feature): | |
take a while to complete installation/uninstallation, so it is a good | ||
idea to use the ``-t`` option to set a longer timeout. | ||
|
||
:param str feature: The name of the feature to remove | ||
|
||
:return: A dictionary containing the results of the uninstall | ||
:rtype: dict | ||
|
||
CLI Example: | ||
|
||
.. code-block:: bash | ||
|
||
salt -t 600 '*' win_servermanager.remove Telnet-Client | ||
''' | ||
out = _srvmgr('Remove-WindowsFeature -Name {0} -erroraction silentlycontinue -warningaction silentlycontinue | format-list'.format( | ||
_cmd_quote(feature))) | ||
out = _pshell('Remove-WindowsFeature -Name {0} ' | ||
'-erroraction silentlycontinue ' | ||
'-warningaction silentlycontinue ' | ||
'| format-list'.format(_cmd_quote(feature))) | ||
return _parse_powershell_list(out) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is python_shell set to True here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cachedout The powershell command requires python_shell=True. The functions have been using cmd_quote on passed parameters for this reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
K. I wasn't aware of that. Thanks for letting me know.