chef-provisioning-vagrant 0.7

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: 5b65e742bcaaae7b84820ac78c83b0988dc8b146
4
+ data.tar.gz: 55cfb0f6afd757fa0bb62e322a284edd7dab8ade
5
+ SHA512:
6
+ metadata.gz: 2e9e0f4f7fd9559a58e1a5c9e1b232709e238db79d95ddd2ef539676b7f04a37f93d9daf0e78a99e68ae7024398f31c2efaed90e970d95eb411ae1e411b16656
7
+ data.tar.gz: d5d6e1de4f0feea055943ef96a2c05ce48f169e32f6d74a79f81c14410c480b7a7aff0ae83de141390015a1d46d8d6edd3842f55c9d50939631446bf504dd0fb
data/LICENSE ADDED
@@ -0,0 +1,201 @@
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.
@@ -0,0 +1,3 @@
1
+ # chef-metal-vagrant
2
+
3
+ This is the Vagrant driver for chef-metal.
@@ -0,0 +1,6 @@
1
+ require 'bundler'
2
+ require 'bundler/gem_tasks'
3
+
4
+ task :spec do
5
+ require File.expand_path('spec/run')
6
+ end
@@ -0,0 +1,44 @@
1
+ require 'chef/provider/lwrp_base'
2
+ require 'chef/mixin/shell_out'
3
+
4
+ class Chef::Provider::VagrantBox < Chef::Provider::LWRPBase
5
+
6
+ use_inline_resources
7
+
8
+ include Chef::Mixin::ShellOut
9
+
10
+ def whyrun_supported?
11
+ true
12
+ end
13
+
14
+ action :create do
15
+ if !list_boxes.has_key?(new_resource.name)
16
+ if new_resource.url
17
+ converge_by "run 'vagrant box add #{new_resource.name} #{new_resource.url}'" do
18
+ shell_out("vagrant box add #{new_resource.name} #{new_resource.url}").error!
19
+ end
20
+ else
21
+ raise "Box #{new_resource.name} does not exist"
22
+ end
23
+ end
24
+ end
25
+
26
+ action :delete do
27
+ if list_boxes.has_key?(new_resource.name)
28
+ converge_by "run 'vagrant box remove #{new_resource.name} #{list_boxes[new_resource.name]}'" do
29
+ shell_out("vagrant box remove #{new_resource.name} #{list_boxes[new_resource.name]}").error!
30
+ end
31
+ end
32
+ end
33
+
34
+ def list_boxes
35
+ @list_boxes ||= shell_out("vagrant box list").stdout.lines.inject({}) do |result, line|
36
+ line =~ /^(\S+)\s+\((.+)\)\s*$/
37
+ result[$1] = $2
38
+ result
39
+ end
40
+ end
41
+
42
+ def load_current_resource
43
+ end
44
+ end
@@ -0,0 +1,40 @@
1
+ require 'chef/provider/lwrp_base'
2
+ require 'cheffish'
3
+
4
+ class Chef::Provider::VagrantCluster < Chef::Provider::LWRPBase
5
+
6
+ use_inline_resources
7
+
8
+ def whyrun_supported?
9
+ true
10
+ end
11
+
12
+ action :create do
13
+ the_base_path = new_resource.path
14
+ Cheffish.inline_resource(self, :create) do
15
+ directory the_base_path
16
+ file ::File.join(the_base_path, 'Vagrantfile') do
17
+ content <<EOM
18
+ Dir.glob('#{::File.join(the_base_path, '*.vm')}') do |vm_file|
19
+ eval(IO.read(vm_file), nil, vm_file)
20
+ end
21
+ EOM
22
+ end
23
+ end
24
+ end
25
+
26
+ action :delete do
27
+ the_base_path = new_resource.path
28
+ Cheffish.inline_resource(self, :delete) do
29
+ file ::File.join(the_base_path, 'Vagrantfile') do
30
+ action :delete
31
+ end
32
+ directory the_base_path do
33
+ action :delete
34
+ end
35
+ end
36
+ end
37
+
38
+ def load_current_resource
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ require 'chef/provisioning/vagrant_driver/driver'
2
+
3
+ ChefMetal.register_driver_class("vagrant", Chef::Provisioning::VagrantDriver::Driver)
@@ -0,0 +1,35 @@
1
+ require 'chef_metal'
2
+ require 'chef/resource/vagrant_cluster'
3
+ require 'chef/provider/vagrant_cluster'
4
+ require 'chef/resource/vagrant_box'
5
+ require 'chef/provider/vagrant_box'
6
+ require 'chef/provisioning/vagrant_driver/driver'
7
+
8
+ class Chef
9
+ module Provisioning
10
+ module VagrantDriver
11
+ def self.with_vagrant_box(run_context, box_name, vagrant_options = {}, &block)
12
+ if box_name.is_a?(Chef::Resource::VagrantBox)
13
+ new_options = { :vagrant_options => { 'vm.box' => box_name.name } }
14
+ new_options[:vagrant_options]['vm.box_url'] = box_name.url if box_name.url
15
+ else
16
+ new_options = { :vagrant_options => { 'vm.box' => box_name } }
17
+ end
18
+
19
+ run_context.chef_metal.add_machine_options(new_options, &block)
20
+ end
21
+ end
22
+ end
23
+
24
+ module DSL
25
+ module Recipe
26
+ def with_vagrant_cluster(cluster_path, &block)
27
+ with_driver("vagrant:#{cluster_path}", &block)
28
+ end
29
+
30
+ def with_vagrant_box(box_name, vagrant_options = {}, &block)
31
+ Chef::Provisioning::VagrantDriver.with_vagrant_box(run_context, box_name, vagrant_options, &block)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,471 @@
1
+ require 'chef/mixin/shell_out'
2
+ require 'chef_metal/driver'
3
+ require 'chef_metal/machine/windows_machine'
4
+ require 'chef_metal/machine/unix_machine'
5
+ require 'chef_metal/convergence_strategy/install_msi'
6
+ require 'chef_metal/convergence_strategy/install_cached'
7
+ require 'chef_metal/transport/winrm'
8
+ require 'chef_metal/transport/ssh'
9
+ require 'chef/provisioning/vagrant_driver/version'
10
+ require 'chef/resource/vagrant_cluster'
11
+ require 'chef/provider/vagrant_cluster'
12
+
13
+ class Chef
14
+ module Provisioning
15
+ module VagrantDriver
16
+ # Provisions machines in vagrant.
17
+ class Driver < ChefMetal::Driver
18
+
19
+ include Chef::Mixin::ShellOut
20
+
21
+ # Create a new vagrant driver.
22
+ #
23
+ # ## Parameters
24
+ # cluster_path - path to the directory containing the vagrant files, which
25
+ # should have been created with the vagrant_cluster resource.
26
+ def initialize(driver_url, config)
27
+ super
28
+ scheme, cluster_path = driver_url.split(':', 2)
29
+ @cluster_path = cluster_path
30
+ end
31
+
32
+ attr_reader :cluster_path
33
+
34
+ def self.from_url(driver_url, config)
35
+ Driver.new(driver_url, config)
36
+ end
37
+
38
+ def self.canonicalize_url(driver_url, config)
39
+ scheme, cluster_path = driver_url.split(':', 2)
40
+ cluster_path = File.expand_path(cluster_path || File.join(Chef::Config.config_dir, 'vms'))
41
+ "vagrant:#{cluster_path}"
42
+ end
43
+
44
+ # Acquire a machine, generally by provisioning it. Returns a Machine
45
+ # object pointing at the machine, allowing useful actions like setup,
46
+ # converge, execute, file and directory.
47
+ def allocate_machine(action_handler, machine_spec, machine_options)
48
+ ensure_vagrant_cluster(action_handler)
49
+ vm_name = machine_spec.name
50
+ vm_file_path = File.join(cluster_path, "#{machine_spec.name}.vm")
51
+ vm_file_updated = create_vm_file(action_handler, vm_name, vm_file_path, machine_options)
52
+ if vm_file_updated || !machine_spec.location
53
+ old_location = machine_spec.location
54
+ machine_spec.location = {
55
+ 'driver_url' => driver_url,
56
+ 'driver_version' => Chef::Provisioning::VagrantDriver::VERSION,
57
+ 'vm_name' => vm_name,
58
+ 'vm_file_path' => vm_file_path,
59
+ 'allocated_at' => Time.now.utc.to_s,
60
+ 'host_node' => action_handler.host_node
61
+ }
62
+ machine_spec.location['needs_reload'] = true if vm_file_updated
63
+ if machine_options[:vagrant_options]
64
+ %w(vm.guest winrm.host winrm.port winrm.username winrm.password).each do |key|
65
+ machine_spec.location[key] = machine_options[:vagrant_options][key] if machine_options[:vagrant_options][key]
66
+ end
67
+ end
68
+ machine_spec.location['chef_client_timeout'] = machine_options[:chef_client_timeout] if machine_options[:chef_client_timeout]
69
+ end
70
+ end
71
+
72
+ def ready_machine(action_handler, machine_spec, machine_options)
73
+ start_machine(action_handler, machine_spec, machine_options)
74
+ machine_for(machine_spec, machine_options)
75
+ end
76
+
77
+ # Connect to machine without acquiring it
78
+ def connect_to_machine(machine_spec, machine_options)
79
+ machine_for(machine_spec, machine_options)
80
+ end
81
+
82
+ def destroy_machine(action_handler, machine_spec, machine_options)
83
+ if machine_spec.location
84
+ vm_name = machine_spec.location['vm_name']
85
+ current_status = vagrant_status(vm_name)
86
+ if current_status != 'not created'
87
+ action_handler.perform_action "run vagrant destroy -f #{vm_name} (status was '#{current_status}')" do
88
+ result = shell_out("vagrant destroy -f #{vm_name}", :cwd => cluster_path)
89
+ if result.exitstatus != 0
90
+ raise "vagrant destroy failed!\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}"
91
+ end
92
+ end
93
+ end
94
+
95
+ convergence_strategy_for(machine_spec, machine_options).
96
+ cleanup_convergence(action_handler, machine_spec)
97
+
98
+ vm_file_path = machine_spec.location['vm_file_path']
99
+ ChefMetal.inline_resource(action_handler) do
100
+ file vm_file_path do
101
+ action :delete
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ def stop_machine(action_handler, machine_spec, machine_options)
108
+ if machine_spec.location
109
+ vm_name = machine_spec.location['vm_name']
110
+ current_status = vagrant_status(vm_name)
111
+ if current_status == 'running'
112
+ action_handler.perform_action "run vagrant halt #{vm_name} (status was '#{current_status}')" do
113
+ result = shell_out("vagrant halt #{vm_name}", :cwd => cluster_path)
114
+ if result.exitstatus != 0
115
+ raise "vagrant halt failed!\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}"
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
121
+
122
+ def ready_machines(action_handler, specs_and_options, parallelizer)
123
+ start_machines(action_handler, specs_and_options)
124
+ machines = []
125
+ specs_and_options.each_pair do |spec, options|
126
+ machine = machine_for(spec, options)
127
+ machines << machine
128
+ yield machine if block_given?
129
+ end
130
+ machines
131
+ end
132
+
133
+ def destroy_machines(action_handler, specs_and_options, parallelizer)
134
+ all_names = []
135
+ all_status = []
136
+ all_outputs = {}
137
+ specs_and_options.each_key do |spec|
138
+ if spec.location
139
+ vm_name = spec.location['vm_name']
140
+ current_status = vagrant_status(vm_name)
141
+ if current_status != 'not created'
142
+ all_names.push(vm_name)
143
+ all_status.push(current_status)
144
+ end
145
+ end
146
+ end
147
+ if all_names.length > 0
148
+ names = all_names.join(" ")
149
+ statuses = all_status.join(", ")
150
+ action_handler.perform_action "run vagrant destroy -f #{names} (status was '#{statuses}')" do
151
+ result = shell_out("vagrant destroy -f #{names}", :cwd => cluster_path)
152
+ if result.exitstatus != 0
153
+ raise "vagrant destroy failed!\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}"
154
+ end
155
+ end
156
+ end
157
+ specs_and_options.each_pair do |spec, options|
158
+ convergence_strategy_for(spec, options).cleanup_convergence(action_handler, spec)
159
+
160
+ vm_file_path = spec.location['vm_file_path']
161
+ ChefMetal.inline_resource(action_handler) do
162
+ file vm_file_path do
163
+ action :delete
164
+ end
165
+ end
166
+ yield spec if block_given?
167
+ end
168
+ end
169
+
170
+ def stop_machines(action_handler, specs_and_options, parallelizer)
171
+ all_names = []
172
+ specs_and_options.each_key do |spec|
173
+ if spec.location
174
+ vm_name = spec.location['vm_name']
175
+ current_status = vagrant_status(vm_name)
176
+ if current_status == 'running'
177
+ all_names.push(vm_name)
178
+ end
179
+ end
180
+ end
181
+ if all_names.length > 0
182
+ names = all_names.join(" ")
183
+ action_handler.perform_action "run vagrant halt #{names} (status was 'running')" do
184
+ result = shell_out("vagrant halt #{names}", :cwd => cluster_path)
185
+ if result.exitstatus != 0
186
+ raise "vagrant halt failed!\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}"
187
+ end
188
+ end
189
+ end
190
+ end
191
+
192
+ # Used by vagrant_cluster and machine to get the string used to configure vagrant
193
+ def self.vagrant_config_string(vagrant_config, variable, line_prefix)
194
+ hostname = name.gsub(/[^A-Za-z0-9\-]/, '-')
195
+
196
+ result = ''
197
+ vagrant_config.each_pair do |key, value|
198
+ result += "#{line_prefix}#{variable}.#{key} = #{value.inspect}\n"
199
+ end
200
+ result
201
+ end
202
+
203
+ def driver_url
204
+ "vagrant:#{cluster_path}"
205
+ end
206
+
207
+ protected
208
+
209
+ def ensure_vagrant_cluster(action_handler)
210
+ _cluster_path = cluster_path
211
+ ChefMetal.inline_resource(action_handler) do
212
+ vagrant_cluster _cluster_path
213
+ end
214
+ end
215
+
216
+ def create_vm_file(action_handler, vm_name, vm_file_path, machine_options)
217
+ # Determine contents of vm file
218
+ vm_file_content = "Vagrant.configure('2') do |outer_config|\n"
219
+ vm_file_content << " outer_config.vm.define #{vm_name.inspect} do |config|\n"
220
+ merged_vagrant_options = { 'vm.hostname' => vm_name }
221
+ if machine_options[:vagrant_options]
222
+ merged_vagrant_options = Cheffish::MergedConfig.new(machine_options[:vagrant_options], merged_vagrant_options)
223
+ end
224
+ merged_vagrant_options.each_pair do |key, value|
225
+ vm_file_content << " config.#{key} = #{value.inspect}\n"
226
+ end
227
+ vm_file_content << machine_options[:vagrant_config] if machine_options[:vagrant_config]
228
+ vm_file_content << " end\nend\n"
229
+
230
+ # Set up vagrant file
231
+ ChefMetal.inline_resource(action_handler) do
232
+ file vm_file_path do
233
+ content vm_file_content
234
+ action :create
235
+ end
236
+ end
237
+ end
238
+
239
+ def start_machine(action_handler, machine_spec, machine_options)
240
+ vm_name = machine_spec.location['vm_name']
241
+ up_timeout = machine_options[:up_timeout] || 10*60
242
+
243
+ current_status = vagrant_status(vm_name)
244
+ vm_file_updated = machine_spec.location['needs_reload']
245
+ machine_spec.location['needs_reload'] = false
246
+ if current_status != 'running'
247
+ # Run vagrant up if vm is not running
248
+ action_handler.perform_action "run vagrant up #{vm_name} (status was '#{current_status}')" do
249
+ result = shell_out("vagrant up #{vm_name}", :cwd => cluster_path,
250
+ :timeout => up_timeout)
251
+ if result.exitstatus != 0
252
+ raise "vagrant up #{vm_name} failed!\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}"
253
+ end
254
+ parse_vagrant_up(result.stdout, machine_spec)
255
+ end
256
+ elsif vm_file_updated
257
+ # Run vagrant reload if vm is running and vm file changed
258
+ action_handler.perform_action "run vagrant reload #{vm_name}" do
259
+ result = shell_out("vagrant reload #{vm_name}", :cwd => cluster_path,
260
+ :timeout => up_timeout)
261
+ if result.exitstatus != 0
262
+ raise "vagrant reload #{vm_name} failed!\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}"
263
+ end
264
+ parse_vagrant_up(result.stdout, machine_spec)
265
+ end
266
+ end
267
+ end
268
+
269
+ def start_machines(action_handler, specs_and_options)
270
+ up_names = []
271
+ up_status = []
272
+ up_specs = {}
273
+ update_names = []
274
+ update_specs = {}
275
+ timeouts = []
276
+ specs_and_options.each_pair do |spec, options|
277
+ vm_name = spec.location['vm_name']
278
+
279
+ vm_file_updated = spec.location['needs_reload']
280
+ spec.location['needs_reload'] = false
281
+
282
+ current_status = vagrant_status(vm_name)
283
+ if current_status != 'running'
284
+ up_names.push(vm_name)
285
+ up_status.push(current_status)
286
+ up_specs[vm_name] = spec
287
+ elsif vm_file_updated
288
+ update_names.push(vm_name)
289
+ update_specs[vm_name] = spec
290
+ end
291
+ timeouts.push(options[:up_timeout])
292
+ end
293
+ # Use the highest timeout, if any exist
294
+ up_timeout = timeouts.compact.max
295
+ up_timeout ||= 10*60
296
+ if up_names.length > 0
297
+ # Run vagrant up if vm is not running
298
+ names = up_names.join(" ")
299
+ statuses = up_status.join(", ")
300
+ action_handler.perform_action "run vagrant up --parallel #{names} (status was '#{statuses}')" do
301
+ result = shell_out("vagrant up --parallel #{names}", :cwd => cluster_path,
302
+ :timeout => up_timeout)
303
+ if result.exitstatus != 0
304
+ raise "vagrant up #{names} failed!\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}"
305
+ end
306
+ parse_multi_vagrant_up(result.stdout, up_specs)
307
+ end
308
+ end
309
+ if update_names.length > 0
310
+ names = update_names.join(" ")
311
+ # Run vagrant reload if vm is running and vm file changed
312
+ action_handler.perform_action "run vagrant reload #{names}" do
313
+ result = shell_out("vagrant reload #{names}", :cwd => cluster_path,
314
+ :timeout => up_timeout)
315
+ if result.exitstatus != 0
316
+ raise "vagrant reload #{names} failed!\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}"
317
+ end
318
+ parse_multi_vagrant_up(result.stdout, update_specs)
319
+ end
320
+ end
321
+ end
322
+
323
+ def parse_vagrant_up(output, machine_spec)
324
+ # Grab forwarded port info
325
+ machine_spec.location['forwarded_ports'] = {}
326
+ in_forwarding_ports = false
327
+ output.lines.each do |line|
328
+ if in_forwarding_ports
329
+ if line =~ /-- (\d+) => (\d+)/
330
+ machine_spec.location['forwarded_ports'][$1] = $2
331
+ else
332
+ in_forwarding_ports = false
333
+ end
334
+ elsif line =~ /Forwarding ports...$/
335
+ in_forwarding_ports = true
336
+ end
337
+ end
338
+ end
339
+
340
+ def parse_multi_vagrant_up(output, all_machine_specs)
341
+ # Grab forwarded port info
342
+ in_forwarding_ports = {}
343
+ all_machine_specs.each_pair do |key, spec|
344
+ spec.location['forwarded_ports'] = {}
345
+ in_forwarding_ports[key] = false
346
+ end
347
+ output.lines.each do |line|
348
+ /^\[(.*?)\]/.match(line)
349
+ node_name = $1
350
+ if in_forwarding_ports[node_name]
351
+ if line =~ /-- (\d+) => (\d+)/
352
+ spec = all_machine_specs[node_name]
353
+ spec.location['forwarded_ports'][$1] = $2
354
+ else
355
+ in_forwarding_ports[node_name] = false
356
+ end
357
+ elsif line =~ /Forwarding ports...$/
358
+ in_forwarding_ports[node_name] = true
359
+ end
360
+ end
361
+ end
362
+
363
+ def machine_for(machine_spec, machine_options)
364
+ if machine_spec.location['vm.guest'].to_s == 'windows'
365
+ ChefMetal::Machine::WindowsMachine.new(machine_spec, transport_for(machine_spec),
366
+ convergence_strategy_for(machine_spec, machine_options))
367
+ else
368
+ ChefMetal::Machine::UnixMachine.new(machine_spec, transport_for(machine_spec),
369
+ convergence_strategy_for(machine_spec, machine_options))
370
+ end
371
+ end
372
+
373
+ def convergence_strategy_for(machine_spec, machine_options)
374
+ if machine_spec.location['vm.guest'].to_s == 'windows'
375
+ @windows_convergence_strategy ||= begin
376
+ ChefMetal::ConvergenceStrategy::InstallMsi.
377
+ new(machine_options[:convergence_options], config)
378
+ end
379
+ else
380
+ @unix_convergence_strategy ||= begin
381
+ ChefMetal::ConvergenceStrategy::InstallCached.
382
+ new(machine_options[:convergence_options], config)
383
+ end
384
+ end
385
+ end
386
+
387
+ def transport_for(machine_spec)
388
+ if machine_spec.location['vm.guest'].to_s == 'windows'
389
+ create_winrm_transport(machine_spec)
390
+ else
391
+ create_ssh_transport(machine_spec)
392
+ end
393
+ end
394
+
395
+ def vagrant_status(name)
396
+ status_output = shell_out("vagrant status #{name}", :cwd => cluster_path).stdout
397
+ if status_output =~ /^#{name}\s+(.+)\s+\((.+)\)$/
398
+ $1
399
+ else
400
+ 'not created'
401
+ end
402
+ end
403
+
404
+ def create_winrm_transport(machine_spec)
405
+ forwarded_ports = machine_spec.location['forwarded_ports']
406
+
407
+ # TODO IPv6 loopback? What do we do for that?
408
+ hostname = machine_spec.location['winrm.host'] || '127.0.0.1'
409
+ port = machine_spec.location['winrm.port'] || 5985
410
+ port = forwarded_ports[port] if forwarded_ports[port]
411
+ endpoint = "http://#{hostname}:#{port}/wsman"
412
+ type = :plaintext
413
+ options = {
414
+ :user => machine_spec.location['winrm.username'] || 'vagrant',
415
+ :pass => machine_spec.location['winrm.password'] || 'vagrant',
416
+ :disable_sspi => true
417
+ }
418
+
419
+ ChefMetal::Transport::WinRM.new(endpoint, type, options)
420
+ end
421
+
422
+ def create_ssh_transport(machine_spec)
423
+ vagrant_ssh_config = vagrant_ssh_config_for(machine_spec)
424
+ hostname = vagrant_ssh_config['HostName']
425
+ username = vagrant_ssh_config['User']
426
+ ssh_options = {
427
+ :port => vagrant_ssh_config['Port'],
428
+ :auth_methods => ['publickey'],
429
+ :user_known_hosts_file => vagrant_ssh_config['UserKnownHostsFile'],
430
+ :paranoid => yes_or_no(vagrant_ssh_config['StrictHostKeyChecking']),
431
+ :keys => [ strip_quotes(vagrant_ssh_config['IdentityFile']) ],
432
+ :keys_only => yes_or_no(vagrant_ssh_config['IdentitiesOnly'])
433
+ }
434
+ ssh_options[:auth_methods] = %w(password) if yes_or_no(vagrant_ssh_config['PasswordAuthentication'])
435
+ options = {
436
+ :prefix => 'sudo '
437
+ }
438
+ ChefMetal::Transport::SSH.new(hostname, username, ssh_options, options, config)
439
+ end
440
+
441
+ def vagrant_ssh_config_for(machine_spec)
442
+ vagrant_ssh_config = {}
443
+ result = shell_out("vagrant ssh-config #{machine_spec.location['vm_name']}",
444
+ :cwd => cluster_path)
445
+ result.stdout.lines.inject({}) do |result, line|
446
+ line =~ /^\s*(\S+)\s+(.+?)(\r\n|\r|\n|\z)/
447
+ vagrant_ssh_config[$1] = $2
448
+ end
449
+ vagrant_ssh_config
450
+ end
451
+
452
+ def yes_or_no(str)
453
+ case str
454
+ when 'yes'
455
+ true
456
+ else
457
+ false
458
+ end
459
+ end
460
+
461
+ def strip_quotes(str)
462
+ if str[0] == '"' && str[-1] == '"' && str.size >= 2
463
+ str[1..-2]
464
+ else
465
+ str
466
+ end
467
+ end
468
+ end
469
+ end
470
+ end
471
+ end
@@ -0,0 +1,7 @@
1
+ class Chef
2
+ module Provisioning
3
+ module VagrantDriver
4
+ VERSION = '0.7'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ require 'chef/resource/lwrp_base'
2
+ require 'chef/provisioning/vagrant_driver'
3
+
4
+ class Chef::Resource::VagrantBox < Chef::Resource::LWRPBase
5
+ self.resource_name = 'vagrant_box'
6
+
7
+ actions :create, :delete, :nothing
8
+ default_action :create
9
+
10
+ attribute :name, :kind_of => String, :name_attribute => true
11
+ attribute :url, :kind_of => String
12
+ attribute :driver_options, :kind_of => Hash
13
+
14
+ def after_created
15
+ super
16
+ Chef::Provisioning::VagrantDriver.with_vagrant_box run_context, self
17
+ end
18
+
19
+ # We are not interested in Chef's cloning behavior here.
20
+ def load_prior_resource
21
+ Chef::Log.debug("Overloading #{resource_name}.load_prior_resource with NOOP")
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ require 'chef/resource/lwrp_base'
2
+ require 'chef/provisioning/vagrant_driver'
3
+
4
+ class Chef::Resource::VagrantCluster < Chef::Resource::LWRPBase
5
+ self.resource_name = 'vagrant_cluster'
6
+
7
+ actions :create, :delete, :nothing
8
+ default_action :create
9
+
10
+ attribute :path, :kind_of => String, :name_attribute => true
11
+
12
+ def after_created
13
+ super
14
+ run_context.chef_metal.with_driver "vagrant:#{path}"
15
+ end
16
+
17
+ # We are not interested in Chef's cloning behavior here.
18
+ def load_prior_resource
19
+ Chef::Log.debug("Overloading #{resource_name}.load_prior_resource with NOOP")
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chef-provisioning-vagrant
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.7'
5
+ platform: ruby
6
+ authors:
7
+ - John Keiser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chef
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: chef-provisioning
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.15'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Driver for creating Vagrant instances in Chef Provisioning.
70
+ email: jkeiser@getchef.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files:
74
+ - README.md
75
+ - LICENSE
76
+ files:
77
+ - LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - lib/chef/provider/vagrant_box.rb
81
+ - lib/chef/provider/vagrant_cluster.rb
82
+ - lib/chef/provisioning/driver_init/vagrant.rb
83
+ - lib/chef/provisioning/vagrant_driver.rb
84
+ - lib/chef/provisioning/vagrant_driver/driver.rb
85
+ - lib/chef/provisioning/vagrant_driver/version.rb
86
+ - lib/chef/resource/vagrant_box.rb
87
+ - lib/chef/resource/vagrant_cluster.rb
88
+ homepage: https://github.com/opscode/chef-provisioning-vagrant
89
+ licenses: []
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Driver for creating Vagrant instances in Chef Provisioning.
111
+ test_files: []
112
+ has_rdoc: