/
cli.rb
129 lines (112 loc) · 3.71 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
require 'rrrspec/client'
require 'launchy'
require 'thor'
module RRRSpec
module Client
class CLI < Thor
WAIT_POLLING_SEC = 10
package_name 'RRRSpec'
default_command 'help'
class_option :config, aliases: '-c', type: :string, default: ''
no_commands do
def setup(conf)
RRRSpec.setup(conf, options[:config].split(':'))
end
def log_exception
yield
rescue
RRRSpec.logger.error($!)
raise
end
end
option :'key-only', type: :boolean
option :'rsync-name', type: :string, default: ENV['USER']
option :'worker-type', type: :string
desc 'start', 'start RRRSpec'
def start
setup(ClientConfiguration.new)
if options[:'worker-type']
RRRSpec.configuration.worker_type = options[:'worker-type']
end
taskset = Support.start_taskset(RRRSpec.configuration, options[:'rsync-name'])
puts taskset.key
if RRRSpec.configuration.rrrspec_web_base && !options[:'key-only']
url = "#{RRRSpec.configuration.rrrspec_web_base}/tasksets/#{taskset.key}"
Launchy.open(url)
end
end
desc 'cancel', 'cancel the taskset'
def cancel(taskset_id)
setup(Configuration.new)
taskset = Taskset.new(taskset_id)
exit(1) unless taskset.exist?
taskset.cancel
end
desc 'cancelall', 'cancel all tasksets whose rsync name is specified name'
def cancelall(rsync_name)
setup(Configuration.new)
ActiveTaskset.all_tasksets_of(rsync_name).each do |taskset|
taskset.cancel
end
end
desc 'actives', 'list up the active tasksets'
def actives
setup(Configuration.new)
ActiveTaskset.list.each { |taskset| puts taskset.key }
end
desc 'nodes', 'list up the active nodes'
def nodes
setup(Configuration.new)
puts "Workers:"
Worker.list.each { |worker| puts "\t#{worker.key}" }
end
option :pollsec, type: :numeric, default: WAIT_POLLING_SEC
desc 'waitfor', 'wait for the taskset'
def waitfor(taskset_id)
setup(Configuration.new)
taskset = Taskset.new(taskset_id)
exit(1) unless taskset.exist?
rd, wt = IO.pipe
Signal.trap(:TERM) { wt.write("1") }
Signal.trap(:INT) { wt.write("1") }
loop do
rs, ws, = IO.select([rd], [], [], options[:pollsec])
if rs == nil
break if taskset.persisted?
elsif rs.size != 0
rs[0].getc
taskset.cancel
end
end
end
option :'failure-exit-code', type: :numeric, default: 1
option :verbose, type: :boolean, default: false
desc 'show', 'show the result of the taskset'
def show(taskset_id)
setup(Configuration.new)
taskset = Taskset.new(taskset_id)
exit 1 unless taskset.exist?
Support.show_result(taskset, options[:verbose])
if taskset.status != 'succeeded'
exit options[:'failure-exit-code']
end
end
desc 'slave', 'run RRRSpec as a slave'
def slave(working_dir=nil, taskset_key=nil)
$0 = 'rrrspec slave'
working_dir ||= ENV['RRRSPEC_WORKING_DIR']
taskset_key ||= ENV['RRRSPEC_TASKSET_KEY']
exit 1 unless taskset_key && working_dir
setup(Configuration.new)
log_exception do
slave = Slave.create
slave_runner = SlaveRunner.new(slave, working_dir, taskset_key)
Thread.abort_on_exception = true
Thread.fork { RRRSpec.pacemaker(slave, 60, 5) }
Thread.fork { slave_runner.work_loop }
Kernel.sleep
end
end
end
end
end