From 31448c4cd4ec5ba728b8872abf85e45e509e7522 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 22 Jan 2013 12:07:37 -0800 Subject: [PATCH] Use the new GracefulHalt built-in for VirtualBox --- plugins/providers/virtualbox/action.rb | 8 +++-- .../virtualbox/action/forced_halt.rb | 25 +++++++++++++ plugins/providers/virtualbox/action/halt.rb | 35 ------------------- 3 files changed, 31 insertions(+), 37 deletions(-) create mode 100644 plugins/providers/virtualbox/action/forced_halt.rb delete mode 100644 plugins/providers/virtualbox/action/halt.rb diff --git a/plugins/providers/virtualbox/action.rb b/plugins/providers/virtualbox/action.rb index 9a0908dd3..756c9a8cf 100644 --- a/plugins/providers/virtualbox/action.rb +++ b/plugins/providers/virtualbox/action.rb @@ -23,8 +23,8 @@ module VagrantPlugins autoload :DestroyUnusedNetworkInterfaces, File.expand_path("../action/destroy_unused_network_interfaces", __FILE__) autoload :DiscardState, File.expand_path("../action/discard_state", __FILE__) autoload :Export, File.expand_path("../action/export", __FILE__) + autoload :ForcedHalt, File.expand_path("../action/forced_halt", __FILE__) autoload :ForwardPorts, File.expand_path("../action/forward_ports", __FILE__) - autoload :Halt, File.expand_path("../action/halt", __FILE__) autoload :HostName, File.expand_path("../action/host_name", __FILE__) autoload :Import, File.expand_path("../action/import", __FILE__) autoload :IsRunning, File.expand_path("../action/is_running", __FILE__) @@ -110,7 +110,11 @@ module VagrantPlugins if env[:result] b2.use CheckAccessible b2.use DiscardState - b2.use Halt + b2.use Call, GracefulHalt, :poweroff, :running do |env2, b3| + if !env[:result] + b3.use ForcedHalt + end + end else b2.use MessageNotCreated end diff --git a/plugins/providers/virtualbox/action/forced_halt.rb b/plugins/providers/virtualbox/action/forced_halt.rb new file mode 100644 index 000000000..8fc20782c --- /dev/null +++ b/plugins/providers/virtualbox/action/forced_halt.rb @@ -0,0 +1,25 @@ +module VagrantPlugins + module ProviderVirtualBox + module Action + class ForcedHalt + def initialize(app, env) + @app = app + end + + def call(env) + current_state = env[:machine].state.id + if current_state == :running || current_state == :gurumeditation + env[:ui].info I18n.t("vagrant.actions.vm.halt.force") + env[:machine].provider.driver.halt + end + + # Sleep for a second to verify that the VM properly + # cleans itself up. Silly VirtualBox. + sleep 1 if !env["vagrant.test"] + + @app.call(env) + end + end + end + end +end diff --git a/plugins/providers/virtualbox/action/halt.rb b/plugins/providers/virtualbox/action/halt.rb deleted file mode 100644 index d51990910..000000000 --- a/plugins/providers/virtualbox/action/halt.rb +++ /dev/null @@ -1,35 +0,0 @@ -module VagrantPlugins - module ProviderVirtualBox - module Action - class Halt - def initialize(app, env) - @app = app - end - - def call(env) - current_state = env[:machine].provider.state.id - 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[:machine].guest.halt - end - - # If we're not powered off now, then force it - if env[:machine].provider.state.id != :poweroff - env[:ui].info I18n.t("vagrant.actions.vm.halt.force") - env[:machine].provider.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