Class: Jester::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/jester/cli.rb

Constant Summary collapse

JOB_RETRY =

Constants

2

Instance Method Summary collapse

Instance Method Details

#buildObject



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
108
109
110
111
112
113
114
115
116
117
118
119
# 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

#newObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

#testObject



25
26
27
28
29
30
31
32
33
34
# 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

#versionObject



123
124
125
# File 'lib/jester/cli.rb', line 123

def version
  puts Jester::VERSION
end