Advertisement
Guest User

Untitled

a guest
Apr 15th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. #!/usr/local/bin/ruby
  2.  
  3. # Ruby script to download metrics from Jenkins
  4. # Jason Fowler March 2015
  5.  
  6. # cron for job:
  7. #*/30 * * * * /usr/local/bin/ruby /home/ubuntu/buildmetrics/buildmetrics.rb
  8.  
  9. require 'json'
  10. require 'jenkins_api_client'
  11. require 'open-uri'
  12. require 'erb'
  13. require 'mysql'
  14. require 'logger'
  15.  
  16. $LOG = Logger.new('bmapp.log', 0, 100 * 1024 * 1024)
  17.  
  18. #$LOG.level = Logger::ERROR
  19. $LOG.level = Logger::DEBUG
  20.  
  21. # Function Section
  22.  
  23. def get_builds(dbjob)
  24. gb = @client.job.get_builds(dbjob)
  25. gb.each do |build|
  26. buildnumber = build['number']
  27. get_details dbjob, buildnumber
  28. end
  29. return
  30. end
  31.  
  32. def get_details(dbjob, buildnumber)
  33.  
  34. job_details = @client.job.get_build_details(dbjob, buildnumber)
  35. uniquejob = job_details['fullDisplayName']
  36. jobname = job_details['fullDisplayName'].split.last
  37. duration = job_details['duration']
  38. timedate = job_details['timestamp']
  39. status = job_details['result']
  40. builds_by_branch_name = job_details['actions'].detect {|x| x['buildsByBranchName'] }
  41.  
  42. if builds_by_branch_name
  43. bbbn = job_details['actions'].detect {|x| x['buildsByBranchName'] }
  44. branch =
  45. if bbbn.empty?
  46. nil
  47. else
  48. bbbn['lastBuiltRevision']['branch'].first['name'].gsub(%r|^origin/|, "")
  49. end
  50. end
  51.  
  52. $LOG.debug("dbjob: #{dbjob}, buildnumber: #{buildnumber},unique: #{uniquejob}, jobname: #{jobname}, duration: #{duration}, timedate: #{timedate}, status: #{status}, branch_builds: #{builds_by_branch_name}")
  53.  
  54. job_sql = @build_db.prepare "insert into buildinfo (uniquejob, dbjob, jobname, branch, buildnumber,
  55. status, duration, timedate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
  56. on duplicate key update dbjob=VALUES(dbjob), jobname=VALUES(jobname), branch=VALUES(branch), buildnumber=VALUES(buildnumber),
  57. status=VALUES(status), duration=VALUES(duration), timedate=VALUES(timedate);"
  58.  
  59. job_sql.execute uniquejob, dbjob, jobname, branch, buildnumber, status, duration, timedate
  60. return
  61. end
  62.  
  63. # Main Section
  64.  
  65. @client = JenkinsApi::Client.new(server_ip: 'my_jenkins_server',
  66. username: '', password: '')
  67.  
  68. @build_db = Mysql.new('webtest2.stage.us-east-1.hootops.com', 'bmadmin', 'xxxxxxxxxx', 'buildmetrics')
  69.  
  70. # Get all Dashboard jobs, loop through to get build # and url
  71. #one job from dashboard jobs
  72. #dbjobs = @client.job.list("Dashboard", "Dashboard-Build")
  73. # all dashboard jobs
  74.  
  75. dbjobs = ["Dashboard", "Dashboard-Build", "Dashboard-Deploy-Production", "Dashboard-Deploy-Production-Candidates", "Dashboard-Deploy-Production-Check",
  76. "Dashboard-Deploy-Staging", "Dashboard-Deploy-Staging-Candidates",
  77. "Dashboard-JS-Documentation", "Dashboard-Metrics", "Dashboard-Post-Deploy-Production",
  78. "Dashboard-SOA-Test-API", "Dashboard-Test-API", "Dashboard-Test-Automation",
  79. "Dashboard-Test-Documentation", "Dashboard-Test-JS", "Dashboard-Test-JS-Casper",
  80. "Dashboard-Test-JS-Casper-MW", "Dashboard-Test-Mobile", "Dashboard-Test-PHP-1",
  81. "Dashboard-Test-PHP-2", "Dashboard-Test-PHP-3"]
  82.  
  83. dbjobs.each do |dbjob|
  84. get_builds(dbjob)
  85. end
  86. @build_db.close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement