In Chef, why are the exceptions raised not saved?

In the chef, why didn't this code catch an abandoned exception in the bash resource? If I run this code, I will never get to the python resource, even if it is an exception. An error is expected, so I will catch it, and then I will go to the pignongo and correct it in this way.

begin bash "mongo fix" do code "mongo --verbose #{filename}" action :run end rescue python 'pymongo reconfig' do code "Pymongo does a catch and reconfig" end end 

And this is an exception output

 [2013-03-05T20:03:55+00:00] FATAL: Mixlib::ShellOut::ShellCommandFailed: bash[mongo fix (noudata::mongo line 77) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '253' ---- Begin output of "bash" "/tmp/chef-script20130305-3916-14xxhn5-0" ---- STDOUT: MongoDB shell version: 2.2.3 Tue Mar 5 20:03:55 versionCmpTest passed Tue Mar 5 20:03:55 versionArrayTest passed connecting to: test Tue Mar 5 20:03:55 creating new connection to:127.0.0.1:27017 Tue Mar 5 20:03:55 BackgroundJob starting: ConnectBG Tue Mar 5 20:03:55 connected connection! true { "errmsg" : "replSetReconfig command must be sent to the current replica set primary.", "ok" : 0 } Tue Mar 5 20:03:55 uncaught exception: [object bson_object] failed to load: /tmp/test.js STDERR: ---- End output of "bash" "/tmp/chef-script20130305-3916-14xxhn5-0" ---- Ran "bash" "/tmp/chef-script20130305-3916-14xxhn5-0" returned 253 
+4
source share
2 answers

The code in the begin block creates an instance of the Chef::Resource::Bash resource, assigns the code and action resource attributes and adds the resource to run_context.resource_collection . A resource does not "work" in any sense. Creating a resource instance does not raise any exceptions, so the code in the rescue block does not run.

Later, once all of your recipe code is finished, Chef will run_context.resource_collection over resources in run_context.resource_collection . For each resource, the chef will trigger actions with resources. In the case of the bash resource, this means that the line specified in the code resource attribute is executed. Note that this happens after your recipe and all other recipes are finished, and this happens well outside of your begin block. Any exceptions caused by this resource when performing its actions do not apply to your begin block, because actions are not called from your begin block, but from it after it.

You can try something like this:

 bash "mongo fix" do code "mongo --verbose #{filename} || python pymongo reconfig" end 
+6
source

Put your code in the definition and derive the definition from the recipe.

0
source

All Articles