chef-provisioning-vra 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 498a6b8f2b9524f2dac8bf883e617760799d14f0
4
+ data.tar.gz: 57ad730009f8f67135296c4268e4f908fe124cdd
5
+ SHA512:
6
+ metadata.gz: 3212bb8f097d182a244a8e44fa12c503e0002e530939f612379b0d28641a6de4f68ba4165d095fef25765a60b4607bb7a87eff736fac921cde7fb4c7122e2b4a
7
+ data.tar.gz: dcf354a7c47311dde53312aa429989af49c17511b8817bf5165b4806928506b3cf3b6b8066cc01504ae70da4f97528d59954a5ba41fc40a9183c8e54d17640b2
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,14 @@
1
+ Metrics/AbcSize:
2
+ Max: 50
3
+ Metrics/ClassLength:
4
+ Max: 125
5
+ Metrics/LineLength:
6
+ Max: 130
7
+ Metrics/MethodLength:
8
+ Max: 25
9
+ Style/Documentation:
10
+ Enabled: false
11
+ Style/SignalException:
12
+ Enabled: false
13
+ Style/SpaceInsideBrackets:
14
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chef-provisioning-vra.gemspec
4
+ gemspec
5
+
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
@@ -0,0 +1,123 @@
1
+ # chef-provisioning-vra
2
+
3
+ Chef Provisioning driver for vRealize Automation
4
+
5
+ ## Configuring and Usage
6
+
7
+ First, configure Chef Provisioning with your vRA server and authentication information. One option is to place a `driver_options` section in your `knife.rb`:
8
+
9
+ ```ruby
10
+ driver_options username: 'my@email.address',
11
+ password: 's00pers33cret',
12
+ tenant: 'vsphere.local',
13
+ verify_ssl: false,
14
+ max_wait_time: 1800
15
+ ```
16
+
17
+ `username`, `password`, and `tenant` are required.
18
+
19
+ `verify_ssl` is optional and defaults to true. If you cannot configure your client or server with trusted certificates, you can set this to false.
20
+
21
+ `max_wait_time` is the number of seconds to wait for any operation where a request was submitted to vRA and Chef Provisioning must wait for it to complete. By default, this is 600.
22
+
23
+ Secondly, in your recipe, configure your bootstrap and transport options for your machine(s). You can do this using the `with_machine_options` method, or you can pass it directly to each machine's `machine_options` property:
24
+
25
+ ```ruby
26
+ machine_options = {
27
+ bootstrap_options: {
28
+ catalog_id: '7bd5f299-8c34-41f6-b63f-c2ac410ee0b6',
29
+ subtenant_id: 'my_subtenant_uuid',
30
+ cpus: 1,
31
+ memory: 1024,
32
+ requested_for: 'my@email.address',
33
+ lease_days: 30,
34
+ key_path: '/path/to/ssh/key'
35
+ },
36
+ transport_options: {
37
+ is_windows: false,
38
+ username: 'ubuntu',
39
+ }
40
+ }
41
+ ```
42
+
43
+ ### Driver URL
44
+
45
+ The driver URL should be `vra:https://my-vra-host.com`. Like any other Chef Provisioning driver, you may set this a variety of ways, including the `with_driver` method in your recipe:
46
+
47
+ ```ruby
48
+ with_driver 'vra:https://vra.corp.local' do
49
+ ... my machines here ...
50
+ end
51
+ ```
52
+
53
+ ### Bootstrap Options
54
+
55
+ The following properties are required:
56
+
57
+ * catalog_id
58
+ * cpus
59
+ * memory
60
+ * requested_for
61
+
62
+ `subtenant_id` is required if your catalog item is a global item and not tied to a specific business group / subtenant.
63
+
64
+ If you wish to use SSH keys, you must specify a `key_path` or `key_name`. Failure to specify one of those will cause an error. If you wish to use password authentication, specify a password in the transport_options.
65
+
66
+ You may specify additional extra parameters to be passed to the vRA API using the `extra_parameters` property hash:
67
+
68
+ ```ruby
69
+ bootstrap_options: {
70
+ extra_parameters: {
71
+ parameter1_name: {
72
+ type: 'string',
73
+ value: 'my value'
74
+ },
75
+ parameter2_name: {
76
+ type: 'integer',
77
+ value: 2
78
+ }
79
+ }
80
+ }
81
+ ```
82
+
83
+ ### Transport Options
84
+
85
+ All transport options are optional.
86
+
87
+ * `is_windows`: set this to `true` if you are creating a windows machine. Unfortunately, you must set this manually as vRA does not offer a consistent method for determining this through the API.
88
+ * `username`: set this to the SSH or WinRM user to use when logging in. Defaults to `root` on Unix and `Administrator` on Windows. On Unix, if a non-root user is supplied, Chef Provisioning will attempt to use sudo to gain the required privileges.
89
+ * `password`:
90
+ * *Windows*: required. Set to the user's password to use to log in via WinRM.
91
+ * *Unix*: optional. If set, SSH key authentication will not be used.
92
+
93
+ ### Resources
94
+
95
+ Only the `machine` resource is supported.
96
+
97
+ ## License and Authors
98
+
99
+ Author:: Chef Partner Engineering (<partnereng@chef.io>)
100
+
101
+ Copyright:: Copyright (c) 2015 Chef Software, Inc.
102
+
103
+ License:: Apache License, Version 2.0
104
+
105
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
106
+ this file except in compliance with the License. You may obtain a copy of the License at
107
+
108
+ ```
109
+ http://www.apache.org/licenses/LICENSE-2.0
110
+ ```
111
+
112
+ Unless required by applicable law or agreed to in writing, software distributed under the
113
+ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
114
+ either express or implied. See the License for the specific language governing permissions
115
+ and limitations under the License.
116
+
117
+ ## Contributing
118
+
119
+ 1. Fork it ( https://github.com/[my-github-username]/vmware-vra-gem/fork )
120
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
121
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
122
+ 4. Push to the branch (`git push origin my-new-feature`)
123
+ 5. Create a new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chef/provisioning/vra_driver/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'chef-provisioning-vra'
8
+ spec.version = Chef::Provisioning::VraDriver::VERSION
9
+ spec.authors = ['Chef Partner Engineering']
10
+ spec.email = ['partnereng@chef.io']
11
+ spec.summary = 'A Chef Provisioning driver for VMware vRealize Automation (vRA)'
12
+ spec.description = spec.summary
13
+ spec.homepage = 'https://github.com/chef-partners/chef-provisioning-vra'
14
+ spec.license = 'Apache 2.0'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = []
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'chef-provisioning'
22
+ spec.add_dependency 'vmware-vra', '~> 1.3.0'
23
+
24
+ spec.add_development_dependency 'chef', '>= 12'
25
+ spec.add_development_dependency 'bundler', '~> 1.7'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.2'
28
+ end
@@ -0,0 +1,21 @@
1
+ #
2
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/provisioning/vra_driver/driver'
20
+
21
+ Chef::Provisioning.register_driver_class('vra', Chef::Provisioning::VraDriver::Driver)
@@ -0,0 +1,385 @@
1
+ #
2
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/provisioning/driver'
20
+ require 'chef/provisioning/machine/windows_machine'
21
+ require 'chef/provisioning/machine/unix_machine'
22
+ require 'chef/provisioning/convergence_strategy/install_msi'
23
+ require 'chef/provisioning/convergence_strategy/install_cached'
24
+ require 'chef/provisioning/transport/winrm'
25
+ require 'chef/provisioning/transport/ssh'
26
+ require 'chef/provisioning/vra_driver/version'
27
+ require 'vra'
28
+
29
+ class Chef
30
+ module Provisioning
31
+ module VraDriver
32
+ class Driver < Chef::Provisioning::Driver # rubocop:disable Metrics/ClassLength
33
+ attr_reader :base_url
34
+
35
+ def self.from_url(driver_url, config)
36
+ Driver.new(driver_url, config)
37
+ end
38
+
39
+ def self.canonicalize_url(driver_url, config)
40
+ [ driver_url, config ]
41
+ end
42
+
43
+ def initialize(driver_url, config)
44
+ super
45
+
46
+ _, @base_url = driver_url.split(':', 2)
47
+ end
48
+
49
+ def allocate_machine(action_handler, machine_spec, machine_options)
50
+ resource = resource_for(machine_spec)
51
+ return unless resource.nil?
52
+
53
+ bootstrap_options = bootstrap_options_for(machine_options)
54
+ transport_options = transport_options_for(machine_options)
55
+
56
+ action_handler.perform_action("Create #{machine_spec.name} with catalog ID #{bootstrap_options[:catalog_id]}") do
57
+ resource = create_resource(action_handler, machine_spec, machine_options)
58
+ end
59
+
60
+ machine_spec.location = {
61
+ 'driver_url' => driver_url,
62
+ 'driver_version' => Chef::Provisioning::VraDriver::VERSION,
63
+ 'resource_id' => resource.id,
64
+ 'allocated_at' => Time.now.utc.to_s,
65
+ 'is_windows' => transport_options[:is_windows]
66
+ }
67
+ end
68
+
69
+ def ready_machine(action_handler, machine_spec, machine_options)
70
+ resource = resource_for(machine_spec)
71
+ raise "Unable to locate machine for #{machine_spec.name}" if resource.nil?
72
+
73
+ action_handler.report_progress("Powering on #{machine_spec.name} if needed")
74
+
75
+ action_handler.perform_action("Power on machine #{machine_spec.name}") do
76
+ power_on_machine(action_handler, resource)
77
+ end
78
+
79
+ action_handler.report_progress("Waiting for #{machine_spec.name} to be reachable")
80
+
81
+ transport = transport_for(machine_spec, machine_options)
82
+ action_handler.perform_action("Confirm #{machine_spec.name} is reachable") do
83
+ wait_for(action_handler) { transport.available? }
84
+ end
85
+
86
+ machine_for(machine_spec, machine_options)
87
+ end
88
+
89
+ def stop_machine(action_handler, machine_spec, _machine_options)
90
+ resource = resource_for(machine_spec)
91
+ raise "Unable to locate machine for #{machine_spec.name}" if resource.nil?
92
+
93
+ action_handler.report_progress("Submitting shutdown / power-off request for #{machine_spec.name}")
94
+
95
+ action_handler.perform_action("Powering off machine #{machine_spec.name}") do
96
+ power_off_machine(action_handler, resource)
97
+ end
98
+ end
99
+
100
+ def destroy_machine(action_handler, machine_spec, _machine_options)
101
+ resource = resource_for(machine_spec)
102
+ return if resource.nil?
103
+
104
+ action_handler.report_progress("Submitting destroy request for #{machine_spec.name}")
105
+
106
+ action_handler.perform_action("Destroy machine #{machine_spec.name}") do
107
+ resource.refresh
108
+
109
+ destroy_req = resource.destroy
110
+ wait_for(action_handler) do
111
+ destroy_req.refresh
112
+ destroy_req.completed?
113
+ end
114
+
115
+ raise "The vRA request failed: #{destroy_req.completion_details}" if destroy_req.failed?
116
+ end
117
+ end
118
+
119
+ def connect_to_machine(machine_spec, machine_options)
120
+ machine_for(machine_spec, machine_options)
121
+ end
122
+
123
+ def description(machine_spec)
124
+ "X-Chef-Provisioning:#{machine_spec.name}"
125
+ end
126
+
127
+ def resource_for(machine_spec)
128
+ return nil if machine_spec.location.nil?
129
+ return @resource if @resource
130
+
131
+ begin
132
+ @resource = vra_client.resources.by_id(machine_spec.location['resource_id'])
133
+ rescue Vra::Exception::NotFound
134
+ return nil
135
+ end
136
+ end
137
+
138
+ def create_resource(action_handler, machine_spec, machine_options)
139
+ action_handler.report_progress("Submitting catalog request for #{machine_spec.name}")
140
+
141
+ submitted_request = catalog_request(machine_spec, machine_options).submit
142
+ action_handler.report_progress("Catalog request #{submitted_request.id} submitted.")
143
+
144
+ wait_for(action_handler) do
145
+ submitted_request.refresh
146
+ submitted_request.completed?
147
+ end
148
+
149
+ raise "The vRA request failed: #{submitted_request.completion_details}" if submitted_request.failed?
150
+
151
+ servers = submitted_request.resources.select(&:vm?)
152
+ raise 'The vRA request created more than one server. The catalog blueprint should only return one.' if servers.size > 1
153
+ raise 'the vRA request did not create any servers.' if servers.size == 0
154
+
155
+ servers.first
156
+ end
157
+
158
+ def catalog_request(machine_spec, machine_options)
159
+ bootstrap_options = bootstrap_options_for(machine_options)
160
+ catalog_request = vra_client.catalog.request(bootstrap_options[:catalog_id])
161
+
162
+ catalog_request.notes = description(machine_spec)
163
+ catalog_request.cpus = bootstrap_options[:cpus]
164
+ catalog_request.memory = bootstrap_options[:memory]
165
+ catalog_request.requested_for = bootstrap_options[:requested_for]
166
+ catalog_request.lease_days = bootstrap_options[:lease_days] unless bootstrap_options[:lease_days].nil?
167
+ catalog_request.subtenant_id = bootstrap_options[:subtenant_id] unless bootstrap_options[:subtenant_id].nil?
168
+
169
+ if bootstrap_options.key?(:extra_parameters) && bootstrap_options[:extra_parameters].respond_to?(:each)
170
+ bootstrap_options[:extra_parameters].each do |key, value_data|
171
+ catalog_request.set_parameter(key, value_data[:type], value_data[:value])
172
+ end
173
+ end
174
+
175
+ catalog_request
176
+ end
177
+
178
+ def power_on_machine(action_handler, resource)
179
+ resource.refresh
180
+ return if resource.machine_on? || resource.machine_turning_on? || resource.machine_in_provisioned_state?
181
+
182
+ action_handler.report_progress("Machine status is #{resource.machine_status}. " \
183
+ "Submitting power-on request for resource #{resource.id}")
184
+ power_on_req = resource.poweron
185
+ wait_for(action_handler) do
186
+ power_on_req.refresh
187
+ power_on_req.completed?
188
+ end
189
+
190
+ raise "The vRA request failed: #{power_on_req.completion_details}" if power_on_req.failed?
191
+
192
+ action_handler.report_progress("Waiting for resource #{resource.id} to be powered on")
193
+ wait_for(action_handler) do
194
+ resource.refresh
195
+ resource.machine_on?
196
+ end
197
+ end
198
+
199
+ def power_off_machine(action_handler, resource)
200
+ resource.refresh
201
+ return if resource.machine_off? || resource.machine_turning_off?
202
+
203
+ action_handler.report_progress("Submitting shutdown/power-off request for resource #{resource.id}")
204
+ begin
205
+ power_off_req = resource.shutdown
206
+ rescue Vra::Exception::NotFound
207
+ power_off_req = resource.poweroff
208
+ end
209
+
210
+ wait_for(action_handler) do
211
+ power_off_req.refresh
212
+ power_off_req.completed?
213
+ end
214
+
215
+ raise "The vRA request failed: #{power_off_req.completion_details}" if power_off_req.failed?
216
+
217
+ action_handler.report_progress("Waiting for resource #{resource.id} to be powered off")
218
+ wait_for(action_handler) do
219
+ resource.refresh
220
+ resource.machine_off?
221
+ end
222
+ end
223
+
224
+ def max_wait_time
225
+ driver_options.fetch(:max_wait_time, 600).to_i
226
+ end
227
+
228
+ def wait_for(action_handler, &block)
229
+ sleep_time = 5
230
+ start_time = Time.now.utc.to_i
231
+
232
+ Timeout.timeout(max_wait_time) do
233
+ loop do
234
+ return if block.call == true
235
+
236
+ time_elapsed = Time.now.utc.to_i - start_time
237
+ action_handler.report_progress("been waiting #{time_elapsed}/#{max_wait_time} seconds" \
238
+ " -- sleeping #{sleep_time} seconds")
239
+ sleep sleep_time
240
+ end
241
+ end
242
+ end
243
+
244
+ def username_for(machine_spec, machine_options, default_username)
245
+ transport_options = transport_options_for(machine_options)
246
+
247
+ machine_spec.reference['username'] || transport_options[:username] || default_username
248
+ end
249
+
250
+ def create_winrm_transport(machine_spec, machine_options, resource)
251
+ transport_options = transport_options_for(machine_options)
252
+ remote_host = remote_host_for(machine_options, resource)
253
+ username = username_for(machine_spec, machine_options, 'Administrator')
254
+ url = "http://#{remote_host}:5985/wsman"
255
+ winrm_options = winrm_options_for(username, transport_options[:password])
256
+
257
+ Chef::Provisioning::Transport::WinRM.new(url, :plaintext, winrm_options, config)
258
+ end
259
+
260
+ def winrm_options_for(username, password)
261
+ auth_type_opt = username.include?('\\') ? :disable_sspi : :basic_auth_only
262
+
263
+ {
264
+ user: username,
265
+ pass: password,
266
+ auth_type_opt => true
267
+ }
268
+ end
269
+
270
+ def create_ssh_transport(machine_spec, machine_options, resource)
271
+ ssh_options = ssh_options_for(machine_spec, machine_options, resource)
272
+ remote_host = remote_host_for(machine_options, resource)
273
+ username = username_for(machine_spec, machine_options, 'root')
274
+ options = ssh_transport_options_for(machine_spec, username)
275
+
276
+ Chef::Provisioning::Transport::SSH.new(remote_host, username, ssh_options, options, config)
277
+ end
278
+
279
+ def ssh_transport_options_for(machine_spec, username)
280
+ options = {}
281
+ options[:prefix] = 'sudo ' if use_sudo?(machine_spec, username)
282
+ options[:ssh_pty_enable] = true
283
+ options[:ssh_gateway] = machine_spec.reference['ssh_gateway'] if machine_spec.reference['ssh_gateway']
284
+
285
+ options
286
+ end
287
+
288
+ def use_sudo?(machine_spec, username)
289
+ machine_spec.reference[:sudo] || (!machine_spec.reference.key?(:sudo) && username != 'root')
290
+ end
291
+
292
+ def remote_host_for(machine_options, resource)
293
+ transport_options = transport_options_for(machine_options)
294
+ if resource.ip_addresses.empty? || transport_options[:use_hostname]
295
+ resource.name
296
+ else
297
+ resource.ip_addresses.first
298
+ end
299
+ end
300
+
301
+ def transport_for(machine_spec, machine_options)
302
+ resource = resource_for(machine_spec)
303
+
304
+ if windows?(machine_spec)
305
+ create_winrm_transport(machine_spec, machine_options, resource)
306
+ else
307
+ create_ssh_transport(machine_spec, machine_options, resource)
308
+ end
309
+ end
310
+
311
+ def machine_for(machine_spec, machine_options)
312
+ if windows?(machine_spec)
313
+ Chef::Provisioning::Machine::WindowsMachine.new(machine_spec,
314
+ transport_for(machine_spec, machine_options),
315
+ convergence_strategy_for(machine_spec, machine_options))
316
+ else
317
+ Chef::Provisioning::Machine::UnixMachine.new(machine_spec,
318
+ transport_for(machine_spec, machine_options),
319
+ convergence_strategy_for(machine_spec, machine_options))
320
+ end
321
+ end
322
+
323
+ def convergence_strategy_for(machine_spec, machine_options)
324
+ if windows?(machine_spec)
325
+ Chef::Provisioning::ConvergenceStrategy::InstallMsi.new(machine_options[:convergence_options], config)
326
+ else
327
+ Chef::Provisioning::ConvergenceStrategy::InstallCached.new(machine_options[:convergence_options], config)
328
+ end
329
+ end
330
+
331
+ def ssh_options_for(machine_spec, machine_options, resource)
332
+ bootstrap_options = bootstrap_options_for(machine_options)
333
+ transport_options = transport_options_for(machine_options)
334
+
335
+ if transport_options.key?(:password)
336
+ ssh_options = {
337
+ auth_methods: [ 'password' ],
338
+ keys_only: false,
339
+ password: transport_options[:password]
340
+ }
341
+ else
342
+ ssh_options = {
343
+ auth_methods: [ 'publickey' ],
344
+ keys_only: true
345
+ }
346
+
347
+ if bootstrap_options[:key_path]
348
+ ssh_options[:key_data] = [ IO.read(bootstrap_options[:key_path]) ]
349
+ elsif bootstrap_options[:key_name]
350
+ ssh_options[:key_data] = [ get_private_key(bootstrap_options[:key_name]) ]
351
+ else
352
+ raise "No key found to connect to #{machine_spec.name}" \
353
+ " - set a key_path or key_name in the machine's bootstrap_options"
354
+ end
355
+ end
356
+
357
+ ssh_options[:host_key_alias] = "#{resource.id}.vra"
358
+ ssh_options.merge(machine_options[:ssh_options] || {})
359
+ end
360
+
361
+ def vra_client
362
+ @vra_client ||= ::Vra::Client.new(
363
+ base_url: base_url,
364
+ username: driver_options[:username],
365
+ password: driver_options[:password],
366
+ tenant: driver_options[:tenant],
367
+ verify_ssl: driver_options[:verify_ssl]
368
+ )
369
+ end
370
+
371
+ def transport_options_for(machine_options)
372
+ machine_options.key?(:transport_options) ? machine_options[:transport_options] : {}
373
+ end
374
+
375
+ def bootstrap_options_for(machine_options)
376
+ machine_options.key?(:bootstrap_options) ? machine_options[:bootstrap_options] : {}
377
+ end
378
+
379
+ def windows?(machine_spec)
380
+ machine_spec.location['is_windows'] ? true : false
381
+ end
382
+ end
383
+ end
384
+ end
385
+ end