Skip to content

Instantly share code, notes, and snippets.

@juanje
Created January 3, 2013 20:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juanje/4446963 to your computer and use it in GitHub Desktop.
Save juanje/4446963 to your computer and use it in GitHub Desktop.
Example of Chef hanfler for sending run fails to Pushover (notifications to iPhone and Android) https://pushover.net/
# cookbook/files/default/pushover_handler.rb
require "net/https"
module MyOrg
class PushOver < Chef::Handler
def initialize(config={})
@config = config
end
def report
return unless run_status.failed?
return if @config.empty?
# The Node is available as +node+
title = "Failed node #{node.name}"
# +run_status+ is a value object with all of the run status data
message = run_status.formatted_exception
url = URI.parse("https://api.pushover.net/1/messages.json")
req = Net::HTTP::Post.new(url.path)
req.set_form_data({
:token => @config[:app_token],
:user => @config[:user_key],
:title => title,
:message => message,
})
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
# It should be OpenSSL::SSL::VERIFY_PEER, but it wasn't working
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
https.start {|http| http.request(req) }
Chef::Log.info("Myorg::PushOver sending Pushover notification")
end
end
end
# cookbook/recipes/reports.rb
pushover_report_path = "#{node['chef_handler']['handler_path']}/pushover_handler.rb"
cookbook_file pushover_report_path
include_recipe "chef_handler::default"
chef_handler 'MyOrg::PushOver' do
source pushover_report_path
arguments :app_token => node['myorg']['pushover']['app_token'],
:user_key => node['myorg']['pushover']['user_key']
action :enable
end
{
"run_list": ["recipe[myorg::reports]", "recipe[myorg::foo]"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment