Show something meaningful to the user in case the container already exists [GH-132]

This commit is contained in:
Fabio Rehm 2014-02-02 19:27:08 -02:00
parent 786bb8a3fe
commit 7e00b96520
5 changed files with 45 additions and 13 deletions

View file

@ -58,6 +58,12 @@ module Vagrant
'--name', @name, '--name', @name,
*(config_opts), *(config_opts),
*extra *extra
rescue Errors::ExecuteError => e
if e.stderr =~ /already exists/i
raise Errors::ContainerAlreadyExists, name: @name
else
raise
end
end end
def destroy def destroy

View file

@ -5,7 +5,16 @@ module Vagrant
module Errors module Errors
class ExecuteError < Vagrant::Errors::VagrantError class ExecuteError < Vagrant::Errors::VagrantError
error_key(:lxc_execute_error) 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 end
class NamespacesNotSupported < Vagrant::Errors::VagrantError class NamespacesNotSupported < Vagrant::Errors::VagrantError
end end
@ -13,6 +22,10 @@ module Vagrant
error_key(:lxc_not_installed) error_key(:lxc_not_installed)
end end
class ContainerAlreadyExists < Vagrant::Errors::VagrantError
error_key(:lxc_container_already_exists)
end
# Box related errors # Box related errors
class TemplateFileMissing < Vagrant::Errors::VagrantError class TemplateFileMissing < Vagrant::Errors::VagrantError
error_key(:lxc_template_file_missing) error_key(:lxc_template_file_missing)

View file

@ -51,7 +51,8 @@ module Vagrant
if @interrupted if @interrupted
@logger.info("Exit code != 0, but interrupted. Ignoring.") @logger.info("Exit code != 0, but interrupted. Ignoring.")
else else
raise LXC::Errors::ExecuteError, :command => command.inspect raise LXC::Errors::ExecuteError,
command: command.inspect, stderr: r.stderr, stdout: r.stdout
end end
end end
end end

View file

@ -60,3 +60,8 @@ en:
lxc_redir_not_installed: |- lxc_redir_not_installed: |-
`redir` is not installed or is not accessible on the PATH. `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.

View file

@ -49,20 +49,27 @@ describe Vagrant::LXC::Driver::CLI do
before do before do
subject.stub(:run) { |*args| @run_args = args } subject.stub(:run) { |*args| @run_args = args }
subject.create(template, config_file, template_args)
end end
it 'issues a lxc-create with provided template, container name and hash of arguments' do it 'issues a lxc-create with provided template, container name and hash of arguments' do
subject.should have_received(:run).with( subject.create(template, config_file, template_args)
:create, subject.should have_received(:run).with(
'--template', template, :create,
'--name', name, '--template', template,
'-f', config_file, '--name', name,
'--', '-f', config_file,
'--extra-param', 'param', '--',
'--other', 'value' '--extra-param', 'param',
) '--other', 'value'
end )
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 end
describe 'destroy' do describe 'destroy' do