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

Constructor Details

#initialize(config = {}) ⇒ JsonFile

Returns a new instance of JsonFile.



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

def initialize(config={})
  @config = config
  @config[:path] ||= "/var/chef/reports"
  @config
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



48
49
50
51
52
53
# File 'lib/chef/handler/json_file.rb', line 48

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

#reportObject



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

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|
    file.puts Chef::JSONCompat.to_json_pretty(data)
  end
end