Expose the provider via the machine object.

This commit is contained in:
Mitchell Hashimoto 2012-08-04 11:16:31 -07:00
parent d8cb02d55d
commit 9db982f7a4
6 changed files with 74 additions and 0 deletions

View File

@ -32,6 +32,11 @@ module Vagrant
# @return [String]
attr_reader :name
# The provider backing this machine.
#
# @return [Object]
attr_reader :provider
# Initialize a new machine.
#
# @param [String] name Name of the virtual machine.

View File

@ -7,6 +7,8 @@ module VagrantPlugins
autoload :CheckVirtualbox, File.expand_path("../action/check_virtualbox", __FILE__)
autoload :Created, File.expand_path("../action/created", __FILE__)
autoload :DestroyConfirm, File.expand_path("../action/destroy_confirm", __FILE__)
autoload :DiscardState, File.expand_path("../action/discard_state", __FILE__)
autoload :Halt, File.expand_path("../action/halt", __FILE__)
autoload :MessageNotCreated, File.expand_path("../action/message_not_created", __FILE__)
autoload :MessageWillNotDestroy, File.expand_path("../action/message_will_not_destroy", __FILE__)

View File

@ -0,0 +1,20 @@
module VagrantPlugins
module ProviderVirtualBox
module Action
class DiscardState
def initialize(app, env)
@app = app
end
def call(env)
if env[:machine].provider.state == :saved
env[:ui].info I18n.t("vagrant.actions.vm.discard_state.discarding")
env[:machine].provider.driver.discard_saved_state
end
@app.call(env)
end
end
end
end
end

View File

@ -0,0 +1,35 @@
module VagrantPlugins
module ProviderVirtualBox
module Action
class Halt
def initialize(app, env)
@app = app
end
def call(env)
current_state = env[:machine].provider.state
if current_state == :running || current_state == :gurumeditation
# If the VM is running and we're not forcing, we can
# attempt a graceful shutdown
if current_state == :running && !env["force"]
env[:ui].info I18n.t("vagrant.actions.vm.halt.graceful")
env[:vm].guest.halt
end
# If we're not powered off now, then force it
if env[:vm].state != :poweroff
env[:ui].info I18n.t("vagrant.actions.vm.halt.force")
env[:vm].driver.halt
end
# Sleep for a second to verify that the VM properly
# cleans itself up
sleep 1 if !env["vagrant.test"]
end
@app.call(env)
end
end
end
end
end

View File

@ -1,6 +1,8 @@
module VagrantPlugins
module ProviderVirtualBox
class Provider < Vagrant.plugin("1", :provider)
attr_reader :driver
def initialize(machine)
@machine = machine
@driver = Driver::Meta.new(@machine.id)

View File

@ -96,6 +96,12 @@ describe Vagrant::Machine do
machine.id.should == "foo"
end
end
it "should NOT have access to the provider" do
provider_init_test do |machine|
machine.provider.should be_nil
end
end
end
end
@ -115,6 +121,10 @@ describe Vagrant::Machine do
it "should provide access to the environment" do
instance.env.should eql(env)
end
it "should provide access to the provider" do
instance.provider.should eql(provider)
end
end
describe "actions" do