cloulu 0.0.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/{cloulu → cl} +0 -0
- data/lib/cc_api_stub/applications.rb +53 -0
- data/lib/cc_api_stub/domains.rb +16 -0
- data/lib/cc_api_stub/frameworks.rb +22 -0
- data/lib/cc_api_stub/helper.rb +131 -0
- data/lib/cc_api_stub/login.rb +21 -0
- data/lib/cc_api_stub/organization_users.rb +21 -0
- data/lib/cc_api_stub/organizations.rb +70 -0
- data/lib/cc_api_stub/routes.rb +26 -0
- data/lib/cc_api_stub/runtimes.rb +22 -0
- data/lib/cc_api_stub/service_bindings.rb +22 -0
- data/lib/cc_api_stub/service_instances.rb +22 -0
- data/lib/cc_api_stub/services.rb +25 -0
- data/lib/cc_api_stub/spaces.rb +49 -0
- data/lib/cc_api_stub/users.rb +84 -0
- data/lib/cc_api_stub.rb +17 -0
- data/lib/cfoundry/auth_token.rb +63 -0
- data/lib/cfoundry/baseclient.rb +201 -0
- data/lib/cfoundry/chatty_hash.rb +46 -0
- data/lib/cfoundry/client.rb +46 -0
- data/lib/cfoundry/concerns/login_helpers.rb +13 -0
- data/lib/cfoundry/errors.rb +160 -0
- data/lib/cfoundry/rest_client.rb +299 -0
- data/lib/cfoundry/test_support.rb +3 -0
- data/lib/cfoundry/trace_helpers.rb +40 -0
- data/lib/cfoundry/uaaclient.rb +112 -0
- data/lib/cfoundry/upload_helpers.rb +187 -0
- data/lib/cfoundry/v1/app.rb +363 -0
- data/lib/cfoundry/v1/base.rb +72 -0
- data/lib/cfoundry/v1/client.rb +193 -0
- data/lib/cfoundry/v1/framework.rb +21 -0
- data/lib/cfoundry/v1/model.rb +178 -0
- data/lib/cfoundry/v1/model_magic.rb +129 -0
- data/lib/cfoundry/v1/runtime.rb +24 -0
- data/lib/cfoundry/v1/service.rb +39 -0
- data/lib/cfoundry/v1/service_instance.rb +32 -0
- data/lib/cfoundry/v1/service_plan.rb +19 -0
- data/lib/cfoundry/v1/user.rb +22 -0
- data/lib/cfoundry/v2/app.rb +392 -0
- data/lib/cfoundry/v2/base.rb +83 -0
- data/lib/cfoundry/v2/client.rb +138 -0
- data/lib/cfoundry/v2/domain.rb +11 -0
- data/lib/cfoundry/v2/framework.rb +14 -0
- data/lib/cfoundry/v2/model.rb +148 -0
- data/lib/cfoundry/v2/model_magic.rb +449 -0
- data/lib/cfoundry/v2/organization.rb +16 -0
- data/lib/cfoundry/v2/route.rb +15 -0
- data/lib/cfoundry/v2/runtime.rb +12 -0
- data/lib/cfoundry/v2/service.rb +19 -0
- data/lib/cfoundry/v2/service_auth_token.rb +9 -0
- data/lib/cfoundry/v2/service_binding.rb +10 -0
- data/lib/cfoundry/v2/service_instance.rb +14 -0
- data/lib/cfoundry/v2/service_plan.rb +12 -0
- data/lib/cfoundry/v2/space.rb +18 -0
- data/lib/cfoundry/v2/user.rb +64 -0
- data/lib/cfoundry/validator.rb +39 -0
- data/lib/cfoundry/version.rb +4 -0
- data/lib/cfoundry/zip.rb +56 -0
- data/lib/cfoundry.rb +2 -0
- data/lib/manifests-vmc-plugin/errors.rb +21 -0
- data/lib/manifests-vmc-plugin/loader/builder.rb +34 -0
- data/lib/manifests-vmc-plugin/loader/normalizer.rb +149 -0
- data/lib/manifests-vmc-plugin/loader/resolver.rb +79 -0
- data/lib/manifests-vmc-plugin/loader.rb +31 -0
- data/lib/manifests-vmc-plugin/plugin.rb +145 -0
- data/lib/manifests-vmc-plugin/version.rb +3 -0
- data/lib/manifests-vmc-plugin.rb +313 -0
- data/lib/mothership/base.rb +99 -0
- data/lib/mothership/callbacks.rb +85 -0
- data/lib/mothership/command.rb +146 -0
- data/lib/mothership/errors.rb +38 -0
- data/lib/mothership/help/commands.rb +53 -0
- data/lib/mothership/help/printer.rb +170 -0
- data/lib/mothership/help.rb +64 -0
- data/lib/mothership/inputs.rb +189 -0
- data/lib/mothership/parser.rb +182 -0
- data/lib/mothership/version.rb +3 -0
- data/lib/mothership.rb +64 -0
- data/lib/tunnel-vmc-plugin/plugin.rb +178 -0
- data/lib/tunnel-vmc-plugin/tunnel.rb +308 -0
- data/lib/tunnel-vmc-plugin/version.rb +3 -0
- data/lib/uaa/http.rb +168 -0
- data/lib/uaa/misc.rb +121 -0
- data/lib/uaa/scim.rb +292 -0
- data/lib/uaa/token_coder.rb +196 -0
- data/lib/uaa/token_issuer.rb +255 -0
- data/lib/uaa/util.rb +235 -0
- data/lib/uaa/version.rb +19 -0
- data/lib/uaa.rb +18 -0
- data/lib/vmc/cli/app/app.rb +45 -0
- data/lib/vmc/cli/app/apps.rb +99 -0
- data/lib/vmc/cli/app/base.rb +90 -0
- data/lib/vmc/cli/app/crashes.rb +42 -0
- data/lib/vmc/cli/app/delete.rb +95 -0
- data/lib/vmc/cli/app/deprecated.rb +11 -0
- data/lib/vmc/cli/app/env.rb +78 -0
- data/lib/vmc/cli/app/files.rb +137 -0
- data/lib/vmc/cli/app/health.rb +26 -0
- data/lib/vmc/cli/app/instances.rb +53 -0
- data/lib/vmc/cli/app/logs.rb +76 -0
- data/lib/vmc/cli/app/push/create.rb +165 -0
- data/lib/vmc/cli/app/push/interactions.rb +94 -0
- data/lib/vmc/cli/app/push/sync.rb +64 -0
- data/lib/vmc/cli/app/push.rb +109 -0
- data/lib/vmc/cli/app/rename.rb +35 -0
- data/lib/vmc/cli/app/restart.rb +20 -0
- data/lib/vmc/cli/app/scale.rb +71 -0
- data/lib/vmc/cli/app/start.rb +143 -0
- data/lib/vmc/cli/app/stats.rb +67 -0
- data/lib/vmc/cli/app/stop.rb +27 -0
- data/lib/vmc/cli/help.rb +11 -0
- data/lib/vmc/cli/interactive.rb +105 -0
- data/lib/vmc/cli/route/base.rb +12 -0
- data/lib/vmc/cli/route/map.rb +82 -0
- data/lib/vmc/cli/route/routes.rb +25 -0
- data/lib/vmc/cli/route/unmap.rb +94 -0
- data/lib/vmc/cli/service/base.rb +8 -0
- data/lib/vmc/cli/service/bind.rb +44 -0
- data/lib/vmc/cli/service/create.rb +126 -0
- data/lib/vmc/cli/service/delete.rb +86 -0
- data/lib/vmc/cli/service/rename.rb +35 -0
- data/lib/vmc/cli/service/service.rb +42 -0
- data/lib/vmc/cli/service/services.rb +114 -0
- data/lib/vmc/cli/service/unbind.rb +38 -0
- data/lib/vmc/cli/start/base.rb +94 -0
- data/lib/vmc/cli/start/colors.rb +13 -0
- data/lib/vmc/cli/start/info.rb +126 -0
- data/lib/vmc/cli/start/login.rb +97 -0
- data/lib/vmc/cli/start/logout.rb +17 -0
- data/lib/vmc/cli/start/target.rb +60 -0
- data/lib/vmc/cli/start/target_interactions.rb +37 -0
- data/lib/vmc/cli/start/targets.rb +16 -0
- data/lib/vmc/cli/user/base.rb +29 -0
- data/lib/vmc/cli/user/create.rb +39 -0
- data/lib/vmc/cli/user/delete.rb +27 -0
- data/lib/vmc/cli/user/passwd.rb +50 -0
- data/lib/vmc/cli/user/register.rb +42 -0
- data/lib/vmc/cli/user/users.rb +32 -0
- data/lib/vmc/cli/v2_check_cli.rb +16 -0
- data/lib/vmc/cli.rb +474 -0
- data/lib/vmc/constants.rb +13 -0
- data/lib/vmc/detect.rb +129 -0
- data/lib/vmc/errors.rb +19 -0
- data/lib/vmc/plugin.rb +56 -0
- data/lib/vmc/spacing.rb +89 -0
- data/lib/vmc/spec_helper.rb +1 -0
- data/lib/vmc/test_support.rb +6 -0
- data/lib/vmc/version.rb +3 -0
- data/lib/vmc.rb +8 -0
- data/vendor/errors/v1.yml +189 -0
- data/vendor/errors/v2.yml +360 -0
- metadata +303 -190
data/lib/mothership.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require "mothership/base"
|
2
|
+
require "mothership/callbacks"
|
3
|
+
require "mothership/command"
|
4
|
+
require "mothership/parser"
|
5
|
+
require "mothership/help"
|
6
|
+
require "mothership/errors"
|
7
|
+
|
8
|
+
class Mothership
|
9
|
+
# [Mothership::Command] global options
|
10
|
+
@@global = Command.new(self, "(global options)")
|
11
|
+
|
12
|
+
# [Fixnum] exit status; reassign as appropriate error code (e.g. 1)
|
13
|
+
@@exit_status = 0
|
14
|
+
|
15
|
+
class << self
|
16
|
+
# define a global option
|
17
|
+
def option(name, options = {}, &interact)
|
18
|
+
@@global.add_input(name, options, &interact)
|
19
|
+
end
|
20
|
+
|
21
|
+
# parse argv, by taking the first arg as the command, and the rest as
|
22
|
+
# arguments and flags
|
23
|
+
#
|
24
|
+
# arguments and flags can be in any order; all flags will be parsed out
|
25
|
+
# first, and the bits left over will be treated as arguments
|
26
|
+
def start(argv)
|
27
|
+
global_parser = Parser.new(@@global)
|
28
|
+
name, *argv = global_parser.parse_flags(argv, @@commands)
|
29
|
+
|
30
|
+
global = new(@@global)
|
31
|
+
global.input = Inputs.new(@@global, global, {}, global_parser.given)
|
32
|
+
|
33
|
+
return global.default_action unless name
|
34
|
+
|
35
|
+
cmdname = name.gsub("-", "_").to_sym
|
36
|
+
|
37
|
+
cmd = @@commands[cmdname]
|
38
|
+
return global.unknown_command(cmdname) unless cmd
|
39
|
+
|
40
|
+
ctx = cmd.context.new
|
41
|
+
|
42
|
+
# make global input available for those wrapping execute
|
43
|
+
ctx.input = global.input
|
44
|
+
|
45
|
+
ctx.execute(cmd, argv, global.input)
|
46
|
+
|
47
|
+
code = @@exit_status
|
48
|
+
|
49
|
+
# reset exit status
|
50
|
+
@@exit_status = 0
|
51
|
+
|
52
|
+
exit code
|
53
|
+
end
|
54
|
+
|
55
|
+
def global_option(name)
|
56
|
+
@@global.inputs[name]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# set the exit status
|
61
|
+
def exit_status(num)
|
62
|
+
@@exit_status = num
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,178 @@
|
|
1
|
+
require "vmc/cli"
|
2
|
+
require "tunnel-vmc-plugin/tunnel"
|
3
|
+
|
4
|
+
module VMCTunnel
|
5
|
+
class Tunnel < VMC::CLI
|
6
|
+
CLIENTS_FILE = "#{VMC::CONFIG_DIR}/tunnel-clients.yml"
|
7
|
+
STOCK_CLIENTS = File.expand_path("../../../config/clients.yml", __FILE__)
|
8
|
+
|
9
|
+
desc "Create a local tunnel to a service."
|
10
|
+
group :services, :manage
|
11
|
+
input(:instance, :argument => :optional,
|
12
|
+
:from_given => find_by_name("service instance"),
|
13
|
+
:desc => "Service instance to tunnel to") { |instances|
|
14
|
+
ask("Which service instance?", :choices => instances,
|
15
|
+
:display => proc(&:name))
|
16
|
+
}
|
17
|
+
input(:client, :argument => :optional,
|
18
|
+
:desc => "Client to automatically launch") { |clients|
|
19
|
+
if clients.empty?
|
20
|
+
"none"
|
21
|
+
else
|
22
|
+
ask("Which client would you like to start?",
|
23
|
+
:choices => clients.keys.unshift("none"))
|
24
|
+
end
|
25
|
+
}
|
26
|
+
input(:port, :default => 10000, :desc => "Port to bind the tunnel to")
|
27
|
+
def tunnel
|
28
|
+
instances = client.service_instances
|
29
|
+
fail "No services available for tunneling." if instances.empty?
|
30
|
+
|
31
|
+
instance = input[:instance, instances.sort_by(&:name)]
|
32
|
+
vendor = v2? ? instance.service_plan.service.label : instance.vendor
|
33
|
+
clients = tunnel_clients[vendor] || {}
|
34
|
+
client_name = input[:client, clients]
|
35
|
+
|
36
|
+
tunnel = CFTunnel.new(client, instance)
|
37
|
+
port = tunnel.pick_port!(input[:port])
|
38
|
+
|
39
|
+
conn_info =
|
40
|
+
with_progress("Opening tunnel on port #{c(port, :name)}") do
|
41
|
+
tunnel.open!
|
42
|
+
end
|
43
|
+
|
44
|
+
if client_name == "none"
|
45
|
+
unless quiet?
|
46
|
+
line
|
47
|
+
display_tunnel_connection_info(conn_info)
|
48
|
+
|
49
|
+
line
|
50
|
+
line "Open another shell to run command-line clients or"
|
51
|
+
line "use a UI tool to connect using the displayed information."
|
52
|
+
line "Press Ctrl-C to exit..."
|
53
|
+
end
|
54
|
+
|
55
|
+
tunnel.wait_for_end
|
56
|
+
else
|
57
|
+
with_progress("Waiting for local tunnel to become available") do
|
58
|
+
tunnel.wait_for_start
|
59
|
+
end
|
60
|
+
|
61
|
+
unless start_local_prog(clients, client_name, conn_info, port)
|
62
|
+
fail "'#{client_name}' execution failed; is it in your $PATH?"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def tunnel_clients
|
68
|
+
return @tunnel_clients if @tunnel_clients
|
69
|
+
stock_config = YAML.load_file(STOCK_CLIENTS)
|
70
|
+
custom_config_file = File.expand_path(CLIENTS_FILE)
|
71
|
+
if File.exists?(custom_config_file)
|
72
|
+
custom_config = YAML.load_file(custom_config_file)
|
73
|
+
@tunnel_clients = deep_merge(stock_config, custom_config)
|
74
|
+
else
|
75
|
+
@tunnel_clients = stock_config
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def display_tunnel_connection_info(info)
|
82
|
+
line "Service connection info:"
|
83
|
+
|
84
|
+
to_show = [nil, nil, nil] # reserved for user, pass, db name
|
85
|
+
info.keys.each do |k|
|
86
|
+
case k
|
87
|
+
when "host", "hostname", "port", "node_id"
|
88
|
+
# skip
|
89
|
+
when "user", "username"
|
90
|
+
# prefer "username" over "user"
|
91
|
+
to_show[0] = k unless to_show[0] == "username"
|
92
|
+
when "password"
|
93
|
+
to_show[1] = k
|
94
|
+
when "name"
|
95
|
+
to_show[2] = k
|
96
|
+
else
|
97
|
+
to_show << k
|
98
|
+
end
|
99
|
+
end
|
100
|
+
to_show.compact!
|
101
|
+
|
102
|
+
align_len = to_show.collect(&:size).max + 1
|
103
|
+
|
104
|
+
indented do
|
105
|
+
to_show.each do |k|
|
106
|
+
# TODO: modify the server services rest call to have explicit knowledge
|
107
|
+
# about the items to return. It should return all of them if
|
108
|
+
# the service is unknown so that we don't have to do this weird
|
109
|
+
# filtering.
|
110
|
+
line "#{k.ljust align_len}: #{b(info[k])}"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
line
|
115
|
+
end
|
116
|
+
|
117
|
+
def start_local_prog(clients, command, info, port)
|
118
|
+
client = clients[File.basename(command)]
|
119
|
+
|
120
|
+
cmdline = "#{command} "
|
121
|
+
|
122
|
+
case client
|
123
|
+
when Hash
|
124
|
+
cmdline << resolve_symbols(client["command"], info, port)
|
125
|
+
client["environment"].each do |e|
|
126
|
+
if e =~ /([^=]+)=(["']?)([^"']*)\2/
|
127
|
+
ENV[$1] = resolve_symbols($3, info, port)
|
128
|
+
else
|
129
|
+
fail "Invalid environment variable: #{e}"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
when String
|
133
|
+
cmdline << resolve_symbols(client, info, port)
|
134
|
+
else
|
135
|
+
raise "Unknown client info: #{client.inspect}."
|
136
|
+
end
|
137
|
+
|
138
|
+
if verbose?
|
139
|
+
line
|
140
|
+
line "Launching '#{cmdline}'"
|
141
|
+
end
|
142
|
+
|
143
|
+
system(cmdline)
|
144
|
+
end
|
145
|
+
|
146
|
+
def resolve_symbols(str, info, local_port)
|
147
|
+
str.gsub(/\$\{\s*([^\}]+)\s*\}/) do
|
148
|
+
sym = $1
|
149
|
+
|
150
|
+
case sym
|
151
|
+
when "host"
|
152
|
+
# TODO: determine proper host
|
153
|
+
"localhost"
|
154
|
+
when "port"
|
155
|
+
local_port
|
156
|
+
when "user", "username"
|
157
|
+
info["username"]
|
158
|
+
when /^ask (.+)/
|
159
|
+
ask($1)
|
160
|
+
else
|
161
|
+
info[sym] || raise("Unknown symbol in config: #{sym}")
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def deep_merge(a, b)
|
167
|
+
merge = proc { |_, old, new|
|
168
|
+
if old.is_a?(Hash) && new.is_a?(Hash)
|
169
|
+
old.merge(new, &merge)
|
170
|
+
else
|
171
|
+
new
|
172
|
+
end
|
173
|
+
}
|
174
|
+
|
175
|
+
a.merge(b, &merge)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
@@ -0,0 +1,308 @@
|
|
1
|
+
require "addressable/uri"
|
2
|
+
require "restclient"
|
3
|
+
require "uuidtools"
|
4
|
+
|
5
|
+
require "caldecott-client"
|
6
|
+
|
7
|
+
|
8
|
+
class CFTunnel
|
9
|
+
HELPER_NAME = "caldecott"
|
10
|
+
HELPER_APP = File.expand_path("../../../helper-app", __FILE__)
|
11
|
+
|
12
|
+
# bump this AND the version info reported by HELPER_APP/server.rb
|
13
|
+
# this is to keep the helper in sync with any updates here
|
14
|
+
HELPER_VERSION = "0.0.4"
|
15
|
+
|
16
|
+
def initialize(client, service, port = 10000)
|
17
|
+
@client = client
|
18
|
+
@service = service
|
19
|
+
@port = port
|
20
|
+
end
|
21
|
+
|
22
|
+
def open!
|
23
|
+
if helper
|
24
|
+
auth = helper_auth
|
25
|
+
|
26
|
+
unless helper_healthy?(auth)
|
27
|
+
delete_helper
|
28
|
+
auth = create_helper
|
29
|
+
end
|
30
|
+
else
|
31
|
+
auth = create_helper
|
32
|
+
end
|
33
|
+
|
34
|
+
bind_to_helper if @service && !helper_already_binds?
|
35
|
+
|
36
|
+
info = get_connection_info(auth)
|
37
|
+
|
38
|
+
start_tunnel(info, auth)
|
39
|
+
|
40
|
+
info
|
41
|
+
end
|
42
|
+
|
43
|
+
def wait_for_start
|
44
|
+
10.times do |n|
|
45
|
+
begin
|
46
|
+
TCPSocket.open("localhost", @port).close
|
47
|
+
return true
|
48
|
+
rescue => e
|
49
|
+
sleep 1
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
raise "Could not connect to local tunnel."
|
54
|
+
end
|
55
|
+
|
56
|
+
def wait_for_end
|
57
|
+
if @local_tunnel_thread
|
58
|
+
@local_tunnel_thread.join
|
59
|
+
else
|
60
|
+
raise "Tunnel wasn't started!"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
PORT_RANGE = 10
|
65
|
+
def pick_port!(port = @port)
|
66
|
+
original = port
|
67
|
+
|
68
|
+
PORT_RANGE.times do |n|
|
69
|
+
begin
|
70
|
+
TCPSocket.open("localhost", port)
|
71
|
+
port += 1
|
72
|
+
rescue
|
73
|
+
return @port = port
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
@port = grab_ephemeral_port
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def helper
|
83
|
+
@helper ||= @client.app_by_name(HELPER_NAME)
|
84
|
+
end
|
85
|
+
|
86
|
+
def create_helper
|
87
|
+
auth = UUIDTools::UUID.random_create.to_s
|
88
|
+
push_helper(auth)
|
89
|
+
start_helper
|
90
|
+
auth
|
91
|
+
end
|
92
|
+
|
93
|
+
def helper_auth
|
94
|
+
helper.env["CALDECOTT_AUTH"]
|
95
|
+
end
|
96
|
+
|
97
|
+
def helper_healthy?(token)
|
98
|
+
return false unless helper.healthy?
|
99
|
+
|
100
|
+
begin
|
101
|
+
response = RestClient.get(
|
102
|
+
"#{helper_url}/info",
|
103
|
+
"Auth-Token" => token
|
104
|
+
)
|
105
|
+
|
106
|
+
info = JSON.parse(response)
|
107
|
+
if info["version"] == HELPER_VERSION
|
108
|
+
true
|
109
|
+
else
|
110
|
+
stop_helper
|
111
|
+
false
|
112
|
+
end
|
113
|
+
rescue RestClient::Exception
|
114
|
+
stop_helper
|
115
|
+
false
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def helper_already_binds?
|
120
|
+
helper.binds? @service
|
121
|
+
end
|
122
|
+
|
123
|
+
def push_helper(token)
|
124
|
+
target_base = @client.target.sub(/^[^\.]+\./, "")
|
125
|
+
|
126
|
+
url = "#{random_helper_url}.#{target_base}"
|
127
|
+
is_v2 = @client.is_a?(CFoundry::V2::Client)
|
128
|
+
|
129
|
+
app = @client.app
|
130
|
+
app.name = HELPER_NAME
|
131
|
+
app.framework = @client.framework_by_name("sinatra")
|
132
|
+
app.runtime = @client.runtime_by_name("ruby19")
|
133
|
+
app.command = "bundle exec ruby server.rb -p $VCAP_APP_PORT"
|
134
|
+
app.total_instances = 1
|
135
|
+
app.memory = 64
|
136
|
+
app.env = { "CALDECOTT_AUTH" => token }
|
137
|
+
|
138
|
+
if is_v2
|
139
|
+
app.space = @client.current_space
|
140
|
+
else
|
141
|
+
app.services = [@service] if @service
|
142
|
+
app.url = url
|
143
|
+
end
|
144
|
+
|
145
|
+
app.create!
|
146
|
+
|
147
|
+
if is_v2
|
148
|
+
app.bind(@service) if @service
|
149
|
+
app.create_route(url)
|
150
|
+
end
|
151
|
+
|
152
|
+
begin
|
153
|
+
app.upload(HELPER_APP)
|
154
|
+
invalidate_tunnel_app_info
|
155
|
+
rescue
|
156
|
+
app.delete!
|
157
|
+
raise
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
def delete_helper
|
162
|
+
helper.delete!
|
163
|
+
invalidate_tunnel_app_info
|
164
|
+
end
|
165
|
+
|
166
|
+
def stop_helper
|
167
|
+
helper.stop!
|
168
|
+
invalidate_tunnel_app_info
|
169
|
+
end
|
170
|
+
|
171
|
+
TUNNEL_CHECK_LIMIT = 60
|
172
|
+
def start_helper
|
173
|
+
helper.start!
|
174
|
+
|
175
|
+
seconds = 0
|
176
|
+
until helper.healthy?
|
177
|
+
sleep 1
|
178
|
+
seconds += 1
|
179
|
+
if seconds == TUNNEL_CHECK_LIMIT
|
180
|
+
raise "Helper application failed to start."
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
invalidate_tunnel_app_info
|
185
|
+
end
|
186
|
+
|
187
|
+
def bind_to_helper
|
188
|
+
helper.bind(@service)
|
189
|
+
helper.restart!
|
190
|
+
end
|
191
|
+
|
192
|
+
def invalidate_tunnel_app_info
|
193
|
+
@helper_url = nil
|
194
|
+
@helper = nil
|
195
|
+
end
|
196
|
+
|
197
|
+
def helper_url
|
198
|
+
return @helper_url if @helper_url
|
199
|
+
|
200
|
+
tun_url = helper.url
|
201
|
+
|
202
|
+
["https", "http"].each do |scheme|
|
203
|
+
url = "#{scheme}://#{tun_url}"
|
204
|
+
begin
|
205
|
+
RestClient.get(url)
|
206
|
+
|
207
|
+
# https failed
|
208
|
+
rescue Errno::ECONNREFUSED
|
209
|
+
|
210
|
+
# we expect a 404 since this request isn't auth'd
|
211
|
+
rescue RestClient::ResourceNotFound
|
212
|
+
return @helper_url = url
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
raise "Cannot determine URL for #{tun_url}"
|
217
|
+
end
|
218
|
+
|
219
|
+
def get_connection_info(token)
|
220
|
+
response = nil
|
221
|
+
10.times do
|
222
|
+
begin
|
223
|
+
response =
|
224
|
+
RestClient.get(
|
225
|
+
helper_url + "/" + safe_path("services", @service.name),
|
226
|
+
"Auth-Token" => token)
|
227
|
+
|
228
|
+
break
|
229
|
+
rescue RestClient::Exception => e
|
230
|
+
sleep 1
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
unless response
|
235
|
+
raise "Remote tunnel helper is unaware of #{@service.name}!"
|
236
|
+
end
|
237
|
+
|
238
|
+
is_v2 = @client.is_a?(CFoundry::V2::Client)
|
239
|
+
|
240
|
+
info = JSON.parse(response)
|
241
|
+
case (is_v2 ? @service.service_plan.service.label : @service.vendor)
|
242
|
+
when "rabbitmq"
|
243
|
+
uri = Addressable::URI.parse info["url"]
|
244
|
+
info["hostname"] = uri.host
|
245
|
+
info["port"] = uri.port
|
246
|
+
info["vhost"] = uri.path[1..-1]
|
247
|
+
info["user"] = uri.user
|
248
|
+
info["password"] = uri.password
|
249
|
+
info.delete "url"
|
250
|
+
|
251
|
+
# we use "db" as the "name" for mongo
|
252
|
+
# existing "name" is junk
|
253
|
+
when "mongodb"
|
254
|
+
info["name"] = info["db"]
|
255
|
+
info.delete "db"
|
256
|
+
|
257
|
+
# our "name" is irrelevant for redis
|
258
|
+
when "redis"
|
259
|
+
info.delete "name"
|
260
|
+
|
261
|
+
when "filesystem"
|
262
|
+
raise "Tunneling is not supported for this type of service"
|
263
|
+
end
|
264
|
+
|
265
|
+
["hostname", "port", "password"].each do |k|
|
266
|
+
raise "Could not determine #{k} for #{@service.name}" if info[k].nil?
|
267
|
+
end
|
268
|
+
|
269
|
+
info
|
270
|
+
end
|
271
|
+
|
272
|
+
def start_tunnel(conn_info, auth)
|
273
|
+
@local_tunnel_thread = Thread.new do
|
274
|
+
Caldecott::Client.start({
|
275
|
+
:local_port => @port,
|
276
|
+
:tun_url => helper_url,
|
277
|
+
:dst_host => conn_info["hostname"],
|
278
|
+
:dst_port => conn_info["port"],
|
279
|
+
:log_file => STDOUT,
|
280
|
+
:log_level => ENV["VMC_TUNNEL_DEBUG"] || "ERROR",
|
281
|
+
:auth_token => auth,
|
282
|
+
:quiet => true
|
283
|
+
})
|
284
|
+
end
|
285
|
+
|
286
|
+
at_exit { @local_tunnel_thread.kill }
|
287
|
+
end
|
288
|
+
|
289
|
+
def random_helper_url
|
290
|
+
random = sprintf("%x", rand(1000000))
|
291
|
+
"caldecott-#{random}"
|
292
|
+
end
|
293
|
+
|
294
|
+
def safe_path(*segments)
|
295
|
+
segments.flatten.collect { |x|
|
296
|
+
URI.encode x.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")
|
297
|
+
}.join("/")
|
298
|
+
end
|
299
|
+
|
300
|
+
def grab_ephemeral_port
|
301
|
+
socket = TCPServer.new("0.0.0.0", 0)
|
302
|
+
socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
|
303
|
+
Socket.do_not_reverse_lookup = true
|
304
|
+
socket.addr[1]
|
305
|
+
ensure
|
306
|
+
socket.close
|
307
|
+
end
|
308
|
+
end
|