Move away from using ifconfig to ip addr for scanning container IPs

This was required since `ifconfig` is sensitive to localization.
Fixes #50
This commit is contained in:
Fabio Rehm 2013-04-05 03:23:03 -03:00
parent 670a43da00
commit 2147ec0ba5
4 changed files with 20 additions and 26 deletions

View file

@ -110,7 +110,7 @@ module Vagrant
def assigned_ip
ip = ''
retryable(:on => LXC::Errors::ExecuteError, :tries => 10, :sleep => 3) do
unless ip = get_container_ip_from_ifconfig
unless ip = get_container_ip_from_ip_addr
# retry
raise LXC::Errors::ExecuteError, :command => "lxc-attach"
end
@ -118,9 +118,10 @@ module Vagrant
ip
end
def get_container_ip_from_ifconfig
output = @cli.attach '/sbin/ifconfig', '-v', 'eth0', namespaces: 'network'
if output =~ /\s+inet addr:([0-9.]+)\s+/
# From: https://github.com/lxc/lxc/blob/staging/src/python-lxc/lxc/__init__.py#L371-L385
def get_container_ip_from_ip_addr
output = @cli.attach '/sbin/ip', '-4', 'addr', 'show', 'scope', 'global', 'eth0', namespaces: 'network'
if output =~ /^\s+inet ([0-9.]+)\/[0-9]+\s+/
return $1.to_s
end
end

View file

@ -1,18 +0,0 @@
eth0 Link encap:Ethernet HWaddr 00:16:3e:7c:dd:44
inet addr:10.0.3.109 Bcast:10.0.3.255 Mask:255.255.255.0
inet6 addr: fe80::216:3eff:fe7c:dd44/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:204 errors:0 dropped:0 overruns:0 frame:0
TX packets:203 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:29663 (29.6 KB) TX bytes:30168 (30.1 KB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:73 errors:0 dropped:0 overruns:0 frame:0
TX packets:73 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:34173 (34.1 KB) TX bytes:34173 (34.1 KB)

2
spec/fixtures/sample-ip-addr-output vendored Normal file
View file

@ -0,0 +1,2 @@
49: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 10.0.254.137/24 brd 10.0.254.255 scope global eth0

View file

@ -125,9 +125,9 @@ describe Vagrant::LXC::Driver do
end
describe 'assigned ip' do
# This ip is set on the sample-ifconfig-output fixture
let(:ip) { "10.0.3.109" }
let(:ifconfig_output) { File.read('spec/fixtures/sample-ifconfig-output') }
# This ip is set on the sample-ip-addr-output fixture
let(:ip) { "10.0.254.137" }
let(:ifconfig_output) { File.read('spec/fixtures/sample-ip-addr-output') }
let(:name) { 'random-container-name' }
let(:cli) { fire_double('Vagrant::LXC::Driver::CLI', :attach => ifconfig_output) }
@ -136,7 +136,16 @@ describe Vagrant::LXC::Driver do
context 'when ip for eth0 gets returned from lxc-attach call' do
it 'gets parsed from ifconfig output' do
subject.assigned_ip.should == ip
cli.should have_received(:attach).with('/sbin/ifconfig', '-v', 'eth0', namespaces: 'network')
cli.should have_received(:attach).with(
'/sbin/ip',
'-4',
'addr',
'show',
'scope',
'global',
'eth0',
namespaces: 'network'
)
end
end
end