Class: Chef::Handler::Jenkins

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

Overview

noinspection RubyStringKeysInHashInspection

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Jenkins

Returns a new instance of Jenkins.

Raises:

  • (ArgumentError)


8
9
10
11
# File 'lib/chef/handler/jenkins.rb', line 8

def initialize(config)
  @config = config
  raise ArgumentError, 'Jenkins URL is not specified' unless @config[:url]
end

Instance Method Details

#reportObject



13
14
15
16
17
18
19
20
21
22
23
24
25
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
51
52
53
54
55
56
57
58
59
60
# File 'lib/chef/handler/jenkins.rb', line 13

def report
  # for interactive exploration
  #require 'pry'
  #binding.pry

  updates = []

  puts "Machine name: #{run_status.node.name}"
  run_status.updated_resources.each do |res|
    # res is instance of CookbookFile
    if res.class <= Chef::Resource::File
      updates << {
        "path" => res.path,
        "action" => res.action,
        "md5" => Digest::MD5.hexdigest(IO.read(res.path)),
        "type" => res.class.name
      }
      # res.checksum is SHA1 sum
    end

#          if res.class == Chef::Resource::SaladJenkinsTracking
#            # TODO is this a good way to check the class name?
#            updates << {
#                "path" => res.path,
#                "md5" => res.checksum,
#                "type" => res.class.name
#            }
#          end
  end

  # add envelop to the data
  env = {
    "node" => run_status.node.name,
    "environment" => run_status.node.environment,
    "start_time" => run_status.start_time.rfc2822,
    "end_time" => run_status.end_time.rfc2822,
    "updates" => updates
  }

  print env.inspect

  if false  # TODO: work in progress
  # if !Chef::Config[:solo]
    # databag submission only works in chef-client
    submit_databag run_status,env
  end
  submit_jenkins run_status,env
end

#submit_databag(run_status, env) ⇒ Object

Submit the tracking report as a databag

Parameters:

  • run_status (Chef::RunStatus)
  • report (Hash)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chef/handler/jenkins.rb', line 66

def submit_databag(run_status, env)
  # TODO: too expensive to load them. all we want to do is to check if databag exists
  #begin
  #  Chef::DataBag.load("jenkins")
  #rescue Net::HTTPServerException => e
  #  if e.response.code=="404"
  #    bag = Chef::DataBag.new
  #    bag.name "jenkins"
  #    bag.save
  #  end
  #end

  i = Chef::DataBagItem.new
  i.data_bag("jenkins")  # set the name

  id = run_status.node.name + '_' + run_status.end_time.strftime("%Y%m%d-%H%M%S")

  i.raw_data = env
  i.save id
end

#submit_jenkins(run_status, env) ⇒ Object



87
88
89
90
# File 'lib/chef/handler/jenkins.rb', line 87

def submit_jenkins(run_status, env)
  r = Chef::REST.new(@config[:url])
  r.post("chef/report", env)
end