Class: Chef::Handler::S3

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

Constant Summary collapse

VERSION =
'0.1.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ S3

Returns a new instance of S3.



20
21
22
23
24
# File 'lib/chef/handler/s3.rb', line 20

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/chef/handler/s3.rb', line 18

def config
  @config
end

Instance Method Details

#build_report_dirObject



109
110
111
112
113
114
# File 'lib/chef/handler/s3.rb', line 109

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

#reportObject



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/chef/handler/s3.rb', line 52

def report
  run_data = Hash.new
  if run_status.failed?
    Chef::Log.error("Creating JSON exception report")
    run_data[:exception] = run_status.formatted_exception.split("\n")
    run_data[:backtrace] = run_status.backtrace
  else
    Chef::Log.info("Creating JSON run report")
  end

  build_report_dir

  if node.include? :cloud
    run_data[:ipaddress] = node.cloud.public_ipv4
  else
    run_data[:ipaddress] = node.ipaddress
  end
  run_data[:name] = node.name
  run_data[:fqdn] = node.fqdn
  run_data[:success] = run_status.success?
  run_data[:start_time] = run_status.start_time
  run_data[:end_time] = run_status.end_time
  run_data[:duration] = run_status.elapsed_time
  run_data[:updated_resources] = []
  run_status.updated_resources.each do |resource|
    run_data[:updated_resources] << {
      :name     => resource.name,
      :kind     => resource.resource_name,
      :provider => resource.provider,
      :action   => resource.action
    }
  end

  # This need to be used carefully. The values of the passed has will be evaluated
  # It is mean to be used at the resource as following example:
  # chef_handler "Chef::Handler::S3" do
  #   source 'chef/handler/s3'
  #   arguments :extra_data => {
  #     'iptables' => %{`iptables -L -n`.split("\n")},
  #     'route'    => %{`route -n`.split("\n")}
  #     }
  # end
  if @config.include? :extra_data and @config[:extra_data].is_a? Hash
    @config[:extra_data].each do |k,v|
      run_data[k] = eval v
    end
  end

  savetime = Time.now.strftime("%Y%m%d%H%M%S")
  file_path = File.join(config[:path], "#{node.name}-#{savetime}.json")
  File.open(file_path, "w") do |file|
    file.puts Chef::JSONCompat.to_json_pretty(run_data)
  end

  upload_file(file_path)
end

#upload_file(file_path) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chef/handler/s3.rb', line 26

def upload_file(file_path)
  bucket_name = config[:bucket_name]
  file_name = File.basename(file_path)

  AWS.config(
    :access_key_id => config[:access_key_id],
    :secret_access_key => config[:secret_access_key]
  )

  s3 = AWS::S3.new
  if s3.buckets[bucket_name].exists?
    bucket = s3.buckets[bucket_name]
  else
    bucket = s3.buckets.create(bucket_name)
  end

  if config.include? :folder
    key = "#{config[:folder]}/#{file_name}"
  else
    key = file_name
  end

  bucket.objects[key].write(:file => file_path)
  Chef::Log.info("Uploading file #{file_name} to bucket #{bucket_name}.")
end