From 1392d9b4a5a895e37412da6036e82873b7d9b6e1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 30 Jan 2013 23:22:45 -0800 Subject: [PATCH] Detect if a VM name is already taken for VirtualBox --- lib/vagrant/errors.rb | 4 ++++ plugins/providers/virtualbox/action.rb | 2 +- .../virtualbox/action/prune_nfs_exports.rb | 4 ++-- plugins/providers/virtualbox/action/set_name.rb | 14 ++++++++++++-- plugins/providers/virtualbox/driver/version_4_0.rb | 6 +++--- plugins/providers/virtualbox/driver/version_4_1.rb | 6 +++--- plugins/providers/virtualbox/driver/version_4_2.rb | 6 +++--- templates/locales/en.yml | 7 +++++++ 8 files changed, 35 insertions(+), 14 deletions(-) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index e51d74a4f..a90c0b21b 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -501,6 +501,10 @@ module Vagrant error_key(:vm_inaccessible) end + class VMNameExists < VagrantError + error_key(:vm_name_exists) + end + class VMNoMatchError < VagrantError status_code(63) error_key(:vm_no_match) diff --git a/plugins/providers/virtualbox/action.rb b/plugins/providers/virtualbox/action.rb index 1506de024..37e462430 100644 --- a/plugins/providers/virtualbox/action.rb +++ b/plugins/providers/virtualbox/action.rb @@ -240,6 +240,7 @@ module VagrantPlugins Vagrant::Action::Builder.new.tap do |b| b.use CheckVirtualbox b.use ConfigValidate + b.use SetName b.use Call, IsRunning do |env, b2| # If the VM is running, then our work here is done, exit next if env[:result] @@ -295,7 +296,6 @@ module VagrantPlugins b2.use CheckBox b2.use Import b2.use CheckGuestAdditions - b2.use SetName b2.use MatchMACAddress end end diff --git a/plugins/providers/virtualbox/action/prune_nfs_exports.rb b/plugins/providers/virtualbox/action/prune_nfs_exports.rb index e74f176fc..581988cc1 100644 --- a/plugins/providers/virtualbox/action/prune_nfs_exports.rb +++ b/plugins/providers/virtualbox/action/prune_nfs_exports.rb @@ -8,8 +8,8 @@ module VagrantPlugins def call(env) if env[:host] - valid_ids = env[:machine].provider.driver.read_vms - env[:host].nfs_prune(valid_ids) + vms = env[:machine].provider.driver.read_vms + env[:host].nfs_prune(vms.values) end @app.call(env) diff --git a/plugins/providers/virtualbox/action/set_name.rb b/plugins/providers/virtualbox/action/set_name.rb index adfaa1b0e..2ee966480 100644 --- a/plugins/providers/virtualbox/action/set_name.rb +++ b/plugins/providers/virtualbox/action/set_name.rb @@ -19,8 +19,18 @@ module VagrantPlugins name = prefix + "_#{Time.now.to_i}" end - @logger.info("Setting the name of the VM: #{name}") - env[:machine].provider.driver.set_name(name) + # Verify the name is not taken + vms = env[:machine].provider.driver.read_vms + raise Vagrant::Errors::VMNameExists, :name => name if \ + vms.has_key?(name) && vms[name] != env[:machine].id + + if vms.has_key?(name) + @logger.info("Not setting the name because our name is already set.") + else + @logger.info("Setting the name of the VM: #{name}") + env[:ui].info(I18n.t("vagrant.actions.set_name.setting_name")) + env[:machine].provider.driver.set_name(name) + end @app.call(env) end diff --git a/plugins/providers/virtualbox/driver/version_4_0.rb b/plugins/providers/virtualbox/driver/version_4_0.rb index 4479cdaae..91bfb62a2 100644 --- a/plugins/providers/virtualbox/driver/version_4_0.rb +++ b/plugins/providers/virtualbox/driver/version_4_0.rb @@ -398,10 +398,10 @@ module VagrantPlugins end def read_vms - results = [] + results = {} execute("list", "vms", :retryable => true).split("\n").each do |line| - if vm = line[/^".+?" \{(.+?)\}$/, 1] - results << vm + if line =~ /^"(.+?)" \{(.+?)\}$/ + results[$1.to_s] = $2.to_s end end diff --git a/plugins/providers/virtualbox/driver/version_4_1.rb b/plugins/providers/virtualbox/driver/version_4_1.rb index baab7f78e..6d0854d32 100644 --- a/plugins/providers/virtualbox/driver/version_4_1.rb +++ b/plugins/providers/virtualbox/driver/version_4_1.rb @@ -398,10 +398,10 @@ module VagrantPlugins end def read_vms - results = [] + results = {} execute("list", "vms", :retryable => true).split("\n").each do |line| - if vm = line[/^".+?" \{(.+?)\}$/, 1] - results << vm + if line =~ /^"(.+?)" \{(.+?)\}$/ + results[$1.to_s] = $2.to_s end end diff --git a/plugins/providers/virtualbox/driver/version_4_2.rb b/plugins/providers/virtualbox/driver/version_4_2.rb index 2a446e9ef..33652154a 100644 --- a/plugins/providers/virtualbox/driver/version_4_2.rb +++ b/plugins/providers/virtualbox/driver/version_4_2.rb @@ -383,10 +383,10 @@ module VagrantPlugins end def read_vms - results = [] + results = {} execute("list", "vms", :retryable => true).split("\n").each do |line| - if line =~ /^".+?" \{(.+?)\}$/ - results << $1.to_s + if line =~ /^"(.+?)" \{(.+?)\}$/ + results[$1.to_s] = $2.to_s end end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 417d3686f..3713b3f46 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -333,6 +333,10 @@ en: with VirtualBox that Vagrant can not cleanly recover from. Please open VirtualBox and clear out your inaccessible virtual machines or find a way to fix them. + vm_name_exists: |- + A VirtualBox machine with the name '%{name}' already exists. + Please use another name or delete the machine with the existing + name, and try again. vm_no_match: |- No virtual machines matched the regular expression given. vm_not_found: |- @@ -682,6 +686,9 @@ en: mounting: Mounting shared folders... mounting_entry: "-- %{guest_path}" nomount_entry: "-- Automounting disabled: %{host_path}" + set_name: + setting_name: |- + Setting the name of the VM... suspend: suspending: Saving VM state and suspending execution...