This repository has been archived by the owner on Jul 10, 2018. It is now read-only.
forked from jenkinsci/jenkins.rb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.rb
186 lines (163 loc) · 5.92 KB
/
cli.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
require 'thor'
require 'active_support/core_ext/object/blank'
require 'hudson/cli/formatting'
require 'hudson/config'
require 'hudson/remote'
module Hudson
class CLI < Thor
include CLI::Formatting
map "-v" => :version, "--version" => :version, "-h" => :help, "--help" => :help
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'
method_option :server, :desc => 'connect to remote hudson server by search'
end
desc "server [options]", "run a hudson server"
method_option :home, :type => :string, :default => File.join(ENV['HOME'], ".hudson", "server"), :banner => "PATH", :desc => "use this directory to store server data"
method_option :port, :type => :numeric, :default => 3001, :desc => "run hudson server on this port", :aliases => "-p"
method_option :control, :type => :numeric, :default => 3002, :desc => "set the shutdown/control port", :aliases => "-c"
method_option :daemon, :type => :boolean, :default => false, :desc => "fork into background and run as a daemon"
method_option :kill, :type => :boolean, :desc => "send shutdown signal to control port", :aliases => "-k"
method_option :logfile, :type => :string, :banner => "PATH", :desc => "redirect log messages to this file"
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]}"
puts cmd.join(" ")
exec(*cmd)
end
desc "create [project_path] [options]", "create a continuous build for your project"
common_options
method_option :name, :banner => "dir_name", :desc => "name of the build"
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
job_config = Hudson::JobConfigBuilder.new(:rubygem) do |c|
c.scm = scm.url
end
name = options[:name] || File.basename(FileUtils.pwd)
if Hudson::Api.create_job(name, job_config)
build_url = "#{@uri}/job/#{name.gsub(/\s/,'%20')}/build"
puts "Added project '#{name}' to Hudson."
puts "Trigger builds via: #{build_url}"
else
error "Failed to create project '#{name}'"
end
end
end
desc "list [options]", "list jobs on a hudson server"
common_options
def list
select_hudson_server(options)
if summary = Hudson::Api.summary
if summary["useSecurity"] == true && !authenticating?
raise_authentication_error
end
unless summary["jobs"].blank?
shell.say "#{@uri} -"
summary["jobs"].each do |job|
color = job['color']
color = 'red' if color == 'red_anime'
color = 'green' if color == 'blue' || color == 'blue_anime'
color = 'yellow' if color == 'grey' || color == 'disabled'
shell.say job['name'], color.to_sym, true
end
shell.say ""
else
display "#{@uri} - no jobs"
end
else
error "#{@uri} - no connection"
end
end
desc "nodes", "list hudson server nodes"
common_options
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
desc "job name [options]", "display job status"
common_options
def job(name)
select_hudson_server(options)
if job = Hudson::Api.job(name)
require "ap"
ap job
else
error "#{@uri} - no connection"
end
end
# desc "add_remote name [options]", "manage remote servers (comming sometime to a theater near you)"
# common_options
# def add_remote(name)
# select_hudson_server(options)
# if Hudson::Remote.add_server(name, @uri)
# display "Added remote server '#{name}' for #{@uri}"
# else
# error "Could not add remote server for '#{@uri}'"
# end
# end
desc "help [command]", "show help for hudson or for a specific command"
def help(*args)
super(*args)
end
desc "version", "show version information"
def version
shell.say "#{Hudson::VERSION} (Hudson Server #{Hudson::HUDSON_VERSION})"
end
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
private
def raise_authentication_error
error "#{@uri} - Authentication required. Please use --username and --password options"
end
def authenticating?
false
end
def select_hudson_server(options)
unless @uri = Hudson::Api.setup_base_url(options)
error "Either use --host or add remote servers."
end
end
def display(text)
shell.say text
exit
end
def error(text)
shell.say "ERROR: #{text}", :red
exit
end
end
end