Class: HipChat::NotifyRoom

Inherits:
Chef::Handler
  • Object
show all
Defined in:
lib/hipchat-chef/chef.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_token, room_name, notify_users = false, report_success = false, excluded_envs = [], override_colors = {}) ⇒ NotifyRoom

Returns a new instance of NotifyRoom.



18
19
20
21
22
23
24
25
# File 'lib/hipchat-chef/chef.rb', line 18

def initialize(api_token, room_name, notify_users=false, report_success=false, excluded_envs=[], override_colors={})
  @api_token = api_token
  @room_name = room_name
  @notify_users = notify_users
  @report_success = report_success
  @excluded_envs = excluded_envs
  @override_colors = override_colors
end

Instance Method Details

#reportObject



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
# File 'lib/hipchat-chef/chef.rb', line 27

def report
  unless @excluded_envs.include?(node.chef_environment)
    msg = if run_status.failed? then "Failure on \"<b>#{node.name}</b>\" (<b>#{node.chef_environment}</b>, <b>#{node['ipaddress']}</b>):\n#{run_status.formatted_exception}"
          elsif run_status.success? && @report_success
            "Chef run on \"#{node.name}\" completed in #{run_status.elapsed_time.round(2)} seconds"
          else nil
          end

    @override_colors.default_proc = proc do |h, k|
      case k
        when String then sym = k.to_sym; h[sym] if h.key?(sym)
        when Symbol then str = k.to_s; h[str] if h.key?(str)
      end
    end

    color = if run_status.success?
              @override_colors[:success].to_s || 'green'
            else
              @override_colors[:failure].to_s || 'red'
            end

    if msg
      client = HipChat::Client.new(@api_token)
      client[@room_name].send('Chef', msg, :notify => @notify_users, :color => color)
    end
  end
end