Class: Vagrant::LXC::Action::FetchIpWithLxcInfo

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

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ FetchIpWithLxcInfo

Returns a new instance of FetchIpWithLxcInfo.



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

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

Instance Method Details

#assigned_ip(env) ⇒ Object



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

def assigned_ip(env)
  config = env[:machine].provider_config
  fetch_ip_tries = config.fetch_ip_tries
  driver = env[:machine].provider.driver
  ip = ''
  return config.ssh_ip_addr if not config.ssh_ip_addr.nil?
  retryable(:on => LXC::Errors::ExecuteError, :tries => fetch_ip_tries, :sleep => 3) do
    unless ip = get_container_ip_from_ip_addr(driver)
      # retry
      raise LXC::Errors::ExecuteError, :command => "lxc-info"
    end
  end
  ip
end

#call(env) ⇒ Object



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

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

#get_container_ip_from_ip_addr(driver) ⇒ Object



35
36
37
38
39
40
# File 'lib/vagrant-lxc/action/fetch_ip_with_lxc_info.rb', line 35

def get_container_ip_from_ip_addr(driver)
  output = driver.info '-iH'
  if output =~ /^([0-9.]+)/
    return $1.to_s
  end
end