From 7e00b96520b66cd91d7ad8a29e78e0ee7496efc8 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sun, 2 Feb 2014 19:27:08 -0200 Subject: [PATCH] Show something meaningful to the user in case the container already exists [GH-132] --- lib/vagrant-lxc/driver/cli.rb | 6 ++++++ lib/vagrant-lxc/errors.rb | 13 +++++++++++++ lib/vagrant-lxc/sudo_wrapper.rb | 3 ++- locales/en.yml | 5 +++++ spec/unit/driver/cli_spec.rb | 31 +++++++++++++++++++------------ 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/lib/vagrant-lxc/driver/cli.rb b/lib/vagrant-lxc/driver/cli.rb index 80e538f..c7fd882 100644 --- a/lib/vagrant-lxc/driver/cli.rb +++ b/lib/vagrant-lxc/driver/cli.rb @@ -58,6 +58,12 @@ module Vagrant '--name', @name, *(config_opts), *extra + rescue Errors::ExecuteError => e + if e.stderr =~ /already exists/i + raise Errors::ContainerAlreadyExists, name: @name + else + raise + end end def destroy diff --git a/lib/vagrant-lxc/errors.rb b/lib/vagrant-lxc/errors.rb index 557cbaa..c75bfa3 100644 --- a/lib/vagrant-lxc/errors.rb +++ b/lib/vagrant-lxc/errors.rb @@ -5,7 +5,16 @@ module Vagrant module Errors class ExecuteError < Vagrant::Errors::VagrantError error_key(:lxc_execute_error) + attr_reader :stderr, :stdout + def initialize(message, *args) + super + if message.is_a?(Hash) + @stderr = message[:stderr] + @stdout = message[:stdout] + end + end end + class NamespacesNotSupported < Vagrant::Errors::VagrantError end @@ -13,6 +22,10 @@ module Vagrant error_key(:lxc_not_installed) end + class ContainerAlreadyExists < Vagrant::Errors::VagrantError + error_key(:lxc_container_already_exists) + end + # Box related errors class TemplateFileMissing < Vagrant::Errors::VagrantError error_key(:lxc_template_file_missing) diff --git a/lib/vagrant-lxc/sudo_wrapper.rb b/lib/vagrant-lxc/sudo_wrapper.rb index cfce001..d906d1e 100644 --- a/lib/vagrant-lxc/sudo_wrapper.rb +++ b/lib/vagrant-lxc/sudo_wrapper.rb @@ -51,7 +51,8 @@ module Vagrant if @interrupted @logger.info("Exit code != 0, but interrupted. Ignoring.") else - raise LXC::Errors::ExecuteError, :command => command.inspect + raise LXC::Errors::ExecuteError, + command: command.inspect, stderr: r.stderr, stdout: r.stdout end end end diff --git a/locales/en.yml b/locales/en.yml index 8a24d1d..2958b99 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -60,3 +60,8 @@ en: lxc_redir_not_installed: |- `redir` is not installed or is not accessible on the PATH. + + lxc_container_already_exists: |- + There is container on your system with the same name you've specified + on your Vagrantfile (%{name}), please choose a different one or + run `lxc-destroy --name %{name}` and try again. diff --git a/spec/unit/driver/cli_spec.rb b/spec/unit/driver/cli_spec.rb index 1b7e00c..5e8dd23 100644 --- a/spec/unit/driver/cli_spec.rb +++ b/spec/unit/driver/cli_spec.rb @@ -49,20 +49,27 @@ describe Vagrant::LXC::Driver::CLI do before do subject.stub(:run) { |*args| @run_args = args } - subject.create(template, config_file, template_args) end - it 'issues a lxc-create with provided template, container name and hash of arguments' do - subject.should have_received(:run).with( - :create, - '--template', template, - '--name', name, - '-f', config_file, - '--', - '--extra-param', 'param', - '--other', 'value' - ) - end + it 'issues a lxc-create with provided template, container name and hash of arguments' do + subject.create(template, config_file, template_args) + subject.should have_received(:run).with( + :create, + '--template', template, + '--name', name, + '-f', config_file, + '--', + '--extra-param', 'param', + '--other', 'value' + ) + end + + it 'wraps a low level error into something more meaningful in case the container already exists' do + subject.stub(:run) { raise Vagrant::LXC::Errors::ExecuteError, stderr: 'alreAdy Exists' } + expect { + subject.create(template, config_file, template_args) + }.to raise_error(Vagrant::LXC::Errors::ContainerAlreadyExists) + end end describe 'destroy' do