Module: Capistrano::Graphite

Defined in:
lib/graphite-notify/capistrano.rb

Class Method Summary collapse

Class Method Details

.load_into(configuration) ⇒ Object

Called when a Capistrano::Configuration.instance exists. Will define two tasks:

* graphite:notify_deploy notify graphite of an app deploy with the user in tags and a message
* graphite:notify_restart notify graphite of an app rollback with the user in tags and a message

Parameters:

  • configuration (Capistrano::Configuration)

    the current capistrano configuration instance



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/graphite-notify/capistrano.rb', line 12

def self.load_into(configuration)
  configuration.load do

    after 'deploy:restart', 'graphite:notify_deploy'
    after 'rollback:restart', 'graphite:notify_rollback'

    local_user = ENV['USER'] || ENV['USERNAME']

    namespace :graphite do
      desc 'notify graphite that a deployment occured'
      task :notify_deploy, :on_error => :continue do
        uri = URI(graphite_url)
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true if uri.scheme == 'https'
        begin
          http.start  do |h|
            if respond_to?(:stage)
              h.post(uri.path, { 'what' => "deploy #{application} in #{stage}",
                                 'tags' => "#{application},#{stage},#{real_revision},deploy",
                                 'data' => "#{local_user}" }.to_json)
            else
              h.post(uri.path, { 'what' => "deploy #{application}",
                                 'tags' => "#{application},#{real_revision},deploy",
                                 'data' => "#{local_user}" }.to_json)
            end
          end
        rescue => e
          puts "graphite:notify_deploy failed: #{e.message}"
        end
      end

      desc 'notify graphite that a rollback occured'
      task :notify_rollback, :on_error => :continue do
        uri = URI(graphite_url)
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true if uri.scheme == 'https'
        begin
          http.start do |h|
            if respond_to?(:stage)
              h.post(uri.path, { 'what' => "rollback #{application} in #{stage}",
                                 'tags' => "#{application},#{stage},#{real_revision},rollback",
                                 'data' => "#{local_user}" }.to_json)
            else
              h.post(uri.path, { 'what' => "rollback #{application}",
                                 'tags' => "#{application},#{real_revision},rollback",
                                 'data' => "#{local_user}" }.to_json)
            end
          end
        rescue => e
          puts "graphite:notify_rollback failed: #{e.message}"
        end
      end
    end
  end
end