vagrant-vmware-free 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/.gitignore +5 -0
  2. data/.ruby-version +1 -0
  3. data/Gemfile +7 -0
  4. data/Gemfile.lock +57 -0
  5. data/LICENSE +21 -0
  6. data/README.md +10 -0
  7. data/Rakefile +3 -0
  8. data/lib/vagrant_vmware_free.rb +5 -0
  9. data/lib/vagrant_vmware_free/action.rb +214 -0
  10. data/lib/vagrant_vmware_free/action/boot.rb +23 -0
  11. data/lib/vagrant_vmware_free/action/check_accessible.rb +23 -0
  12. data/lib/vagrant_vmware_free/action/check_created.rb +21 -0
  13. data/lib/vagrant_vmware_free/action/check_guest_additions.rb +45 -0
  14. data/lib/vagrant_vmware_free/action/check_running.rb +21 -0
  15. data/lib/vagrant_vmware_free/action/check_virtualbox.rb +22 -0
  16. data/lib/vagrant_vmware_free/action/clean_machine_folder.rb +43 -0
  17. data/lib/vagrant_vmware_free/action/clear_forwarded_ports.rb +18 -0
  18. data/lib/vagrant_vmware_free/action/clear_network_interfaces.rb +31 -0
  19. data/lib/vagrant_vmware_free/action/clear_shared_folders.rb +17 -0
  20. data/lib/vagrant_vmware_free/action/created.rb +20 -0
  21. data/lib/vagrant_vmware_free/action/customize.rb +43 -0
  22. data/lib/vagrant_vmware_free/action/destroy.rb +19 -0
  23. data/lib/vagrant_vmware_free/action/destroy_unused_network_interfaces.rb +23 -0
  24. data/lib/vagrant_vmware_free/action/discard_state.rb +20 -0
  25. data/lib/vagrant_vmware_free/action/export.rb +57 -0
  26. data/lib/vagrant_vmware_free/action/forced_halt.rb +25 -0
  27. data/lib/vagrant_vmware_free/action/forward_ports.rb +89 -0
  28. data/lib/vagrant_vmware_free/action/get_network_address.rb +27 -0
  29. data/lib/vagrant_vmware_free/action/import.rb +56 -0
  30. data/lib/vagrant_vmware_free/action/is_paused.rb +20 -0
  31. data/lib/vagrant_vmware_free/action/is_running.rb +20 -0
  32. data/lib/vagrant_vmware_free/action/is_saved.rb +20 -0
  33. data/lib/vagrant_vmware_free/action/match_mac_address.rb +21 -0
  34. data/lib/vagrant_vmware_free/action/message_already_running.rb +16 -0
  35. data/lib/vagrant_vmware_free/action/message_not_created.rb +16 -0
  36. data/lib/vagrant_vmware_free/action/message_not_running.rb +16 -0
  37. data/lib/vagrant_vmware_free/action/message_will_not_destroy.rb +17 -0
  38. data/lib/vagrant_vmware_free/action/network.rb +424 -0
  39. data/lib/vagrant_vmware_free/action/package.rb +20 -0
  40. data/lib/vagrant_vmware_free/action/package_vagrantfile.rb +33 -0
  41. data/lib/vagrant_vmware_free/action/prepare_forwarded_port_collision_params.rb +35 -0
  42. data/lib/vagrant_vmware_free/action/prepare_nfs_settings.rb +69 -0
  43. data/lib/vagrant_vmware_free/action/prune_nfs_exports.rb +20 -0
  44. data/lib/vagrant_vmware_free/action/resume.rb +26 -0
  45. data/lib/vagrant_vmware_free/action/sane_defaults.rb +91 -0
  46. data/lib/vagrant_vmware_free/action/set_name.rb +52 -0
  47. data/lib/vagrant_vmware_free/action/set_network.rb +17 -0
  48. data/lib/vagrant_vmware_free/action/setup_package_files.rb +51 -0
  49. data/lib/vagrant_vmware_free/action/share_folders.rb +128 -0
  50. data/lib/vagrant_vmware_free/action/suspend.rb +20 -0
  51. data/lib/vagrant_vmware_free/action/wait_for_vm_tools.rb +19 -0
  52. data/lib/vagrant_vmware_free/config.rb +38 -0
  53. data/lib/vagrant_vmware_free/driver/base.rb +14 -0
  54. data/lib/vagrant_vmware_free/driver/fusion.rb +17 -0
  55. data/lib/vagrant_vmware_free/driver/fusion_6.rb +188 -0
  56. data/lib/vagrant_vmware_free/driver/meta.rb +122 -0
  57. data/lib/vagrant_vmware_free/driver/vix.rb +19 -0
  58. data/lib/vagrant_vmware_free/driver/vix/functions.rb +38 -0
  59. data/lib/vagrant_vmware_free/driver/vix/helpers.rb +63 -0
  60. data/lib/vagrant_vmware_free/driver/vix/types.rb +143 -0
  61. data/lib/vagrant_vmware_free/errors.rb +9 -0
  62. data/lib/vagrant_vmware_free/plugin.rb +43 -0
  63. data/lib/vagrant_vmware_free/provider.rb +62 -0
  64. data/lib/vagrant_vmware_free/util/vmx.rb +48 -0
  65. data/vagrant-vmware-free.gemspec +21 -0
  66. metadata +190 -0
@@ -0,0 +1,27 @@
1
+ module VagrantPlugins
2
+ module ProviderVMwareFree
3
+ module Action
4
+ class GetNetworkAddress
5
+ include VagrantPlugins::ProviderVMwareFree::Driver::VIX
6
+
7
+ def initialize(app, env)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ vm_handle = env[:machine].provider.driver.vm_handle
13
+ handle = VixVM_ReadVariable(vm_handle, :VIX_VM_GUEST_VARIABLE, 'ip', 0, nil, nil)
14
+ code, properties = wait_with_pointers(handle, [:VIX_PROPERTY_JOB_RESULT_VM_VARIABLE_STRING])
15
+
16
+
17
+ raise VIXError, code: code if (code != 0)
18
+ ipPtr = properties[:VIX_PROPERTY_JOB_RESULT_VM_VARIABLE_STRING].read_pointer
19
+ ip = ipPtr.read_string
20
+ env[:machine].provider.driver.ip_address = ip
21
+
22
+ @app.call(env)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,56 @@
1
+ module VagrantPlugins
2
+ module ProviderVMwareFree
3
+ module Action
4
+ class Import
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:ui].info I18n.t("vagrant.actions.vm.import.importing",
11
+ :name => env[:machine].box.name)
12
+
13
+ # Import the virtual machine
14
+ vmx_file = Dir.glob(env[:machine].box.directory.join('*.vmx'))[0]
15
+ dest_file = env[:machine].data_dir.join(File.basename(vmx_file))
16
+
17
+ env[:machine].id = env[:machine].provider.driver.import(vmx_file, dest_file.to_s) do |progress|
18
+ env[:ui].clear_line
19
+ env[:ui].report_progress(progress, 100, false)
20
+ end
21
+
22
+ # Clear the line one last time since the progress meter doesn't disappear
23
+ # immediately.
24
+ env[:ui].clear_line
25
+
26
+ # If we got interrupted, then the import could have been
27
+ # interrupted and its not a big deal. Just return out.
28
+ return if env[:interrupted]
29
+
30
+ # Flag as erroneous and return if import failed
31
+ raise Vagrant::Errors::VMImportFailure if !env[:machine].id
32
+
33
+ # Import completed successfully. Continue the chain
34
+ @app.call(env)
35
+ end
36
+
37
+ def recover(env)
38
+ if env[:machine].provider.state.id != :not_created
39
+ return if env["vagrant.error"].is_a?(Vagrant::Errors::VagrantError)
40
+
41
+ # If we're not supposed to destroy on error then just return
42
+ return if !env[:destroy_on_error]
43
+
44
+ # Interrupted, destroy the VM. We note that we don't want to
45
+ # validate the configuration here, and we don't want to confirm
46
+ # we want to destroy.
47
+ destroy_env = env.clone
48
+ destroy_env[:config_validate] = false
49
+ destroy_env[:force_confirm_destroy] = true
50
+ env[:action_runner].run(Action.action_destroy, destroy_env)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,20 @@
1
+ module VagrantPlugins
2
+ module ProviderVMwareFree
3
+ module Action
4
+ class IsPaused
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ # Set the result to be true if the machine is paused.
11
+ env[:result] = env[:machine].state.id == :paused
12
+
13
+ # Call the next if we have one (but we shouldn't, since this
14
+ # middleware is built to run with the Call-type middlewares)
15
+ @app.call(env)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module VagrantPlugins
2
+ module ProviderVMwareFree
3
+ module Action
4
+ class IsRunning
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ # Set the result to be true if the machine is running.
11
+ env[:result] = env[:machine].state.id == :running
12
+
13
+ # Call the next if we have one (but we shouldn't, since this
14
+ # middleware is built to run with the Call-type middlewares)
15
+ @app.call(env)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module VagrantPlugins
2
+ module ProviderVMwareFree
3
+ module Action
4
+ class IsSaved
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ # Set the result to be true if the machine is saved.
11
+ env[:result] = env[:machine].state.id == :saved
12
+
13
+ # Call the next if we have one (but we shouldn't, since this
14
+ # middleware is built to run with the Call-type middlewares)
15
+ @app.call(env)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module VagrantPlugins
2
+ module ProviderVMwareFree
3
+ module Action
4
+ class MatchMACAddress
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ raise Vagrant::Errors::VMBaseMacNotSpecified if !env[:machine].config.vm.base_mac
11
+
12
+ # Create the proc which we want to use to modify the virtual machine
13
+ env[:ui].info I18n.t("vagrant.actions.vm.match_mac.matching")
14
+ env[:machine].provider.driver.set_mac_address(env[:machine].config.vm.base_mac)
15
+
16
+ @app.call(env)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ module VagrantPlugins
2
+ module ProviderVMwareFree
3
+ module Action
4
+ class MessageAlreadyRunning
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:ui].info I18n.t("vagrant.commands.common.vm_already_running")
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module VagrantPlugins
2
+ module ProviderVMwareFree
3
+ module Action
4
+ class MessageNotCreated
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:ui].info I18n.t("vagrant.commands.common.vm_not_created")
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module VagrantPlugins
2
+ module ProviderVMwareFree
3
+ module Action
4
+ class MessageNotRunning
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:ui].info I18n.t("vagrant.commands.common.vm_not_running")
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module VagrantPlugins
2
+ module ProviderVMwareFree
3
+ module Action
4
+ class MessageWillNotDestroy
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:ui].info I18n.t("vagrant.commands.destroy.will_not_destroy",
11
+ :name => env[:machine].name)
12
+ @app.call(env)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,424 @@
1
+ require "set"
2
+
3
+ require "log4r"
4
+
5
+ require "vagrant/util/network_ip"
6
+ require "vagrant/util/scoped_hash_override"
7
+
8
+ module VagrantPlugins
9
+ module ProviderVMwareFree
10
+ module Action
11
+ # This middleware class sets up all networking for the VirtualBox
12
+ # instance. This includes host only networks, bridged networking,
13
+ # forwarded ports, etc.
14
+ #
15
+ # This handles all the `config.vm.network` configurations.
16
+ class Network
17
+ include Vagrant::Util::NetworkIP
18
+ include Vagrant::Util::ScopedHashOverride
19
+
20
+ def initialize(app, env)
21
+ @logger = Log4r::Logger.new("vagrant::plugins::virtualbox::network")
22
+ @app = app
23
+ end
24
+
25
+ def call(env)
26
+ # TODO: Validate network configuration prior to anything below
27
+ @env = env
28
+
29
+ # Get the list of network adapters from the configuration
30
+ network_adapters_config = env[:machine].provider_config.network_adapters.dup
31
+
32
+ # Assign the adapter slot for each high-level network
33
+ available_slots = Set.new(1..8)
34
+ network_adapters_config.each do |slot, _data|
35
+ available_slots.delete(slot)
36
+ end
37
+
38
+ @logger.debug("Available slots for high-level adapters: #{available_slots.inspect}")
39
+ @logger.info("Determining network adapters required for high-level configuration...")
40
+ available_slots = available_slots.to_a.sort
41
+ env[:machine].config.vm.networks.each do |type, options|
42
+ # We only handle private and public networks
43
+ next if type != :private_network && type != :public_network
44
+
45
+ options = scoped_hash_override(options, :virtualbox)
46
+
47
+ # Figure out the slot that this adapter will go into
48
+ slot = options[:adapter]
49
+ if !slot
50
+ if available_slots.empty?
51
+ raise Vagrant::Errors::VirtualBoxNoRoomForHighLevelNetwork
52
+ end
53
+
54
+ slot = available_slots.shift
55
+ end
56
+
57
+ # Configure it
58
+ data = nil
59
+ if type == :private_network
60
+ # private_network = hostonly
61
+ data = [:hostonly, options]
62
+ elsif type == :public_network
63
+ # public_network = bridged
64
+ data = [:bridged, options]
65
+ end
66
+
67
+ # Store it!
68
+ @logger.info(" -- Slot #{slot}: #{data[0]}")
69
+ network_adapters_config[slot] = data
70
+ end
71
+
72
+ @logger.info("Determining adapters and compiling network configuration...")
73
+ adapters = []
74
+ networks = []
75
+ network_adapters_config.each do |slot, data|
76
+ type = data[0]
77
+ options = data[1]
78
+
79
+ @logger.info("Network slot #{slot}. Type: #{type}.")
80
+
81
+ # Get the normalized configuration for this type
82
+ config = send("#{type}_config", options)
83
+ config[:adapter] = slot
84
+ @logger.debug("Normalized configuration: #{config.inspect}")
85
+
86
+ # Get the VirtualBox adapter configuration
87
+ adapter = send("#{type}_adapter", config)
88
+ adapters << adapter
89
+ @logger.debug("Adapter configuration: #{adapter.inspect}")
90
+
91
+ # Get the network configuration
92
+ network = send("#{type}_network_config", config)
93
+ network[:auto_config] = config[:auto_config]
94
+ networks << network
95
+ end
96
+
97
+ if !adapters.empty?
98
+ # Enable the adapters
99
+ @logger.info("Enabling adapters...")
100
+ env[:ui].info I18n.t("vagrant.actions.vm.network.preparing")
101
+ env[:machine].provider.driver.enable_adapters(adapters)
102
+ end
103
+
104
+ # Continue the middleware chain.
105
+ @app.call(env)
106
+
107
+ # If we have networks to configure, then we configure it now, since
108
+ # that requires the machine to be up and running.
109
+ if !adapters.empty? && !networks.empty?
110
+ assign_interface_numbers(networks, adapters)
111
+
112
+ # Only configure the networks the user requested us to configure
113
+ networks_to_configure = networks.select { |n| n[:auto_config] }
114
+ if !networks_to_configure.empty?
115
+ env[:ui].info I18n.t("vagrant.actions.vm.network.configuring")
116
+ env[:machine].guest.capability(:configure_networks, networks_to_configure)
117
+ end
118
+ end
119
+ end
120
+
121
+ def bridged_config(options)
122
+ return {
123
+ :auto_config => true,
124
+ :bridge => nil,
125
+ :mac => nil,
126
+ :nic_type => nil,
127
+ :use_dhcp_assigned_default_route => false
128
+ }.merge(options || {})
129
+ end
130
+
131
+ def bridged_adapter(config)
132
+ # Find the bridged interfaces that are available
133
+ bridgedifs = @env[:machine].provider.driver.read_bridged_interfaces
134
+ bridgedifs.delete_if { |interface| interface[:status] == "Down" }
135
+
136
+ # The name of the chosen bridge interface will be assigned to this
137
+ # variable.
138
+ chosen_bridge = nil
139
+
140
+ if config[:bridge]
141
+ @logger.debug("Bridge was directly specified in config, searching for: #{config[:bridge]}")
142
+
143
+ # Search for a matching bridged interface
144
+ bridgedifs.each do |interface|
145
+ if interface[:name].downcase == config[:bridge].downcase
146
+ @logger.debug("Specific bridge found as configured in the Vagrantfile. Using it.")
147
+ chosen_bridge = interface[:name]
148
+ break
149
+ end
150
+ end
151
+
152
+ # If one wasn't found, then we notify the user here.
153
+ if !chosen_bridge
154
+ @env[:ui].info I18n.t("vagrant.actions.vm.bridged_networking.specific_not_found",
155
+ :bridge => config[:bridge])
156
+ end
157
+ end
158
+
159
+ # If we still don't have a bridge chosen (this means that one wasn't
160
+ # specified in the Vagrantfile, or the bridge specified in the Vagrantfile
161
+ # wasn't found), then we fall back to the normal means of searchign for a
162
+ # bridged network.
163
+ if !chosen_bridge
164
+ if bridgedifs.length == 1
165
+ # One bridgable interface? Just use it.
166
+ chosen_bridge = bridgedifs[0][:name]
167
+ @logger.debug("Only one bridged interface available. Using it by default.")
168
+ else
169
+ # More than one bridgable interface requires a user decision, so
170
+ # show options to choose from.
171
+ @env[:ui].info I18n.t("vagrant.actions.vm.bridged_networking.available",
172
+ :prefix => false)
173
+ bridgedifs.each_index do |index|
174
+ interface = bridgedifs[index]
175
+ @env[:ui].info("#{index + 1}) #{interface[:name]}", :prefix => false)
176
+ end
177
+
178
+ # The range of valid choices
179
+ valid = Range.new(1, bridgedifs.length)
180
+
181
+ # The choice that the user has chosen as the bridging interface
182
+ choice = nil
183
+ while !valid.include?(choice)
184
+ choice = @env[:ui].ask("What interface should the network bridge to? ")
185
+ choice = choice.to_i
186
+ end
187
+
188
+ chosen_bridge = bridgedifs[choice - 1][:name]
189
+ end
190
+ end
191
+
192
+ @logger.info("Bridging adapter #{config[:adapter]} to #{chosen_bridge}")
193
+
194
+ # Given the choice we can now define the adapter we're using
195
+ return {
196
+ :adapter => config[:adapter],
197
+ :type => :bridged,
198
+ :bridge => chosen_bridge,
199
+ :mac_address => config[:mac],
200
+ :nic_type => config[:nic_type]
201
+ }
202
+ end
203
+
204
+ def bridged_network_config(config)
205
+ if config[:ip]
206
+ options = {
207
+ :auto_config => true,
208
+ :mac => nil,
209
+ :netmask => "255.255.255.0",
210
+ :type => :static
211
+ }.merge(config)
212
+ options[:type] = options[:type].to_sym
213
+ return options
214
+ end
215
+
216
+ return {
217
+ :type => :dhcp,
218
+ :use_dhcp_assigned_default_route => config[:use_dhcp_assigned_default_route]
219
+ }
220
+ end
221
+
222
+ def hostonly_config(options)
223
+ options = {
224
+ :auto_config => true,
225
+ :mac => nil,
226
+ :nic_type => nil,
227
+ :netmask => "255.255.255.0",
228
+ :type => :static
229
+ }.merge(options)
230
+
231
+ # Make sure the type is a symbol
232
+ options[:type] = options[:type].to_sym
233
+
234
+ # Default IP is in the 20-bit private network block for DHCP based networks
235
+ options[:ip] = "172.28.128.1" if options[:type] == :dhcp && !options[:ip]
236
+
237
+ # Calculate our network address for the given IP/netmask
238
+ netaddr = network_address(options[:ip], options[:netmask])
239
+
240
+ # Verify that a host-only network subnet would not collide
241
+ # with a bridged networking interface.
242
+ #
243
+ # If the subnets overlap in any way then the host only network
244
+ # will not work because the routing tables will force the
245
+ # traffic onto the real interface rather than the VirtualBox
246
+ # interface.
247
+ @env[:machine].provider.driver.read_bridged_interfaces.each do |interface|
248
+ that_netaddr = network_address(interface[:ip], interface[:netmask])
249
+ raise Vagrant::Errors::NetworkCollision if \
250
+ netaddr == that_netaddr && interface[:status] != "Down"
251
+ end
252
+
253
+ # Split the IP address into its components
254
+ ip_parts = netaddr.split(".").map { |i| i.to_i }
255
+
256
+ # Calculate the adapter IP, which we assume is the IP ".1" at
257
+ # the end usually.
258
+ adapter_ip = ip_parts.dup
259
+ adapter_ip[3] += 1
260
+ options[:adapter_ip] ||= adapter_ip.join(".")
261
+
262
+ dhcp_options = {}
263
+ if options[:type] == :dhcp
264
+ # Calculate the DHCP server IP, which is the network address
265
+ # with the final octet + 2. So "172.28.0.0" turns into "172.28.0.2"
266
+ dhcp_ip = ip_parts.dup
267
+ dhcp_ip[3] += 2
268
+ dhcp_options[:dhcp_ip] ||= dhcp_ip.join(".")
269
+
270
+ # Calculate the lower and upper bound for the DHCP server
271
+ dhcp_lower = ip_parts.dup
272
+ dhcp_lower[3] += 3
273
+ dhcp_options[:dhcp_lower] ||= dhcp_lower.join(".")
274
+
275
+ dhcp_upper = ip_parts.dup
276
+ dhcp_upper[3] = 254
277
+ dhcp_options[:dhcp_upper] ||= dhcp_upper.join(".")
278
+ end
279
+
280
+ return {
281
+ :adapter_ip => options[:adapter_ip],
282
+ :auto_config => options[:auto_config],
283
+ :ip => options[:ip],
284
+ :mac => options[:mac],
285
+ :netmask => options[:netmask],
286
+ :nic_type => options[:nic_type],
287
+ :type => options[:type]
288
+ }.merge(dhcp_options)
289
+ end
290
+
291
+ def hostonly_adapter(config)
292
+ @logger.info("Searching for matching hostonly network: #{config[:ip]}")
293
+ interface = hostonly_find_matching_network(config)
294
+
295
+ if !interface
296
+ @logger.info("Network not found. Creating if we can.")
297
+
298
+ # It is an error if a specific host only network name was specified
299
+ # but the network wasn't found.
300
+ if config[:name]
301
+ raise Vagrant::Errors::NetworkNotFound, :name => config[:name]
302
+ end
303
+
304
+ # Create a new network
305
+ interface = hostonly_create_network(config)
306
+ @logger.info("Created network: #{interface[:name]}")
307
+ end
308
+
309
+ if config[:type] == :dhcp
310
+ # Check that if there is a DHCP server attached on our interface,
311
+ # then it is identical. Otherwise, we can't set it.
312
+ if interface[:dhcp]
313
+ valid = interface[:dhcp][:ip] == config[:dhcp_ip] &&
314
+ interface[:dhcp][:lower] == config[:dhcp_lower] &&
315
+ interface[:dhcp][:upper] == config[:dhcp_upper]
316
+
317
+ raise Errors::NetworkDHCPAlreadyAttached if !valid
318
+
319
+ @logger.debug("DHCP server already properly configured")
320
+ else
321
+ # Configure the DHCP server for the network.
322
+ @logger.debug("Creating a DHCP server...")
323
+ @env[:machine].provider.driver.create_dhcp_server(interface[:name], config)
324
+ end
325
+ end
326
+
327
+ return {
328
+ :adapter => config[:adapter],
329
+ :hostonly => interface[:name],
330
+ :mac => config[:mac],
331
+ :nic_type => config[:nic_type],
332
+ :type => :hostonly
333
+ }
334
+ end
335
+
336
+ def hostonly_network_config(config)
337
+ return {
338
+ :type => config[:type],
339
+ :adapter_ip => config[:adapter_ip],
340
+ :ip => config[:ip],
341
+ :netmask => config[:netmask]
342
+ }
343
+ end
344
+
345
+ def nat_config(options)
346
+ return {
347
+ :auto_config => false
348
+ }
349
+ end
350
+
351
+ def nat_adapter(config)
352
+ return {
353
+ :adapter => config[:adapter],
354
+ :type => :nat,
355
+ }
356
+ end
357
+
358
+ def nat_network_config(config)
359
+ return {}
360
+ end
361
+
362
+ #-----------------------------------------------------------------
363
+ # Misc. helpers
364
+ #-----------------------------------------------------------------
365
+ # Assigns the actual interface number of a network based on the
366
+ # enabled NICs on the virtual machine.
367
+ #
368
+ # This interface number is used by the guest to configure the
369
+ # NIC on the guest VM.
370
+ #
371
+ # The networks are modified in place by adding an ":interface"
372
+ # field to each.
373
+ def assign_interface_numbers(networks, adapters)
374
+ current = 0
375
+ adapter_to_interface = {}
376
+
377
+ # Make a first pass to assign interface numbers by adapter location
378
+ vm_adapters = @env[:machine].provider.driver.read_network_interfaces
379
+ vm_adapters.sort.each do |number, adapter|
380
+ if adapter[:type] != :none
381
+ # Not used, so assign the interface number and increment
382
+ adapter_to_interface[number] = current
383
+ current += 1
384
+ end
385
+ end
386
+
387
+ # Make a pass through the adapters to assign the :interface
388
+ # key to each network configuration.
389
+ adapters.each_index do |i|
390
+ adapter = adapters[i]
391
+ network = networks[i]
392
+
393
+ # Figure out the interface number by simple lookup
394
+ network[:interface] = adapter_to_interface[adapter[:adapter]]
395
+ end
396
+ end
397
+
398
+ #-----------------------------------------------------------------
399
+ # Hostonly Helper Functions
400
+ #-----------------------------------------------------------------
401
+ # This creates a host only network for the given configuration.
402
+ def hostonly_create_network(config)
403
+ @env[:machine].provider.driver.create_host_only_network(
404
+ :adapter_ip => config[:adapter_ip],
405
+ :netmask => config[:netmask]
406
+ )
407
+ end
408
+
409
+ # This finds a matching host only network for the given configuration.
410
+ def hostonly_find_matching_network(config)
411
+ this_netaddr = network_address(config[:ip], config[:netmask])
412
+
413
+ @env[:machine].provider.driver.read_host_only_interfaces.each do |interface|
414
+ return interface if config[:name] && config[:name] == interface[:name]
415
+ return interface if this_netaddr == \
416
+ network_address(interface[:ip], interface[:netmask])
417
+ end
418
+
419
+ nil
420
+ end
421
+ end
422
+ end
423
+ end
424
+ end