-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsin.rb
60 lines (52 loc) · 1.74 KB
/
sin.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
require "rubygems"
require "sinatra"
require "yaml"
CONFIG = YAML.load_file("kowalski.yml")
get '/' do
"<p>do a \"GET /#{CONFIG["project"]}/:task\" to initiate a capistrano task</p>" +
"<p>\"GET /#{CONFIG["project"]}/:task.raw\" will render a stream with the output</p>"
end
get "/#{CONFIG["project"]}/:task" do |task|
cmd, format = task.split(".")
time = Time.now
system "mkdir -p #{CONFIG["master"]["main_path"]}/logs"
filename = "#{CONFIG["master"]["main_path"]}/logs/#{time.to_i}-#{cmd}.txt"
case format
when 'raw'
content_type :txt
IO.popen "cap #{cmd} 2>&1 | tee #{filename}"
else
system "cap #{cmd} > #{filename} 2>&1"
system "cp #{filename} #{CONFIG["master"]["main_path"]}/logs/latest-#{cmd}.txt"
output = `cat #{filename} | sed -r "s/\\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"`
output.gsub!(/>/, '>')
output.gsub!(/</, '<')
html = '<table style="width:100%">'
html << '<thead><tr>'
hosts = output[/Alive runners: (.*?)\n/, 1].split(", ")
hosts.each do |host|
html << "<th>#{host}</th>"
end
html << '</tr></thead><tbody>'
host_lines = {}
output.split("\n").each do |line|
unless line =~ /^[\s\*]+\[[^\]]+\]/
html << "<tr>\n"
hosts.each do |host|
html << "<td><pre>#{(host_lines[host] || []) * "</br>"}</pre></td>\n"
end
html << "</tr>\n"
host_lines = {}
html << "<tr><td style=\"color:#999\" colspan=\"#{hosts.size}\"><pre>#{line}</pre></td></tr>\n"
else
hostname = line[/(#{hosts * '|'})/, 1]
host_lines[hostname] ||= []
host_lines[hostname] << line.strip.gsub(/^[\s\*]*\[[^\]]+\]/, '').strip
end
end
html << "</tbody></table>"
"<p>Done in #{Time.now - time} seconds</p>" +
"<p><small><pre>#{filename}</pre></small></p>" +
"<br /><br />" + html
end
end