Class: Hudson::CLI

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

Defined Under Namespace

Modules: Formatting

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Formatting

included

Class Method Details

.common_optionsObject



13
14
15
16
# File 'lib/hudson/cli.rb', line 13

def self.common_options
  method_option :host, :desc => 'connect to hudson server on this host'
  method_option :port, :desc => 'connect to hudson server on this port'
end

.help(shell) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/hudson/cli.rb', line 183

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

Usage: hudson 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



164
165
166
167
168
169
170
171
# File 'lib/hudson/cli.rb', line 164

def add_node(slave_host)
  select_hudson_server(options)
  if Hudson::Api.add_node({:slave_host => slave_host}.merge(options))
    shell.say "Added slave node #{slave_host}", :green
  else
    error "Failed to add slave node #{slave_host}"
  end
end

#build(project_path = ".") ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hudson/cli.rb', line 98

def build(project_path = ".")
  select_hudson_server(options)
  FileUtils.chdir(project_path) do
    name = File.basename(FileUtils.pwd)
    if Hudson::Api.build_job(name)
      shell.say "Build for '#{name}' running now..."
    else
      error "No job '#{name}' on server."
    end
  end
end

#create(project_path) ⇒ Object



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hudson/cli.rb', line 56

def create(project_path)
  select_hudson_server(options)
  FileUtils.chdir(project_path) do
    unless scm = Hudson::ProjectScm.discover
      error "Cannot determine project SCM. Currently supported: #{Hudson::ProjectScm.supported}"
    end
    unless File.exists?("Gemfile")
      error "Ruby/Rails projects without a Gemfile are currently unsupported."
    end
    begin
      template = options[:template]
      job_config = Hudson::JobConfigBuilder.new(template) do |c|
        c.scm = scm.url
        c.assigned_node = options[:"assigned-node"] if options[:"assigned-node"]
        c.public_scm = options[:"public-scm"]
      end
      name = File.basename(FileUtils.pwd)
      if Hudson::Api.create_job(name, job_config, options)
        build_url = "#{@uri}/job/#{name.gsub(/\s/,'%20')}/build"
        shell.say "Added #{template} project '#{name}' to Hudson.", :green
        unless options[:"no-build"]
          shell.say "Triggering initial build..."
          Hudson::Api.build_job(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 #{name}", :yellow
      else
        error "Failed to create project '#{name}'"
      end
    rescue Hudson::JobConfigBuilder::InvalidTemplate
      error "Invalid job template '#{template}'."
    rescue Hudson::Api::JobAlreadyExistsError
      error "Job '#{name}' already exists."
    end
  end
end

#help(*args) ⇒ Object



174
175
176
# File 'lib/hudson/cli.rb', line 174

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

#listObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/hudson/cli.rb', line 126

def list
  select_hudson_server(options)
  summary = Hudson::Api.summary
  unless summary["jobs"].blank?
    shell.say "#{@uri}:", :bold
    summary["jobs"].each do |job|
      color = job['color']
      bold  = color =~ /anime/
      color = 'red' if color == 'red_anime'
      color = 'green' if color == 'blue' || color == 'blue_anime'
      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



147
148
149
150
151
152
153
154
# File 'lib/hudson/cli.rb', line 147

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

#remove(project_path) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/hudson/cli.rb', line 112

def remove(project_path)
  select_hudson_server(options)
  FileUtils.chdir(project_path) do
    name = File.basename(FileUtils.pwd)
    if Hudson::Api.delete_job(name)
      shell.say "Removed project '#{name}' from Hudson."
    else
      error "Failed to delete project '#{name}'."
    end
  end
end

#serverObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hudson/cli.rb', line 25

def server
  if options[:kill]
    require 'socket'
    TCPSocket.open("localhost", options[:control]) do |sock|
      sock.write("0")
    end
    exit
  end

  serverhome = File.join(options[:home])
  javatmp = File.join(serverhome, "javatmp")
  FileUtils.mkdir_p serverhome
  FileUtils.mkdir_p javatmp
  FileUtils.cp_r Hudson::PLUGINS, serverhome
  ENV['HUDSON_HOME'] = serverhome
  cmd = ["java", "-Djava.io.tmpdir=#{javatmp}", "-jar", Hudson::WAR]
  cmd << "--daemon" if options[:daemon]
  cmd << "--logfile=#{File.expand_path(options[:logfile])}" if options[:logfile]
  cmd << "--httpPort=#{options[:port]}"
  cmd << "--controlPort=#{options[:control]}"
  shell.say cmd.join(" ")
  exec(*cmd)
end

#versionObject



179
180
181
# File 'lib/hudson/cli.rb', line 179

def version
  shell.say "#{Hudson::VERSION} (Hudson Server #{Hudson::HUDSON_VERSION})"
end