opsmgr 0.33.5 → 0.34.0

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 (34) hide show
  1. checksums.yaml +4 -4
  2. data/lib/opsmgr/api/client.rb +6 -3
  3. data/lib/opsmgr/api/http_client.rb +168 -26
  4. data/lib/opsmgr/api/version20/endpoints.rb +18 -2
  5. data/lib/opsmgr/bosh_command_runner.rb +21 -17
  6. data/lib/opsmgr/cmd/bosh_command.rb +54 -78
  7. data/lib/opsmgr/cmd/ops_manager.rb +7 -4
  8. data/lib/opsmgr/environments.rb +34 -5
  9. data/lib/opsmgr/errand_runner.rb +1 -3
  10. data/lib/opsmgr/product_upload_wrapper.rb +8 -8
  11. data/lib/opsmgr/tasks/bosh.rake +1 -7
  12. data/lib/opsmgr/tasks/destroy.rake +2 -2
  13. data/lib/opsmgr/tasks/info.rake +1 -1
  14. data/lib/opsmgr/tasks/opsmgr.rake +11 -8
  15. data/lib/opsmgr/tasks/product.rake +9 -6
  16. data/lib/opsmgr/ui_helpers/add_first_user_spec.rb +2 -2
  17. data/lib/opsmgr/ui_helpers/config_helper.rb +18 -18
  18. data/lib/opsmgr/ui_helpers/delete_installation_spec.rb +2 -2
  19. data/lib/opsmgr/ui_helpers/delete_product_if_present_spec.rb +2 -2
  20. data/lib/opsmgr/ui_helpers/delete_product_spec.rb +2 -2
  21. data/lib/opsmgr/ui_helpers/export_installation_spec.rb +2 -2
  22. data/lib/opsmgr/ui_helpers/get_latest_install_log_spec.rb +2 -2
  23. data/lib/opsmgr/ui_helpers/import_stemcell_spec.rb +2 -2
  24. data/lib/opsmgr/ui_helpers/microbosh/configure_microbosh_spec.rb +2 -2
  25. data/lib/opsmgr/ui_helpers/{import_installation_spec.rb → post_import_configuration_spec.rb} +7 -5
  26. data/lib/opsmgr/ui_helpers/trigger_install_spec.rb +2 -2
  27. data/lib/opsmgr/ui_helpers/ui_spec_runner.rb +2 -4
  28. data/lib/opsmgr/ui_helpers/uncheck_errands_spec.rb +2 -2
  29. data/lib/opsmgr/ui_helpers/upload_and_add_product_spec.rb +2 -2
  30. data/lib/opsmgr/ui_helpers/upload_and_upgrade_product_spec.rb +2 -2
  31. data/lib/opsmgr/version.rb +1 -1
  32. data/sample_env_files/aws.yml +1 -0
  33. metadata +23 -10
  34. data/lib/opsmgr/api/endpoints_factory.rb +0 -39
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b18d79ccc6d7f697a7c029c61cc657bfa030f0cd
4
- data.tar.gz: 0eed6e834fa16071278eadff2cb1f93bdc8057a4
3
+ metadata.gz: 79c4d9ba31bb27d2f4b65ed0d02f1ddb30a39724
4
+ data.tar.gz: d6e5eca5bece6e0914d3e8da79bf11ea45a6fd08
5
5
  SHA512:
6
- metadata.gz: e990b53cd30725a594b95d8d4277009992fda2572e9f4239e6a56ca13c1b666bfac51bc5bb5fd887ddfb00c2eb223269c64effa9a2be90db3becc987db6ee0d8
7
- data.tar.gz: dce5f0b9c1de785980e7e28a06b8b479618bdb1dac3ac51beae1e78a5804acb660d87bff99d80c857d026e712704368b0d4cddffcc8068d860f38ef00c197baf
6
+ metadata.gz: aa084e0c3b6fe3729646bec8e725b5f7a6d9ad7dde7ecbf4bdcedb63b95376d42409e626d3a54c65972cdd01c96e1836bf9798e523c542e52f1e3cc4f93e6216
7
+ data.tar.gz: b7ae4c288832a47d61538142009cdb7cf81a9bdd46b60ad2923f4c3831b08d2fb5b89b4bd563ef623885d9cbb65d0c22218f39852fa926f19135870e0fd51156
@@ -1,3 +1,6 @@
1
+ require 'backport_refinements'
2
+ using OpsManagerUiDrivers::BackportRefinements
3
+
1
4
  require 'json'
2
5
  require 'opsmgr/api/http_client'
3
6
  require 'opsmgr/api/results'
@@ -8,9 +11,9 @@ module Opsmgr
8
11
  class Client
9
12
  attr_reader :environment_name
10
13
 
11
- def initialize(environment)
12
- @http_client = HttpClient.build(environment)
13
- @environment_name = environment.settings.name.freeze
14
+ def initialize(environment, om_version)
15
+ @http_client = HttpClient.build(environment, om_version)
16
+ @environment_name = environment.settings.dig('name').freeze
14
17
  end
15
18
 
16
19
  def installation_settings
@@ -1,9 +1,14 @@
1
+ # rubocop:disable Metrics/ClassLength
2
+ require 'backport_refinements'
3
+ using OpsManagerUiDrivers::BackportRefinements
4
+
1
5
  require 'net/http'
2
6
  require 'net/http/post/multipart'
3
7
  require 'json'
4
8
  require 'open4'
5
- require 'opsmgr/api/endpoints_factory'
6
9
  require 'opsmgr/log'
10
+ require 'opsmgr/api/version20/endpoints'
11
+ require 'uaa'
7
12
 
8
13
  # noinspection RubyResolve
9
14
  module Opsmgr
@@ -12,14 +17,15 @@ module Opsmgr
12
17
  include Loggable
13
18
  attr_reader :uri, :environment, :endpoints
14
19
 
15
- def self.build(environment)
16
- new(environment, Opsmgr::Api::EndpointsFactory.create(environment))
20
+ def self.build(environment, om_version)
21
+ new(environment, Opsmgr::Api::Version20::Endpoints.new, om_version)
17
22
  end
18
23
 
19
- def initialize(environment, endpoints)
24
+ def initialize(environment, endpoints, om_version)
20
25
  @environment = environment
21
- @uri = URI.parse(environment.settings.ops_manager.url)
26
+ @uri = URI.parse(environment.settings.dig('ops_manager', 'url'))
22
27
  @endpoints = endpoints
28
+ @om_version = om_version
23
29
  end
24
30
 
25
31
  def upload_product_installation_settings(settings_file_path)
@@ -33,7 +39,7 @@ module Opsmgr
33
39
  )
34
40
  )
35
41
 
36
- req.basic_auth(web_auth_user, web_auth_password)
42
+ add_auth_header(req)
37
43
 
38
44
  http.request(req)
39
45
  end
@@ -50,8 +56,19 @@ module Opsmgr
50
56
  #{uri}#{endpoints.upload_product_path}
51
57
  -F #{endpoints.upload_product_form_key}[file]=@#{product_path}
52
58
  -X POST
53
- -u #{web_auth_user}:#{web_auth_password}
54
59
  )
60
+
61
+ if Gem::Version.new(@om_version) >= Gem::Version.new('1.7')
62
+ log.info('using oauth')
63
+ token = get_oauth_token(web_auth_user)
64
+ upload_component_command.push("-H")
65
+ upload_component_command.push("Authorization: #{token}")
66
+ else
67
+ log.info('using basic auth')
68
+ upload_component_command.push("-u")
69
+ upload_component_command.push("#{web_auth_user}:#{web_auth_password}")
70
+ end
71
+
55
72
  log.info('uploading product')
56
73
 
57
74
  error = nil
@@ -72,7 +89,7 @@ module Opsmgr
72
89
  def add_product(product_name, version)
73
90
  req = Net::HTTP::Post.new(endpoints.add_product_path)
74
91
  req.set_form_data('name' => product_name, 'product_version' => version)
75
- req.basic_auth(web_auth_user, web_auth_password)
92
+ add_auth_header(req)
76
93
 
77
94
  http.request(req)
78
95
  end
@@ -80,7 +97,7 @@ module Opsmgr
80
97
  def upgrade_product(product_guid, to_version)
81
98
  req = Net::HTTP::Put.new(endpoints.upgrade_product_path(product_guid))
82
99
  req.set_form_data('to_version' => to_version)
83
- req.basic_auth(web_auth_user, web_auth_password)
100
+ add_auth_header(req)
84
101
 
85
102
  http.request(req)
86
103
  end
@@ -88,7 +105,7 @@ module Opsmgr
88
105
  def trigger_install
89
106
  req = Net::HTTP::Post.new(endpoints.install_post_path)
90
107
  req.body = ''
91
- req.basic_auth(web_auth_user, web_auth_password)
108
+ add_auth_header(req)
92
109
 
93
110
  http.request(req)
94
111
  end
@@ -118,7 +135,7 @@ module Opsmgr
118
135
 
119
136
  def installation_delete
120
137
  req = Net::HTTP::Delete.new(endpoints.installation_path)
121
- req.basic_auth(web_auth_user, web_auth_password)
138
+ add_auth_header(req)
122
139
 
123
140
  http.request(req)
124
141
  end
@@ -128,11 +145,23 @@ module Opsmgr
128
145
  end
129
146
 
130
147
  def create_the_first_user
131
- req = Net::HTTP::Post.new(endpoints.configure_user_path)
132
- req.set_form_data(
133
- 'user[user_name]' => web_auth_user,
134
- 'user[password]' => web_auth_password,
135
- 'user[password_confirmation]' => web_auth_password)
148
+ if Gem::Version.new(@om_version) < Gem::Version.new('1.7')
149
+ req = Net::HTTP::Post.new(endpoints.api_user_path)
150
+ req.set_form_data(
151
+ 'user[user_name]' => web_auth_user,
152
+ 'user[password]' => web_auth_password,
153
+ 'user[password_confirmation]' => web_auth_password,
154
+ )
155
+ else
156
+ req = Net::HTTP::Post.new(endpoints.api_setup_path)
157
+ req.set_form_data(
158
+ 'setup[user_name]' => web_auth_user,
159
+ 'setup[password]' => web_auth_password,
160
+ 'setup[password_confirmation]' => web_auth_password,
161
+ 'setup[eula_accepted]' => true
162
+ )
163
+ end
164
+
136
165
  http.request(req)
137
166
  end
138
167
 
@@ -146,14 +175,14 @@ module Opsmgr
146
175
 
147
176
  def mark_product_for_deletion(product_guid)
148
177
  req = Net::HTTP::Delete.new(endpoints.mark_for_deletion_path(product_guid))
149
- req.basic_auth(web_auth_user, web_auth_password)
178
+ add_auth_header(req)
150
179
 
151
180
  http.request(req)
152
181
  end
153
182
 
154
183
  def delete_unused_products
155
184
  req = Net::HTTP::Delete.new(endpoints.list_products_path)
156
- req.basic_auth(web_auth_user, web_auth_password)
185
+ add_auth_header(req)
157
186
 
158
187
  http.request(req)
159
188
  end
@@ -163,6 +192,8 @@ module Opsmgr
163
192
  end
164
193
 
165
194
  def import_installation(installation_file, password)
195
+ create_the_first_user unless Gem::Version.new(@om_version) >= Gem::Version.new('1.7')
196
+
166
197
  req = Net::HTTP::Post::Multipart.new(
167
198
  endpoints.import_installation_path,
168
199
  'installation[file]' =>
@@ -171,12 +202,98 @@ module Opsmgr
171
202
  'application/octet-stream',
172
203
  File.basename(installation_file)
173
204
  ),
174
- 'password' => password
205
+ import_secret_parameter_name => password
175
206
  )
176
207
 
177
- req.basic_auth(web_auth_user, web_auth_password)
208
+ add_auth_header(req) unless Gem::Version.new(@om_version) >= Gem::Version.new('1.7')
178
209
 
179
- http.request(req)
210
+ response = http.request(req)
211
+
212
+ if Gem::Version.new(@om_version) >= Gem::Version.new('1.7')
213
+ wait_for_uaa_to_be_available
214
+
215
+ ensure_env_admin_user_exists
216
+ end
217
+
218
+ response
219
+ end
220
+
221
+ def wait_for_uaa_to_be_available(sleep_interval = 10, max_retry_count = 24)
222
+ retry_count = 0
223
+
224
+ loop do
225
+ request = Net::HTTP::Get.new(
226
+ endpoints.login_ensure_availability
227
+ )
228
+
229
+ response = http.request(request)
230
+
231
+ http_location = response['location']
232
+
233
+ break if response.code == '302' && http_location == auth_redirect_location
234
+
235
+ retry_count += 1
236
+
237
+ if retry_count >= max_retry_count
238
+ retry_wait_time = sleep_interval * retry_count
239
+
240
+ raise("Timed out waiting for UAA to be available after #{retry_wait_time} seconds")
241
+ end
242
+
243
+ Kernel.sleep(sleep_interval)
244
+ end
245
+ end
246
+
247
+ def ensure_env_admin_user_exists
248
+ token = get_oauth_token('admin')
249
+
250
+ create_env_admin_user(token) unless verify_if_env_admin_user_exists?(token)
251
+ end
252
+
253
+ def verify_if_env_admin_user_exists?(token)
254
+ request = Net::HTTP::Get.new(
255
+ endpoints.uaa_get_user_by_username_path(web_auth_user)
256
+ )
257
+ request['Authorization'] = token
258
+
259
+ response = http.request(request)
260
+
261
+ if response.code != '200'
262
+ raise("Invalid response from UAA: #{response.code}")
263
+ end
264
+
265
+ reply = JSON.parse(response.body)
266
+
267
+ if reply['resources'].nil?
268
+ raise("Invalid response body from UAA: #{response.body}")
269
+ end
270
+
271
+ reply['resources'].length > 0
272
+ end
273
+
274
+ def create_env_admin_user(token)
275
+ request = Net::HTTP::Post.new(
276
+ endpoints.uaa_create_user
277
+ )
278
+ request['Content-Type'] = 'application/json'
279
+ request['Authorization'] = token
280
+ request.body = {
281
+ userName: web_auth_user,
282
+ password: web_auth_password,
283
+ emails: [
284
+ { value: web_auth_user }
285
+ ]
286
+ }.to_json
287
+
288
+ response = http.request(request)
289
+
290
+ if response.code != '201'
291
+ raise("Unexpected response from UAA: code #{response.code}, body: #{response.body}")
292
+ end
293
+ end
294
+
295
+ def import_secret_parameter_name
296
+ Gem::Version.new(@om_version) >= Gem::Version.new('1.7') ? 'passphrase' : 'password'
180
297
  end
181
298
 
182
299
  def import_stemcell(stemcell_file)
@@ -190,23 +307,48 @@ module Opsmgr
190
307
  )
191
308
  )
192
309
 
193
- req.basic_auth(web_auth_user, web_auth_password)
310
+ add_auth_header(req)
194
311
 
195
312
  http.request(req)
196
313
  end
197
314
 
198
315
  def read_timeout
199
- environment.settings.iaas_type != 'vcloud' ? 600 : 3600
316
+ environment.settings.dig('iaas_type') != 'vcloud' ? 600 : 3600
317
+ end
318
+
319
+ def auth_redirect_location
320
+ host = environment.settings.dig('ops_manager', 'url')
321
+
322
+ host.concat('/') unless host.end_with? '/'
323
+
324
+ host+'auth/cloudfoundry'
200
325
  end
201
326
 
202
327
  private
203
328
 
329
+ def add_auth_header(request)
330
+ if Gem::Version.new(@om_version) >= Gem::Version.new('1.7')
331
+ log.info('using oauth')
332
+ token = get_oauth_token(web_auth_user)
333
+ request['Authorization'] = token
334
+ else
335
+ log.info('using basic auth')
336
+ request.basic_auth(web_auth_user, web_auth_password)
337
+ end
338
+ end
339
+
340
+ def get_oauth_token(user)
341
+ target_url = environment.settings.dig('ops_manager', 'url') + '/uaa'
342
+ token_issuer = CF::UAA::TokenIssuer.new(target_url, 'opsman', nil, skip_ssl_validation: true)
343
+ token_issuer.owner_password_grant(user, web_auth_password).auth_header
344
+ end
345
+
204
346
  def web_auth_user
205
- environment.settings.ops_manager.username
347
+ environment.settings.dig('ops_manager', 'username')
206
348
  end
207
349
 
208
350
  def web_auth_password
209
- environment.settings.ops_manager.password
351
+ environment.settings.dig('ops_manager', 'password')
210
352
  end
211
353
 
212
354
  def http
@@ -221,7 +363,7 @@ module Opsmgr
221
363
 
222
364
  def get_with_basic_auth(path)
223
365
  req = Net::HTTP::Get.new(path)
224
- req.basic_auth(web_auth_user, web_auth_password)
366
+ add_auth_header(req)
225
367
 
226
368
  http.request(req)
227
369
  end
@@ -24,8 +24,12 @@ module Opsmgr
24
24
  '/api/installation'
25
25
  end
26
26
 
27
- def configure_user_path
28
- '/api/users'
27
+ def api_user_path
28
+ '/api/user'
29
+ end
30
+
31
+ def api_setup_path
32
+ '/api/setup'
29
33
  end
30
34
 
31
35
  def show_installation_status(id)
@@ -83,6 +87,18 @@ module Opsmgr
83
87
  def import_stemcell_path
84
88
  '/api/stemcells'
85
89
  end
90
+
91
+ def uaa_get_user_by_username_path(username)
92
+ "/uaa/Users?attributes=id&filter=userName%20eq%20'#{username}'"
93
+ end
94
+
95
+ def uaa_create_user
96
+ '/uaa/Users'
97
+ end
98
+
99
+ def login_ensure_availability
100
+ '/login/ensure_availability'
101
+ end
86
102
  end
87
103
  end
88
104
  end
@@ -1,38 +1,34 @@
1
1
  module Opsmgr
2
2
  class BoshCommandRunner
3
- def initialize(iaas_gateway:, bosh_command:, logger:)
4
- @iaas_gateway = iaas_gateway
3
+ def initialize(bosh_command:, logger:)
5
4
  @bosh_command = bosh_command
6
5
  @logger = logger
7
6
  end
8
7
 
9
8
  def run(command)
10
- iaas_gateway.gateway do
11
- system_or_fail(
12
- "#{bosh_command_prefix} #{command}",
13
- "bosh #{command} failed"
14
- )
15
- end
9
+ system_or_fail(
10
+ "#{bosh_command_prefix} #{command}",
11
+ "bosh #{command} failed"
12
+ )
16
13
  end
17
14
 
18
15
  def run_and_capture_output(command)
19
- iaas_gateway.gateway do
20
- capture_output_or_fail(
21
- "#{bosh_command_prefix} #{command}",
22
- "bosh #{command} failed"
23
- )
24
- end
16
+ capture_output_or_fail(
17
+ "#{bosh_command_prefix} #{command}",
18
+ "bosh #{command} failed"
19
+ )
25
20
  end
26
21
 
27
22
  private
28
23
 
29
24
  def system_or_fail(command, failure_message)
30
- logger.info("Running #{command}")
25
+ clean_and_log(command)
31
26
  Bundler.clean_system(command) || fail(failure_message)
32
27
  end
33
28
 
34
29
  def capture_output_or_fail(command, failure_message)
35
- logger.info("Running #{command}")
30
+ clean_and_log(command)
31
+
36
32
  Bundler.with_clean_env do
37
33
  output, status = Open3.capture2(command)
38
34
  fail(failure_message) unless status.success?
@@ -40,11 +36,19 @@ module Opsmgr
40
36
  end
41
37
  end
42
38
 
39
+ def clean_and_log(command)
40
+ scrubbed_command = command.slice(command.index('bosh')..-1)
41
+ scrubbed_command.sub!(/\s\-p\s\b\w+\b/, ' -p <PASSWORD>')
42
+ scrubbed_command.sub!(/\s\-u\s\b\w+\b/, ' -u <USER>')
43
+
44
+ logger.info("Running #{scrubbed_command}")
45
+ end
46
+
43
47
  def bosh_command_prefix
44
48
  @bosh_command_prefix ||= bosh_command.command
45
49
  end
46
50
 
47
- attr_reader :iaas_gateway, :bosh_command, :logger
51
+ attr_reader :bosh_command, :logger
48
52
  end
49
53
  end
50
54
  # Copyright (c) 2014-2015 Pivotal Software, Inc.