Class: GerritEventRouter::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/ger/router.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, appid, config) ⇒ Router

Returns a new instance of Router.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ger/router.rb', line 4

def initialize(name, appid, config)
  @name = name
  @appid = appid
  begin
    @gerrit = config.gerrits[name]
    raise "Gerrit name is not found: #{name}" unless @gerrit
    @broker = config.brokers[@gerrit.broker]
    raise "Broker name is not found: #{@gerrit.broker}" unless @broker

    GER.logger.info "Configured router: #{@gerrit.header}(#{@gerrit.name}) -> broker#{@broker.header}(#{@broker.name})"
    @configured = true
  rescue
    @configured = false
    raise
  end
end

Instance Method Details

#header(provider) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ger/router.rb', line 21

def header(provider)
    headers = {
      "gerrit-name" => provider["name"],
      "gerrit-host" => provider["host"],
      "gerrit-scheme" => provider["scheme"],
      "gerrit-port" => provider["port"],
      "gerrit-front-url" => provider["url"],
      "gerrit-version" => provider["version"]
    }
    return headers
end

#startObject



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.header} #{err} (#{err.class})" }
          EM.stop
        end

        connection.callback do |session|
          GER.logger.info("#{@gerrit.header} connection established.")

          GER::Broker.connect(@broker) do |broker|
            GER.logger.info("#{@broker.header} connection established.")
            GER.logger.debug("#{@broker.header} 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 => header(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