Class: HashiCorp::VagrantVMwareDesktop::Action::BaseMacToIp

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/vagrant-vmware-desktop/action/base_mac_to_ip.rb

Overview

This action maps an IP address to a MAC address in the DHCP server for the default NAT interface

Instance Method Summary collapse

Methods included from Common

#to_s

Constructor Details

#initialize(app, env) ⇒ BaseMacToIp

Returns a new instance of BaseMacToIp.



15
16
17
18
# File 'lib/vagrant-vmware-desktop/action/base_mac_to_ip.rb', line 15

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("hashicorp::provider::vmware::basemactoip")
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant-vmware-desktop/action/base_mac_to_ip.rb', line 20

def call(env)
  if env[:machine].provider_config.base_address
    ip = env[:machine].provider_config.base_address
    mac = env[:machine].provider_config.base_mac

    validate_address!(env[:machine].provider.driver,
      ip, env[:machine].provider_config.nat_device)

    env[:machine].provider.driver.reserve_dhcp_address(ip, mac, env[:machine].provider_config.nat_device)

    env[:ui].info(I18n.t("hashicorp.vagrant_vmware_desktop.mac_to_ip_mapping",
      address: ip, mac: mac))
  end
  @app.call(env)
end

#validate_address!(driver, address, nat_device) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vagrant-vmware-desktop/action/base_mac_to_ip.rb', line 36

def validate_address!(driver, address, nat_device)
  devices = driver.read_vmnet_devices
  vmnet = devices.detect { |v| v[:name] == nat_device }
  if !vmnet
    raise Errors::MissingNATDevice
  end
  # NOTE: VMware dhcpd is configured as follows for addressing:
  # .1          -> host machine
  # .2 - .127   -> static address (valid here)
  # .128 - .253 -> DHCP assigned
  # .254        -> DHCP server
  dev_subnet = "#{vmnet[:hostonly_subnet]}/25"
  dev_addr = IPAddr.new(dev_subnet)
  if address.end_with?(".1") || !dev_addr.include?(address)
    raise Errors::BaseAddressRange,
      range_start: dev_addr.to_range.first.succ.succ.to_s,
      range_end: dev_addr.to_range.last.to_s
  end
end