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)
end
class VMNameExists < VagrantError
error_key(:vm_name_exists)
end
class VMNoMatchError < VagrantError
status_code(63)
error_key(:vm_no_match)

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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...