From 9db982f7a4073854fe7398d6e7f80441bc318f93 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 4 Aug 2012 11:16:31 -0700 Subject: [PATCH] Expose the provider via the machine object. --- lib/vagrant/machine.rb | 5 +++ plugins/providers/virtualbox/action.rb | 2 ++ .../virtualbox/action/discard_state.rb | 20 +++++++++++ plugins/providers/virtualbox/action/halt.rb | 35 +++++++++++++++++++ plugins/providers/virtualbox/provider.rb | 2 ++ test/unit/vagrant/machine_test.rb | 10 ++++++ 6 files changed, 74 insertions(+) create mode 100644 plugins/providers/virtualbox/action/discard_state.rb create mode 100644 plugins/providers/virtualbox/action/halt.rb diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index 16fd03584..294e4983b 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -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. diff --git a/plugins/providers/virtualbox/action.rb b/plugins/providers/virtualbox/action.rb index 722817cab..dcba609a6 100644 --- a/plugins/providers/virtualbox/action.rb +++ b/plugins/providers/virtualbox/action.rb @@ -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__) diff --git a/plugins/providers/virtualbox/action/discard_state.rb b/plugins/providers/virtualbox/action/discard_state.rb new file mode 100644 index 000000000..b19ccbfea --- /dev/null +++ b/plugins/providers/virtualbox/action/discard_state.rb @@ -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 diff --git a/plugins/providers/virtualbox/action/halt.rb b/plugins/providers/virtualbox/action/halt.rb new file mode 100644 index 000000000..b4f2d8bba --- /dev/null +++ b/plugins/providers/virtualbox/action/halt.rb @@ -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 diff --git a/plugins/providers/virtualbox/provider.rb b/plugins/providers/virtualbox/provider.rb index 9bbe2a090..a3180b7d4 100644 --- a/plugins/providers/virtualbox/provider.rb +++ b/plugins/providers/virtualbox/provider.rb @@ -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) diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index d21300530..2c6d6a09c 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -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