Added vagrant-halt to forcibly shut down vagrant instance.

This commit is contained in:
Mitchell Hashimoto 2010-03-02 21:53:16 -08:00
parent aaae366e9d
commit c119a34f0e
10 changed files with 74 additions and 18 deletions

29
bin/vagrant-halt Executable file
View File

@ -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

View File

@ -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
end

View File

@ -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|

View File

@ -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}.

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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