Drivers now take a UUID

This commit is contained in:
Mitchell Hashimoto 2011-12-18 22:43:20 -08:00
parent a8e4e62264
commit c59defa7e8
5 changed files with 24 additions and 20 deletions

View File

@ -12,7 +12,7 @@ module Vagrant
def call(env) def call(env)
# Use the raw interface for now, while the virtualbox gem # Use the raw interface for now, while the virtualbox gem
# doesn't support guest properties (due to cross platform issues) # doesn't support guest properties (due to cross platform issues)
version = env[:vm].driver.guest_additions_version(env[:vm].uuid) version = env[:vm].driver.read_guest_additions_version
if !version if !version
env[:ui].warn I18n.t("vagrant.actions.vm.check_guest_additions.not_detected") env[:ui].warn I18n.t("vagrant.actions.vm.check_guest_additions.not_detected")
else else

View File

@ -8,7 +8,7 @@ module Vagrant
def call(env) def call(env)
env[:ui].info I18n.t("vagrant.actions.vm.destroy.destroying") env[:ui].info I18n.t("vagrant.actions.vm.destroy.destroying")
env[:vm].driver.delete(env[:vm].uuid) env[:vm].driver.delete
env[:vm].uuid = nil env[:vm].uuid = nil
@app.call(env) @app.call(env)

View File

@ -11,8 +11,7 @@ module Vagrant
# Create the proc which we want to use to modify the virtual machine # Create the proc which we want to use to modify the virtual machine
env[:ui].info I18n.t("vagrant.actions.vm.match_mac.matching") env[:ui].info I18n.t("vagrant.actions.vm.match_mac.matching")
env[:vm].driver.set_mac_address(env[:vm].uuid, env[:vm].driver.set_mac_address(env[:vm].config.vm.base_mac)
env[:vm].config.vm.base_mac)
@app.call(env) @app.call(env)
end end

View File

@ -10,7 +10,9 @@ module Vagrant
# The version of virtualbox that is running. # The version of virtualbox that is running.
attr_reader :version attr_reader :version
def initialize def initialize(uuid)
@uuid = uuid
# Read and assign the version of VirtualBox we know which # Read and assign the version of VirtualBox we know which
# specific driver to instantiate. # specific driver to instantiate.
begin begin
@ -35,21 +37,21 @@ module Vagrant
end end
# This deletes the VM with the given name. # This deletes the VM with the given name.
def delete(uuid) def delete
execute("unregistervm", uuid, "--delete") execute("unregistervm", @uuid, "--delete")
end end
# This reads the guest additions version for a VM. # This reads the guest additions version for a VM.
def guest_additions_version(uuid) def read_guest_additions_version
output = execute("guestproperty", "get", uuid, "/VirtualBox/GuestAdd/Version") output = execute("guestproperty", "get", @uuid, "/VirtualBox/GuestAdd/Version")
return $1.to_s if output =~ /^Value: (.+?)$/ return $1.to_s if output =~ /^Value: (.+?)$/
return nil return nil
end end
# This reads the state for the given UUID. The state of the VM # This reads the state for the given UUID. The state of the VM
# will be returned as a symbol. # will be returned as a symbol.
def read_state(uuid) def read_state
output = execute("showvminfo", uuid, "--machinereadable") output = execute("showvminfo", @uuid, "--machinereadable")
if output =~ /^name="<inaccessible>"$/ if output =~ /^name="<inaccessible>"$/
return :inaccessible return :inaccessible
elsif output =~ /^VMState="(.+?)"$/ elsif output =~ /^VMState="(.+?)"$/
@ -60,8 +62,8 @@ module Vagrant
end end
# This sets the MAC address for a network adapter. # This sets the MAC address for a network adapter.
def set_mac_address(uuid, mac) def set_mac_address(mac)
execute("modifyvm", uuid, "--macaddress1", mac) execute("modifyvm", @uuid, "--macaddress1", mac)
end end
protected protected

View File

@ -20,12 +20,13 @@ module Vagrant
@env = env @env = env
@config = config @config = config
@box = env.boxes.find(config.vm.box) @box = env.boxes.find(config.vm.box)
@driver = Driver::VirtualBox.new
# Look for the VM if it exists # Load the UUID if its saved.
active = env.local_data[:active] || {} active = env.local_data[:active] || {}
@uuid = active[@name.to_s] @uuid = active[@name.to_s]
# @vm = VirtualBox::VM.find(@uuid) if @uuid
# Reload ourselves to get the state
reload!
# Load the associated guest. # Load the associated guest.
load_guest! load_guest!
@ -78,7 +79,7 @@ module Vagrant
# @return [Symbol] # @return [Symbol]
def state def state
return :not_created if !@uuid return :not_created if !@uuid
state = @driver.read_state(@uuid) state = @driver.read_state
return :not_created if !state return :not_created if !state
return state return state
end end
@ -96,8 +97,6 @@ module Vagrant
# to persist the VM. Otherwise, it will remove itself from the # to persist the VM. Otherwise, it will remove itself from the
# local data (if it exists). # local data (if it exists).
def uuid=(value) def uuid=(value)
@uuid = value
env.local_data[:active] ||= {} env.local_data[:active] ||= {}
if value if value
env.local_data[:active][name.to_s] = value env.local_data[:active][name.to_s] = value
@ -108,10 +107,14 @@ module Vagrant
# Commit the local data so that the next time vagrant is initialized, # Commit the local data so that the next time vagrant is initialized,
# it realizes the VM exists # it realizes the VM exists
env.local_data.commit env.local_data.commit
# Store the uuid and reload the instance
@uuid = value
reload!
end end
def reload! def reload!
@vm = VirtualBox::VM.find(@vm.uuid) @driver = Driver::VirtualBox.new(@uuid)
end end
def package(options=nil) def package(options=nil)