Skip to content

Commit d2fc239

Browse files
committed
Read options from stdin
1 parent 1ad4060 commit d2fc239

File tree

1 file changed

+26
-44
lines changed

1 file changed

+26
-44
lines changed

workers/event_catcher/event_catcher.rb

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
require "sd_notify"
66

77
class EventCatcher
8-
def initialize(ems_id, hostname, username, password, port, messaging_host, messaging_port, page_size = 20)
8+
def initialize(ems_id, default_endpoint, default_authentication, messaging_opts, page_size = 20)
99
@ems_id = ems_id
10-
@hostname = hostname
11-
@username = username
12-
@password = password
13-
@port = port
14-
@messaging_host = messaging_host
15-
@messaging_port = messaging_port
10+
@hostname = default_endpoint["hostname"]
11+
@username = default_authentication["userid"]
12+
@password = default_authentication["password"]
13+
@port = default_endpoint["port"]
14+
@messaging_host = messaging_opts["host"]
15+
@messaging_port = messaging_opts["port"]
1616
@page_size = page_size
1717
end
1818

@@ -24,18 +24,23 @@ def run!
2424
notify_started
2525

2626
wait_for_updates(vim) do |property_change|
27-
next unless property_change.name =~ /latestPage.*/
27+
next unless property_change.name.match?(/latestPage.*/)
2828

2929
events = Array(property_change.val).map { |event| parse_event(event) }
3030
publish_events(events)
3131
end
32+
rescue Interrupt
33+
# Catch SIGINT
3234
ensure
3335
notify_stopping
3436
property_filter&.DestroyPropertyFilter
3537
event_history_collector&.DestroyCollector
3638
vim&.close
3739
end
3840

41+
def stop!
42+
end
43+
3944
private
4045

4146
attr_reader :ems_id, :hostname, :messaging_host, :messaging_port, :password, :port, :page_size, :username
@@ -47,7 +52,7 @@ def connect
4752
:ssl => true,
4853
:insecure => true,
4954
:path => '/sdk',
50-
:port => 443,
55+
:port => port,
5156
:rev => '6.5',
5257
}
5358

@@ -69,13 +74,13 @@ def create_event_history_collector(vim, page_size)
6974

7075
def create_property_filter(vim, event_history_collector)
7176
vim.propertyCollector.CreateFilter(
72-
:spec => RbVmomi::VIM.PropertyFilterSpec(
73-
:objectSet => [
77+
:spec => RbVmomi::VIM.PropertyFilterSpec(
78+
:objectSet => [
7479
RbVmomi::VIM.ObjectSpec(
7580
:obj => event_history_collector
7681
)
7782
],
78-
:propSet => [
83+
:propSet => [
7984
RbVmomi::VIM.PropertySpec(
8085
:type => event_history_collector.class.wsdl_name,
8186
:all => false,
@@ -130,7 +135,7 @@ def parse_event(event)
130135
def publish_events(events)
131136
events.each do |event|
132137
messaging_client.publish_topic(
133-
:service => "manageiq.ems-events",
138+
:service => "manageiq.ems",
134139
:sender => ems_id,
135140
:event => event[:event_type],
136141
:payload => event
@@ -155,30 +160,14 @@ def notify_started
155160
end
156161

157162
def heartbeat
158-
if ENV["NOTIFY_SOCKET"]
159-
SdNotify.watchdog
160-
else
161-
heartbeat_file = File.join(ENV["APP_ROOT"], "tmp", "#{ENV["GUID"]}.hb")
162-
timeout = 120
163-
File.write(heartbeat_file, (Time.now.utc + timeout).to_s)
164-
end
163+
SdNotify.watchdog if ENV["NOTIFY_SOCKET"]
165164
end
166165

167166
def notify_stopping
168167
SdNotify.stopping if ENV["NOTIFY_SOCKET"]
169168
end
170169
end
171170

172-
def decrypt_env_vars
173-
require "open3"
174-
output, status = Open3.capture2("tools/decrypt_env_vars", :chdir => ENV["APP_ROOT"])
175-
176-
# Skip the ** ManageIQ master, codename: Lasker comment
177-
output = output.split("\n")[1..-1].join("\n")
178-
179-
YAML.load(output)
180-
end
181-
182171
def setproctitle
183172
proc_title = "MIQ: Vmware::InfraManager::EventCatcher guid: #{ENV["GUID"]}"
184173
Process.setproctitle(proc_title)
@@ -187,24 +176,17 @@ def setproctitle
187176
def main(args)
188177
setproctitle
189178

190-
event_catcher = EventCatcher.new(*args.values_at(:ems_id, :hostname, :username, :password, :port, :messaging_host, :messaging_port))
179+
default_endpoint = args["endpoints"]&.detect { |ep| ep["role"] == "default" }
180+
default_authentication = args["authentications"]&.detect { |auth| auth["authtype"] == "default" }
181+
182+
event_catcher = EventCatcher.new(args["ems_id"], default_endpoint, default_authentication, args["messaging_opts"])
183+
191184
event_catcher.run!
192185
end
193186

194187
def parse_args
195-
require "optimist"
196-
197-
env_vars = decrypt_env_vars
198-
199-
Optimist.options do
200-
opt :ems_id, "EMS ID", :type => :int, :default => env_vars["EMS_ID"]&.to_i, :required => env_vars["EMS_ID"].nil?
201-
opt :hostname, "Hostname", :type => :string, :default => env_vars["HOSTNAME"], :required => env_vars["HOSTNAME"].nil?
202-
opt :username, "Username", :type => :string, :default => env_vars["USERNAME"], :required => env_vars["USERNAME"].nil?
203-
opt :password, "Password", :type => :string, :default => env_vars["PASSWORD"], :required => env_vars["PASSWORD"].nil?
204-
opt :messaging_host, "Messaging Host", :type => :string, :default => env_vars["MESSAGING_HOST"], :required => env_vars["MESSAGING_HOST"].nil?
205-
opt :messaging_port, "Messaging Port", :type => :int, :default => env_vars["MESSAGING_PORT"]&.to_i, :required => env_vars["MESSAGING_PORT"].nil?
206-
opt :port, "Port", :type => :int, :default => (env_vars["PORT"] || 443).to_i
207-
end
188+
require "json"
189+
JSON.parse($stdin.read)
208190
end
209191

210192
main(parse_args)

0 commit comments

Comments
 (0)