Module: Xhyve::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

Class Method Summary collapse

Class Method Details

.get_ip_for_mac(mac) ⇒ Object



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

def self.get_ip_for_mac(mac)
  attempts = 0
  max_attempts = ENV.key?('MAX_IP_WAIT') ? ENV['MAX_IP_WAIT'].to_i : MAX_ATTEMPTS
  while attempts < max_attempts
    attempts += 1
    ip = parse_lease_file_for_mac(mac)
    return ip if ip
    sleep(WAIT_TIME)
  end
end

.parse_lease_file_for_mac(mac) ⇒ Object



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

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