jdc 0.2.1 → 0.2.2.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. data/LICENSE +1277 -24
  2. data/Rakefile +13 -0
  3. data/bin/jdc +12 -2
  4. data/lib/admin/README.md +15 -0
  5. data/lib/admin/curl.rb +60 -0
  6. data/lib/admin/guid.rb +89 -0
  7. data/lib/admin/plugin.rb +6 -0
  8. data/lib/admin/service_auth_token.rb +94 -0
  9. data/lib/admin/service_broker/add.rb +47 -0
  10. data/lib/admin/service_broker/service_brokers.rb +24 -0
  11. data/lib/admin/set_quota.rb +44 -0
  12. data/lib/console/README.md +8 -0
  13. data/lib/console/console.rb +187 -0
  14. data/lib/console/plugin.rb +33 -0
  15. data/lib/jdc.rb +15 -2
  16. data/lib/jdc/cli.rb +556 -0
  17. data/lib/jdc/cli/app/app.rb +43 -0
  18. data/lib/jdc/cli/app/apps.rb +87 -0
  19. data/lib/jdc/cli/app/base.rb +72 -0
  20. data/lib/jdc/cli/app/delete.rb +95 -0
  21. data/lib/jdc/cli/app/deprecated.rb +11 -0
  22. data/lib/jdc/cli/app/env.rb +78 -0
  23. data/lib/jdc/cli/app/events.rb +45 -0
  24. data/lib/jdc/cli/app/files.rb +137 -0
  25. data/lib/jdc/cli/app/health.rb +26 -0
  26. data/lib/jdc/cli/app/instances.rb +53 -0
  27. data/lib/jdc/cli/app/logs.rb +76 -0
  28. data/lib/jdc/cli/app/push.rb +103 -0
  29. data/lib/jdc/cli/app/push/create.rb +108 -0
  30. data/lib/jdc/cli/app/push/interactions.rb +86 -0
  31. data/lib/jdc/cli/app/push/sync.rb +57 -0
  32. data/lib/jdc/cli/app/rename.rb +35 -0
  33. data/lib/jdc/cli/app/restart.rb +31 -0
  34. data/lib/jdc/cli/app/scale.rb +63 -0
  35. data/lib/jdc/cli/app/start.rb +161 -0
  36. data/lib/jdc/cli/app/stats.rb +67 -0
  37. data/lib/jdc/cli/app/stop.rb +27 -0
  38. data/lib/jdc/cli/domain/base.rb +9 -0
  39. data/lib/jdc/cli/domain/domains.rb +40 -0
  40. data/lib/jdc/cli/domain/map.rb +55 -0
  41. data/lib/jdc/cli/domain/unmap.rb +56 -0
  42. data/lib/jdc/cli/help.rb +15 -0
  43. data/lib/jdc/cli/interactive.rb +105 -0
  44. data/lib/jdc/cli/login_requirements.rb +15 -0
  45. data/lib/jdc/cli/organization/base.rb +14 -0
  46. data/lib/jdc/cli/organization/create.rb +37 -0
  47. data/lib/jdc/cli/organization/delete.rb +63 -0
  48. data/lib/jdc/cli/organization/org.rb +45 -0
  49. data/lib/jdc/cli/organization/orgs.rb +30 -0
  50. data/lib/jdc/cli/organization/rename.rb +37 -0
  51. data/lib/jdc/cli/populators/base.rb +16 -0
  52. data/lib/jdc/cli/populators/organization.rb +32 -0
  53. data/lib/jdc/cli/populators/populator_methods.rb +64 -0
  54. data/lib/jdc/cli/populators/space.rb +33 -0
  55. data/lib/jdc/cli/populators/target.rb +13 -0
  56. data/lib/jdc/cli/route/base.rb +9 -0
  57. data/lib/jdc/cli/route/delete.rb +28 -0
  58. data/lib/jdc/cli/route/map.rb +68 -0
  59. data/lib/jdc/cli/route/routes.rb +26 -0
  60. data/lib/jdc/cli/route/unmap.rb +56 -0
  61. data/lib/jdc/cli/service/base.rb +9 -0
  62. data/lib/jdc/cli/service/bind.rb +44 -0
  63. data/lib/jdc/cli/service/create.rb +159 -0
  64. data/lib/jdc/cli/service/delete.rb +83 -0
  65. data/lib/jdc/cli/service/rename.rb +36 -0
  66. data/lib/jdc/cli/service/service.rb +42 -0
  67. data/lib/jdc/cli/service/service_instance_helper.rb +99 -0
  68. data/lib/jdc/cli/service/services.rb +111 -0
  69. data/lib/jdc/cli/service/unbind.rb +37 -0
  70. data/lib/jdc/cli/space/base.rb +29 -0
  71. data/lib/jdc/cli/space/create.rb +67 -0
  72. data/lib/jdc/cli/space/delete.rb +56 -0
  73. data/lib/jdc/cli/space/rename.rb +38 -0
  74. data/lib/jdc/cli/space/space.rb +66 -0
  75. data/lib/jdc/cli/space/spaces.rb +57 -0
  76. data/lib/jdc/cli/space/switch.rb +19 -0
  77. data/lib/jdc/cli/start/base.rb +41 -0
  78. data/lib/jdc/cli/start/colors.rb +13 -0
  79. data/lib/jdc/cli/start/target.rb +50 -0
  80. data/lib/jdc/cli/start/target_prettifier.rb +17 -0
  81. data/lib/jdc/cli/start/targets.rb +16 -0
  82. data/lib/jdc/cli/user/base.rb +30 -0
  83. data/lib/jdc/cli/user/create.rb +52 -0
  84. data/lib/jdc/cli/user/passwd.rb +37 -0
  85. data/lib/jdc/cli/user/register.rb +43 -0
  86. data/lib/jdc/cli/user/users.rb +32 -0
  87. data/lib/jdc/constants.rb +11 -0
  88. data/lib/jdc/errors.rb +19 -0
  89. data/lib/jdc/object_extensions.rb +15 -0
  90. data/lib/jdc/plugin.rb +56 -0
  91. data/lib/jdc/spacing.rb +89 -0
  92. data/lib/jdc/spec_helper.rb +1 -0
  93. data/lib/jdc/test_support.rb +6 -0
  94. data/lib/jdc/version.rb +3 -0
  95. data/lib/manifests/errors.rb +35 -0
  96. data/lib/manifests/loader.rb +31 -0
  97. data/lib/manifests/loader/builder.rb +39 -0
  98. data/lib/manifests/loader/normalizer.rb +145 -0
  99. data/lib/manifests/loader/resolver.rb +79 -0
  100. data/lib/manifests/manifests.rb +344 -0
  101. data/lib/manifests/plugin.rb +140 -0
  102. data/lib/micro/README.md +9 -0
  103. data/lib/micro/errors.rb +4 -0
  104. data/lib/{jdc → micro}/micro.rb +15 -15
  105. data/lib/micro/plugin.rb +197 -0
  106. data/lib/micro/switcher/base.rb +79 -0
  107. data/lib/{jdc/micro → micro}/switcher/darwin.rb +5 -3
  108. data/lib/{jdc/micro → micro}/switcher/dummy.rb +1 -1
  109. data/lib/micro/switcher/linux.rb +16 -0
  110. data/lib/{jdc/micro → micro}/switcher/windows.rb +5 -5
  111. data/lib/{jdc/micro → micro}/vmrun.rb +26 -19
  112. data/lib/tasks/gem_release.rake +42 -0
  113. data/lib/tunnel/README.md +29 -0
  114. data/{config → lib/tunnel/config}/clients.yml +2 -2
  115. data/lib/tunnel/helper-app/Gemfile +10 -0
  116. data/lib/tunnel/helper-app/Gemfile.lock +48 -0
  117. data/{caldecott_helper → lib/tunnel/helper-app}/server.rb +5 -5
  118. data/lib/tunnel/plugin.rb +183 -0
  119. data/lib/tunnel/tunnel.rb +295 -0
  120. metadata +371 -210
  121. data/README.md +0 -102
  122. data/config/micro/paths.yml +0 -22
  123. data/config/micro/refresh_ip.rb +0 -20
  124. data/lib/cli.rb +0 -53
  125. data/lib/cli/commands/admin.rb +0 -58
  126. data/lib/cli/commands/apps.rb +0 -1129
  127. data/lib/cli/commands/base.rb +0 -228
  128. data/lib/cli/commands/manifest.rb +0 -56
  129. data/lib/cli/commands/micro.rb +0 -115
  130. data/lib/cli/commands/misc.rb +0 -126
  131. data/lib/cli/commands/services.rb +0 -178
  132. data/lib/cli/commands/user.rb +0 -14
  133. data/lib/cli/config.rb +0 -173
  134. data/lib/cli/console_helper.rb +0 -170
  135. data/lib/cli/core_ext.rb +0 -122
  136. data/lib/cli/errors.rb +0 -19
  137. data/lib/cli/frameworks.rb +0 -265
  138. data/lib/cli/manifest_helper.rb +0 -300
  139. data/lib/cli/runner.rb +0 -505
  140. data/lib/cli/services_helper.rb +0 -84
  141. data/lib/cli/tunnel_helper.rb +0 -332
  142. data/lib/cli/usage.rb +0 -86
  143. data/lib/cli/version.rb +0 -7
  144. data/lib/cli/zip_util.rb +0 -77
  145. data/lib/jdc/client.rb +0 -457
  146. data/lib/jdc/const.rb +0 -25
  147. data/lib/jdc/micro/switcher/base.rb +0 -97
  148. data/lib/jdc/micro/switcher/linux.rb +0 -16
  149. data/lib/jdc/signature/version.rb +0 -27
  150. data/lib/jdc/signer.rb +0 -13
  151. data/lib/jdc/timer.rb +0 -12
@@ -0,0 +1,33 @@
1
+ require "jdc/plugin"
2
+ require "console/console"
3
+ =begin
4
+ module JDCConsolePlugin
5
+ class Console < JDC::CLI
6
+ desc "Open a console connected to your app"
7
+ group :apps, :manage
8
+ input :app, :argument => :required, :from_given => by_name("app"),
9
+ :desc => "App to connect to"
10
+ input :port, :default => 10000
11
+ def console
12
+ app = input[:app]
13
+
14
+ console = JDCConsole.new(client, app)
15
+ port = console.pick_port!(input[:port])
16
+
17
+ with_progress("Opening console on port #{c(port, :name)}") do
18
+ console.open!
19
+ console.wait_for_start
20
+ end
21
+
22
+ console.start_console
23
+ end
24
+
25
+ ::ManifestsPlugin.default_to_app_from_manifest :console, true
26
+
27
+ filter(:start, :start_app) do |app|
28
+ app.console = true
29
+ app
30
+ end
31
+ end
32
+ end
33
+ =end
data/lib/jdc.rb CHANGED
@@ -1,3 +1,16 @@
1
- module JDC; end
1
+ require "jdc/version"
2
2
 
3
- require 'jdc/client'
3
+ require "jdc/object_extensions"
4
+
5
+ require "jdc/cli"
6
+
7
+ command_files = "../jdc/cli/{app,route,domain,organization,space,service,start,user}/*.rb"
8
+ Dir[File.expand_path(command_files, __FILE__)].each do |file|
9
+ require file unless File.basename(file) == 'base.rb'
10
+ end
11
+
12
+ require "manifests/plugin"
13
+ require "admin/plugin"
14
+ require "console/plugin"
15
+ #require "tunnel/plugin"
16
+ require "micro/plugin"
data/lib/jdc/cli.rb ADDED
@@ -0,0 +1,556 @@
1
+ require "yaml"
2
+ require "socket"
3
+ require "net/http"
4
+ require "multi_json"
5
+ require "fileutils"
6
+
7
+ require "mothership"
8
+
9
+ require "jfoundry"
10
+
11
+ require "jdc/constants"
12
+ require "jdc/errors"
13
+ require "jdc/spacing"
14
+
15
+ require "jdc/cli/help"
16
+ require "jdc/cli/interactive"
17
+ require "jdc/cli/login_requirements"
18
+
19
+
20
+ $cf_asked_auth = false
21
+
22
+ module JDC
23
+ class CLI < Mothership
24
+ include JDC::Interactive
25
+ include JDC::Spacing
26
+
27
+ option :help, :desc => "Show command usage", :alias => "-h",
28
+ :default => false
29
+
30
+ option :http_proxy, :desc => "Connect though an http proxy server", :alias => "--http-proxy",
31
+ :value => :http_proxy
32
+
33
+ option :https_proxy, :desc => "Connect though an https proxy server", :alias => "--https-proxy",
34
+ :value => :https_proxy
35
+
36
+ option :version, :desc => "Print version number", :alias => "-v",
37
+ :default => false
38
+
39
+ option :verbose, :desc => "Print extra information", :alias => "-V",
40
+ :default => false
41
+
42
+ option :force, :desc => "Skip interaction when possible", :alias => "-f",
43
+ :type => :boolean, :default => proc { input[:script] }
44
+
45
+ option :debug, :desc => "Print full stack trace (instead of crash log)",
46
+ :type => :boolean, :default => false
47
+
48
+ option :quiet, :desc => "Simplify output format", :alias => "-q",
49
+ :type => :boolean, :default => proc { input[:script] }
50
+
51
+ option :script, :desc => "Shortcut for --quiet and --force",
52
+ :type => :boolean, :default => proc { !$stdout.tty? }
53
+
54
+ option :color, :desc => "Use colorful output",
55
+ :type => :boolean, :default => proc { !input[:quiet] }
56
+
57
+ option :trace, :desc => "Show API traffic", :alias => "-t",
58
+ :default => false
59
+
60
+
61
+ def default_action
62
+ if input[:version]
63
+ line "jdc #{VERSION}"
64
+ else
65
+ super
66
+ end
67
+ end
68
+
69
+ def check_key
70
+ unless ENV["ACCESS_KEY"]
71
+ fail "Please set the env ACCESS_KEY."
72
+ end
73
+
74
+ unless ENV["SECRET_KEY"]
75
+ fail "Please set the env SECRET_KEY."
76
+ end
77
+ end
78
+
79
+ def check_target
80
+ unless client && client.target
81
+ fail "Please select a target with 'jdc target'."
82
+ end
83
+ end
84
+
85
+ def check_organization_and_space
86
+ info = target_info(client.target)
87
+
88
+ info_changed = false
89
+ if (info.key? :organization) && info[:access_key] != ENV['ACCESS_KEY']
90
+ info.delete(:organization)
91
+ client.current_organization = nil
92
+ info_changed = true
93
+ end
94
+
95
+ if (info.key? :space) && info[:access_key] != ENV['ACCESS_KEY']
96
+ info.delete(:space)
97
+ client.current_space = nil
98
+ info_changed = true
99
+ end
100
+
101
+ if info[:access_key] != ENV['ACCESS_KEY']
102
+ info[:access_key] = ENV['ACCESS_KEY']
103
+ info_changed = true
104
+ end
105
+
106
+ if info_changed
107
+ save_target_info(info, client.target)
108
+ end
109
+
110
+ unless info.key? :organization
111
+ client.current_organization = JDC::Populators::Organization.new(input).populate_and_save!
112
+ client.current_space = JDC::Populators::Space.new(input, client.current_organization).populate_and_save!
113
+ end
114
+ end
115
+
116
+ def check_logged_in
117
+ check_target
118
+ unless client.logged_in?
119
+ if force?
120
+ fail "Please log in with 'jdc login'."
121
+ else
122
+ line c("Please log in first to proceed.", :warning)
123
+ line
124
+ invoke :login
125
+ invalidate_client
126
+ end
127
+ end
128
+ end
129
+
130
+ desc "Help!"
131
+ input :command, :argument => :optional
132
+ def help
133
+ if name = input[:command]
134
+ if cmd = @@commands[name.gsub("-", "_").to_sym]
135
+ Mothership::Help.command_help(cmd)
136
+ else
137
+ unknown_command(name)
138
+ end
139
+ elsif Help.has_groups?
140
+ puts "#{help_header}"
141
+
142
+ Mothership::Help.print_help_groups(@@global, true)
143
+ else
144
+ Mothership::Help.basic_help(@@commands, @@global)
145
+ end
146
+ end
147
+
148
+ def help_header
149
+ <<EOS
150
+ Jing Dong Foundry Command Line Interface, version [#{JDC::VERSION}]
151
+ Use 'jdc help [command]' for command details.
152
+ For docs and support visit http://appengine.jd.com/help/jdc.html
153
+
154
+ USAGE EXAMPLES
155
+ $ jdc target api.jd-app.com <-- sets the JDC instance target where your app will be pushed
156
+ $ jdc push <-- deploys app to current app space on current target
157
+ $ jdc target -s staging <-- changes currently targeted app space to 'staging'
158
+
159
+ EOS
160
+ end
161
+
162
+ def wrap_errors
163
+ yield
164
+ rescue JFoundry::Timeout => e
165
+ err(e.message)
166
+ rescue Interrupt
167
+ exit_status 130
168
+ rescue Mothership::Error
169
+ raise
170
+ rescue UserError => e
171
+ log_error(e)
172
+ err e.message
173
+ rescue SystemExit
174
+ raise
175
+ rescue UserFriendlyError => e
176
+ err e.message
177
+ =begin
178
+ rescue JFoundry::InvalidAuthToken => e
179
+ line
180
+ line c("Invalid authentication token. Try logging in again with 'cf login'. If problems continue, please contact your Cloud Operator.", :warning)
181
+ rescue JFoundry::Forbidden => e
182
+ if !$cf_asked_auth
183
+ $cf_asked_auth = true
184
+
185
+ line
186
+ line c("Not authenticated! Try logging in:", :warning)
187
+
188
+ # TODO: there's no color here; global flags not being passed
189
+ # through (mothership bug?)
190
+ invoke :login
191
+
192
+ retry
193
+ end
194
+
195
+ log_error(e)
196
+
197
+ err "Denied: #{e.description}"
198
+ =end
199
+ rescue JFoundry::StagingError => e
200
+ message = "Application failed to stage"
201
+ formatted_exception_output(e, message)
202
+
203
+ rescue Exception => e
204
+ formatted_exception_output(e, add_exception_name_to_msg(e))
205
+ end
206
+
207
+ def formatted_exception_output(e, msg)
208
+ log_error(e)
209
+ msg << "\ncat #{JDC::CRASH_FILE} # for more details"
210
+ err msg
211
+
212
+ raise if debug?
213
+ end
214
+
215
+ def add_exception_name_to_msg(e)
216
+ msg = e.class.name
217
+ msg << ": #{e}" unless e.to_s.empty?
218
+ msg
219
+ end
220
+
221
+ def execute(cmd, argv, global = {})
222
+ if input[:help]
223
+ invoke :help, :command => cmd.name.to_s
224
+ else
225
+ wrap_errors do
226
+ @command = cmd
227
+ precondition if respond_to? :precondition
228
+
229
+ save_token_if_it_changes do
230
+ super
231
+ end
232
+ end
233
+ end
234
+ end
235
+
236
+ def save_token_if_it_changes
237
+ return yield unless client
238
+ yield
239
+ =begin
240
+ return yield unless client && client.token
241
+
242
+ before_token = client.token
243
+
244
+ yield
245
+
246
+ after_token = client.token
247
+
248
+ return unless after_token
249
+
250
+ if before_token != after_token
251
+ info = target_info
252
+ info[:token] = after_token.auth_header
253
+ save_target_info(info)
254
+ end
255
+ =end
256
+ end
257
+
258
+ def log_error(e)
259
+ ensure_config_dir
260
+
261
+ msg = e.class.name
262
+ msg << ": #{e}" unless e.to_s.empty?
263
+
264
+ crash_file = File.expand_path(JDC::CRASH_FILE)
265
+
266
+ FileUtils.mkdir_p(File.dirname(crash_file))
267
+
268
+ File.open(crash_file, "w") do |f|
269
+ f.puts "Time of crash:"
270
+ f.puts " #{Time.now}"
271
+ f.puts ""
272
+ f.puts msg
273
+ f.puts ""
274
+
275
+ if e.respond_to?(:request_trace)
276
+ f.puts "<<<"
277
+ f.puts e.request_trace
278
+ end
279
+
280
+ if e.respond_to?(:response_trace)
281
+ f.puts e.response_trace
282
+ f.puts ">>>"
283
+ f.puts ""
284
+ end
285
+
286
+ jdc_dir = File.expand_path("../../../..", __FILE__) + "/"
287
+ e.backtrace.each do |loc|
288
+ if loc =~ /\/gems\//
289
+ f.puts loc.sub(/.*\/gems\//, "")
290
+ else
291
+ f.puts loc.sub(jdc_dir, "")
292
+ end
293
+ end
294
+ end
295
+ end
296
+
297
+ def quiet?
298
+ input[:quiet]
299
+ end
300
+
301
+ def force?
302
+ input[:force]
303
+ end
304
+
305
+ def debug?
306
+ !!input[:debug]
307
+ end
308
+
309
+ def color_enabled?
310
+ input[:color]
311
+ end
312
+
313
+ def verbose?
314
+ input[:verbose]
315
+ end
316
+
317
+ def user_colors
318
+ return @user_colors if @user_colors
319
+
320
+ colors = File.expand_path(COLORS_FILE)
321
+
322
+ @user_colors = super.dup
323
+
324
+ # most terminal schemes are stupid, so use cyan instead
325
+ @user_colors.each do |k, v|
326
+ if v == :blue
327
+ @user_colors[k] = :cyan
328
+ end
329
+ end
330
+
331
+ if File.exists?(colors)
332
+ YAML.load_file(colors).each do |k, v|
333
+ @user_colors[k.to_sym] = v.to_sym
334
+ end
335
+ end
336
+
337
+ @user_colors
338
+ end
339
+
340
+ def err(msg, status = 1)
341
+ $stderr.puts c(msg, :error)
342
+ exit_status status
343
+ end
344
+
345
+ def fail(msg)
346
+ raise UserError, msg
347
+ end
348
+
349
+ def table(headers, rows)
350
+ tabular(
351
+ !quiet? && headers.collect { |h| h && b(h) },
352
+ *rows)
353
+ end
354
+
355
+ def name_list(xs)
356
+ if xs.empty?
357
+ d("none")
358
+ else
359
+ xs.collect { |x| c(x.name, :name) }.join(", ")
360
+ end
361
+ end
362
+
363
+ def sane_target_url(url)
364
+ unless url =~ /^https?:\/\//
365
+ begin
366
+ TCPSocket.new(url, Net::HTTP.https_default_port)
367
+ url = "https://#{url}"
368
+ rescue Errno::ECONNREFUSED, SocketError, Timeout::Error
369
+ url = "http://#{url}"
370
+ end
371
+ end
372
+
373
+ url.gsub(/\/$/, "")
374
+ end
375
+
376
+ def one_of(*paths)
377
+ paths.each do |p|
378
+ exp = File.expand_path(p)
379
+ return exp if File.exist? exp
380
+ end
381
+
382
+ File.expand_path(paths.first)
383
+ end
384
+
385
+ def client_target
386
+ return File.read(target_file).chomp if File.exists?(target_file)
387
+ sane_target_url(JDC::DEFAULT_API_URL)
388
+ end
389
+
390
+ def ensure_config_dir
391
+ config = File.expand_path(JDC::CONFIG_DIR)
392
+ FileUtils.mkdir_p(config) unless File.exist? config
393
+ end
394
+
395
+ def set_target(url)
396
+ ensure_config_dir
397
+
398
+ File.open(File.expand_path(JDC::TARGET_FILE), "w") do |f|
399
+ f.write(sane_target_url(url))
400
+ end
401
+
402
+ invalidate_client
403
+ end
404
+
405
+ def targets_info
406
+ new_toks = File.expand_path(JDC::TOKENS_FILE)
407
+
408
+ info =
409
+ if File.exist? new_toks
410
+ YAML.load_file(new_toks)
411
+ end
412
+
413
+ info ||= {}
414
+
415
+ normalize_targets_info(info)
416
+ end
417
+
418
+ def normalize_targets_info(info_by_url)
419
+ info_by_url.reduce({}) do |hash, pair|
420
+ key, value = pair
421
+ hash[key] = value.is_a?(String) ? { :token => value } : value
422
+ hash
423
+ end
424
+ end
425
+
426
+ def target_info(target = client_target)
427
+ targets_info[target] || {}
428
+ end
429
+
430
+ def save_targets(ts)
431
+ ensure_config_dir
432
+
433
+ File.open(File.expand_path(JDC::TOKENS_FILE), "w") do |io|
434
+ YAML.dump(ts, io)
435
+ end
436
+ end
437
+
438
+ def save_target_info(info, target = client_target)
439
+ ts = targets_info
440
+ ts[target] = info
441
+ save_targets(ts)
442
+ end
443
+
444
+ def remove_target_info(target = client_target)
445
+ ts = targets_info
446
+ ts.delete target
447
+ save_targets(ts)
448
+ end
449
+
450
+ def invalidate_client
451
+ @@client = nil
452
+ client
453
+ end
454
+
455
+ def client(target = client_target)
456
+ return @@client if defined?(@@client) && @@client
457
+ return unless target
458
+
459
+ info = target_info(target)
460
+ #token = info[:token] && JFoundry::AuthToken.from_hash(info)
461
+
462
+ fail "V1 targets are no longer supported." if info[:version] == 1
463
+
464
+ @@client = build_client(target)
465
+
466
+ @@client.trace = input[:trace]
467
+
468
+ uri = URI.parse(target)
469
+ @@client.log = File.expand_path("#{LOGS_DIR}/#{uri.host}.log")
470
+
471
+ info_changed = false
472
+ unless info.key? :version
473
+ info[:version] = @@client.version
474
+ info_changed = true
475
+ end
476
+
477
+ unless info.key? :access_key
478
+ info[:access_key] = ENV['ACCESS_KEY']
479
+ info_changed = true
480
+ end
481
+
482
+ save_target_info(info, target) unless !info_changed
483
+
484
+ unless info.key? :organization
485
+ @@client.current_organization = JDC::Populators::Organization.new(input).populate_and_save!
486
+ @@client.current_space = JDC::Populators::Space.new(input, @@client.current_organization).populate_and_save!
487
+ end
488
+
489
+ @@client.current_organization = @@client.organization(info[:organization]) if info[:organization]
490
+ @@client.current_space = @@client.space(info[:space]) if info[:space]
491
+
492
+ @@client
493
+ rescue JFoundry::InvalidTarget
494
+ end
495
+
496
+ def build_client(target)
497
+ client = JFoundry::V2::Client.new(target, ENV['ACCESS_KEY'], ENV['SECRET_KEY'], JDC::VERSION)
498
+ client.http_proxy = input[:http_proxy] || ENV['HTTP_PROXY'] || ENV['http_proxy']
499
+ client.https_proxy = input[:https_proxy] || ENV['HTTPS_PROXY'] || ENV['https_proxy']
500
+ client
501
+ end
502
+
503
+ def fail_unknown(display, name)
504
+ fail("Unknown #{display} '#{name}'.")
505
+ end
506
+
507
+ class << self
508
+ def client
509
+ @@client
510
+ end
511
+
512
+ def client=(c)
513
+ @@client = c
514
+ end
515
+
516
+ private
517
+
518
+ def find_by_name(display, &blk)
519
+ proc { |name, *args|
520
+ choices, _ = args
521
+ choices ||= instance_exec(&blk) if block_given?
522
+
523
+ choices.find { |c| c.name == name } ||
524
+ fail_unknown(display, name)
525
+ }
526
+ end
527
+
528
+ def by_name(what, display = what)
529
+ proc { |name, *_|
530
+ client.send(:"#{what}_by_name", name) ||
531
+ fail_unknown(display, name)
532
+ }
533
+ end
534
+
535
+ def find_by_name_insensitive(display, &blk)
536
+ proc { |name, *args|
537
+ choices, _ = args
538
+ choices ||= instance_exec(&blk) if block_given?
539
+
540
+ choices.find { |c| c.name.upcase == name.upcase } ||
541
+ fail_unknown(display, name)
542
+ }
543
+ end
544
+ end
545
+
546
+ private
547
+
548
+ def target_file
549
+ File.expand_path(JDC::TARGET_FILE)
550
+ end
551
+
552
+ def tokens_file
553
+ File.expand_path(JDC::TOKENS_FILE)
554
+ end
555
+ end
556
+ end