Class: Chef::Handler::Jenkins_Notifier
- Inherits:
-
Chef::Handler
- Object
- Chef::Handler
- Chef::Handler::Jenkins_Notifier
- Defined in:
- lib/chef/handler/jenkins_notifier.rb
Overview
noinspection RubyStringKeysInHashInspection
Instance Method Summary collapse
-
#initialize(config) ⇒ Jenkins_Notifier
constructor
A new instance of Jenkins_Notifier.
- #report ⇒ Object
- #submit_jenkins(run_status, log, result) ⇒ Object
Constructor Details
#initialize(config) ⇒ Jenkins_Notifier
Returns a new instance of Jenkins_Notifier.
10 11 12 13 14 15 |
# File 'lib/chef/handler/jenkins_notifier.rb', line 10 def initialize(config) @config = config raise ArgumentError, 'Jenkins Host is not specified' unless @config[:host] raise ArgumentError, 'Jenkins Port is not specified' unless @config[:port] raise ArgumentError, 'Jenkins Job Path is not specified' unless @config[:path] end |
Instance Method Details
#report ⇒ Object
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 |
# File 'lib/chef/handler/jenkins_notifier.rb', line 17 def report log = [] if not run_status.success? result = 1 log << "\n" log << "Chef run failed on #{run_status.node.name}" if !run_status.exception.nil? log << run_status.formatted_exception.encode('UTF-8', {:invalid => :replace, :undef => :replace, :replace => '?'}) end log << "\n" log = log.join("\n") submit_jenkins run_status,log,result else result = 0 log << "\n" log << "Chef run Success on #{run_status.node.name}" log << "environment: " + run_status.node.environment log << "start_time: " + run_status.start_time.rfc2822 log << "end_time: " + run_status.end_time.rfc2822 log << "elapsed_time: " + run_status.elapsed_time.to_s + "s" log << "\n" log = log.join("\n") submit_jenkins run_status,log,result end end |
#submit_jenkins(run_status, log, result) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/chef/handler/jenkins_notifier.rb', line 43 def submit_jenkins(run_status, log, result) binlog = log.unpack("H*").first ms = (run_status.elapsed_time * 1000).round data = "<run><log encoding='hexBinary'>#{binlog}</log><result>#{result}</result><duration>#{ms}</duration></run>" req = Net::HTTP::Post.new(@config[:path]) if @config[:user] && @config[:pass] req.basic_auth @config[:user], @config[:pass] end req.set_body_internal(data) res = Net::HTTP.new(@config[:host],@config[:port]).start {|http| http.request(req)} end |