From c119a34f0e0fea6ac0012af3c3e8beb8ccfe304d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 2 Mar 2010 21:53:16 -0800 Subject: [PATCH] Added vagrant-halt to forcibly shut down vagrant instance. --- bin/vagrant-halt | 29 +++++++++++++++++++++ lib/vagrant/actions/vm/{stop.rb => halt.rb} | 4 +-- lib/vagrant/actions/vm/reload.rb | 2 +- lib/vagrant/commands.rb | 12 +++++++++ lib/vagrant/vm.rb | 2 +- test/vagrant/actions/vm/halt_test.rb | 15 +++++++++++ test/vagrant/actions/vm/reload_test.rb | 2 +- test/vagrant/actions/vm/stop_test.rb | 12 --------- test/vagrant/commands_test.rb | 12 +++++++++ test/vagrant/vm_test.rb | 2 +- 10 files changed, 74 insertions(+), 18 deletions(-) create mode 100755 bin/vagrant-halt rename lib/vagrant/actions/vm/{stop.rb => halt.rb} (86%) create mode 100644 test/vagrant/actions/vm/halt_test.rb delete mode 100644 test/vagrant/actions/vm/stop_test.rb diff --git a/bin/vagrant-halt b/bin/vagrant-halt new file mode 100755 index 000000000..a619324d4 --- /dev/null +++ b/bin/vagrant-halt @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +begin + require File.expand_path('../../.bundle/environment', __FILE__) +rescue LoadError + # Fallback on rubygems + require "rubygems" +end + +require 'git-style-binary/command' + +# Get library +libdir = File.join(File.dirname(__FILE__), '..', 'lib') +$:.unshift(libdir) unless $:.include?(libdir) +require 'vagrant' + +GitStyleBinary.command do + short_desc "forcibly halts the vagrant environment" + banner <<-EOS +Usage: #{command.full_name} #{all_options_string} + +Forcibly halts the vagrant environment. This is the equivalent +of pulling the power on your computer. + +EOS + + run do |command| + Vagrant::Commands.halt + end +end diff --git a/lib/vagrant/actions/vm/stop.rb b/lib/vagrant/actions/vm/halt.rb similarity index 86% rename from lib/vagrant/actions/vm/stop.rb rename to lib/vagrant/actions/vm/halt.rb index 32e84efaa..4da6ec03a 100644 --- a/lib/vagrant/actions/vm/stop.rb +++ b/lib/vagrant/actions/vm/halt.rb @@ -1,7 +1,7 @@ module Vagrant module Actions module VM - class Stop < Base + class Halt < Base def execute! logger.info "Forcing shutdown of VM..." @runner.vm.stop(true) @@ -9,4 +9,4 @@ module Vagrant end end end -end \ No newline at end of file +end diff --git a/lib/vagrant/actions/vm/reload.rb b/lib/vagrant/actions/vm/reload.rb index 8e6357a0f..12e2d22e5 100644 --- a/lib/vagrant/actions/vm/reload.rb +++ b/lib/vagrant/actions/vm/reload.rb @@ -3,7 +3,7 @@ module Vagrant module VM class Reload < Base def prepare - steps = [Stop, ForwardPorts, SharedFolders, Start] + steps = [Halt, ForwardPorts, SharedFolders, Start] steps << Provision if Vagrant.config.chef.enabled steps.each do |action_klass| diff --git a/lib/vagrant/commands.rb b/lib/vagrant/commands.rb index 85f193f2d..d61511a59 100644 --- a/lib/vagrant/commands.rb +++ b/lib/vagrant/commands.rb @@ -79,6 +79,18 @@ error SSH.connect end + # Halts a running vagrant instance. This forcibly halts the instance; + # it is the equivalent of pulling the power on a machine. The instance + # can be restarted again with {up}. + # + # This command requires than an instance already be brought up with + # `vagrant up`. + def halt + Env.load! + Env.require_persisted_vm + Env.persisted_vm.execute!(Actions::VM::Halt) + end + # Suspend a running vagrant instance. This suspends the instance, saving # the state of the VM and "pausing" it. The instance can be resumed # again with {resume}. diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index b81382d62..b71c4ecf2 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -26,7 +26,7 @@ module Vagrant end def destroy - execute!(Actions::VM::Stop) if @vm.running? + execute!(Actions::VM::Halt) if @vm.running? logger.info "Destroying VM and associated drives..." @vm.destroy(:destroy_image => true) diff --git a/test/vagrant/actions/vm/halt_test.rb b/test/vagrant/actions/vm/halt_test.rb new file mode 100644 index 000000000..39c0abdf2 --- /dev/null +++ b/test/vagrant/actions/vm/halt_test.rb @@ -0,0 +1,15 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') + +class HaltActionTest < Test::Unit::TestCase + setup do + @runner, @vm, @action = mock_action(Vagrant::Actions::VM::Halt) + mock_config + end + + context "executing" do + should "force the VM to stop" do + @vm.expects(:stop).with(true).once + @action.execute! + end + end +end diff --git a/test/vagrant/actions/vm/reload_test.rb b/test/vagrant/actions/vm/reload_test.rb index c7f19b591..568ea7e41 100644 --- a/test/vagrant/actions/vm/reload_test.rb +++ b/test/vagrant/actions/vm/reload_test.rb @@ -8,7 +8,7 @@ class ReloadActionTest < Test::Unit::TestCase context "sub-actions" do setup do - @default_order = [Vagrant::Actions::VM::Stop, Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Start] + @default_order = [Vagrant::Actions::VM::Halt, Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Start] end def setup_action_expectations diff --git a/test/vagrant/actions/vm/stop_test.rb b/test/vagrant/actions/vm/stop_test.rb deleted file mode 100644 index 6f9b2ac0b..000000000 --- a/test/vagrant/actions/vm/stop_test.rb +++ /dev/null @@ -1,12 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') - -class StopActionTest < Test::Unit::TestCase - setup do - @mock_vm, @vm, @action = mock_action(Vagrant::Actions::VM::Stop) - end - - should "force the VM to stop" do - @vm.expects(:stop).with(true).once - @action.execute! - end -end diff --git a/test/vagrant/commands_test.rb b/test/vagrant/commands_test.rb index 8461d754e..4c6f1c672 100644 --- a/test/vagrant/commands_test.rb +++ b/test/vagrant/commands_test.rb @@ -103,6 +103,18 @@ class CommandsTest < Test::Unit::TestCase end end + context "halt" do + should "require a persisted VM" do + Vagrant::Env.expects(:require_persisted_vm).once + Vagrant::Commands.halt + end + + should "call the `halt` action on the VM" do + @persisted_vm.expects(:execute!).with(Vagrant::Actions::VM::Halt).once + Vagrant::Commands.halt + end + end + context "suspend" do setup do @persisted_vm.stubs(:save_state) diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index 7fb6abca5..5cd0e57ff 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -62,7 +62,7 @@ class VMTest < Test::Unit::TestCase should "stop the VM if its running" do @mock_vm.expects(:running?).returns(true) @mock_vm.expects(:destroy).with(:destroy_image => true).once - @vm.expects(:execute!).with(Vagrant::Actions::VM::Stop).once + @vm.expects(:execute!).with(Vagrant::Actions::VM::Halt).once @vm.destroy end end