From 31a3a3f2e24307d3094d042b47b8283046fdf5b9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 28 Jul 2012 10:43:16 -0700 Subject: [PATCH] Start moving the halt commands over to the new provider interface --- plugins/commands/halt/command.rb | 9 +--- plugins/providers/virtualbox/action.rb | 49 +++++++++++++------ .../virtualbox/action/message_not_created.rb | 16 ++++++ 3 files changed, 52 insertions(+), 22 deletions(-) create mode 100644 plugins/providers/virtualbox/action/message_not_created.rb diff --git a/plugins/commands/halt/command.rb b/plugins/commands/halt/command.rb index 150b7eb37..1bda63eea 100644 --- a/plugins/commands/halt/command.rb +++ b/plugins/commands/halt/command.rb @@ -22,13 +22,8 @@ module VagrantPlugins @logger.debug("Halt command: #{argv.inspect} #{options.inspect}") with_target_vms(argv) do |vm| - if vm.created? - @logger.info("Halting #{vm.name}") - vm.halt(:force => options[:force]) - else - @logger.info("Not halting #{vm.name}, since not created.") - vm.ui.info I18n.t("vagrant.commands.common.vm_not_created") - end + # XXX: "force" + vm.action(:halt) end # Success, exit status 0 diff --git a/plugins/providers/virtualbox/action.rb b/plugins/providers/virtualbox/action.rb index 1e1125e15..65f929d26 100644 --- a/plugins/providers/virtualbox/action.rb +++ b/plugins/providers/virtualbox/action.rb @@ -6,6 +6,7 @@ module VagrantPlugins autoload :CheckAccessible, File.expand_path("../action/check_accessible", __FILE__) autoload :CheckVirtualbox, File.expand_path("../action/check_virtualbox", __FILE__) autoload :Created, File.expand_path("../action/created", __FILE__) + autoload :MessageNotCreated, File.expand_path("../action/message_not_created", __FILE__) # Include the built-in modules so that we can use them as top-level # things. @@ -17,24 +18,42 @@ module VagrantPlugins Vagrant::Action::Builder.new.tap do |b| b.use CheckVirtualbox b.use Call, Created do |env1, b2| - if env1[:result] - # If the VM is created, then we confirm that we want to - # destroy it. - message = I18n.t("vagrant.commands.destroy.confirmation", - :name => env[:machine].name) - confirm = Vagrant::Action::Builder.build(Confirm, message) + if !env1[:result] + b2.use MessageNotCreated + next + end - b2.use Call, confirm do |env2, b3| - if env2[:result] - b3.use Vagrant::Action::General::Validate - b3.use CheckAccessible - else - env2[:ui].info I18n.t("vagrant.commands.destroy.will_not_destroy", - :name => env[:machine.name]) - end + # If the VM is created, then we confirm that we want to + # destroy it. + message = I18n.t("vagrant.commands.destroy.confirmation", + :name => env[:machine].name) + confirm = Vagrant::Action::Builder.build(Confirm, message) + + b2.use Call, confirm do |env2, b3| + if env2[:result] + b3.use Vagrant::Action::General::Validate + b3.use CheckAccessible + else + env2[:ui].info I18n.t("vagrant.commands.destroy.will_not_destroy", + :name => env[:machine.name]) end + end + end + end + end + + # This is the action that is primarily responsible for halting + # the virtual machine, gracefully or by force. + def self.action_halt + Vagrant::Action::Builder.new.tap do |b| + b.use CheckVirtualbox + b.use Call, Created do |env, b2| + if env[:result] + b2.use CheckAccessible + b2.use DiscardState + b2.use Halt else - env1[:ui].info I18n.t("vagrant.commands.common.vm_not_created") + b2.use MessageNotCreated end end end diff --git a/plugins/providers/virtualbox/action/message_not_created.rb b/plugins/providers/virtualbox/action/message_not_created.rb new file mode 100644 index 000000000..1bd792c9e --- /dev/null +++ b/plugins/providers/virtualbox/action/message_not_created.rb @@ -0,0 +1,16 @@ +module VagrantPlugins + module ProviderVirtualBox + module Action + class MessageNotCreated + def initialize(app, env) + @app = app + end + + def call(env) + env[:ui].info I18n.t("vagrant.commands.common.vm_not_created") + @app.call(env) + end + end + end + end +end