2013-07-29 15:08:22 +00:00
|
|
|
module Vagrant
|
|
|
|
module LXC
|
|
|
|
module Action
|
2013-10-21 19:11:43 +00:00
|
|
|
class FetchIpWithLxcAttach
|
|
|
|
# Include this so we can use `Subprocess` more easily.
|
|
|
|
include Vagrant::Util::Retryable
|
2013-07-29 15:08:22 +00:00
|
|
|
|
2013-10-21 19:11:43 +00:00
|
|
|
def initialize(app, env)
|
|
|
|
@app = app
|
|
|
|
@logger = Log4r::Logger.new("vagrant::lxc::action::fetch_ip_with_lxc_attach")
|
|
|
|
end
|
2013-07-29 15:08:22 +00:00
|
|
|
|
2013-10-21 19:11:43 +00:00
|
|
|
def call(env)
|
|
|
|
env[:machine_ip] ||= assigned_ip(env)
|
|
|
|
rescue LXC::Errors::NamespacesNotSupported
|
|
|
|
@logger.info 'The `lxc-attach` command available does not support the --namespaces parameter, falling back to dnsmasq leases to fetch container ip'
|
|
|
|
ensure
|
|
|
|
@app.call(env)
|
|
|
|
end
|
2013-07-29 15:08:22 +00:00
|
|
|
|
2013-10-21 19:11:43 +00:00
|
|
|
def assigned_ip(env)
|
2016-11-09 14:38:05 +00:00
|
|
|
config = env[:machine].provider_config
|
|
|
|
fetch_ip_tries = config.fetch_ip_tries
|
2013-10-21 19:11:43 +00:00
|
|
|
driver = env[:machine].provider.driver
|
|
|
|
ip = ''
|
2016-11-09 14:38:05 +00:00
|
|
|
retryable(:on => LXC::Errors::ExecuteError, :tries => fetch_ip_tries, :sleep => 3) do
|
2013-10-21 19:11:43 +00:00
|
|
|
unless ip = get_container_ip_from_ip_addr(driver)
|
|
|
|
# retry
|
|
|
|
raise LXC::Errors::ExecuteError, :command => "lxc-attach"
|
2013-07-29 15:08:22 +00:00
|
|
|
end
|
|
|
|
end
|
2013-10-21 19:11:43 +00:00
|
|
|
ip
|
|
|
|
end
|
2013-07-29 15:08:22 +00:00
|
|
|
|
2013-10-21 19:11:43 +00:00
|
|
|
# From: https://github.com/lxc/lxc/blob/staging/src/python-lxc/lxc/__init__.py#L371-L385
|
|
|
|
def get_container_ip_from_ip_addr(driver)
|
2014-07-24 23:08:42 +00:00
|
|
|
output = driver.attach '/sbin/ip', '-4', 'addr', 'show', 'scope', 'global', 'eth0', namespaces: ['network', 'mount']
|
2013-10-21 19:11:43 +00:00
|
|
|
if output =~ /^\s+inet ([0-9.]+)\/[0-9]+\s+/
|
|
|
|
return $1.to_s
|
2013-07-29 15:08:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-21 19:11:43 +00:00
|
|
|
end
|