Last active
August 29, 2015 13:57
-
-
Save arangamani/9490082 to your computer and use it in GitHub Desktop.
Chef Handler Example Exception Handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# attributes | |
default['cron']['service_name'] = 'cron' | |
# recipe | |
include_recipe 'chef_handler' | |
cookbook_file "#{node['chef_handler']['handler_path']}/blah.rb" do | |
source 'blah.rb' | |
action :create | |
end | |
chef_handler 'Rightscale::BlahHandler' do | |
source "#{node['chef_handler']['handler_path']}/blah.rb" | |
action :enable | |
end | |
service node['cron']['service_name'] do | |
action :stop | |
end | |
# The failing resource | |
execute 'blah' | |
service node['cron']['service_name'] do | |
action :start | |
end | |
# The handler ruby code (files/default/blah.rb) | |
module Rightscale | |
class BlahHandler < Chef::Handler | |
def report | |
# Node can be accessed by `run_context.node` | |
cron_service_name = run_context.node['cron']['service_name'] | |
# Find the resource from the resource collection | |
cron_resource = run_context.resource_collection.lookup("service[#{cron_service_name}]") | |
# Run the :start action of the resource found. | |
cron_resource.run_action(:start) if cron_resource | |
Chef::Log.error 'Cron service started.' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool!