Exception handling for the Execute Resource


#1

Greetings!

I’ve got a situation where I’m iterating through a list and performing
operations on them. One of these operations looks like this:

execute "Format data device for #{osd_id} using XFS" do
  command "mkfs.xfs -f #{mkfs_opts} -L osd.#{osd_device['osd_id']}

#{osd_device[‘data_dev’]}"
end

What I would like to do is instead of aborting the entire chef run
because this failed, and instead mark it failed and move on to the next
one. What is the best practice for catching an exception from an
execute resource and taking action instead of aborting?

Thanks!

-Carl


#2

you can run execute resource right here and catch the exception as usual

begin

  • execute “foo” do*
  • action :nothing*
    
  • end.run_action(:run)*
    rescue Mixlib::Shellout::ShelloutCommandFailed => e
  • puts “Exception is here #{e}”*
    end

or you can add other then “0” status codes to the “returns” option

execute “foo” do

  • returns [0, 255, 1, 2, 3]*
    end

Thank you,
Max

On Thu, Apr 19, 2012 at 3:17 PM, Carl Perry carl.perry@dreamhost.comwrote:

Greetings!

I’ve got a situation where I’m iterating through a list and performing
operations on them. One of these operations looks like this:

execute “Format data device for #{osd_id} using XFS” do
command "mkfs.xfs -f #{mkfs_opts} -L osd.#{osd_device[‘osd_id’]}
#{osd_device[‘data_dev’]}"
end

What I would like to do is instead of aborting the entire chef run
because this failed, and instead mark it failed and move on to the next
one. What is the best practice for catching an exception from an
execute resource and taking action instead of aborting?

Thanks!

-Carl