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
|
# File 'lib/ger/router.rb', line 33
def start
raise "Router still not be configured" unless @configured
Signal.trap(:INT) do
Thread.new do
GER.logger.info "Receive signal: INT. terminating."
end
EM.stop
end
Signal.trap(:TERM) do
Thread.new do
GER.logger.info "Receive signal: TERM. terminating."
end
EM.stop
end
Signal.trap(:USR2) do
Thread.new do
GER.logger.debug "Receive signal: USR2"
end
if GER.logger.level == GER::LOG_NORMAL then
GER.logger.level = GER::LOG_DEBUG
else
GER.logger.level = GER::LOG_NORMAL
end
end
begin
uri = URI.parse(@gerrit.uri)
uri.port = @gerrit.default_port unless uri.port
ssh_options = { :port => uri.port }
ssh_options[:keys] = @gerrit.ssh_keys if @gerrit.ssh_keys
provider = {
"name" => @gerrit.name,
"host" => uri.host,
"port" => uri.port.to_s,
"scheme" => "ssh",
"url" => @gerrit.weburl
}
EM.run do
EM::Ssh.start(uri.host, uri.user, ssh_options) do |connection|
connection.errback do |err|
GER.logger.error { "#{@gerrit.} #{err} (#{err.class})" }
EM.stop
end
connection.callback do |session|
GER.logger.info("#{@gerrit.} connection established.")
GER::Broker.connect(@broker) do |broker|
GER.logger.info("#{@broker.} connection established.")
GER.logger.debug("#{@broker.} channel id = #{broker.channel.id}")
session.exec(@gerrit.version) do |channel, stream, data|
if data then
version = data.strip.delete("gerrit version")
provider["version"] = version
GER.logger.info("version: #{version}")
end
end
session.exec(@gerrit.command) do |channel, stream, data|
channel.on_data do |ch, data|
if @broker.mode == "raw" then
str = %Q(#{data.strip})
else
json = JSON.parse(data.strip)
provider["event_id"] = Digest::SHA256.hexdigest(data.strip)
json["provider"] = provider
str = JSON.generate(json)
end
broker.send(str, :app_id => @appid, :routing_key => @gerrit.routing_key, :headers => (provider))
end
end
end
end
end
end
rescue => e
GER.logger.error { "#{e} (#{e.class})" }
GER.logger.debug { e.backtrace.join("\n") }
EM.stop
end
end
|