diff options
author | Adam Edwards <adamed@opscode.com> | 2014-09-01 08:32:32 -0700 |
---|---|---|
committer | Jay Mundrawala <jdmundrawala@gmail.com> | 2014-09-19 12:48:02 -0700 |
commit | 86143fc12263f685fa6c829aa29cd46f3cf0684c (patch) | |
tree | 8a90d0b47d3d1341e85d6a27e2610f59ec79f74d /lib/chef | |
parent | b89d0bc184fed51236782d8ae001e36389713f73 (diff) | |
download | chef-86143fc12263f685fa6c829aa29cd46f3cf0684c.tar.gz |
Correctly handle dsc resource import failures
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/util/dsc/local_configuration_manager.rb | 52 | ||||
-rw-r--r-- | lib/chef/util/powershell/cmdlet_result.rb | 4 |
2 files changed, 20 insertions, 36 deletions
diff --git a/lib/chef/util/dsc/local_configuration_manager.rb b/lib/chef/util/dsc/local_configuration_manager.rb index d25f144095..e7e37718a7 100644 --- a/lib/chef/util/dsc/local_configuration_manager.rb +++ b/lib/chef/util/dsc/local_configuration_manager.rb @@ -27,24 +27,10 @@ class Chef::Util::DSC clear_execution_time end - LCM_MODULE_NOT_INSTALLED_ERROR_CODE = 0x80131500 - def test_configuration(configuration_document) status = run_configuration_cmdlet(configuration_document) - command_output = status.return_value - unless status.succeeded? - if status.exit_code == LCM_MODULE_NOT_INSTALLED_ERROR_CODE - Chef::Log::warn('Unable to test configuration because a required DSC PowerShell module may not be installed.') - command_output = '' - end - if status.stderr.gsub(/\s+/, ' ') =~ /A parameter cannot be found that matches parameter name 'Whatif'/ - # LCM returns an error if any of the resources do not support the opptional What-If - Chef::Log::warn("Received error while testing configuration due to resource not supporting 'WhatIf'") - else - raise Chef::Exceptions::PowershellCmdletException, "Powershell Cmdlet failed: #{status.stderr.gsub(/\s+/, ' ')}" - end - end - configuration_update_required?(command_output) + handle_what_if_exception!(status.stderr) unless status.succeeded? + configuration_update_required?(status.return_value) end def set_configuration(configuration_document) @@ -88,25 +74,27 @@ class Chef::Util::DSC def lcm_command_code(configuration_path, test_only_parameters) <<-EOH -try -{ - $ProgressPreference = 'SilentlyContinue';start-dscconfiguration -path #{@configuration_path} -wait -force #{test_only_parameters} -erroraction 'Stop' -} -catch [Microsoft.Management.Infrastructure.CimException] -{ - $exception = $_.Exception - write-error -Exception $exception - $StatusCode = 1 - if ( $exception.HResult -ne 0 ) - { - $StatusCode = $exception.HResult - } - $exception | format-table -property * -force - exit $StatusCode -} +$ProgressPreference = 'SilentlyContinue';start-dscconfiguration -path #{@configuration_path} -wait -force -erroraction 'stop' #{test_only_parameters} EOH end + def handle_what_if_exception!(what_if_exception_output) + if what_if_exception_output.gsub(/\s+/, ' ') =~ /A parameter cannot be found that matches parameter name 'Whatif'/i + # LCM returns an error if any of the resources do not support the opptional What-If + Chef::Log::warn("Received error while testing configuration due to resource not supporting 'WhatIf'") + elsif output_has_dsc_module_failure?(what_if_exception_output) + Chef::Log::warn('Received error while testing configuration due to a module for an imported resource possibly not being fully installed:\n#{what_if_exception_output.gsub(/\s+/, ' ')}') + else + raise Chef::Exceptions::PowershellCmdletException, "Powershell Cmdlet failed: #{what_if_exception_output.gsub(/\s+/, ' ')}" + end + end + + def output_has_dsc_module_failure?(what_if_output) + !! (what_if_output.match(/\sCimException/) && + what_if_output =~ /ProviderOperationExecutionFailure/ && + what_if_output =~ /\smodule is installed/) + end + def configuration_update_required?(what_if_output) Chef::Log.debug("DSC: DSC returned the following '-whatif' output from test operation:\n#{what_if_output}") begin diff --git a/lib/chef/util/powershell/cmdlet_result.rb b/lib/chef/util/powershell/cmdlet_result.rb index 0e9ccc3523..696013e684 100644 --- a/lib/chef/util/powershell/cmdlet_result.rb +++ b/lib/chef/util/powershell/cmdlet_result.rb @@ -39,10 +39,6 @@ class Chef::Util::Powershell end end - def exit_code - @status.status.exitstatus - end - def succeeded? @succeeded = @status.status.exitstatus == 0 end |