From f9dd5392ff3d330bea7d0e822b1e3509a2d6d823 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sat, 28 Sep 2013 22:37:31 -0300 Subject: [PATCH] Improve checking for support for fetching IP with `lxc-attach` [GH-118] --- lib/vagrant-lxc/action/fetch_ip_with_lxc_attach.rb | 12 +++--------- lib/vagrant-lxc/driver/cli.rb | 7 ++++++- lib/vagrant-lxc/errors.rb | 2 ++ 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/vagrant-lxc/action/fetch_ip_with_lxc_attach.rb b/lib/vagrant-lxc/action/fetch_ip_with_lxc_attach.rb index 45e6231..dd8641f 100644 --- a/lib/vagrant-lxc/action/fetch_ip_with_lxc_attach.rb +++ b/lib/vagrant-lxc/action/fetch_ip_with_lxc_attach.rb @@ -12,20 +12,14 @@ module Vagrant def call(env) env[:machine_ip] ||= assigned_ip(env) - rescue LXC::Errors::ExecuteError - @logger.info 'Unable to fetch IP with `lxc-attach`!' + rescue LXC::Errors::NamespacesNotSupported + @logger.info 'The `lxc-attach` command available does not support the --namespaces parameter, falling back to dnsmasq leases to fetch container ip' ensure @app.call(env) end 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 - + driver = env[:machine].provider.driver ip = '' retryable(:on => LXC::Errors::ExecuteError, :tries => 10, :sleep => 3) do unless ip = get_container_ip_from_ip_addr(driver) diff --git a/lib/vagrant-lxc/driver/cli.rb b/lib/vagrant-lxc/driver/cli.rb index c963ac7..71b8906 100644 --- a/lib/vagrant-lxc/driver/cli.rb +++ b/lib/vagrant-lxc/driver/cli.rb @@ -81,7 +81,12 @@ module Vagrant if cmd.last.is_a?(Hash) opts = cmd.pop namespaces = Array(opts[:namespaces]).map(&:upcase).join('|') - extra = ['--namespaces', namespaces] if namespaces + + if run(:attach, '-h').include?('--namespaces') + extra = ['--namespaces', namespaces] if namespaces + else + raise LXC::Errors::NamespacesNotSupported + end end run :attach, '--name', @name, *((extra || []) + cmd) diff --git a/lib/vagrant-lxc/errors.rb b/lib/vagrant-lxc/errors.rb index e2c612f..557cbaa 100644 --- a/lib/vagrant-lxc/errors.rb +++ b/lib/vagrant-lxc/errors.rb @@ -6,6 +6,8 @@ module Vagrant class ExecuteError < Vagrant::Errors::VagrantError error_key(:lxc_execute_error) end + class NamespacesNotSupported < Vagrant::Errors::VagrantError + end class LxcNotInstalled < Vagrant::Errors::VagrantError error_key(:lxc_not_installed)