class Vagrant::LXC::Action::FetchIpFromDnsmasqLeases

Constants

LEASES_PATHS

Public Class Methods

new(app, env) click to toggle source
# 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

Public Instance Methods

assigned_ip(env) click to toggle source
# 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 (mac: #{mac_address})"
    @logger.debug dnsmasq_leases
    if dnsmasq_leases =~ /#{Regexp.escape mac_address.to_s}\s+([0-9.]+)\s+/i
      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) click to toggle source
# 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_leases() click to toggle source
# File lib/vagrant-lxc/action/fetch_ip_from_dnsmasq_leases.rb, line 41
def read_dnsmasq_leases
  Dir["{#{LEASES_PATHS.join(',')}}"].map do |file|
    File.read(file)
  end.join("\n")
end