forked from rubys/awdwr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.rb
executable file
·403 lines (345 loc) · 12.1 KB
/
dashboard.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/ruby
require 'wunderbar/job-control'
require 'time'
require 'yaml'
$HOME ||= ENV['HOME']
$home = $HOME.sub(/\/?$/, '/').untaint
home_re = /(\$HOME|\~)\//
$dashboard = File.join(File.dirname(__FILE__), 'dashboard.yml')
config = YAML.load_file($dashboard)
$logdir = config.delete('log').sub(home_re, $home).untaint
$bindir = config.delete('bin').sub(home_re, $home).untaint
testrails = Array(config.delete('testrails')).
map {|name| File.expand_path(name)}.
find {|name| File.exist? name}
testrails = YAML.load_file(testrails.untaint) if testrails
# set up symbolic links
if ARGV == ['--symlink']
config['book'].each do |edition|
edition.each do |name, vars|
unless File.exist? vars['web']
system "ln -s #{vars['home']} #{vars['web']}"
end
end
end
exit
end
require "#{$home}/git/awdwr/environment".untaint
# identify the unique jobs
$jobs = config['book'].map do |editions|
editions = [editions] if editions.respond_to? :push
editions.map do |book, book_info|
book_info['env'].map do |info|
if testrails
rails = info['rails'].gsub('.', '')
ruby = info['ruby'].gsub('.', '')
info = AWDWR::config(testrails, book, rails, ruby).merge(info)
else
info['source'] = book_info['home'].sub(home_re, $HOME)
end
info['book']=book
info['path']=File.join(info['source'], info['work']).untaint
info['id']=info['work'].gsub('.','') + '-' + book.to_s
info.update(book_info)
end
end
end.flatten
module Dashboard
def self.env=(env)
@@env = env
end
def self.root
@root ||= @@env['REQUEST_URI'].split('/').map{'..'}.join('/').
sub(/\.*$/, '')
end
def self.status
# determine if any processes are active
active = `ps xo start,args`.
scan(/^(?:\d\d:\d\d:\d\d|.\d:\d\d[AP]M) .*/).
grep(/(\d |\/)testrails/)
log = []
unless active.empty?
start = Time.parse(active.first.split.first)
logs = Dir["#{$logdir}/makedepot*.log"]
latest = logs.sort_by {|file| File.stat(file.untaint).mtime}.last
if latest and File.stat(latest).mtime >= start
log.push *open(latest) {|file| file.readlines.grep(/====>/).pop(3)}
end
log.map! {|line| line.chomp.sub('====> ','')}
log.unshift '' unless log.empty?
end
[active, log]
end
# checkdepot link for a given job
def self.checkdepot(job, status, static=false)
if static
link = "#{job['work'].sub('work','checkdepot')}.html"
else
link = "#{job['work']}/checkdepot.html"
end
checkdir = "#{job['path']}/checkdepot/"
checkfile = checkdir.sub(/\/$/,'.html')
if File.directory?(checkdir) and File.exist?(checkfile)
if File.stat(checkdir).mtime >= File.stat(checkfile).mtime
link.sub! ".html", '/index.html'
end
end
if status == 'NO OUTPUT'
link= "#{link.sub('.html','/')}makedepot.log"
end
"#{root}#{job['web']}/#{link}"
end
end
# main output
_html do
Dashboard.env = env
# submit a new run in the background
if _.post?
if config['path']
ENV['PATH'] = config['path'] + File::PATH_SEPARATOR + ENV['PATH']
end
testrails = "#{$bindir}/testrails"
testrails = "#{Dir.pwd}/testrails.rb" unless File.exist? testrails
if _.env["SERVER_PORT"] == "3000"
testrails += ' --port=3001'
end
require 'shellwords'
@args = Shellwords.join(@args.split).untaint
_.submit "ruby #{testrails.untaint} #{@args} " +
"> #{$logdir}/post.out 2> #{$logdir}/post.err "
sleep 2
end
active, log = Dashboard.status
_head_ do
_title 'Depot Dashboard'
_style :type => "text/css" do
_ <<-'EOF'
body {margin:0; background: #f5f5dc}
h1 {
background: #9C9; color: #282; text-align: center;
font: 40px "Times New Roman",serif;
font-weight: normal; font-variant: small-caps;
padding: 10px 0; border-bottom: 2px solid; margin: 0 0 0.5em 0;
}
h2 {border-spacing: 0; margin-left: 2em}
table {border-spacing: 0; margin-left: auto; margin-right:auto}
th {color: #000; background-color: #99C; border: solid #000 1px}
.odd {background-color: #CCC}
th, td {border: solid 1px; padding: 0.5em}
.selected td:nth-child(5) {border: 3px solid #d2691e}
.hilite {background-color: #FF9}
.pass {background-color: #9C9}
.fail {background-color: #F99}
td a, td a:visited {color: black}
thead th:first-child { border-radius: 1em 0 0 0 }
thead th:last-child { border-radius: 0 1em 0 0 }
tfoot th:first-child { border-radius: 0 0 0 1em }
tfoot th:last-child { border-radius: 0 0 1em 0 }
EOF
_ <<-'EOF' unless @static
.deploylink {float: left; margin-top: -4em; margin-left: 1em}
.deploylink {color: #282; text-decoration: none}
.headerlink {float: right; margin-top: -4em; margin-right: 1em}
.headerlink {color: #282; text-decoration: none}
#executing pre {margin-left: 5em}
form {width: 16.2em; margin: 1em auto}
EOF
_ 'form {display: none}' unless active.empty?
_ '#executing {display: none}' if active.empty?
end
_script :src => Dashboard.root + 'jquery.min.js'
_script :src => Dashboard.root + 'jquery-ui.min.js'
_script :src => Dashboard.root + 'jquery.tablesorter.min.js'
_script %{
setTimeout(update, 1000);
function update() {
$.post("", {}, function(data) {
for (var id in data.config) {
var line = data.config[id];
$('#'+id+' td:eq(3) a').attr('href', line.link);
$('#'+id+' td:eq(3) a').text(line.status);
$('#'+id+' td:eq(4)').text(line.mtime);
if (line.running) {
$('#'+id+' td:eq(3)').addClass('hilite').
removeClass('pass').removeClass('fail');
} else if (line.status.match(/ 0 failures, 0 errors/)) {
$('#'+id+' td:eq(3)').addClass('pass').
removeClass('hilite').removeClass('fail');
} else {
$('#'+id+' td:eq(3)').addClass('fail').
removeClass('hilite').removeClass('pass');
}
}
if (data.deploy) {
$('.deploylink').show();
} else {
$('.deploylink').hide();
}
if (data.active.length == 0) {
$('form').show();
$('#executing').hide();
} else {
$('form').hide();
$('#executing').show();
$('#active').text(data.active.join("\\n"));
}
$('table').trigger('update');
setTimeout(update, 5000);
}, 'json');
}
$(document).ready(function() {
$('tr td:nth-child(5)').click(function(event) {
var parent = $(this).parent();
var args =
parent.find('td:eq(0)').text().replace(/\\D/g,'') + ' ' +
parent.find('td:eq(1)').text().replace(/\\D/g,'') + ' ' +
parent.find('td:eq(2)').text().replace(/\\D/g,'');
if (parent.find('td:eq(0)').text() == 'svn') args = 'svn' + args;
if (event.ctrlKey || event.shiftKey || event.metaKey) {
inputs = $('input[name=args]').val().split(/,\\s*/)
if (inputs[0] == '') inputs.shift();
var index = $.inArray(args, inputs);
if (index == -1) {
inputs.push(args);
parent.addClass('selected');
} else {
inputs.splice(index, 1);
parent.removeClass('selected');
}
args = inputs.join(', ').replace(/,+\\s*/g, ', ')
} else {
$('tr').removeClass('selected');
parent.addClass('selected');
}
$('input[name=args]').val(args);
$(this).css({backgroundColor: '#ff0'});
$(this).animate({backgroundColor: '#fff'}, 'slow');
$('input[name=args]').css({backgroundColor: '#f70'});
$('input[name=args]').animate({backgroundColor: '#fff'}, 'slow');
});
});
} unless @static
_script! %{
$(document).ready(function() {
$.tablesorter.addParser({
id: 'summary',
is: function(s) {return false},
format: function(value, table, cell) {
var vals = $(cell).text().match(/\d+/g);
if (!vals) return '9999';
if (vals.length == 4) {
vals = [vals[3],vals[2],9999-vals[0],9999-vals[1]];
}
var rjust = function(x) {
return ('000'+x).substr(x.toString().length-1,4)
};
return vals.map(rjust).join('');
},
type: 'text'
});
$('table').tablesorter({headers: {3:{sorter:'summary'}}});
});
}
end
_body do
_h1 'The Depot Dashboard'
unless @static
deploy = File.join(File.dirname($dashboard), 'checkdeploy.html')
_a 'deploy', :href => 'checkdeploy', :class => 'deploylink',
:style => ('display: none' unless File.exist? deploy.untaint)
_a 'logs', :href => 'logs', :class => 'headerlink'
end
_table_ do
_thead do
_tr do
_th 'Book'
_th 'Ruby'
_th 'Rails'
_th 'Status'
_th 'Time'
end
end
_tbody_ do
$jobs.flatten.each do |job|
statfile = "#{job['path']}/checkdepot.status"
statfile = "#{job['path']}/status" unless File.exist? statfile
status = open(statfile) {|file| file.read.chomp} rescue 'missing'
status.gsub! /, 0 (pendings|omissions|notifications)/, ''
mtime = File.stat(statfile).mtime.iso8601 rescue 'missing'
attrs = {:id => job['id']}
attrs[:class]='odd' if %w(5.2 5.0 4.1).include? job['rails']
if File.exist?(statfile.sub('checkdepot.','')+'.run')
color = 'hilite'
elsif status =~ / 0 failures, 0 errors/
color = 'pass'
else
color = 'fail'
end
_tr_ attrs do
_td job['book'], {:align=>'right'}.
merge(job['book']=='4' ? {} : {:class=>'hilite'})
_td job['ruby'], ({:class=>'hilite'} if job['ruby']!='2.4.0')
_td job['rails'], ({:class=>'hilite'} if job['rails']!='5.1')
_td :class=>color, :align=>'center' do
_a status, :href => Dashboard.checkdepot(job, status, @static)
end
_td mtime.sub('T',' ').sub(/[+-]\d\d:\d\d$/,'')
end
end
end
_tfoot do
_tr do
5.times { _th '' }
end
end
end
unless @static
_form :method=>'post' do
_input :name=>'args'
_input :type=>'submit', :value=>'submit'
if File.exist? "#$home/.awdwr"
_h2 'Overrides'
_pre File.read("#$home/.awdwr")
end
end
_div_ :id=>'executing' do
_h2 'Currently Executing'
_pre active.join("\n"), :id => 'active'
end
end
end
end
# dynamic status updates
_json do
Dashboard.env = env
active, log = Dashboard.status
config={}
$jobs.each do |job|
statfile = "#{job['path']}/checkdepot.status"
statfile = "#{job['path']}/status" unless File.exist? statfile
status = open(statfile) {|file| file.read.chomp} rescue 'missing'
status.gsub! /, 0 (pendings|omissions|notifications)/, ''
mtime = File.stat(statfile).mtime.iso8601 rescue 'missing'
running = File.exist?(statfile.sub('checkdepot.','')+'.run')
config[job['id']] = {
'rails' => job['rails'],
'ruby' => job['ruby'],
'branch' => job['branch'],
'status' => status,
'pass' => !!(status =~ / 0 failures, 0 errors/),
'mtime' => mtime.sub('T',' ').sub(/[+-]\d\d:\d\d$/,''),
'running' => running,
'link' => Dashboard.checkdepot(job, status)
}
end
webdir = File.dirname($dashboard)
_deploy File.exist? File.join(webdir, 'checkdeploy.html').untaint
_config config
_active active + log
end
Dir["#{__dir__}/*.js"].each do |file|
_file "/#{File.basename(file)}", content: File.read(file)
end
__END__
ENV['SCRIPT_FILENAME'] ||= __FILE__
$dashboard = File.join(File.dirname(ENV['SCRIPT_FILENAME']), 'dashboard.yml')