Skip to content

Instantly share code, notes, and snippets.

@nolan
Created January 26, 2011 19:17
Show Gist options
  • Save nolan/797240 to your computer and use it in GitHub Desktop.
Save nolan/797240 to your computer and use it in GitHub Desktop.
Capistrano Notifo notification recipe w/ growlnotify fallback
# ~/.recipes/growl.rb
#
# Notifo setup in deploy scripts:
# set :notifo_service "NOTIFO_SERVICE_NAME" # Only if different from :application
# set :notifo_service_secret "NOTIFO_API_SECRET" # Only if different from application name
# set :notifo_subscriber_list "username1, username2" # Define default below to your username
# set :notifo_hostname 'example.com' # Defaults to :server_hostname. Set to empty string to use Notifo default
require 'notifo' rescue nil
namespace :notifo do
task :notify do
notifo_send(ENV['NOTIFO_MESSAGE'])
end
end
after :deploy do
ENV["NOTIFO_MESSAGE"] = "Successfully deployed to #{fetch(:notifo_hostname, fetch(:server_hostname))}"
notifo.notify
end
after :rollback do
ENV["NOTIFO_MESSAGE"] = "Rolled back #{fetch(:notifo_hostname, fetch(:server_hostname))}"
notifo.notify
end
def notifo_send(message)
# TODO Combine with params
notifo_service = fetch(:notifo_service, fetch(:application))
notifo_service_secret = fetch(:notifo_service_secret)
notifo_hostname = fetch(:notifo_hostname, fetch(:server_hostname))
notifo_subscriber_list = fetch(:notifo_subscriber_list)
begin
if notifo_service_secret && notifo_subscriber_list
params = {
:username => notifo_service,
:secret => notifo_service_secret,
:title => "Deploy",
:message => message,
:uri => notifo_hostname ? "http://#{notifo_hostname}" : nil
}
notifo = Notifo.new(params)
notifo_subscriber_list.split(',').each do |username|
# Ensure user is subscribed
# TODO Decide if this should happen every time?
puts "Notifo - Add Subscriber #{username.inspect} # => " + notifo.subscribe_user(username.strip).parsed_response['response_message']
# TODO only send if subscribed
puts "Notifo - Send Notification # => " + notifo.send_notification(params.merge!(:to => username.strip)).parsed_response['response_message']
end
return
end
rescue
puts 'Notifo gem is not installed. Please run "gem install notifo". Falling back to growlnotify'
else
puts 'Something went wrong with Notifo. Please see above.'
end
`growlnotify -n Capistrano -m #{message.inspect} Capistrano`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment