class Chef::Handler::LrcReportFull

Class for Lrc report

Attributes

config[R]

Public Class Methods

new(config = {}) click to toggle source

Initalizing Variables

# File lib/lrc_report_full.rb, line 18
def initialize(config = {})
  @config = config
  @config[:url] ||= 'https://exemple.com'
  @config[:upload] ||= true
  @config[:ssl_verify] ||= true
  @config[:pathSave] ||= "/var/chef/reports"
  @config[:drop_file] ||= true
  @ssl_verify = @config[:ssl_verify] ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
end

Public Instance Methods

build_report_dir() click to toggle source
# File lib/lrc_report_full.rb, line 76
def build_report_dir
  unless File.exist?(config[:pathSave])
    FileUtils.mkdir_p(config[:pathSave])
    File.chmod(00700, config[:pathSave])
  end
end
lrc_request(path, body, method = 'post') click to toggle source
# File lib/lrc_report_full.rb, line 60
def lrc_request(path, body, method = 'post')
  uri = URI.parse(config[:url])
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = @ssl_verify
  req = Net::HTTP.const_get(method.capitalize).new("#{uri}#{path}")
  req.add_field('Accept', 'application/json')
  req.add_field('Content-Type', 'application/json')
  req.add_field('User-Agent', "LRC_handler_report v1.0, NODE:#{body['report']['host']}, chef-client v#{Chef::VERSION}")
  req.body = body
  response = http.request(req)
  Chef::Log.info("The report API has return: #{response.inspect} from #{uri}#{path}")
  Chef::Log.debug("Report Content: #{body}")
  return response.kind_of? Net::HTTPSuccess
end
report() click to toggle source

Building the report

# File lib/lrc_report_full.rb, line 29
def report
  # Report Array in json
  if exception
    Chef::Log.error("Creating JSON exception report")
  else
    Chef::Log.info("Creating JSON run report")
  end
  result = false       
  build_report_dir
  savetime = Time.now.strftime("%Y%m%d%H%M%S")
  File.open(File.join(config[:pathSave], "chef-run-report-#{savetime}.json"), "w") do |file|

    # ensure start time and end time are output in the json properly in the event activesupport happens to be on the system
    run_data = data
    run_data[:start_time] = run_data[:start_time].to_s
    run_data[:end_time] = run_data[:end_time].to_s
    encoder = FFI_Yajl::Encoder.new( :validate_utf8 => false )
    report = encoder.encode( run_data )
    #file.puts Chef::JSONCompat.to_json_pretty(run_data)
    #report = Chef::JSONCompat.to_json_pretty(run_data)
    file.puts report
    report = report.gsub('""', '"NULL"')
    if config[:upload]
      result = lrc_request('/api/v2/report/publish', report)
    end
  end
      if result
         File.delete(config[:pathSave] + "/chef-run-report-#{savetime}.json")
      end
end