Class: CycleChefHandler

Inherits:
Chef::Handler
  • Object
show all
Defined in:
lib/cycle_chef_handler.rb

Constant Summary collapse

VERSION =
'1.2.0'

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ CycleChefHandler

Returns a new instance of CycleChefHandler.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cycle_chef_handler.rb', line 31

def initialize(params)
  defaults = {:queue       => 'chef.converges',
              :exchange    => 'chef.converges'}

  @amqp_config = defaults.merge(params[:amqp_config])
  check_amqp_config

  @extras = params[:extras]
  @converge_index_file = params[:converge_index_file] || '/var/run/chef/converge_index'
  @failed_converge_file = params[:failed_converge_count_file] || '/var/run/chef/failed_converge_count'
end

Instance Method Details

#check_amqp_configObject



43
44
45
46
47
48
49
# File 'lib/cycle_chef_handler.rb', line 43

def check_amqp_config
  [:host, :queue, :exchange].each do |i|
    if not @amqp_config[i]
      raise ArgumentError, ":amqp_config missing value for #{i}"
    end
  end
end

#clear_count_file(count_file) ⇒ Object



161
162
163
164
165
# File 'lib/cycle_chef_handler.rb', line 161

def clear_count_file(count_file)
  if File.exist? count_file
    FileUtils.rm count_file
  end
end

#create_adObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cycle_chef_handler.rb', line 92

def create_ad
  ad = ClassAd.new
  ad['AdType']              = 'Chef.Host'
  ad['ChefNode']            = Chef::Config[:node_name]
  ad['ConvergeStartTime']   = start_time
  ad['ConvergeEndTime']     = end_time
  ad['ConvergeElapsedTime'] = RelativeTime.new(elapsed_time)

  updated = []
  if not updated_resources.nil?
    updated = updated_resources.map {|x| x.to_s}
  end

  ad['UpdatedResources']      = updated
  ad['UpdatedResourcesCount'] = updated.size
  ad['ConvergeIndex']         = increment_count_file(@converge_index_file)
  ad['ChefServerUrl']         = Chef::Config[:chef_server_url]
  ad['ChefServerHostName']    = URI.parse(Chef::Config[:chef_server_url]).host
  ad['ChefClientVersion']     = Chef::VERSION
  ad['CycleChefHandlerVersion'] = CycleChefHandler::VERSION
  ad['Success']               = success?

  exception = nil
  backtrace = nil
  if failed?
    exception = run_status.formatted_exception
    backtrace = run_status.backtrace
    ad['FailedConvergeCount'] = increment_count_file(@failed_converge_file)
  else
    clear_count_file(@failed_converge_file)
    ad['FailedConvergeCount'] = 0
  end

  ad['Exception']             = exception
  ad['Backtrace']             = backtrace

  @extras.each do |k,v|
    ad[k] = v
  end

  ad
end

#increment_count_file(count_file) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cycle_chef_handler.rb', line 135

def increment_count_file(count_file)
  file_dir = File.dirname(count_file)
  if not File.directory? file_dir
    FileUtils.mkdir_p file_dir
  end
 
  count = nil
  if File.exists? count_file
    File.open(count_file) do |file|
      count = file.readline.chomp.to_i
    end
  end

  if count.nil?
    count = 1
  else
    count += 1
  end

  File.open(count_file, "w") do |file|
    file.puts(count)
  end
    
  count
end

#reportObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cycle_chef_handler.rb', line 51

def report

  ## Create and Post a classad
  ad = create_ad
  payload = "<classads>" + ad.to_xml + "</classads>"
  
  begin

    b = Bunny.new(@amqp_config)

    b.start
    e = b.exchange(@amqp_config[:exchange], 
                   :type => :topic, 
                   :durable => true, 
                   :auto_delete => false)

    # declare and bind a non-auto-delete queue here to make sure we don't
    # lose any messages posted to an exchange w/o a bound queue
    # make the queue non-durable so we can drop it with a broker reboot
    q = b.queue(@amqp_config[:queue], :auto_delete => false)
    q.bind(@amqp_config[:exchange], :key => @amqp_config[:queue])

    e.publish(payload, :key => @amqp_config[:queue])

  rescue Exception => e

    # log any exceptions, but don't throw one.
    trace = e.backtrace.join("\n")
    Chef::Log.error("Failed to post converge history report: #{e.message} #{trace}")
    return

  ensure

    b.stop

  end

  Chef::Log.info("Posted converge history report")

end