Class: Vagrant::LXC::Action::FetchIpFromDnsmasqLeases

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-lxc/action/fetch_ip_from_dnsmasq_leases.rb

Constant Summary collapse

LEASES_PATHS =
%w(
  /var/lib/misc/dnsmasq.*.leases
  /var/lib/misc/dnsmasq.leases
  /var/lib/dnsmasq/dnsmasq.leases
  /var/db/dnsmasq.leases
)

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ FetchIpFromDnsmasqLeases

Returns a new instance of FetchIpFromDnsmasqLeases.



5
6
7
8
# File 'lib/vagrant-lxc/action/fetch_ip_from_dnsmasq_leases.rb', line 5

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant::lxc::action::fetch_ip_from_dnsmasq_leases")
end

Instance Method Details

#assigned_ip(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vagrant-lxc/action/fetch_ip_from_dnsmasq_leases.rb', line 15

def assigned_ip(env)
  mac_address = env[:machine].provider.driver.mac_address
  ip = nil
  10.times do
    dnsmasq_leases = read_dnsmasq_leases
    @logger.debug 'Attempting to load ip from dnsmasq leases'
    @logger.debug dnsmasq_leases
    if dnsmasq_leases =~ /#{Regexp.escape mac_address}\s+([0-9.]+)\s+/
      ip = $1.to_s
      break
    else
      @logger.debug 'Ip could not be parsed from dnsmasq leases file'
      sleep 2
    end
  end
  ip
end

#call(env) ⇒ Object



10
11
12
13
# File 'lib/vagrant-lxc/action/fetch_ip_from_dnsmasq_leases.rb', line 10

def call(env)
  env[:machine_ip] ||= assigned_ip(env)
  @app.call(env)
end

#read_dnsmasq_leasesObject



40
41
42
43
44
# File 'lib/vagrant-lxc/action/fetch_ip_from_dnsmasq_leases.rb', line 40

def read_dnsmasq_leases
  Dir["{#{LEASES_PATHS.join(',')}}"].map do |file|
    File.read(file)
  end.join("\n")
end