Class: Vagrant::LXC::Action::FetchIpWithLxcAttach

Inherits:
Object
  • Object
show all
Includes:
Util::Retryable
Defined in:
lib/vagrant-lxc/action/fetch_ip_with_lxc_attach.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ FetchIpWithLxcAttach

Returns a new instance of FetchIpWithLxcAttach.



8
9
10
11
# File 'lib/vagrant-lxc/action/fetch_ip_with_lxc_attach.rb', line 8

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant::lxc::action::fetch_ip_with_lxc_attach")
end

Instance Method Details

#assigned_ip(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant-lxc/action/fetch_ip_with_lxc_attach.rb', line 18

def assigned_ip(env)
  driver  = env[:machine].provider.driver
  version = driver.version.match(/^(\d+\.\d+)\./)[1].to_f
  unless version >= 0.8
    @logger.debug "lxc version does not support the --namespaces argument to lxc-attach"
    return nil
  end

  ip = ''
  retryable(:on => LXC::Errors::ExecuteError, :tries => 10, :sleep => 3) do
    unless ip = get_container_ip_from_ip_addr(driver)
      # retry
      raise LXC::Errors::ExecuteError, :command => "lxc-attach"
    end
  end
  ip
end

#call(env) ⇒ Object



13
14
15
16
# File 'lib/vagrant-lxc/action/fetch_ip_with_lxc_attach.rb', line 13

def call(env)
  env[:machine_ip] ||= assigned_ip(env)
  @app.call(env)
end

#get_container_ip_from_ip_addr(driver) ⇒ Object



37
38
39
40
41
42
# File 'lib/vagrant-lxc/action/fetch_ip_with_lxc_attach.rb', line 37

def get_container_ip_from_ip_addr(driver)
  output = driver.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