rhc 0.92.11 → 0.93.18

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 (55) hide show
  1. data/README.md +1 -9
  2. data/Rakefile +1 -55
  3. data/bin/rhc +31 -1
  4. data/bin/rhc-app +62 -127
  5. data/bin/rhc-chk +6 -7
  6. data/bin/rhc-create-app +8 -8
  7. data/bin/rhc-create-domain +6 -86
  8. data/bin/rhc-ctl-app +3 -3
  9. data/bin/rhc-ctl-domain +4 -4
  10. data/bin/rhc-domain +16 -18
  11. data/bin/rhc-domain-info +3 -3
  12. data/bin/rhc-port-forward +127 -24
  13. data/bin/rhc-snapshot +7 -46
  14. data/bin/rhc-sshkey +13 -79
  15. data/bin/rhc-tail-files +16 -8
  16. data/lib/rhc-common.rb +406 -230
  17. data/lib/rhc-rest.rb +3 -3
  18. data/lib/rhc-rest/client.rb +1 -1
  19. data/lib/rhc-rest/domain.rb +1 -1
  20. data/lib/rhc.rb +20 -0
  21. data/lib/rhc/cli.rb +38 -0
  22. data/lib/rhc/client.rb +15 -0
  23. data/lib/rhc/commands.rb +32 -0
  24. data/lib/rhc/commands/base.rb +67 -0
  25. data/lib/rhc/commands/server.rb +24 -0
  26. data/lib/rhc/config.rb +141 -0
  27. data/lib/rhc/core_ext.rb +15 -0
  28. data/lib/rhc/helpers.rb +142 -0
  29. data/lib/rhc/json.rb +52 -0
  30. data/lib/rhc/targz.rb +46 -0
  31. data/lib/rhc/vendor/okjson.rb +600 -0
  32. data/lib/rhc/vendor/zliby.rb +628 -0
  33. data/lib/rhc/wizard.rb +579 -0
  34. data/spec/rhc/assets/foo.txt +1 -0
  35. data/spec/rhc/assets/targz_corrupted.tar.gz +1 -0
  36. data/spec/rhc/assets/targz_sample.tar.gz +0 -0
  37. data/spec/rhc/cli_spec.rb +24 -0
  38. data/spec/rhc/command_spec.rb +88 -0
  39. data/spec/rhc/commands/server_spec.rb +39 -0
  40. data/spec/rhc/helpers_spec.rb +171 -0
  41. data/spec/rhc/json_spec.rb +30 -0
  42. data/spec/rhc/targz_spec.rb +42 -0
  43. data/spec/rhc/wizard_spec.rb +426 -0
  44. data/spec/spec_helper.rb +192 -0
  45. data/test/functional/application_test.rb +71 -0
  46. data/test/functional/domain_test.rb +123 -0
  47. data/test/functional/test_credentials.rb +5 -0
  48. data/test/sample-usage.rb +122 -0
  49. data/test/support/server.rb +14 -0
  50. data/test/support/testcase.rb +3 -0
  51. data/test/test_helper.rb +4 -0
  52. data/test/unit/command_test.rb +19 -0
  53. metadata +181 -29
  54. data/ext/mkrf_conf.rb +0 -58
  55. data/lib/rhc +0 -115
data/bin/rhc-chk CHANGED
@@ -14,7 +14,6 @@ end
14
14
  require 'test/unit'
15
15
  require 'test/unit/ui/console/testrunner'
16
16
 
17
-
18
17
  #
19
18
  # print help
20
19
  #
@@ -64,7 +63,7 @@ if 0 != ARGV.length
64
63
  end
65
64
 
66
65
  # If provided a config path, check it
67
- check_cpath($opt)
66
+ RHC::Config.check_cpath($opt)
68
67
 
69
68
  # Need to store the config information so tests can use it
70
69
  # since they are technically new objects
@@ -168,8 +167,8 @@ module TestBase
168
167
  return response
169
168
  ensure
170
169
  $debuginfo['fetches'] ||= []
171
- body = JSON.parse(response.body)
172
- body['data'] = body['data'] && body['data'] != '' ? JSON.parse(body['data']) : ''
170
+ body = RHC::json_decode(response.body)
171
+ body['data'] = body['data'] && body['data'] != '' ? RHC::json_decode(body['data']) : ''
173
172
  $debuginfo['fetches'] << {url.to_s => {
174
173
  'body' => body,
175
174
  'header' => response.instance_variable_get(:@header)
@@ -243,7 +242,7 @@ class Test2_Authentication < Test::Unit::TestCase
243
242
 
244
243
  data = {'rhlogin' => $rhlogin}
245
244
  response = fetch_url_json("/broker/userinfo", data)
246
- resp_json = JSON.parse(response.body)
245
+ resp_json = RHC::json_decode(response.body)
247
246
 
248
247
  case response.code.to_i
249
248
  when 404
@@ -256,7 +255,7 @@ class Test2_Authentication < Test::Unit::TestCase
256
255
  assert false, error_for(:_other_http, resp_json['result'])
257
256
  end
258
257
 
259
- $user_info = JSON.parse(resp_json['data'].to_s)
258
+ $user_info = RHC::json_decode(resp_json['data'].to_s)
260
259
  end
261
260
  end
262
261
 
@@ -282,7 +281,7 @@ class Test3_SSH < Test::Unit::TestCase
282
281
  check_permissions(@libra_kfile, /[4-7]00/) # Needs to at least be readable by user and nobody else
283
282
 
284
283
  # Derive the public key from the private key
285
- key_dump = `ssh-keygen -f #{@libra_kfile} -y`
284
+ key_dump = SSHKey.new(File.read(@libra_kfile)).ssh_public_key
286
285
  @@local_derived_ssh_pubkey = key_dump.to_s.strip.split(' ')[1]
287
286
 
288
287
  assert_not_nil @@local_derived_ssh_pubkey, error_for(:no_derive)
data/bin/rhc-create-app CHANGED
@@ -4,7 +4,7 @@ require 'rhc-common'
4
4
  def p_usage(error_code = 255)
5
5
  libra_server = get_var('libra_server')
6
6
  rhlogin = get_var('default_rhlogin') ? "Default: #{get_var('default_rhlogin')}" : "required"
7
- type_keys = RHC::get_cartridge_listing(nil, ', ', libra_server, @http, 'standalone', false)
7
+ type_keys = RHC::get_cartridge_listing(nil, ', ', libra_server, RHC::Config.default_proxy, 'standalone', false)
8
8
  puts <<USAGE
9
9
 
10
10
  Usage: #{$0}
@@ -60,7 +60,7 @@ end
60
60
 
61
61
 
62
62
  # If provided a config path, check it
63
- check_cpath(opt)
63
+ RHC::Config.check_cpath(opt)
64
64
 
65
65
  # Pull in configs from files
66
66
  libra_server = get_var('libra_server')
@@ -106,7 +106,7 @@ if !password
106
106
  password = RHC::get_password
107
107
  end
108
108
 
109
- user_info = RHC::get_user_info(libra_server, opt['rhlogin'], password, @http, false)
109
+ user_info = RHC::get_user_info(libra_server, opt['rhlogin'], password, RHC::Config.default_proxy, false)
110
110
  app_info = user_info['app_info']
111
111
 
112
112
  if app_info[opt['app']]
@@ -212,8 +212,8 @@ unless opt['nogit']
212
212
  end
213
213
 
214
214
  if jenkins_app_name && !has_jenkins
215
- jenkins_app = RHC::create_app(libra_server, @http, user_info, jenkins_app_name, 'jenkins-1.4', opt['rhlogin'], password, nil, false, true, true)
216
- available = RHC::check_app_available(@http, jenkins_app[:app_name], jenkins_app[:fqdn], jenkins_app[:health_check_path], jenkins_app[:result], jenkins_app[:git_url], nil, true)
215
+ jenkins_app = RHC::create_app(libra_server, RHC::Config.default_proxy, user_info, jenkins_app_name, 'jenkins-1.4', opt['rhlogin'], password, nil, false, true, true)
216
+ available = RHC::check_app_available(RHC::Config.default_proxy, jenkins_app[:app_name], jenkins_app[:fqdn], jenkins_app[:health_check_path], jenkins_app[:result], jenkins_app[:git_url], nil, true)
217
217
  if !available
218
218
  puts "Unable to access your new Jenkins application."
219
219
  exit 1
@@ -223,7 +223,7 @@ end
223
223
  #
224
224
  # Create remote application space
225
225
  #
226
- main_app = RHC::create_app(libra_server, @http, user_info, opt['app'], opt['type'], opt['rhlogin'], password, opt['repo'], opt['no-dns'], opt['nogit'], false, gear_size, opt['scaling'])
226
+ main_app = RHC::create_app(libra_server, RHC::Config.default_proxy, user_info, opt['app'], opt['type'], opt['rhlogin'], password, opt['repo'], opt['no-dns'], opt['nogit'], false, gear_size, opt['scaling'])
227
227
 
228
228
  if !main_app
229
229
  puts "Create app failed"
@@ -232,11 +232,11 @@ end
232
232
 
233
233
  if jenkins_app_name
234
234
  puts "Now embedding the jenkins client into '#{opt['app']}'..."
235
- RHC::ctl_app(libra_server, @http, opt['app'], opt['rhlogin'], password, 'configure', true, 'jenkins-client-1.4', nil, false)
235
+ RHC::ctl_app(libra_server, RHC::Config.default_proxy, opt['app'], opt['rhlogin'], password, 'configure', true, 'jenkins-client-1.4', nil, false)
236
236
  end
237
237
 
238
238
  unless opt['no-dns']
239
- available = RHC::check_app_available(@http, main_app[:app_name], main_app[:fqdn], main_app[:health_check_path], main_app[:result], main_app[:git_url], opt['repo'], opt['nogit'])
239
+ available = RHC::check_app_available(RHC::Config.default_proxy, main_app[:app_name], main_app[:fqdn], main_app[:health_check_path], main_app[:result], main_app[:git_url], opt['repo'], opt['nogit'])
240
240
  if !available
241
241
  puts "Unable to access your new application."
242
242
  exit 1
@@ -49,7 +49,7 @@ rescue Exception => e
49
49
  end
50
50
 
51
51
  # If provided a config path, check it
52
- check_cpath(opt)
52
+ RHC::Config.check_cpath(opt)
53
53
 
54
54
  # Pull in configs from files
55
55
  libra_server = get_var('libra_server')
@@ -104,84 +104,6 @@ def add_rhlogin_config(rhlogin, uuid)
104
104
  f.close
105
105
  end
106
106
 
107
- #
108
- # Check / add new host to ~/.ssh/config
109
- #
110
- def add_ssh_config_host(rhc_domain, ssh_key_file_path, ssh_config, ssh_config_d)
111
-
112
- puts "Checking ~/.ssh/config"
113
- ssh_key_file_name = File.basename(ssh_key_file_path)
114
- if ssh_key_file_path =~ /^#{ENV['HOME']}/
115
- ssh_key_file_path = ssh_key_file_path[ENV['HOME'].length..-1]
116
- if ssh_key_file_path =~ /^\// || ssh_key_file_path =~ /^\\/
117
- ssh_key_file_path = '~' + ssh_key_file_path
118
- else
119
- ssh_key_file_path = '~/' + ssh_key_file_path
120
- end
121
- end
122
- if (ssh_key_file_name != 'id_rsa')
123
- found = false
124
-
125
- begin
126
- File.open(ssh_config, "r") do |sline|
127
- while(line = sline.gets)
128
- if line.to_s.index("Host *.#{rhc_domain}") == 0
129
- found = true
130
- break
131
- end
132
- end
133
- end
134
- rescue Errno::EACCES
135
- puts "Could not read from #{ssh_config}"
136
- puts "Reason: " + $!
137
- puts
138
- puts "Please correct this first. Then run rerun."
139
- puts
140
- exit 213
141
- rescue Errno::ENOENT
142
- puts "Could not find #{ssh_config}. This is ok, continuing"
143
- end
144
- if found
145
- puts "Found #{rhc_domain} in ~/.ssh/config... No need to adjust"
146
- else
147
- puts " Adding #{rhc_domain} to ~/.ssh/config"
148
- begin
149
- f = File.open(ssh_config, "a")
150
- f.puts <<SSH
151
-
152
- # Added by rhc-create-domain on #{`date`}
153
- Host *.#{rhc_domain}
154
- IdentityFile #{ssh_key_file_path}
155
- VerifyHostKeyDNS yes
156
- StrictHostKeyChecking no
157
- UserKnownHostsFile ~/.ssh/libra_known_hosts
158
-
159
- SSH
160
- f.close
161
- rescue Errno::EACCES
162
- puts "Could not write to #{ssh_config}"
163
- puts "Reason: " + $!
164
- puts
165
- puts "Please correct this first. Then run rerun."
166
- puts
167
- exit 214
168
- rescue Errno::ENOENT
169
- # Make directory and config if they do not exist
170
- puts "Could not find directory: " + $!
171
- puts "creating"
172
- FileUtils.mkdir_p ssh_config_d
173
- file = File.open(ssh_config, 'w')
174
- file.close
175
- retry
176
- end
177
- end
178
-
179
- File.chmod(0700, ssh_config_d)
180
- File.chmod(0600, ssh_config)
181
- end
182
- end
183
-
184
-
185
107
  #
186
108
  # Check to see if a ssh_key_file_path exists, if not create it.
187
109
  #
@@ -206,7 +128,7 @@ data = {'namespace' => opt['namespace'],
206
128
  if (opt['alter'])
207
129
  data[:alter] = true
208
130
  not_found_message = "A user with rhlogin '#{opt['rhlogin']}' does not have a registered domain. Be sure to run rhc-create-domain without -a|--alter first."
209
- user_info = RHC::get_user_info(libra_server, opt['rhlogin'], password, @http, true, not_found_message)
131
+ user_info = RHC::get_user_info(libra_server, opt['rhlogin'], password, RHC::Config.default_proxy, true, not_found_message)
210
132
  end
211
133
  if debug
212
134
  data[:debug] = true
@@ -215,16 +137,14 @@ RHC::print_post_data(data)
215
137
  json_data = RHC::generate_json(data)
216
138
 
217
139
  url = URI.parse("https://#{libra_server}/broker/domain")
218
- response = RHC::http_post(@http, url, json_data, password)
140
+ response = RHC::http_post(RHC::Config.default_proxy, url, json_data, password)
219
141
 
220
142
  if response.code == '200'
221
143
  begin
222
- json_resp = JSON.parse(response.body)
144
+ json_resp = RHC::json_decode(response.body)
223
145
  RHC::print_response_success(json_resp)
224
- json_rhlogininfo = JSON.parse(json_resp['data'])
146
+ json_rhlogininfo = RHC::json_decode(json_resp['data'])
225
147
  add_rhlogin_config(json_rhlogininfo['rhlogin'], json_rhlogininfo['uuid'])
226
- add_ssh_config_host(json_rhlogininfo['rhc_domain'], ssh_key_file_path, ssh_config, ssh_config_d)
227
-
228
148
  if opt['alter'] != ''
229
149
  puts <<EOF
230
150
  Creation successful
@@ -271,7 +191,7 @@ EOF
271
191
  puts ""
272
192
  end
273
193
  exit 0
274
- rescue JSON::ParserError
194
+ rescue RHC::JsonError
275
195
  RHC::print_response_err(response)
276
196
  end
277
197
  else
data/bin/rhc-ctl-app CHANGED
@@ -32,7 +32,7 @@ def p_embedded_list
32
32
  puts ""
33
33
  puts "List of supported embedded cartridges:"
34
34
  puts ""
35
- type_keys = RHC::get_cartridge_listing(nil, ', ', libra_server, @http, 'embedded', false)
35
+ type_keys = RHC::get_cartridge_listing(nil, ', ', libra_server, RHC::Config.default_proxy, 'embedded', false)
36
36
  puts type_keys
37
37
  puts ""
38
38
  exit 255
@@ -63,7 +63,7 @@ rescue Exception => e
63
63
  end
64
64
 
65
65
  # If provided a config path, check it
66
- check_cpath(opt)
66
+ RHC::Config.check_cpath(opt)
67
67
 
68
68
  # Pull in configs from files
69
69
  libra_server = get_var('libra_server')
@@ -165,4 +165,4 @@ else
165
165
  url = URI.parse("https://#{libra_server}/broker/cartridge")
166
166
  end
167
167
 
168
- RHC::ctl_app(libra_server, @http, opt['app'], opt['rhlogin'], password, action, opt['embed'], framework, opt['alias'])
168
+ RHC::ctl_app(libra_server, RHC::Config.default_proxy, opt['app'], opt['rhlogin'], password, action, opt['embed'], framework, opt['alias'])
data/bin/rhc-ctl-domain CHANGED
@@ -61,7 +61,7 @@ rescue Exception => e
61
61
  end
62
62
 
63
63
  # If provided a config path, check it
64
- check_cpath(opt)
64
+ RHC::Config.check_cpath(opt)
65
65
 
66
66
  # Pull in configs from files
67
67
  libra_server = get_var('libra_server')
@@ -167,16 +167,16 @@ end
167
167
  RHC::print_post_data(data)
168
168
  json_data = RHC::generate_json(data)
169
169
 
170
- response = RHC::http_post(@http, url, json_data, password)
170
+ response = RHC::http_post(RHC::Config.default_proxy, url, json_data, password)
171
171
 
172
172
  if response.code == '200'
173
173
  begin
174
- json_resp = JSON.parse(response.body)
174
+ json_resp = RHC::json_decode(response.body)
175
175
  RHC::update_server_api_v(json_resp)
176
176
  RHC::print_response_success(json_resp)
177
177
  puts "Success"
178
178
  exit 0
179
- rescue JSON::ParserError
179
+ rescue RHC::JsonError
180
180
  RHC::print_response_err(response)
181
181
  end
182
182
  else
data/bin/rhc-domain CHANGED
@@ -43,20 +43,20 @@ end
43
43
 
44
44
  def validate_args(val_namespace=true, val_timeout=true)
45
45
  # If provided a config path, check it
46
- check_cpath($opt)
47
-
46
+ RHC::Config.check_cpath($opt)
47
+
48
48
  # Pull in configs from files
49
49
  $libra_server = get_var('libra_server')
50
50
  debug = get_var('debug') == 'false' ? nil : get_var('debug')
51
-
51
+
52
52
  $opt['rhlogin'] = get_var('default_rhlogin') unless $opt['rhlogin']
53
53
  p_usage if !RHC::check_rhlogin($opt['rhlogin'])
54
-
54
+
55
55
  p_usage if (val_namespace && !RHC::check_namespace($opt['namespace']))
56
56
 
57
57
  debug = $opt["debug"] ? true : false
58
58
  RHC::debug(debug)
59
-
59
+
60
60
  RHC::timeout($opt["timeout"], get_var('timeout')) if val_timeout
61
61
  RHC::connect_timeout($opt["timeout"], get_var('timeout')) if val_timeout
62
62
 
@@ -65,10 +65,10 @@ end
65
65
 
66
66
  def create_or_alter_domain(alter=false)
67
67
  validate_args()
68
-
68
+
69
69
  ssh_key_file_path = get_kfile(false)
70
70
  ssh_pub_key_file_path = get_kpfile(ssh_key_file_path, alter)
71
-
71
+
72
72
  ssh_config = "#{ENV['HOME']}/.ssh/config"
73
73
  ssh_config_d = "#{ENV['HOME']}/.ssh/"
74
74
 
@@ -98,7 +98,7 @@ def create_or_alter_domain(alter=false)
98
98
  if alter
99
99
  data[:alter] = true
100
100
  not_found_message = "A user with rhlogin '#{$opt['rhlogin']}' does not have a registered domain. Be sure to run 'rhc domain create' first."
101
- user_info = RHC::get_user_info($libra_server, $opt['rhlogin'], $password, @http, true, not_found_message)
101
+ user_info = RHC::get_user_info($libra_server, $opt['rhlogin'], $password, RHC::Config.default_proxy, true, not_found_message)
102
102
  end
103
103
  if @mydebug
104
104
  data[:debug] = true
@@ -107,16 +107,14 @@ def create_or_alter_domain(alter=false)
107
107
  json_data = RHC::generate_json(data)
108
108
 
109
109
  url = URI.parse("https://#{$libra_server}/broker/domain")
110
- response = RHC::http_post(@http, url, json_data, $password)
110
+ response = RHC::http_post(RHC::Config.default_proxy, url, json_data, $password)
111
111
 
112
112
  if response.code == '200'
113
113
  begin
114
- json_resp = JSON.parse(response.body)
114
+ json_resp = RHC::json_decode(response.body)
115
115
  RHC::print_response_success(json_resp)
116
- json_rhlogininfo = JSON.parse(json_resp['data'])
116
+ json_rhlogininfo = RHC::json_decode(json_resp['data'])
117
117
  add_rhlogin_config(json_rhlogininfo['rhlogin'], json_rhlogininfo['uuid'])
118
- add_ssh_config_host(json_rhlogininfo['rhc_domain'], ssh_key_file_path, ssh_config, ssh_config_d)
119
-
120
118
  if !alter
121
119
  puts <<EOF
122
120
  Creation successful
@@ -163,7 +161,7 @@ EOF
163
161
  puts ""
164
162
  end
165
163
  exit 0
166
- rescue JSON::ParserError
164
+ rescue RHC::JsonError
167
165
  RHC::print_response_err(response)
168
166
  end
169
167
  else
@@ -184,16 +182,16 @@ def destroy_domain()
184
182
  RHC::print_post_data(data)
185
183
  json_data = RHC::generate_json(data)
186
184
 
187
- response = RHC::http_post(@http, url, json_data, $password)
185
+ response = RHC::http_post(RHC::Config.default_proxy, url, json_data, $password)
188
186
 
189
187
  if response.code == '200'
190
188
  begin
191
- json_resp = JSON.parse(response.body)
189
+ json_resp = RHC::json_decode(response.body)
192
190
  RHC::update_server_api_v(json_resp)
193
191
  RHC::print_response_success(json_resp)
194
192
  puts "Success"
195
193
  exit 0
196
- rescue JSON::ParserError
194
+ rescue RHC::JsonError
197
195
  RHC::print_response_err(response)
198
196
  end
199
197
  else
@@ -206,7 +204,7 @@ end
206
204
  def show_domain_info()
207
205
  validate_args(false, true)
208
206
 
209
- user_info = RHC::get_user_info($libra_server, $opt['rhlogin'], $password, @http, true)
207
+ user_info = RHC::get_user_info($libra_server, $opt['rhlogin'], $password, RHC::Config.default_proxy, true)
210
208
 
211
209
  domains = user_info['user_info']['domains']
212
210
  num_domains = domains.length
data/bin/rhc-domain-info CHANGED
@@ -45,7 +45,7 @@ rescue Exception => e
45
45
  end
46
46
 
47
47
  # If provided a config path, check it
48
- check_cpath(opt)
48
+ RHC::Config.check_cpath(opt)
49
49
 
50
50
  # Pull in configs from files
51
51
  libra_server = get_var('libra_server')
@@ -74,10 +74,10 @@ if !password
74
74
  end
75
75
 
76
76
  opt['apps'] = true if not opt['info'] and not opt['apps']
77
- user_info = RHC::get_user_info(libra_server, opt['rhlogin'], password, @http, true)
77
+ user_info = RHC::get_user_info(libra_server, opt['rhlogin'], password, RHC::Config.default_proxy, true)
78
78
 
79
79
  if opt['info']
80
- ssh_keys = RHC::get_ssh_keys(libra_server, opt['rhlogin'], password, @http)
80
+ ssh_keys = RHC::get_ssh_keys(libra_server, opt['rhlogin'], password, RHC::Config.default_proxy)
81
81
 
82
82
  domains = user_info['user_info']['domains']
83
83
  num_domains = domains.length
data/bin/rhc-port-forward CHANGED
@@ -43,7 +43,7 @@ rescue Exception => e
43
43
  end
44
44
 
45
45
  # If provided a config path, check it
46
- check_cpath(opt)
46
+ RHC::Config.check_cpath(opt)
47
47
 
48
48
  # Pull in configs from files
49
49
  libra_server = get_var('libra_server')
@@ -71,7 +71,7 @@ if !password
71
71
  password = RHC::get_password
72
72
  end
73
73
 
74
- user_info = RHC::get_user_info(libra_server, opt['rhlogin'], password, @http, false)
74
+ user_info = RHC::get_user_info(libra_server, opt['rhlogin'], password, RHC::Config.default_proxy, false)
75
75
 
76
76
  app_name = opt['app']
77
77
 
@@ -83,41 +83,144 @@ unless user_info['app_info'][app_name]
83
83
  exit 101
84
84
  end
85
85
 
86
+
87
+ # mock for windows
88
+ if defined?(UNIXServer) != 'constant' or UNIXServer.class != Class
89
+ class UNIXServer; end
90
+ end
91
+
86
92
  app_uuid = user_info['app_info'][app_name]['uuid']
87
93
  namespace = user_info['user_info']['domains'][0]['namespace']
88
94
  rhc_domain = user_info['user_info']['rhc_domain']
95
+ ssh_host = "#{app_name}-#{namespace}.#{rhc_domain}"
89
96
 
90
97
  puts "Checking available ports..."
91
98
 
92
- hosts_and_ports, hosts_and_ports_descriptions = RHC::list_ports(rhc_domain, namespace, app_name, app_uuid, debug)
99
+ ip_and_port_simple_regex = /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:[0-9]{1,5}/
93
100
 
94
- if hosts_and_ports.length == 0
95
- puts
96
- puts "No available ports to forward"
97
- exit 102
98
- end
101
+ hosts_and_ports = []
102
+ hosts_and_ports_descriptions = []
103
+ scaled_uuids = []
99
104
 
100
- puts
105
+ puts "Using #{app_uuid}@#{ssh_host}..." if debug
101
106
 
102
- hosts_and_ports_descriptions.each { |description| puts "Binding #{description}..." }
107
+ Net::SSH.start(ssh_host, app_uuid) do |ssh|
103
108
 
104
- ssh_cmd = "ssh -N "
105
- hosts_and_ports.each { |port| ssh_cmd << "-L #{port}:#{port} " }
106
- ssh_cmd << "#{app_uuid}@#{app_name}-#{namespace}.#{rhc_domain}"
109
+ ssh.exec! "rhc-list-ports" do |channel, stream, data|
107
110
 
108
- puts ssh_cmd if debug
109
- puts
110
- puts "Use ctl + c to stop"
111
+ # data comes from a linux server so it is a one-line string for windows, we need to split \n manually
112
+ if RHC::Helpers.windows?
113
+ data = data.split /\n/
114
+ end
115
+
116
+ if stream == :stderr
117
+
118
+ data.each { |line|
119
+ line = line.chomp
120
+
121
+ if line.downcase =~ /permission denied/
122
+ puts line
123
+ exit 1
124
+ end
125
+
126
+ if line.index(ip_and_port_simple_regex)
127
+ hosts_and_ports_descriptions << line
128
+ end
129
+ }
130
+
131
+ else
132
+
133
+ data.each { |line|
134
+ line = line.chomp
135
+
136
+ if line.downcase =~ /scale/
137
+ scaled_uuid = line[5..-1]
138
+ scaled_uuids << scaled_uuid
139
+
140
+ else
141
+ if ip_and_port_simple_regex.match(line)
142
+ hosts_and_ports << line
143
+ end
144
+ end
145
+ }
146
+
147
+ end
148
+
149
+ end
150
+
151
+ scaled_uuids.each { |scaled_uuid|
152
+
153
+ puts "Using #{scaled_uuid}@#{ssh_host} (scaled instance)..." if debug
154
+
155
+ Net::SSH.start(ssh_host, scaled_uuid) do |ssh|
156
+
157
+ ssh.exec! "rhc-list-ports" do |channel, stream, data|
158
+ # data comes from a linux server so it is a one-line string for windows, we need to split \n manually
159
+ if RHC::Helpers.windows?
160
+ data = data.split /\n/
161
+ end
162
+ if stream == :stderr
163
+ data.each { |line|
164
+ line = line.chomp
165
+ if line.downcase =~ /permission denied/
166
+ puts line
167
+ exit 1
168
+ end
169
+ if line.index(ip_and_port_simple_regex)
170
+ hosts_and_ports_descriptions << line
171
+ end
172
+ }
173
+ else
174
+ data.each { |line|
175
+ line = line.chomp
176
+ if ip_and_port_simple_regex.match(line)
177
+ hosts_and_ports << line
178
+ end
179
+ }
180
+ end
181
+ end
182
+ end
183
+ }
184
+
185
+ if hosts_and_ports.length == 0
186
+ puts
187
+ puts "No available ports to forward"
188
+ exit 102
189
+ end
111
190
 
112
- begin
113
- exec ssh_cmd
114
- rescue SystemCallError
115
- puts
116
- puts "Error trying to forward ports. You can try to forward manually by running:"
117
- puts
118
- puts ssh_cmd
119
191
  puts
120
- exit 1
192
+
193
+ hosts_and_ports_descriptions.each { |description| puts "Binding #{description}..." }
194
+
195
+ begin
196
+
197
+ Net::SSH.start(ssh_host, app_uuid) do |ssh|
198
+ puts "Use ctl + c to stop"
199
+ hosts_and_ports.each do |host_and_port|
200
+ host, port = host_and_port.split(/:/)
201
+ ssh.forward.local(host, port.to_i, host, port.to_i)
202
+ end
203
+ ssh.loop { true }
204
+ end
205
+
206
+ rescue Interrupt
207
+ puts
208
+ puts "Terminating..."
209
+ exit 0
210
+
211
+ rescue Exception => e
212
+ puts
213
+ puts e.message if debug
214
+ puts "Error trying to forward ports. You can try to forward manually by running:"
215
+ ssh_cmd = "ssh -N "
216
+ hosts_and_ports.each { |port| ssh_cmd << "-L #{port}:#{port} " }
217
+ ssh_cmd << "#{app_uuid}@#{app_name}-#{namespace}.#{rhc_domain}"
218
+ puts
219
+ puts ssh_cmd
220
+ puts
221
+ exit 1
222
+ end
223
+
121
224
  end
122
225
 
123
226
  # never