From 6afd4c5d9a8c5a90b029b8b5a15b4a4f28e663ff Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sat, 2 Mar 2013 13:55:05 -0300 Subject: [PATCH] Add support for grabbing the container ip from lxc dns server --- lib/vagrant-lxc/container.rb | 28 ++++++++++++++++++++++++++++ spec/unit/container_spec.rb | 16 ++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/vagrant-lxc/container.rb b/lib/vagrant-lxc/container.rb index d24f765..7279e45 100644 --- a/lib/vagrant-lxc/container.rb +++ b/lib/vagrant-lxc/container.rb @@ -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 diff --git a/spec/unit/container_spec.rb b/spec/unit/container_spec.rb index 9dccdfd..887a23b 100644 --- a/spec/unit/container_spec.rb +++ b/spec/unit/container_spec.rb @@ -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