Skip to content
This repository has been archived by the owner on Nov 24, 2022. It is now read-only.

Commit

Permalink
First stab at handling a custom lxc-dhcp IP
Browse files Browse the repository at this point in the history
Will help on #23
  • Loading branch information
fgrehm committed Mar 8, 2013
1 parent 3e7488f commit 9a16895
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
9 changes: 8 additions & 1 deletion lib/vagrant-lxc/config.rb
Expand Up @@ -6,8 +6,15 @@ class Config < Vagrant.plugin("2", :config)
# @return [Array]
attr_reader :start_opts

# The ip set for the built in LXC dhcp server (defaults to configured ip
# at /etc/default/lxc or 10.0.3.1)
#
# @return [String]
attr_accessor :lxc_dhcp_ip

def initialize
@start_opts = []
@start_opts = []
@lxc_dhcp_ip = '10.0.3.1'
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/vagrant-lxc/container.rb
Expand Up @@ -115,23 +115,23 @@ def state
end
end

def dhcp_ip
def dhcp_ip(server_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 => 3) 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')
r = (raw 'dig', @name, "@#{server_ip}", '+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
raise LXC::Errors::ExecuteError, :command => ['dig', @name, "@#{server_ip}", '+short'].inspect
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/vagrant-lxc/provider.rb
Expand Up @@ -52,7 +52,7 @@ def ssh_info
return nil if state == :not_created

{
:host => @container.dhcp_ip,
:host => @container.dhcp_ip(@machine.provider_config.lxc_dhcp_ip),
:port => 22 # @driver.ssh_port(@machine.config.ssh.guest_port)
}
end
Expand Down
9 changes: 5 additions & 4 deletions spec/unit/container_spec.rb
Expand Up @@ -181,8 +181,9 @@
end

describe 'dhcp ip' do
let(:name) { 'random-container-name' }
let(:ip) { "10.0.3.123" }
let(:name) { 'random-container-name' }
let(:ip) { "10.0.4.123" }
let(:server_ip) { "10.0.4.1" }

before do
subject.stub(:raw) {
Expand All @@ -191,8 +192,8 @@
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')
subject.dhcp_ip(server_ip).should == ip
subject.should have_received(:raw).with('dig', name, "@#{server_ip}", '+short')
end
end
end

0 comments on commit 9a16895

Please sign in to comment.