This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
/
vcap_components.rb
421 lines (348 loc) · 9.91 KB
/
vcap_components.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# Copyright (c) 2009-2011 VMware, Inc.
#
require 'yaml'
require 'fileutils'
require File.expand_path("./vcap_common.rb", File.dirname(__FILE__))
class Component
@@named_components = {}
attr :name
def self.create(name, configuration_file=nil)
sub_class = @@named_components[name]
if sub_class
sub_class.new(name, configuration_file)
else
nil
end
end
def self.register(name)
@@named_components[name] = self
end
def self.getNamedComponents()
@@named_components
end
def initialize(name, configuration_file = nil)
@name = name
@configuration_path = configuration_file || get_configuration_path
end
def is_cloud_controller?
@name =~ /cloud_controller/i
end
def is_router?
@name =~ /router/i
end
def to_s
name
end
def exists?
File.exists? get_path
end
def vcap_bin
DIR
end
def get_path
@path ||= File.join(vcap_bin, name)
end
def configuration_file_in
$config_dir ||= ENV['CLOUD_FOUNDRY_CONFIG_PATH']
$config_dir
end
def get_configuration_path
if @configuration_path.nil?
if configuration_file_in
@configuration_path = File.join(configuration_file_in, "#{name}.yml")
else
@configuration_path = File.join(vcap_bin, "..", name, "config", "#{name}.yml")
end
end
@configuration_path
end
def configuration
@configuration ||= YAML.load(File.read(get_configuration_path))
end
def pid_file
configuration["pid"] || raise("#{get_configuration_path} does not specify location of pid file")
end
def log_file?
!configuration["log_file"].nil?
end
def log_file
log_file = configuration["log_file"]
log_file || File.join($log_dir, "#{name}.log")
end
def pid
if File.exists?(pid_file)
body = File.read(pid_file)
body.to_i if body
end
end
def running?
running = false
# Only evaluate 'pid' once per call to 'running?'
if procid = pid
running = `ps -o rss= -p #{procid}`.length > 0
end
running
end
def component_start_path
exec_path = (get_path).dup
exec_path << " -c #{get_configuration_path}"
if is_router? && $port
exec_path << " -p #{$port}"
end
exec_path
end
def start
if !running?
pid = fork do
# Capture STDOUT when no log file is configured
if !log_file?
stdout = File.open(log_file, 'a')
stdout.truncate(0)
STDOUT.reopen(stdout)
stderr = File.open(log_file, 'a')
STDERR.reopen(stderr)
end
# Make sure db is setup, this is slow and we should make it faster, but
# should help for now.
if is_cloud_controller?
cc_dir = File.expand_path(File.join($vcap_home, 'cloud_controller', 'cloud_controller'))
Dir.chdir(cc_dir) { `bundle exec rake db:migrate` }
end
exec("#{component_start_path}")
end
Process.detach(pid)
start = Time.now
while ((Time.now - start) < 20)
break if running?
sleep (0.25)
end
end
status
if !running?
if File.exists?(log_file)
log = File.read(log_file)
STDERR.puts "LOG:\n #{log}" if !log.empty?
end
end
end
def stop
return status unless running?
kill = "kill -TERM #{pid}"
`#{kill} 2> /dev/null`
if $? != 0
STDERR.puts "#{'Failed'.red} to stop #{name}, possible permission problem\?"
return
end
# Return status if we succeeded in stopping
return status unless running?
if running?
sleep(0.25)
if running?
kill = "kill -9 #{pid}"
`#{kill} 2> /dev/null`
end
end
status
end
def status
status = running? ? 'RUNNING'.green : 'STOPPED'.red
puts "#{name.ljust(30)}:\t #{status}"
end
end
class CoreComponent < Component
def core?
true
end
def initialize(name, configuration_file = nil)
@path ||= File.join($vcap_home, name, "bin", name)
super
end
end
class StagerComponent < CoreComponent
def pid_file
configuration["pid_filename"] || raise("#{get_configuration_path} does not specify location of pid file")
end
end
class UAAComponent < CoreComponent
def initialize(*args)
@path = File.join($vcap_home, "bin", "uaa")
super
end
end
class ACMComponent < CoreComponent
def initialize(*args)
@path = File.join($vcap_home, "bin", "acm")
super
end
end
class CCComponent < CoreComponent
def initialize(*args)
@path = File.join($vcap_home, "cloud_controller", "cloud_controller", "bin", "cloud_controller")
super
end
end
class HMComponent < CoreComponent
def initialize(*args)
@path = File.join($vcap_home, "cloud_controller", "health_manager", "bin", "health_manager")
super
end
end
class ServicesRedisComponent < Component
def services_redis?
true
end
def get_path
return @path if @path
if $config_dir.nil?
raise "Fail to get path of redis-server for ServicesRedisComponent #{name}"
end
@path = File.join($config_dir, "..", "deploy", "redis", "bin", "redis-server")
end
def get_configuration_path
return @configuration_path if @configuration_path
if $config_dir.nil?
raise "Fail to get configuration file of redis-server for ServicesRedisComponent #{name}"
end
@configuration_path = File.join($config_dir, "#{name}.conf")
end
def component_start_path
exec_path = "#{get_path.dup} #{get_configuration_path}"
end
def log_file?
config_file = get_configuration_path
ret = `cat #{config_file} | grep logfile`.strip
if ret.nil? || ret.empty?
`echo "logfile #{log_file}" >> #{config_file}`
end
super
end
def pid_file
config_file = get_configuration_path
ret = `cat #{config_file} | grep pidfile | awk '{print $2}'`.strip
if ret.nil? || ret.empty?
raise("#{get_configuration_path} does not specify location of pid file")
end
ret
end
end
class ServiceComponent < Component
def service?
true
end
def node?
@name =~ /_node/i
end
def gateway?
@name =~ /_gateway/i
end
def worker?
@name =~ /_worker/i
end
def pid_file
return super unless worker?
pre = name.sub(/_worker/, '')
tmp_node_pid_file = Component.create("#{pre}_node").pid_file
pid_dir = File.dirname(tmp_node_pid_file)
return File.join(pid_dir, "#{name}.pid")
end
def get_path
return @path if @path
pre = name.sub(/_node|_gateway|_worker/,'')
# mapping 'rabbitmq' in dev_setup to 'rabbit' in services
pre = 'rabbit' if pre == 'rabbitmq'
bin_name = name.index('rabbitmq')? name.sub(/mq/, '') : name
@path = File.join(vcap_bin, "../services", pre, "bin", bin_name)
end
def get_configuration_path
@configuration_path ||= (File.join(configuration_file_in, "#{name}.yml") if configuration_file_in)
unless @configuration_path && File.exist?(@configuration_path)
pre = name.sub(/_node|_gateway|_worker/,'')
# mapping 'rabbitmq' in dev_setup to 'rabbit' in services
pre = 'rabbit' if pre == 'rabbitmq'
@configuration_path = File.join(vcap_bin, "../services", pre, "config", "#{name}.yml")
end
@configuration_path
end
def component_start_path
return super unless worker?
# FIXME
# only support single worker
exec_path = (get_path).dup
config_dir_path = File.dirname(get_configuration_path)
exec_path = "env CLOUD_FOUNDRY_CONFIG_PATH=#{config_dir_path} PIDFILE=#{pid_file} #{exec_path} 1"
return exec_path
end
end
class ServiceAuxiliaryComponent < Component
def service_auxiliary?
true
end
def get_path
@path ||= File.join(vcap_bin, "../services", name, "bin", name)
end
def get_configuration_path
@configuration_path ||= (File.join(configuration_file_in, "#{name}.yml") if configuration_file_in)
unless @configuration_path && File.exist?(@configuration_path)
@configuration_path = File.join(vcap_bin, "../services", name, "config", "#{name}.yml")
end
@configuration_path
end
end
class ServiceToolComponent < Component
def service_tool?
true
end
def get_path
return @path if @path
if name =~ /backup_manager|snapshot_manager/
@path = File.join(vcap_bin, "../services", "tools", "backup", "manager", "bin", name)
else
@path = File.join(vcap_bin, "../services", "tools", name, "bin", name)
end
@path
end
def get_configuration_path
@configuration_path ||= (File.join(configuration_file_in, "#{name}.yml") if configuration_file_in)
unless @configuration_path && File.exist?(@configuration_path)
if name =~ /backup_manager|snapshot_manager/
@configuration_path = File.join(vcap_bin, "../services", "tools", "backup", "manager", "config", "#{name}.yml")
else
@configuration_path = File.join(vcap_bin, "../services", "tools", name, "config", "#{name}.yml")
end
end
@configuration_path
end
end
# register valid named components
## core
%w(router dea).each do |core|
CoreComponent.register(core)
end
StagerComponent.register("stager")
ACMComponent.register("acm")
UAAComponent.register("uaa")
CCComponent.register("cloud_controller")
HMComponent.register("health_manager")
## standalone
%w(services_redis).each do |redis|
ServicesRedisComponent.register(redis)
end
## services: gateways & nodes
%w(redis mysql mongodb rabbitmq postgresql vblob neo4j memcached couchdb elasticsearch filesystem echo).each do |service|
ServiceComponent.register("#{service}_gateway")
end
%w(redis mysql mongodb rabbitmq postgresql vblob neo4j memcached couchdb elasticsearch echo).each do |service|
ServiceComponent.register("#{service}_node")
end
%w(redis mysql mongodb postgresql).each do |service|
ServiceComponent.register("#{service}_worker")
end
ServiceComponent.register("serialization_data_server")
## service auxiliary
%w(service_broker).each do |auxiliary|
ServiceAuxiliaryComponent.register(auxiliary)
end
## service tools
%w(backup_manager snapshot_manager).each do |tool|
ServiceToolComponent.register(tool)
end