Detect if a VM name is already taken for VirtualBox

This commit is contained in:
Mitchell Hashimoto 2013-01-30 23:22:45 -08:00
parent 4fa194899d
commit 1392d9b4a5
8 changed files with 35 additions and 14 deletions

View File

@ -501,6 +501,10 @@ module Vagrant
error_key(:vm_inaccessible) error_key(:vm_inaccessible)
end end
class VMNameExists < VagrantError
error_key(:vm_name_exists)
end
class VMNoMatchError < VagrantError class VMNoMatchError < VagrantError
status_code(63) status_code(63)
error_key(:vm_no_match) error_key(:vm_no_match)

View File

@ -240,6 +240,7 @@ module VagrantPlugins
Vagrant::Action::Builder.new.tap do |b| Vagrant::Action::Builder.new.tap do |b|
b.use CheckVirtualbox b.use CheckVirtualbox
b.use ConfigValidate b.use ConfigValidate
b.use SetName
b.use Call, IsRunning do |env, b2| b.use Call, IsRunning do |env, b2|
# If the VM is running, then our work here is done, exit # If the VM is running, then our work here is done, exit
next if env[:result] next if env[:result]
@ -295,7 +296,6 @@ module VagrantPlugins
b2.use CheckBox b2.use CheckBox
b2.use Import b2.use Import
b2.use CheckGuestAdditions b2.use CheckGuestAdditions
b2.use SetName
b2.use MatchMACAddress b2.use MatchMACAddress
end end
end end

View File

@ -8,8 +8,8 @@ module VagrantPlugins
def call(env) def call(env)
if env[:host] if env[:host]
valid_ids = env[:machine].provider.driver.read_vms vms = env[:machine].provider.driver.read_vms
env[:host].nfs_prune(valid_ids) env[:host].nfs_prune(vms.values)
end end
@app.call(env) @app.call(env)

View File

@ -19,8 +19,18 @@ module VagrantPlugins
name = prefix + "_#{Time.now.to_i}" name = prefix + "_#{Time.now.to_i}"
end end
@logger.info("Setting the name of the VM: #{name}") # Verify the name is not taken
env[:machine].provider.driver.set_name(name) 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) @app.call(env)
end end

View File

@ -398,10 +398,10 @@ module VagrantPlugins
end end
def read_vms def read_vms
results = [] results = {}
execute("list", "vms", :retryable => true).split("\n").each do |line| execute("list", "vms", :retryable => true).split("\n").each do |line|
if vm = line[/^".+?" \{(.+?)\}$/, 1] if line =~ /^"(.+?)" \{(.+?)\}$/
results << vm results[$1.to_s] = $2.to_s
end end
end end

View File

@ -398,10 +398,10 @@ module VagrantPlugins
end end
def read_vms def read_vms
results = [] results = {}
execute("list", "vms", :retryable => true).split("\n").each do |line| execute("list", "vms", :retryable => true).split("\n").each do |line|
if vm = line[/^".+?" \{(.+?)\}$/, 1] if line =~ /^"(.+?)" \{(.+?)\}$/
results << vm results[$1.to_s] = $2.to_s
end end
end end

View File

@ -383,10 +383,10 @@ module VagrantPlugins
end end
def read_vms def read_vms
results = [] results = {}
execute("list", "vms", :retryable => true).split("\n").each do |line| execute("list", "vms", :retryable => true).split("\n").each do |line|
if line =~ /^".+?" \{(.+?)\}$/ if line =~ /^"(.+?)" \{(.+?)\}$/
results << $1.to_s results[$1.to_s] = $2.to_s
end end
end end

View File

@ -333,6 +333,10 @@ en:
with VirtualBox that Vagrant can not cleanly recover from. Please open VirtualBox 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 and clear out your inaccessible virtual machines or find a way to fix
them. 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: |- vm_no_match: |-
No virtual machines matched the regular expression given. No virtual machines matched the regular expression given.
vm_not_found: |- vm_not_found: |-
@ -682,6 +686,9 @@ en:
mounting: Mounting shared folders... mounting: Mounting shared folders...
mounting_entry: "-- %{guest_path}" mounting_entry: "-- %{guest_path}"
nomount_entry: "-- Automounting disabled: %{host_path}" nomount_entry: "-- Automounting disabled: %{host_path}"
set_name:
setting_name: |-
Setting the name of the VM...
suspend: suspend:
suspending: Saving VM state and suspending execution... suspending: Saving VM state and suspending execution...