summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornimisha <nimisha.sharad@clogeny.com>2017-07-27 15:36:31 +0530
committernimisha <nimisha.sharad@clogeny.com>2017-08-16 14:48:48 +0530
commitb8c447a3f7e821773dfec4bc443e344c05d906a0 (patch)
tree5f9b7bd0b28813943e9ed2fa7b86942bf7568a7c
parent515e2a4b04521a1a9f49b9681680507e410101b0 (diff)
downloadchef-b8c447a3f7e821773dfec4bc443e344c05d906a0.tar.gz
Added windows_path resource
Signed-off-by: nimisha <nimisha.sharad@clogeny.com>
-rw-r--r--lib/chef/provider/windows_path.rb62
-rw-r--r--lib/chef/providers.rb1
-rw-r--r--lib/chef/resource/windows_path.rb41
-rw-r--r--lib/chef/resources.rb1
4 files changed, 105 insertions, 0 deletions
diff --git a/lib/chef/provider/windows_path.rb b/lib/chef/provider/windows_path.rb
new file mode 100644
index 0000000000..d52baa2e7a
--- /dev/null
+++ b/lib/chef/provider/windows_path.rb
@@ -0,0 +1,62 @@
+#
+# Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.com>)
+# Copyright:: Copyright 2008-2016, Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require "Win32API" if Chef::Platform.windows?
+require "chef/exceptions"
+
+class Chef
+ class Provider
+ class WindowsPath < Chef::Provider
+ ExpandEnvironmentStrings = Win32API.new("kernel32", "ExpandEnvironmentStrings", %w{ P P L }, "L") if Chef::Platform.windows? && !defined?(ExpandEnvironmentStrings)
+
+ def load_current_resource
+ @current_resource = Chef::Resource::WindowsPath.new(new_resource.name)
+ @current_resource.path(new_resource.path)
+ @current_resource
+ end
+
+ def action_add
+ declare_resource(:env, "path") do
+ action :modify
+ delim ::File::PATH_SEPARATOR
+ value new_resource.path.tr("/", '\\')
+ end
+ ENV["PATH"] = expand_env_vars(ENV["PATH"])
+ end
+
+ def action_remove
+ declare_resource(:env, "path") do
+ action :delete
+ delim ::File::PATH_SEPARATOR
+ value new_resource.path.tr("/", '\\')
+ end
+ end
+
+ # Expands the environment variables
+ def expand_env_vars(path)
+ # 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 Chef::Provider::WindowsPath::ExpandEnvironmentStrings.call(path.dup, buf, buf.length) == 0
+ raise Chef::Exceptions::Win32APIError, "Failed calling ExpandEnvironmentStrings (received 0)"
+ end
+ buf.strip
+ end
+ end
+ end
+end
diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb
index e5b39c4e3c..a3332477e7 100644
--- a/lib/chef/providers.rb
+++ b/lib/chef/providers.rb
@@ -61,6 +61,7 @@ require "chef/provider/whyrun_safe_ruby_block"
require "chef/provider/yum_repository"
require "chef/provider/windows_task"
require "chef/provider/zypper_repository"
+require "chef/provider/windows_path"
require "chef/provider/env/windows"
diff --git a/lib/chef/resource/windows_path.rb b/lib/chef/resource/windows_path.rb
new file mode 100644
index 0000000000..fb47ec6791
--- /dev/null
+++ b/lib/chef/resource/windows_path.rb
@@ -0,0 +1,41 @@
+#
+# Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.com>)
+# Copyright:: Copyright 2008-2017, Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require "chef/resource"
+
+class Chef
+ class Resource
+ class WindowsPath < Chef::Resource
+
+ provides :windows_path, os: "windows"
+
+ allowed_actions :add, :remove
+ default_action :add
+
+ def initialize(name, run_context = nil)
+ super
+ @resource_name = :windows_path
+ @task_name = name
+ @provider = Chef::Provider::WindowsPath
+ @action = :add
+ end
+
+ property :path, String, name_property: true
+ end
+ end
+end
diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb
index a1f4f8ac0b..54d21fd53c 100644
--- a/lib/chef/resources.rb
+++ b/lib/chef/resources.rb
@@ -101,3 +101,4 @@ require "chef/resource/cab_package"
require "chef/resource/powershell_package"
require "chef/resource/msu_package"
require "chef/resource/windows_task"
+require "chef/resource/windows_path"