diff --git a/lib/vagrant-lxc/driver.rb b/lib/vagrant-lxc/driver.rb index bccb09e..f15f629 100644 --- a/lib/vagrant-lxc/driver.rb +++ b/lib/vagrant-lxc/driver.rb @@ -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 diff --git a/spec/fixtures/sample-ifconfig-output b/spec/fixtures/sample-ifconfig-output deleted file mode 100644 index bb481a6..0000000 --- a/spec/fixtures/sample-ifconfig-output +++ /dev/null @@ -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) - diff --git a/spec/fixtures/sample-ip-addr-output b/spec/fixtures/sample-ip-addr-output new file mode 100644 index 0000000..eafc9ef --- /dev/null +++ b/spec/fixtures/sample-ip-addr-output @@ -0,0 +1,2 @@ +49: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 + inet 10.0.254.137/24 brd 10.0.254.255 scope global eth0 diff --git a/spec/unit/driver_spec.rb b/spec/unit/driver_spec.rb index 9eb5e6d..f593319 100644 --- a/spec/unit/driver_spec.rb +++ b/spec/unit/driver_spec.rb @@ -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