Class: DumblogChef::Handler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group = '/chef', stream = nil, region = nil) ⇒ Handler

Returns a new instance of Handler.



8
9
10
11
12
13
14
15
# File 'lib/dumblog-chef/handler.rb', line 8

def initialize(group='/chef', stream=nil, region=nil)
  @time          = Time.now.utc
  @instance_id   = `wget -qO- http://169.254.169.254/latest/meta-data/instance-id`

  @group = group
  @stream = [nil, ''].include?(stream) ? "#{time.strftime("%Y/%m/%d")}/#{instance_id}" : stream
  @region = [nil, ''].include?(region) ? 'us-west-2' : region
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



6
7
8
# File 'lib/dumblog-chef/handler.rb', line 6

def group
  @group
end

#instance_idObject (readonly)

Returns the value of attribute instance_id.



5
6
7
# File 'lib/dumblog-chef/handler.rb', line 5

def instance_id
  @instance_id
end

#regionObject (readonly)

Returns the value of attribute region.



6
7
8
# File 'lib/dumblog-chef/handler.rb', line 6

def region
  @region
end

#streamObject (readonly)

Returns the value of attribute stream.



6
7
8
# File 'lib/dumblog-chef/handler.rb', line 6

def stream
  @stream
end

#timeObject (readonly)

Returns the value of attribute time.



5
6
7
# File 'lib/dumblog-chef/handler.rb', line 5

def time
  @time
end

Instance Method Details

#reportObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dumblog-chef/handler.rb', line 17

def report
  if run_status.failed?
    message = {}
    message[:subject]     = "chef-client Run Failed"
    message[:time]        = time.iso8601
    message[:node]        = "Chef run failed on #{node.name}"
    message[:instance_id] = instance_id
    message[:exception]   = run_status.formatted_exception
    message[:backtrace]   = Array(backtrace).join('\n')

    file = Tempfile.new('dumblog-chef')
    begin
      file.write message.to_json
      file.flush
      `for i in {1..5}; do dumblog -group #{group} -stream #{stream} -region #{region} $(cat #{file.path}) && break || sleep 15; done`
    ensure
      file.close
      file.unlink
    end
  end
end