From a5913518b5525a9142ea4e6fd4ef433be5272244 Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Mon, 8 Sep 2014 14:56:51 -0700 Subject: [PATCH 1/4] Expand environment variables in path --- libraries/windows_helper.rb | 9 +++++++++ providers/path.rb | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/libraries/windows_helper.rb b/libraries/windows_helper.rb index ca6a4bc7..9f9e284b 100644 --- a/libraries/windows_helper.rb +++ b/libraries/windows_helper.rb @@ -18,12 +18,14 @@ # limitations under the License. require 'uri' +require 'Win32API' if Chef::Platform.windows? module Windows module Helper AUTO_RUN_KEY = 'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run'.freeze unless defined?(AUTO_RUN_KEY) ENV_KEY = 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'.freeze unless defined?(ENV_KEY) + ExpandEnvironmentStrings = Win32API.new('kernel32', 'ExpandEnvironmentStrings', ['P', 'P', 'L'], 'L') if Chef::Platform.windows? # returns windows friendly version of the provided path, # ensures backslashes are used everywhere @@ -82,6 +84,13 @@ def cached_file(source, checksum=nil, windows_path=true) end end + # Expands the environment variables + def expand_env_vars(path) + buf = 0.chr * 32 * (1 << 10) # 32k + ExpandEnvironmentStrings.call(path, buf, buf.length) + buf.strip + end + end end diff --git a/providers/path.rb b/providers/path.rb index d297aec9..3de58267 100644 --- a/providers/path.rb +++ b/providers/path.rb @@ -19,11 +19,27 @@ # use_inline_resources if defined?(use_inline_resources) +include Windows::Helper + action :add do env "path" do action :modify delim ::File::PATH_SEPARATOR value new_resource.path + notifies :run, "ruby_block[fix ruby ENV['PATH']]" + end + + # The windows Env provider does not correctly expand variables in + # the PATH environment variable. Ruby expects these to be expanded. + # This is a temporary fix for that. + # + # Follow at https://github.com/opscode/chef/pull/1876 + # + ruby_block "fix ruby ENV['PATH']" do + block do + ENV['PATH'] = expand_env_vars(ENV['PATH']) + end + action :nothing end end From 32ff502b81ff6e9f041beb3c2c5df81bf97f1371 Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Wed, 17 Sep 2014 13:36:21 -0700 Subject: [PATCH 2/4] Throw an exception for errored return value when expanding env strings --- libraries/windows_helper.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libraries/windows_helper.rb b/libraries/windows_helper.rb index 9f9e284b..4b4fb349 100644 --- a/libraries/windows_helper.rb +++ b/libraries/windows_helper.rb @@ -19,6 +19,7 @@ require 'uri' require 'Win32API' if Chef::Platform.windows? +require 'chef/exceptions' module Windows module Helper @@ -87,7 +88,9 @@ def cached_file(source, checksum=nil, windows_path=true) # Expands the environment variables def expand_env_vars(path) buf = 0.chr * 32 * (1 << 10) # 32k - ExpandEnvironmentStrings.call(path, buf, buf.length) + if ExpandEnvironmentStrings.call(path, buf, buf.length) == 0 + raise Chef::Exceptions::Win32APIError, "Failed calling ExpandEnvironmentStrings (received 0)" + end buf.strip end From 77a3c4886531f2ee77cfa425abfa8e72aa96fc6d Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Wed, 17 Sep 2014 13:37:03 -0700 Subject: [PATCH 3/4] Fix notifies --- providers/path.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/path.rb b/providers/path.rb index 3de58267..88c49780 100644 --- a/providers/path.rb +++ b/providers/path.rb @@ -26,7 +26,7 @@ action :modify delim ::File::PATH_SEPARATOR value new_resource.path - notifies :run, "ruby_block[fix ruby ENV['PATH']]" + notifies :run, "ruby_block[fix ruby ENV['PATH']]", :immediately end # The windows Env provider does not correctly expand variables in From 9b5c07bf4b37c80c47ea6a5f0d821226306991f9 Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Mon, 22 Sep 2014 11:55:51 -0700 Subject: [PATCH 4/4] Added comment about 32k for path expansion --- libraries/windows_helper.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/windows_helper.rb b/libraries/windows_helper.rb index 4b4fb349..b36c4785 100644 --- a/libraries/windows_helper.rb +++ b/libraries/windows_helper.rb @@ -87,7 +87,9 @@ def cached_file(source, checksum=nil, windows_path=true) # Expands the environment variables def expand_env_vars(path) - buf = 0.chr * 32 * (1 << 10) # 32k + # We pick 32k because that is the largest it could be: + # http://msdn.microsoft.com/en-us/library/windows/desktop/ms724265%28v=vs.85%29.aspx + buf = 0.chr * 32 * 1024 # 32k if ExpandEnvironmentStrings.call(path, buf, buf.length) == 0 raise Chef::Exceptions::Win32APIError, "Failed calling ExpandEnvironmentStrings (received 0)" end