Class: HashiCorp::VagrantVMwareDesktop::Action::Network
- Inherits:
-
Object
- Object
- HashiCorp::VagrantVMwareDesktop::Action::Network
- Includes:
- Common, Vagrant::Util::NetworkIP, Vagrant::Util::ScopedHashOverride
- Defined in:
- lib/vagrant-vmware-desktop/action/network.rb
Overview
This action sets up all the network adapters for the machine and also tells the guest to configure the networks.
Constant Summary collapse
- DEFAULT_VMNET_NAT =
"vmnet8"
Instance Method Summary collapse
-
#assign_interface_numbers(networks, adapters) ⇒ Object
—————————————————————– Misc.
- #bridged_adapter(config) ⇒ Object
- #bridged_config(options) ⇒ Object
- #bridged_network_config(config) ⇒ Object
- #call(env) ⇒ Object
- #hostonly_adapter(config) ⇒ Object
- #hostonly_config(options) ⇒ Object
- #hostonly_network_config(config) ⇒ Object
-
#initialize(app, env) ⇒ Network
constructor
A new instance of Network.
- #nat_adapter(config) ⇒ Object
- #nat_config(options) ⇒ Object
- #nat_network_config(config) ⇒ Object
-
#routing_table ⇒ Helper::RoutingTable
This is a lazy loaded Helper::RoutingTable.
-
#vmware_mac_format(mac) ⇒ String
This converts the Vagrant configured MAC address format to a typical MAC address format.
Methods included from Common
Constructor Details
#initialize(app, env) ⇒ Network
Returns a new instance of Network.
24 25 26 27 |
# File 'lib/vagrant-vmware-desktop/action/network.rb', line 24 def initialize(app, env) @app = app @logger = Log4r::Logger.new("hashicorp::provider::vmware::network") end |
Instance Method Details
#assign_interface_numbers(networks, adapters) ⇒ Object
Misc. helpers
Assigns the actual interface number of a network based on the enabled NICs on the virtual machine.
This interface number is used by the guest to configure the NIC on the guest VM.
The networks are modified in place by adding an “:interface” field to each.
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/vagrant-vmware-desktop/action/network.rb', line 296 def assign_interface_numbers(networks, adapters) # First create a mapping of adapter slot to interface number # by reading over the existing network adapters. slots_in_use = [] vm_adapters = @env[:machine].provider.driver.read_network_adapters vm_adapters.each do |adapter| slots_in_use << adapter[:slot].to_i end slot_to_interface = {} slots_in_use.sort.each_index do |i| slot_to_interface[slots_in_use[i]] = i end # Make a pass through the adapters to assign the :interface # key to each network configuration. adapters.each_index do |i| adapter = adapters[i] network = networks[i] # Figure out the interface number by simple lookup network[:interface] = slot_to_interface[adapter[:slot]] end end |
#bridged_adapter(config) ⇒ Object
259 260 261 262 263 264 265 266 267 |
# File 'lib/vagrant-vmware-desktop/action/network.rb', line 259 def bridged_adapter(config) mac_address = config[:mac] mac_address = vmware_mac_format(mac_address) if mac_address return { :type => :bridged, :mac_address => mac_address } end |
#bridged_config(options) ⇒ Object
251 252 253 254 255 256 257 |
# File 'lib/vagrant-vmware-desktop/action/network.rb', line 251 def bridged_config() return { :auto_config => true, :mac => nil, :type => :dhcp }.merge( || {}) end |
#bridged_network_config(config) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/vagrant-vmware-desktop/action/network.rb', line 269 def bridged_network_config(config) if config[:ip] = { auto_config: true, mac: nil, netmask: "255.255.255.0", }.merge(config) [:type] = :static return end return { :type => :dhcp } end |
#call(env) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/vagrant-vmware-desktop/action/network.rb', line 29 def call(env) # Set this to an ivar so that helper methods have access to it @env = env # Get the list of network adapters from the configuration network_adapters_config = env[:machine].provider_config.network_adapters.dup # Assign the adapter slot for each high-level network available_slots = Set.new(1..8) network_adapters_config.each do |slot, _data| available_slots.delete(slot) end @logger.debug("Available slots for high-level adapters: #{available_slots.inspect}") @logger.info("Determining network adapters required for high-level configuration...") available_slots = available_slots.to_a.sort env[:machine].config.vm.networks.each do |type, | # We only handle private and public networks next if type != :private_network && type != :public_network scope_key = "vmware_#{PRODUCT_NAME}".to_sym = scoped_hash_override(, scope_key) # Figure out the slot that this adapter will go into slot = [:adapter] if !slot if available_slots.empty? raise Errors::NetworkingNoSlotsForHighLevel end slot = available_slots.shift end # Configure it data = nil if type == :private_network # private_network = hostonly data = [:hostonly, ] elsif type == :public_network # public_network = bridged data = [:bridged, ] end # Store it! @logger.info(" -- Slot #{slot}: #{data[0]}") network_adapters_config[slot] = data end @logger.info("Determining adapters and compiling network configuration...") adapters = [] networks = [] network_adapters_config.each do |slot, data| type = data[0] = data[1] if slot == 0 && env[:machine].provider_config.nat_device != DEFAULT_VMNET_NAT # TODO: what's the device name on windows? [:device] = "/dev/#{env[:machine].provider_config.nat_device}" end @logger.info("Slot #{slot}. Type: #{type}") # Get normalized configuration so we can add/scrub values config = send("#{type}_config", ) @logger.debug("Normalized configuration: #{config.inspect}") # Get the adapter configuration for the driver adapter = send("#{type}_adapter", config) adapter[:slot] = slot adapters << adapter @logger.debug("Adapter configuration: #{adapter.inspect}") # Get the network configuration for the guest network = send("#{type}_network_config", config) network[:auto_config] = config[:auto_config] networks << network @logger.debug("Network configuration: #{network.inspect}") end if !adapters.empty? # Modify the VM metadata to add adapters @logger.info("Enabling #{adapters.length} adapters...") Helper::Lock.lock(env[:machine], "vmware-network") do env[:ui].info(I18n.t("hashicorp.vagrant_vmware_desktop.enabling_adapters")) env[:machine].provider.driver.setup_adapters(adapters, env[:machine].provider_config.allowlist_verified) end end @app.call(env) if !networks.empty? # Assign interface numbers to the networks assign_interface_numbers(networks, adapters) networks_to_configure = networks.select { |n| n[:auto_config] } env[:ui].info(I18n.t("hashicorp.vagrant_vmware_desktop.configuring_networks")) env[:machine].guest.capability(:configure_networks, networks_to_configure) end end |
#hostonly_adapter(config) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/vagrant-vmware-desktop/action/network.rb', line 188 def hostonly_adapter(config) # If we're just doing normal DHCP, then we just connect to the # basic default adapter. if config[:type] == :dhcp return { :type => :hostonly } end # Otherwise we want a static IP. Start by trying to find # an existing network that matches our needs. vmnet = nil @env[:machine].provider.driver.read_vmnet_devices.each do |device| if device[:hostonly_subnet] == config[:subnet_ip] @logger.info("Found matching vmnet device: #{device[:name]}") vmnet = device break end end # Check for collisions by checking for if there is another device # that the IP would route to. The basic logic is: if there is # a device, and it is NOT the vmnet we care about, then it # is an error. @logger.info("Checking for hostonly network collisions...") device = routing_table.device_for_route(config[:ip]) if device if !vmnet || device != vmnet[:name] # There is a collision with some other networking device. raise Errors::NetworkingHostOnlyCollision, :device => device, :ip => config[:ip] end end if !vmnet @logger.info("No collisions detected, creating new vmnet device.") vmnet = @env[:machine].provider.driver.create_vmnet_device( :netmask => config[:netmask], :subnet_ip => config[:subnet_ip]) end # Determine MAC address of the adapter mac_address = config[:mac] mac_address = vmware_mac_format(mac_address) if mac_address # Return a more complex configuration to describe what we need return { :type => :custom, :mac_address => mac_address, :vnet => vmnet[:name] } end |
#hostonly_config(options) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/vagrant-vmware-desktop/action/network.rb', line 150 def hostonly_config() # Get the default configuration built up config = { :auto_config => true, :netmask => "255.255.255.0", :type => :dhcp }.merge( || {}) if [:ip] # Check if we are using ipv6, which is not supported ip = IPAddr.new([:ip]) if ip.ipv6? raise Errors::VMNetNoIPV6 end # We are using static if we have an IP set config[:type] = :static # Get the static IP and use the static IP + subnet mask to # determine the subnet IP. static_ip = config[:ip] subnet_ip = network_address(static_ip, config[:netmask]) config[:subnet_ip] = subnet_ip # Calculate the actual IP of the adapter itself, which is usually # just the network address "+ 1" in the last octet ip_parts = subnet_ip.split(".").map { |i| i.to_i } adapter_ip = ip_parts.dup adapter_ip[3] += 1 config[:adapter_ip] ||= adapter_ip.join(".") end # Make sure the type is a symbol config[:type] = config[:type].to_sym return config end |
#hostonly_network_config(config) ⇒ Object
242 243 244 245 246 247 248 249 |
# File 'lib/vagrant-vmware-desktop/action/network.rb', line 242 def hostonly_network_config(config) return { :type => config[:type], :adapter_ip => config[:adapter_ip], :ip => config[:ip], :netmask => config[:netmask] } end |
#nat_adapter(config) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/vagrant-vmware-desktop/action/network.rb', line 136 def nat_adapter(config) { type: :nat, mac_address: config[:mac_address], vnet: config[:device] }.compact end |
#nat_config(options) ⇒ Object
129 130 131 132 133 134 |
# File 'lib/vagrant-vmware-desktop/action/network.rb', line 129 def nat_config() return { :auto_config => true, :type => :dhcp }.merge() end |
#nat_network_config(config) ⇒ Object
144 145 146 147 148 |
# File 'lib/vagrant-vmware-desktop/action/network.rb', line 144 def nat_network_config(config) return { :type => :dhcp } end |
#routing_table ⇒ Helper::RoutingTable
This is a lazy loaded Helper::RoutingTable.
333 334 335 |
# File 'lib/vagrant-vmware-desktop/action/network.rb', line 333 def routing_table @routing_table ||= Helper::RoutingTable.new end |
#vmware_mac_format(mac) ⇒ String
This converts the Vagrant configured MAC address format to a typical MAC address format.
326 327 328 |
# File 'lib/vagrant-vmware-desktop/action/network.rb', line 326 def vmware_mac_format(mac) mac.scan(/.{2}/).join(":") end |