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 setting Path for Windows #1876

Closed
wants to merge 1 commit into from
Closed

Fix setting Path for Windows #1876

wants to merge 1 commit into from

Conversation

alex-slynko
Copy link
Contributor

ENV['PATH'] is broken after changing path in windows. It replaces Path in Ruby to path with system variables.
That leads to broken recipes that use executables in System32.

Variant 1 is proposed in this pull request is to replace all systemroot variables to new ones.
Variant 2 would be to change ENV['Path'] separately from changing actual path.

@adamedx
Copy link
Contributor

adamedx commented Aug 21, 2014

@alex-slynko-wonga, can you give an example recipe that exhibits bad behavior without this change and describe the resulting behavior? Also, is this a regression from a particular Chef release, or has this behavior always been present?

@@ -33,7 +33,9 @@ def create_env
end
obj.variablevalue = @new_resource.value
obj.put_
ENV[@new_resource.key_name] = @new_resource.value
value = @new_resource.value
value.gsub!(/%SystemRoot%/, ENV['SystemRoot']) if @new_resource.key_name.upcase == 'PATH'
Copy link
Contributor

Choose a reason for hiding this comment

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

Theoretically PATH could be specified as lowercase path, or mixed case, correct? So we'd need to handle that scenario. Same for %SystemRoot% in the environment variable value. And there may be parts of the path that contain other environment variables (e.g. %systemdrive%, %windir%, %programfiles%, etc.).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, that is why I check key_name.upcase
windows_path recipe uses 'Path' at the moment.

And as for other variables - can be done.

@alex-slynko
Copy link
Contributor Author

Hi @adamedx
Here is the issue.
We are including some directory in the path using windows_path recipe.
Then in next recipe we are installing other package using msiexec.
Result is

 ---- Begin output of msiexec /qn /i "c:\chef\cache\node-v0.10.18-x64.msi" ----
 172.30.183.24
STDOUT:
172.30.183.24
 STDERR: 'msiexec' is not recognized as an internal or external command,

That happens because in mixlib-shellout there is a search for executable candidates using ENV['PATH']

And this happened only with latest version.

@alex-slynko
Copy link
Contributor Author

fixing tests for replacing multiple variables

@adamedx
Copy link
Contributor

adamedx commented Aug 21, 2014

@alex-slynko-wonga very interesting. Can you see if reverting to an old version of the windows cookbook, 1.34.0, resolves this? We had some fun with 'path' due to a regression in Chef that was fixed in 11.14.6.
Also, can you confirm that you reproduce this in Chef 11.14.6?

@alex-slynko
Copy link
Contributor Author

@Adamex that breaks only 11.14.6
Windows 1.34.0 doesn't help

@alex-slynko
Copy link
Contributor Author

@adamedx happens with 11.14.2 and 11.14.6.
Windows 1.34.0 doesn't help, just breaks it earlier.
I believe that happened because of saving changed ENV variable.

@jaym
Copy link
Contributor

jaym commented Sep 8, 2014

Verified this is an issue.

Ruby does not look like it expands the variables, and the new representation does not expand them for us. Our implementation needs to take care of this in https://github.com/opscode/chef/blob/master/lib/chef/provider/env/windows.rb

If we want to get this out to people ASAP, we can patch this in the windows cookbook for the meantime.

@lamont-granquist
Copy link
Contributor

So it looks like mixlib-shellout has had this bug for > 2 years?

https://github.com/opscode/mixlib-shellout/blob/master/lib/mixlib/shellout/windows.rb#L290

It seems like that's the appropriate place to fix it?

@lamont-granquist
Copy link
Contributor

^^^ okay so it looks like that is incorrect, but it wasn't clear.

the behavior in pry looks like ENV['PATH'] has its %-variables expanded by ruby on startup and then the defacto 'API' expects any changes to the ENV['PATH'] to also be expanded.

it smells like somewhere in ruby there should be a function to do that %-variable expansion and we should try to reuse that if possible, also we can't possibly be the only people to run into this... (any relevant stackoverflow questions?)

@btm
Copy link
Contributor

btm commented Sep 8, 2014

It looks like reading ENV['PATH'] in Ruby returns the same format as echo $env:PATH in powershell (no embedded variables):

PS C:\Users\btm_000> echo $env:PATH
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\ansicon;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wb
em;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Amazon\AWSCLI\;C:\Program Files (x86)\OpenSSH\bin;C:\Pr
ogram Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Git\cmd;C:\HashiCorp\Vagrant\bin;C:\Program Files (x86)\7-
Zip;C:\Program Files (x86)\WiX Toolset v3.8\bin;C:\Program Files\Oracle\VirtualBox;C:\opscode\chef\bin;C:\opscode\chef\
embedded\bin;C:\opscode\chef\bin;C:\opscode\chef\embedded\bin;C:\Ruby193\bin

irb(main):003:0> ENV['PATH']
=> "C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\ansicon;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS
\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Amazon\\AWSCLI\\;C:\\Program Files
(x86)\\OpenSSH\\bin;C:\\Program Files (x86)\\Windows Live\\Shared;C:\\Program Files (x86)\\Git\\cmd;C:\\HashiCorp\\Vagra
nt\\bin;C:\\Program Files (x86)\\7-Zip;C:\\Program Files (x86)\\WiX Toolset v3.8\\bin;C:\\Program Files\\Oracle\\Virtual
Box;C:\\opscode\\chef\\bin;C:\\opscode\\chef\\embedded\\bin;C:\\opscode\\chef\\bin;C:\\opscode\\chef\\embedded\\bin;C:\\
Ruby193\\bin"

But the data we get back from WmiLite has embedded variables:

irb(main):002:0> WmiLite::Wmi.new.query("select * from Win32_Environment where name = 'PATH'")
=> [#<WmiLite::Wmi::Instance:0x2590158 @wmi_ole_object=#<WIN32OLE:0x2590170>, @property_map={"caption"=>"<SYSTEM>\\Path"
, "description"=>"<SYSTEM>\\Path", "installdate"=>nil, "name"=>"Path", "status"=>"OK", "systemvariable"=>true, "username
"=>"<SYSTEM>", "variablevalue"=>"C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\ansicon;%SystemRoot%\\sy
stem32;%SystemRoot%;%SystemRoot%\\System32\\Wbem;%SYSTEMROOT%\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Am
azon\\AWSCLI\\;C:\\Program Files (x86)\\OpenSSH\\bin;C:\\Program Files (x86)\\Windows Live\\Shared;C:\\Program Files (x8
6)\\Git\\cmd;C:\\HashiCorp\\Vagrant\\bin;C:\\Program Files (x86)\\7-Zip;C:\\Program Files (x86)\\WiX Toolset v3.8\\bin;C
:\\Program Files\\Oracle\\VirtualBox;C:\\opscode\\chef\\bin;C:\\opscode\\chef\\embedded\\bin"}>, #<WmiLite::Wmi::Instanc
e:0x257f238 @wmi_ole_object=#<WIN32OLE:0x257f268>, @property_map={"caption"=>"BTM-DESKTOP\\btm_000\\Path", "description"
=>"BTM-DESKTOP\\btm_000\\Path", "installdate"=>nil, "name"=>"Path", "status"=>"OK", "systemvariable"=>false, "username"=
>"BTM-DESKTOP\\btm_000", "variablevalue"=>"C:\\opscode\\chef\\bin;C:\\opscode\\chef\\embedded\\bin;C:\\Ruby193\\bin"}>]

And the problem is when we modify that and then save it to ENV['PATH'].

If I set something like $env:PATH = "$env:PATH;%SYSTEMROOT%\foo" in powershell, then %SYSTEMROOT% isn't evaluated, but I think that's user error. Setting $env:PATH = "$env:PATH;$env:SYSTEMROOT\foo" works fine.

So I think fixing mixlib-shellout#which is probably a separate bug, and we do need to make sure we don't save these unexpanded variables back to ENV['PATH'] but I'm feeling that we need to do it without hardcoding a list of potential variable names.

private

def expand_path(path)
system_vars = %w(HomeDrive HomePath ProgramFiles SystemDirectory SystemDrive SystemRoot Temp Tmp UserProfile WinDir)
Copy link
Contributor

Choose a reason for hiding this comment

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

We do not need to limit to these

@alex-slynko
Copy link
Contributor Author

@adamedx still broken in 11.16 :(

@sbotman
Copy link
Contributor

sbotman commented Sep 9, 2014

Indeed, just ran into this issue also. See #2016 for details.

@btm
Copy link
Contributor

btm commented Sep 9, 2014

The fix that @jdmundrawala is working on in the windows cookbook should be released this week which will work around the bug: chef-boneyard/windows#112

Fixing this will close #2009 as well. I'm leaving it open as an "issue" for now since this is an individual pull request.

@jaym
Copy link
Contributor

jaym commented Sep 10, 2014

@btm @adamedx I've updated this PR to use the windows api:
#2024

@btm
Copy link
Contributor

btm commented Oct 13, 2014

This was fixed by #2024. Anyone experiencing any environment issues should be sure they replicate on 11.16.4.

@btm btm closed this Oct 13, 2014
@chef chef locked and limited conversation to collaborators Nov 16, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants