Module: Xhyve::DHCP

Extended by:
DHCP
Included in:
DHCP
Defined in:
lib/xhyve/dhcp.rb

Overview

Parse DHCP leases file for a MAC address, and get its ip.

Constant Summary collapse

LEASES_FILE =
'/var/db/dhcpd_leases'
WAIT_TIME =
1
MAX_ATTEMPTS =
60

Instance Method Summary collapse

Instance Method Details

#get_ip_for_mac(mac) ⇒ Object



9
10
11
12
13
14
# File 'lib/xhyve/dhcp.rb', line 9

def get_ip_for_mac(mac)
  max = ENV.key?('MAX_IP_WAIT') ? ENV['MAX_IP_WAIT'].to_i : nil
  ip = wait_for(max: max) do
    ip = parse_lease_file_for_mac(mac)
  end
end

#parse_lease_file_for_mac(mac) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/xhyve/dhcp.rb', line 16

def parse_lease_file_for_mac(mac)
  lease_file = (ENV['LEASES_FILE'] || LEASES_FILE)
  contents = wait_for do
    File.read(lease_file) if File.exists?(lease_file)
  end
  pattern = contents.match(/ip_address=(\S+)\n\thw_address=\d+,#{mac}/)
  if pattern
    addrs = pattern.captures
    addrs.first if addrs
  end
end