From e473325de679718ab0695d4d3177eeeef20dc994 Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Thu, 4 Sep 2014 12:05:25 -0700 Subject: DscScript resource will raise an error if dsc is not available --- lib/chef/exceptions.rb | 2 ++ lib/chef/platform/query_helpers.rb | 6 +++++- lib/chef/resource/dsc_script.rb | 19 ++++++++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb index ea053cae64..7298f1f4d1 100644 --- a/lib/chef/exceptions.rb +++ b/lib/chef/exceptions.rb @@ -181,6 +181,8 @@ class Chef class ChildConvergeError < RuntimeError; end + class NoProviderAvailable < RuntimeError; end + class MissingRole < RuntimeError NULL = Object.new diff --git a/lib/chef/platform/query_helpers.rb b/lib/chef/platform/query_helpers.rb index f6f5309de5..334ab278d1 100644 --- a/lib/chef/platform/query_helpers.rb +++ b/lib/chef/platform/query_helpers.rb @@ -45,7 +45,11 @@ class Chef is_server_2003 end - end + def supports_dsc?(node) + node[:languages] && node[:languages][:powershell] && + node[:languages][:powershell][:version].to_i >= 4 + end + end end end diff --git a/lib/chef/resource/dsc_script.rb b/lib/chef/resource/dsc_script.rb index 37546c3b6a..2972ace1aa 100644 --- a/lib/chef/resource/dsc_script.rb +++ b/lib/chef/resource/dsc_script.rb @@ -16,6 +16,8 @@ # limitations under the License. # +require 'chef/exceptions' + class Chef class Resource class DscScript < Chef::Resource @@ -26,7 +28,12 @@ class Chef super @allowed_actions.push(:run) @action = :run - provider(Chef::Provider::DscScript) + if(run_context && Chef::Platform.supports_dsc?(run_context.node)) + @provider = Chef::Provider::DscScript + else + raise Chef::Exceptions::NoProviderAvailable, + "#{powershell_info_str(run_context)}\nPowershell 4.0 or higher was not detected on your system and is required to use the dsc_script resource." + end end def code(arg=nil) @@ -118,6 +125,16 @@ class Chef :kind_of => [ Integer ] ) end + + private + + def powershell_info_str(run_context) + if run_context && run_context.node[:languages] && run_context.node[:languages][:powershell] + install_info = "Powershell #{run_context.node[:languages][:powershell][:version]} was found on the system." + else + install_info = 'Powershell was not found.' + end + end end end end -- cgit v1.2.1