2013-04-10 05:02:36 +00:00
|
|
|
module Vagrant
|
|
|
|
module LXC
|
2013-07-29 15:08:22 +00:00
|
|
|
module Action
|
|
|
|
class FetchIpFromDnsmasqLeases
|
|
|
|
def initialize(app, env)
|
|
|
|
@app = app
|
|
|
|
@logger = Log4r::Logger.new("vagrant::lxc::action::fetch_ip_from_dnsmasq_leases")
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
env[:machine_ip] ||= assigned_ip(env)
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assigned_ip(env)
|
|
|
|
mac_address = env[:machine].provider.driver.mac_address
|
2013-04-10 05:02:36 +00:00
|
|
|
ip = nil
|
|
|
|
10.times do
|
2013-08-01 02:00:20 +00:00
|
|
|
dnsmasq_leases = read_dnsmasq_leases
|
2013-09-28 04:34:26 +00:00
|
|
|
@logger.debug "Attempting to load ip from dnsmasq leases (mac: #{mac_address})"
|
2013-08-01 02:00:20 +00:00
|
|
|
@logger.debug dnsmasq_leases
|
2013-04-10 05:02:36 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
LEASES_PATHS = %w(
|
2013-08-01 02:01:08 +00:00
|
|
|
/var/lib/misc/dnsmasq.*.leases
|
2013-04-10 05:02:36 +00:00
|
|
|
/var/lib/misc/dnsmasq.leases
|
|
|
|
/var/lib/dnsmasq/dnsmasq.leases
|
|
|
|
/var/db/dnsmasq.leases
|
2014-03-13 09:49:24 +00:00
|
|
|
/var/lib/libvirt/dnsmasq/*.leases
|
2013-04-10 05:02:36 +00:00
|
|
|
)
|
|
|
|
|
2013-08-01 02:00:20 +00:00
|
|
|
def read_dnsmasq_leases
|
2013-08-01 02:00:35 +00:00
|
|
|
Dir["{#{LEASES_PATHS.join(',')}}"].map do |file|
|
|
|
|
File.read(file)
|
2013-04-10 05:02:36 +00:00
|
|
|
end.join("\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|