From bb554694302cf7ef607766f9521335f3345b50c3 Mon Sep 17 00:00:00 2001 From: Nathan L Smith Date: Mon, 28 Jan 2013 16:45:43 -0600 Subject: [PATCH] Add newrelic --- README.md | 19 +++++++ lib/capistrano/notifier/new_relic.rb | 64 ++++++++++++++++++++++ spec/capistrano/notifier/new_relic_spec.rb | 30 ++++++++++ 3 files changed, 113 insertions(+) create mode 100644 lib/capistrano/notifier/new_relic.rb create mode 100644 spec/capistrano/notifier/new_relic_spec.rb diff --git a/README.md b/README.md index f37aeb2..47c1948 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,25 @@ The following is a list of some popular variables that don't require the use of - `stage`: Name of the stage from the capistrano multistage extension. - `user_name`: Name of the local git author. +## New Relic + +This notifier can be useful if you're using [New Relic](https://newrelic.com/) but not [using the Ruby agent](https://newrelic.com/docs/ruby/recording-deployments-with-the-ruby-agent) (so you don't have the rpm_newrelic gem installed or a config/newrelic.yml file.) + +```rb +require 'capistrano/notifier/new_relic' + +set :notifier_new_relic_options, { + :api_key => 'fffffffffffffffffffffffffffffffffffffffffffffff', + :application_id => 1234567 +} +``` + +Both `:api_key` and `:application_id` must be present. If you set a `NOTES` environment variable: + + cap deploy NOTES="fix everything" + +The notes will be added as the deployment description. + ## StatsD ```rb diff --git a/lib/capistrano/notifier/new_relic.rb b/lib/capistrano/notifier/new_relic.rb new file mode 100644 index 0000000..51ec710 --- /dev/null +++ b/lib/capistrano/notifier/new_relic.rb @@ -0,0 +1,64 @@ +require 'capistrano/notifier' +require 'net/http' +require 'uri' + +class Capistrano::Notifier::NewRelic < Capistrano::Notifier::Base + DEFAULTS = {} + + def self.load_into(configuration) + configuration.load do + namespace :deploy do + namespace :notify do + desc 'Notify New Relic of deploy.' + task :new_relic do + run Capistrano::Notifier::NewRelic.new(configuration).command + end + end + end + + after 'deploy', 'deploy:notify:new_relic' + end + end + + def command + post = Net::HTTP::Post.new(uri.request_uri) + post.add_field 'x-api-key', options[:api_key] + post.set_form_data form_data + response_text = "" + Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http| + response = http.request(post) + response_text = "#{response.code} #{response.message}" + end + "echo New Relic Deploy notification result: #{response_text}" + end + + private + + def options + if cap.respond_to? :notifier_new_relic_options + cap.notifier_new_relic_options.reverse_merge DEFAULTS + else + DEFAULTS + end + end + + # Form data to be posted + def form_data + @form_data ||= { + 'deployment[app_name]' => options[:app_name], + 'deployment[application_id]' => options[:application_id], + 'deployment[description]' => ENV['NOTES'], + 'deployment[revision]' => git_current_revision, + 'deployment[changelog]' => git_log, + 'deployment[user]' => user_name + } + end + + def uri + @uri ||= URI('https://rpm.newrelic.com/deployments.xml') + end +end + +if Capistrano::Configuration.instance + Capistrano::Notifier::NewRelic.load_into(Capistrano::Configuration.instance) +end diff --git a/spec/capistrano/notifier/new_relic_spec.rb b/spec/capistrano/notifier/new_relic_spec.rb new file mode 100644 index 0000000..970d7aa --- /dev/null +++ b/spec/capistrano/notifier/new_relic_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' +require 'capistrano/notifier/new_relic' + +describe Capistrano::Notifier::NewRelic do + let(:configuration) { Capistrano::Configuration.new } + let(:post) { mock(:post, :add_field => nil, + :set_body_internal => nil, + :response_body_permitted? => nil, + :[] => nil, + :[]= => nil, + :path => nil, + :exec => nil, + :set_form_data => nil) } + subject { described_class.new configuration } + + before :each do + configuration.load do + set :application, 'example' + set :notifier_new_relic_options, { + :api_key => 'ffffff', + :application_id => 12345 + } + end + Net::HTTP::Post.stub(:new).and_return(post) + end + + describe '#command' do + it 'posts to the newrelic api' + end +end