Add support for grabbing the container ip from lxc dns server

This commit is contained in:
Fabio Rehm 2013-03-02 13:55:05 -03:00
parent e1f62e4e56
commit 6afd4c5d9a
2 changed files with 44 additions and 0 deletions

View file

@ -73,6 +73,34 @@ module Vagrant
end
end
def dhcp_ip
ip = ''
# Right after creation lxc reports the container as running
# before DNS is returning the right IP, so have to wait for a while
retryable(:on => LXC::Errors::ExecuteError, :tries => 10, :sleep => 1) do
# By default LXC supplies a dns server on 10.0.3.1 so we request the IP
# of our target from there.
# Tks to: https://github.com/neerolyte/vagueant/blob/master/bin/vagueant#L340
r = (raw 'dig', @name, '@10.0.3.1', '+short')
# If the command was a failure then raise an exception that is nicely
# handled by Vagrant.
if r.exit_code != 0
if @interrupted
@logger.info("Exit code != 0, but interrupted. Ignoring.")
else
raise LXC::Errors::ExecuteError, :command => command.inspect
end
end
ip = r.stdout.gsub("\r\n", "\n").strip
if ip.empty?
raise LXC::Errors::ExecuteError, 'Unable to identify container ip'
end
end
ip
end
# TODO: Review code below this line, it was pretty much a copy and paste from VirtualBox base driver
def execute(*command, &block)
# Get the options hash if it exists

View file

@ -175,4 +175,20 @@ describe Vagrant::LXC::Container do
subject.state.should == :stopped
end
end
describe 'dhcp ip' do
let(:name) { 'random-container-name' }
let(:ip) { "10.0.3.123" }
before do
subject.stub(:raw) {
mock(stdout: "#{ip}\n", exit_code: 0)
}
end
it 'digs the container ip from lxc dns server' do
subject.dhcp_ip.should == ip
subject.should have_received(:raw).with('dig', name, '@10.0.3.1', '+short')
end
end
end