-
Notifications
You must be signed in to change notification settings - Fork 827
/
uaa
executable file
·119 lines (95 loc) · 4.03 KB
/
uaa
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
#!/usr/bin/env ruby
# Cloud Foundry 2012.02.03 Beta
# Copyright (c) [2009-2012] VMware, Inc. All Rights Reserved.
#
# This product is licensed to you under the Apache License, Version 2.0 (the "License").
# You may not use this product except in compliance with the License.
#
# This product includes a number of subcomponents with
# separate copyright notices and license terms. Your use of these
# subcomponents is subject to the terms and conditions of the
# subcomponent's license, as noted in the LICENSE file.
#
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile", __FILE__)
require "bundler"
Bundler.setup
require 'yaml'
require 'nats/client'
require 'vcap/common'
require 'vcap/logging'
require 'rest_client'
VCAP::Logging.setup_from_config()
logger = VCAP::Logging.logger('uaa')
root=File.absolute_path(File.join(File.dirname(__FILE__), ".."))
if File.exist?(root) then
logger.info("Using root=#{root}")
else
abort("#{root} is not a directory. Do you need to init submodules?")
end
cfg_path = ENV['CLOUD_FOUNDRY_CONFIG_PATH'] ? ENV['CLOUD_FOUNDRY_CONFIG_PATH'] : File.join(root, "config")
logger.info("Config path: #{cfg_path}")
cfg_file = File.join(cfg_path, "uaa.yml")
config = {"uaa"=>{}, "varz"=>{}}
File.exist?(cfg_file) && File.open(cfg_file) do |f|
logger.info("Loading config file from #{cfg_file}")
config.update(YAML.load(f))
end
config["varz"]["username"] ||= VCAP.secure_uuid
config["varz"]["password"] ||= VCAP.secure_uuid
logger.info("Launching tomcat from PID=#{Process.pid}")
maven_opts = "-DPID=$$"
config["jvm_args"] && maven_opts += " " + config["jvm_args"]
config["spring_profiles"] && maven_opts += " -Dspring.profiles.active=" + config["spring_profiles"]
maven_opts += """ -Dvarz.username=#{config["varz"]["username"]} -Dvarz.password=#{config["varz"]["password"]}"""
logger.info("Tomcat JVM args: #{maven_opts}")
# The form of this next bit is very sensitive. We need the PID for
# logging so we need to exec from the shell to stay in the same
# process...
pid = spawn("export MAVEN_OPTS=\"#{maven_opts}\" && exec mvn tomcat:run -P vcap", {:chdir=>root})
logger.info("Tomcat PID = #{pid}")
pidfile = config["pid"]
if !pidfile then
Process.kill("ABRT", pid)
abort("No 'pid' file path defined in #{cfg_file}")
end
File.open(pidfile, 'w') {|f| f.write(pid) }
logger.info("Preparing to launch NATS from #{pid}")
router_msg = { :host => config["uaa"]["host"]||"localhost", :port => config["uaa"]["port"]||8080, :uris => config["uaa"]["uris"]||["uaa.vcap.me"], :tags => {:component => "UAA"} }
router_json = Yajl::Encoder.encode(router_msg)
discover_msg = { :type => 'UAA',
:host => "%s:%s" % [config["uaa"]["host"]||"localhost", config["uaa"]["port"]||8080],
:index => 0,
:credentials => [config["varz"]["username"], config["varz"]["password"]]
}
discover_json = Yajl::Encoder.encode(discover_msg)
logger.info("Sending Router info to NATS: json="+router_json)
mbus = config["mbus"] || "nats://localhost:4222/"
Thread.new do
max_wait = 30
uaa_is_up = false
start_time = Time.now
logger.info("Waiting for uaa to start within #{max_wait} seconds")
begin
response = RestClient.get "#{discover_msg[:host]}/login", accept: "application/json",
open_timeout: 2, timeout: 2
uaa_is_up = response.code == 200 && response.body =~ /prompts/
rescue Exception => e
logger.info "waiting for uaa to start: #{e}"
sleep 2
end while !uaa_is_up && Time.now - start_time < max_wait
# NATS will block and we want to wait for the tomcat process so this
# has to go in a new thread
logger.info("UAA started, registering with NATS")
NATS.start(:uri => mbus) do
NATS.subscribe('vcap.component.discover') do |msg, reply|
NATS.publish(reply, discover_json)
end
# Also announce ourselves on startup..
NATS.publish('vcap.component.announce', discover_json)
# Tell all current routers where to find us.
NATS.publish('router.register', router_json)
# Listen for router starts/restarts
NATS.subscribe('router.start') { NATS.publish('router.register', router_json) }
end
end
Process.wait(pid)