Skip to content
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 bug where cmd.powershell fails to return #36958

Merged
merged 4 commits into from
Oct 19, 2016

Conversation

twangboy
Copy link
Contributor

What does this PR do?

Fixes a bug where some powershell commands would take hours to complete due to the extreme depth setting for ConvertTo-JSON. Reverted to the Windows default (2) and added the option to pass a different value.

Added additional notes to the documentation explaining the purpose of the cmd.powershell function as the name is misleading. Made recommendations to use cmd.run with shell=powershell instead unless they need the data output to be a dictionary.

What issues does this PR fix or reference?

#31509

Previous Behavior

Basic powershell commands such as dir take exponentially longer times to complete as the depth value for ConvertTo-JSON increases. Anything above 4 is painful. It was hard-coded to 32.

New Behavior

Changed the depth value to use Windows defaults (2) with the option to pass a different value.

Tests written?

No

@terminalmage
Copy link
Contributor

We should make it explicit that the depth parameter is new to 2016.3.4.

@terminalmage
Copy link
Contributor

Oops, sorry, accidentally clicked to close and comment instead of just to comment.

@terminalmage terminalmage added the Pending-Discussion The issue or pull request needs more discussion before it can be closed or merged label Oct 13, 2016
@twangboy
Copy link
Contributor Author

@terminalmage Added .. versionadded:: 2016.3.4 to the depth option

**kwargs):
'''
Execute the passed PowerShell command and return the output as a string.
Execute the passed PowerShell command and return the output as a dictionary.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look accurate. This is executing a cmd.run, which most certainly does not return a dictionary. There is a difference between a dictionary, which is a data structure, and a string of JSON, whichis not. JSON looks like a Python dictionary, but it is not a dictionary. Please change this back to what it was originally, or at least clarify and say "return the output in JSON format".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disregard, I wrongly thought that we were returning the result of the run() command.

.. versionadded:: 2016.3.4

:returns:
:dict: A dictionary of data returned by the powershell command.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, wrong. Not a dictionary. It would be accurate to say "A JSON-formatted string of data returned by the powershell command."

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disregard, I wrongly thought that we were returning the result of the run() command.

@damon-atkins
Copy link
Contributor

damon-atkins commented Oct 14, 2016

Could you make depth a kwargs please.
Effectively it is any way, as no one is going to call it with all the parameters to eventual reach it to set the depth. I need to open a generic item for this against all public functions.

@terminalmage
Copy link
Contributor

@damon-atkins that should not be necessary. It can be called on the Salt using depth=8 and Salt's CLI client will format the function call correctly.

@damon-atkins
Copy link
Contributor

It creates a support issue with the order of the parameters, as you can not control how user base will use it. If you expect to always call it with depth=8 then it should be a kwargs Why create a maintenance overhead. e.g.
A shell script with

#!/bin/bash
# do some CI/CD
salt windows cmd.powershell 'mycmd'  None  None  None  powershell  None  False  None  False  None  'info'  True   60   True  False  'test'  True  'abc'
# do more stuff

use_vt is marked experimental, let say SaltStack decide its no good and should be removed.
The param can not be removed because it would upset the order of the params, as its not the last param, its the 3rd last one. Current method locks you in.

If most of them were kwargs, then it could be ignored, or you could provide a warning for it when its set.

I have started an issue for it.

@terminalmage
Copy link
Contributor

terminalmage commented Oct 14, 2016

I believe that the arg=value usage is the recommended usage anyway. At least that's how I remember it being from when I started using salt, and it's how I've always used it.

@thatch45 thoughts? I personally think that if you're going to be worried about argument order, then why not make everything with a default value a kwarg, everywhere in Salt?

@thatch45
Copy link
Contributor

It is better this way, having it be an explicit argument like this makes it detectable by salt and the state system which makes it easier for our automated argument forwarding to better report on it.

@twangboy
Copy link
Contributor Author

@terminalmage What else do I need to do on this one?

@terminalmage
Copy link
Contributor

@twangboy Looks like there are still a couple docstring edits that need to be made, see my comments from last week here.

The rest of the PR looks good.

@twangboy
Copy link
Contributor Author

@terminalmage The last thing the function does is run json.loads on the result which I believe is a Python dictionary (line 2801).

@terminalmage
Copy link
Contributor

You're right, I misread and thought that the result of the run() was being returned. Sorry!

@terminalmage terminalmage merged commit 8d44efe into saltstack:2016.3 Oct 19, 2016
@twangboy twangboy deleted the fix_cmd_powershell branch August 21, 2017 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pending-Discussion The issue or pull request needs more discussion before it can be closed or merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants