Class: Chef::Handler::JsonFile

Inherits:
Chef::Handler show all
Defined in:
lib/chef/handler/json_file.rb

Instance Attribute Summary collapse

Attributes inherited from Chef::Handler

#run_status

Instance Method Summary collapse

Methods inherited from Chef::Handler

#data, exception_handlers, report_handlers, run_exception_handlers, run_report_handlers, #run_report_safely, #run_report_unsafe, run_start_handlers, start_handlers

Constructor Details

#initialize(config = {}) ⇒ JsonFile

Returns a new instance of JsonFile.



29
30
31
32
33
# File 'lib/chef/handler/json_file.rb', line 29

def initialize(config={})
  @config = config
  @config[:path] ||= Chef::Config.platform_specific_path("/var/chef/reports")
  @config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



27
28
29
# File 'lib/chef/handler/json_file.rb', line 27

def config
  @config
end

Instance Method Details

#build_report_dirObject



55
56
57
58
59
60
# File 'lib/chef/handler/json_file.rb', line 55

def build_report_dir
  unless File.exists?(config[:path])
    FileUtils.mkdir_p(config[:path])
    File.chmod(00700, config[:path])
  end
end

#reportObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chef/handler/json_file.rb', line 35

def report
  if exception
    Chef::Log.error("Creating JSON exception report")
  else
    Chef::Log.info("Creating JSON run report")
  end

  build_report_dir
  savetime = Time.now.strftime("%Y%m%d%H%M%S")
  File.open(File.join(config[:path], "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          

    file.puts Chef::JSONCompat.to_json_pretty(run_data)
  end
end