Class: Jenkins::CLI

Inherits:
Thor show all
Includes:
Formatting
Defined in:
lib/jenkins/cli.rb,
lib/jenkins/cli/formatting.rb

Defined Under Namespace

Modules: Formatting

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Formatting

included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)



295
296
297
# File 'lib/jenkins/cli.rb', line 295

def method_missing(name, *args)
  console(name, *args)
end

Class Method Details

.common_optionsObject



13
14
15
16
17
18
19
# File 'lib/jenkins/cli.rb', line 13

def self.common_options
  method_option :host,     :desc => 'connect to jenkins server on this host'
  method_option :port,     :desc => 'connect to jenkins server on this port'
  method_option :ssl,      :desc => 'connect to jenkins server with ssl', :type => :boolean, :default => false
  method_option :username, :desc => 'connect to jenkins server with username'
  method_option :password, :desc => 'connect to jenkins server with password'
end

.help(shell) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/jenkins/cli.rb', line 277

def self.help(shell, *)
  list = printable_tasks
  shell.say <<-USEAGE
Jenkins.rb is a smart set of utilities for making
continuous integration as simple as possible

Usage: jenkins command [arguments] [options]

USEAGE

  shell.say "Commands:"
  shell.print_table(list, :ident => 2, :truncate => true)
  shell.say
  class_options_help(shell)
end

Instance Method Details

#add_node(slave_host) ⇒ Object



234
235
236
237
238
239
240
241
# File 'lib/jenkins/cli.rb', line 234

def add_node(slave_host)
  select_jenkins_server(options)
  if results = Jenkins::Api.add_node({:slave_host => slave_host}.merge(options))
    shell.say "Added slave node '#{results[:name]}' to #{results[:slave_host]}", :green
  else
    error "Failed to add slave node #{slave_host}"
  end
end

#auth_infoObject



260
261
262
263
264
265
# File 'lib/jenkins/cli.rb', line 260

def auth_info
  if auth = Jenkins::Config.config['basic_auth']
    shell.say "username: #{auth["username"]}"
    shell.say "password: #{auth["password"]}"
  end
end

#build(project_path = ".") ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/jenkins/cli.rb', line 115

def build(project_path = ".")
  select_jenkins_server(options)
  FileUtils.chdir(project_path) do
    if Jenkins::Api.build_job(project_name)
      shell.say "Build for '#{project_name}' running now..."
    else
      error "No job '#{project_name}' on server."
    end
  end
end

#build_details(job_name, build_number = "lastBuild") ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/jenkins/cli.rb', line 168

def build_details(job_name, build_number="lastBuild")
  select_jenkins_server(options)
  if build_details = Jenkins::Api.build_details(job_name, build_number)
    if options[:hash]
      require "ap"
      ap build_details.parsed_response
    elsif options[:json]
      puts build_details.parsed_response.to_json
    elsif options[:yaml]
      require "yaml"
      puts build_details.parsed_response.to_yaml
    else
      error "Select an output format: --json, --yaml, --hash"
    end
  else
    error "Cannot find project '#{job_name}'."
  end
end

#configureObject



254
255
256
257
# File 'lib/jenkins/cli.rb', line 254

def configure
  Jenkins::Api.setup_base_url(options)
  Jenkins::Api.cache_configuration!
end

#console(job_name, axe = nil, build_number = "lastBuild") ⇒ Object



189
190
191
192
# File 'lib/jenkins/cli.rb', line 189

def console(job_name, axe=nil, build_number="lastBuild")
  select_jenkins_server(options)
  puts Jenkins::Api.console(job_name, build_number, axe)
end

#create(project_path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jenkins/cli.rb', line 50

def create(project_path)
  select_jenkins_server(options)
  FileUtils.chdir(project_path) do
    scm = discover_scm(options)
    ruby_or_rails_project_without_gemfile?(options)
    begin
      template = options[:"no-template"] ? 'none' : options[:template]
      job_config = build_job_config(scm, template, options)

      if Jenkins::Api.create_job(project_name, job_config, options)
        build_url = "#{@uri}/job/#{project_name.gsub(/\s/,'%20')}/build"
        shell.say "Added#{' ' + template unless template == 'none'} project '#{project_name}' to Jenkins.", :green
        unless options[:"no-build"]
          shell.say "Triggering initial build..."
          Jenkins::Api.build_job(project_name)
          shell.say "Trigger additional builds via:"
        else
          shell.say "Trigger builds via:"
        end
        shell.say "  URL: "; shell.say "#{build_url}", :yellow
        shell.say "  CLI: "; shell.say "#{cmd} build #{project_name}", :yellow
      else
        error "Failed to create project '#{project_name}'"
      end
    rescue Jenkins::JobConfigBuilder::InvalidTemplate
      error "Invalid job template '#{template}'."
    rescue Jenkins::Api::JobAlreadyExistsError
      error "Job '#{project_name}' already exists."
    end
  end
end

#default_hostObject



244
245
246
247
248
249
250
# File 'lib/jenkins/cli.rb', line 244

def default_host
  if select_jenkins_server({})
    display Jenkins::Api.base_uri
  else
    display "No default host yet. Use '--host host --port port' on your first request."
  end
end

#help(*args) ⇒ Object



268
269
270
# File 'lib/jenkins/cli.rb', line 268

def help(*args)
  super(*args)
end

#job(name) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/jenkins/cli.rb', line 144

def job(name)
  select_jenkins_server(options)
  if job = Jenkins::Api.job(name)
    if options[:hash]
      require "ap"
      ap job.parsed_response
    elsif options[:json]
      puts job.parsed_response.to_json
    elsif options[:yaml]
      require "yaml"
      puts job.parsed_response.to_yaml
    else
      error "Select an output format: --json, --xml, --yaml, --hash"
    end
  else
    error "Cannot find project '#{name}'."
  end
end

#listObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/jenkins/cli.rb', line 196

def list
  select_jenkins_server(options)
  summary = Jenkins::Api.summary
  unless summary["jobs"].blank?
    shell.say "#{@uri}:", :bold
    summary["jobs"].each do |job|
      bold  = job['color'] =~ /anime/
      color = 'red' if job['color'] =~ /red/
      color = 'green' if job['color'] =~ /(blue|green)/
      color ||= 'yellow' # if color =~ /grey/ || color == 'disabled'
      shell.say "* "; shell.say(shell.set_color(job['name'], color.to_sym, bold), nil, true)
    end
    shell.say ""
  else
    shell.say "#{@uri}: "; shell.say "no jobs", :yellow
  end
end

#nodesObject



216
217
218
219
220
221
222
223
# File 'lib/jenkins/cli.rb', line 216

def nodes
  select_jenkins_server(options)
  nodes = Jenkins::Api.nodes
  nodes["computer"].each do |node|
    color = node["offline"] ? :red : :green
    shell.say node["displayName"], color
  end
end

#remove(project_path) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/jenkins/cli.rb', line 128

def remove(project_path)
  select_jenkins_server(options)
  FileUtils.chdir(project_path) do
    if Jenkins::Api.delete_job(project_name)
      shell.say "Removed project '#{project_name}' from Jenkins."
    else
      error "Failed to delete project '#{project_name}'."
    end
  end
end

#serverObject



28
29
30
31
32
33
34
35
36
# File 'lib/jenkins/cli.rb', line 28

def server
  begin
    require 'jenkins/war'
    Jenkins::War::server(options)
  rescue LoadError
    puts "To run a jenkins server, you need to install the jenkins-war gem. try:"
    puts "gem install jenkins-war"
  end
end

#update(project_path) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/jenkins/cli.rb', line 93

def update(project_path)
  select_jenkins_server(options)
  FileUtils.chdir(project_path) do
    scm = discover_scm(options)
    ruby_or_rails_project_without_gemfile?(options)
    begin
      template = options[:"no-template"] ? 'none' : options[:template]
      job_config = build_job_config(scm, template, options)

      if Jenkins::Api.update_job(project_name, job_config)
        shell.say "Updated#{' ' + template unless template == 'none'} project '#{project_name}'.", :green
      else
        error "Failed to update project '#{project_name}'"
      end
    rescue Jenkins::JobConfigBuilder::InvalidTemplate
      error "Invalid job template '#{template}'."
    end
  end
end

#versionObject



273
274
275
# File 'lib/jenkins/cli.rb', line 273

def version
  shell.say "#{Jenkins::VERSION}"
end