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

#action_collection, #all_resources, #data, exception_handlers, #failed_resources, handler_for, report_handlers, resolve_handler_instance, run_exception_handlers, run_report_handlers, #run_report_safely, #run_report_unsafe, run_start_handlers, #skipped_resources, start_handlers, #unprocessed_resources, #up_to_date_resources, #updated_resources

Constructor Details

#initialize(config = {}) ⇒ JsonFile

Returns a new instance of JsonFile.



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

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

#build_report_dirObject



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

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

#reportObject



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

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