class Jester::Cli
Constants
- JOB_RETRY
Constants
Public Instance Methods
build()
click to toggle source
# File lib/jester/cli.rb, line 65 def build if File.file?(@options[:pipeline_file]) pipeline = File.read(@options[:pipeline_file]) else puts "File not found: " + @options[:pipeline_file] raise 'FileNotFound' end job_params = { description: @options[:job_name], script: pipeline } xml = pipeline_xml(job_params) if job_exists?(@options[:job_name]) path = "/job/" + @options[:job_name] + "/config.xml" puts "Job already exists, using path: #{path}" if debug else path = "/createItem?name=" + @options[:job_name] puts "Job doesn't exist yet, using path: #{path}" if debug end begin r = post( @options[:url] + path, xml ) if r.status != 200 puts "Job config update failed." raise 'JobPostFailed' else puts "Job config update succeeded." end rescue Exception => e puts "Exception: " + e.message puts "POST response status: #{r.status}" puts "POST response body: #{r.body}" if debug return e end build_path = "/job/" + @options[:job_name] + "/build?delay=0sec" build_resp = post( @options[:url] + build_path ) if build_resp.status != 201 puts "Unable to run build. Quit" quit else puts "Build running - getting output..." end resp = get(@options[:url] + "/job/" + @options[:job_name] + "/api/json") json = JSON.parse(resp.body) if json['inQueue'] == false build_num = json['lastBuild']['number'] end build = build_result(@options[:job_name], build_num) puts "Job " + build_num.to_s + " result: " + build['result'] log = log_result(@options[:job_name], build_num) puts "DEBUG: " + log.body if debug File.write("#{@options[:job_name]}.log", log.body) puts "See #{@options[:job_name]}.log for output." end
new()
click to toggle source
# File lib/jester/cli.rb, line 40 def new puts "Checking if job, '#{@options[:job_name]}', already exists..." if job_exists?(@options[:job_name]) puts "Job already exists! Quit." else puts "Creating new pipeline job named #{@options[:job_name]}..." job_params = { description: @options[:job_name], script: '// empty job created by jester\n node {print "test"}' } xml = pipeline_xml(job_params) r = post( @options[:url] + "/createItem?name=#{@options[:job_name]}", xml ) if ! (defined? r.status).nil? && r.status == 200 puts "Job successfully created." else puts "Job creation failed." end end end
test()
click to toggle source
# File lib/jester/cli.rb, line 25 def test puts "Testing authenticated connectivity to #{@options[:url]}..." r = get( @options[:url] ) version = r['x-jenkins'] if version.nil? puts "Fail" else puts "Success! Running Jenkins version " + version end end
version()
click to toggle source
# File lib/jester/cli.rb, line 123 def version puts Jester::VERSION end
Private Instance Methods
build_result(job_name, build_number)
click to toggle source
# File lib/jester/cli.rb, line 212 def build_result (job_name, build_number) result = nil while result.nil? resp = JSON.parse( get( @options[:url] + "/job/" + job_name + "/" + build_number.to_s + "/api/json").body ) if resp['building'] == true || resp['result'].nil? sleep JOB_RETRY print "." if debug end result = resp['result'] end resp end
debug()
click to toggle source
# File lib/jester/cli.rb, line 130 def debug @options[:verbose] end
get(url, params = {})
click to toggle source
# File lib/jester/cli.rb, line 135 def get (url, params = {}) begin c = Faraday.new(url: url) do |conn| conn.basic_auth(@options[:username], @options[:password]) conn.adapter Faraday.default_adapter end resp = c.get rescue Exception => e puts e.message return e end end
get_crumb(url, user, pass)
click to toggle source
# File lib/jester/cli.rb, line 176 def get_crumb (url, user, pass) p_url = URI.parse(url) base_url = p_url.scheme + "://" + p_url.host + ":" + p_url.port.to_s r = get(base_url + '/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)' ) r.body.split(':')[1] end
job_exists?(job_name)
click to toggle source
# File lib/jester/cli.rb, line 202 def job_exists? (job_name) job = get( @options[:url] + "/job/" + job_name ) if job.reason_phrase == "Found" true else false end end
log_result(job_name, build_number)
click to toggle source
# File lib/jester/cli.rb, line 228 def log_result (job_name, build_number) resp = get( @options[:url] + "/job/" + job_name + "/" + build_number.to_s + "/consoleText") end
pipeline_xml(params = {})
click to toggle source
params = { description, script }
# File lib/jester/cli.rb, line 186 def pipeline_xml (params = {}) return %Q{<?xml version='1.1' encoding='UTF-8'?> <flow-definition plugin="workflow-job@2.17"> <description>#{params[:description]}</description> <keepDependencies>false</keepDependencies> <properties/> <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@2.46"> <script>#{params[:script]}</script> <sandbox>true</sandbox> </definition> <triggers/> <disabled>false</disabled> </flow-definition>} end
post(url, body = nil, params = {})
click to toggle source
# File lib/jester/cli.rb, line 149 def post (url, body = nil, params = {}) begin crumb = get_crumb(url, @options[:username], @options[:password]) puts "DEBUG: crumb = " + crumb if @options[:verbose] c = Faraday.new(url: url) do |conn| conn.basic_auth(@options[:username], @options[:password]) conn.adapter Faraday.default_adapter end resp = c.post do |conn| conn.headers['Jenkins-Crumb'] = crumb conn.headers['Content-Type'] = 'application/xml' if body != nil conn.body = body end end if @options[:verbose] puts "DEBUG: " puts resp.to_hash[:response_headers] end return resp rescue Exception => e puts e.message return e end end