Can now specify which VMs to up with `vagrant up` or will up all by default

This commit is contained in:
Mitchell Hashimoto 2010-05-16 18:34:35 -07:00
parent 3b4d2ab795
commit 81e1e8932a
5 changed files with 86 additions and 47 deletions

View File

@ -9,18 +9,40 @@ module Vagrant
description "Creates the vagrant environment" description "Creates the vagrant environment"
def execute(args=[]) def execute(args=[])
args = parse_options(args)
if args[0]
up_single(args[0])
else
up_all
end
end
def up_single(name)
vm = env.vms[name.to_sym]
if vm.nil?
error_and_exit(:unknown_vm, :vm => name)
return # for tests
end
if vm.created?
logger.info "VM '#{name}' already created. Booting if its not already running..."
vm.start
else
vm.env.require_box
logger.info "Creating VM '#{name}'"
vm.up
end
end
def up_all
# First verify that all VMs have valid boxes # First verify that all VMs have valid boxes
env.vms.each { |name, vm| vm.env.require_box unless vm.created? } env.vms.each { |name, vm| vm.env.require_box unless vm.created? }
# Next, handle each VM # Next, handle each VM
env.vms.each do |name, vm| env.vms.keys.each do |name|
if vm.created? up_single(name)
logger.info "VM '#{name}' already created. Booting if its not already running..."
vm.start
else
logger.info "Creating VM '#{name}'"
vm.execute!(Actions::VM::Up)
end
end end
end end

View File

@ -99,6 +99,10 @@ module Vagrant
execute! execute!
end end
def up
execute!(Actions::VM::Up)
end
def start def start
return if @vm.running? return if @vm.running?

View File

@ -4,13 +4,7 @@ class CommandsHaltTest < Test::Unit::TestCase
setup do setup do
@klass = Vagrant::Commands::Halt @klass = Vagrant::Commands::Halt
@persisted_vm = mock("persisted_vm")
@persisted_vm.stubs(:execute!)
@env = mock_environment @env = mock_environment
@env.stubs(:require_persisted_vm)
@env.stubs(:vm).returns(@persisted_vm)
@instance = @klass.new(@env) @instance = @klass.new(@env)
end end

View File

@ -6,29 +6,59 @@ class CommandsUpTest < Test::Unit::TestCase
@env = mock_environment @env = mock_environment
@instance = @klass.new(@env) @instance = @klass.new(@env)
@persisted_vm = mock("persisted_vm")
@persisted_vm.stubs(:execute!)
end end
context "executing" do context "executing" do
should "call up_all if no name is given" do
@instance.expects(:up_all).once
@instance.execute
end
should "call up_single if a name is given" do
@instance.expects(:up_single).with("foo").once
@instance.execute(["foo"])
end
end
context "upping a single VM" do
setup do setup do
@new_vm = mock("vm") @vm = mock("vm")
@new_vm.stubs(:execute!) @vm.stubs(:env).returns(@env)
@vms = {}
@vms = {:foo => @vm}
@env.stubs(:vms).returns(@vms)
end
should "error and exit if the VM doesn't exist" do
@env.stubs(:vms).returns({})
@instance.expects(:error_and_exit).with(:unknown_vm, :vm => :foo).once
@instance.up_single(:foo)
end
should "start created VMs" do
@vm.stubs(:created?).returns(true)
@vm.expects(:start).once
@instance.up_single(:foo)
end
should "up non-created VMs" do
@vm.stubs(:created?).returns(false)
@vm.env.expects(:require_box).once
@vm.expects(:up).once
@vm.expects(:start).never
@instance.up_single(:foo)
end
end
context "upping all VMs" do
setup do
@vms = {}
@env.stubs(:vms).returns(@vms) @env.stubs(:vms).returns(@vms)
@env.stubs(:require_box)
end end
def create_vm def create_vm
env = mock_environment
env.stubs(:require_box)
vm = mock("vm") vm = mock("vm")
vm.stubs(:env).returns(env) vm.stubs(:env).returns(mock_environment)
vm.stubs(:execute!)
vm.stubs(:created?).returns(false) vm.stubs(:created?).returns(false)
vm vm
end end
@ -39,28 +69,10 @@ class CommandsUpTest < Test::Unit::TestCase
@vms.each do |name, vm| @vms.each do |name, vm|
vm.env.expects(:require_box).once vm.env.expects(:require_box).once
@instance.expects(:up_single).with(name).once
end end
@instance.execute @instance.up_all
end
should "start created VMs" do
vm = create_vm
vm.stubs(:created?).returns(true)
@vms[:foo] = vm
vm.expects(:start).once
@instance.execute
end
should "up non-created VMs" do
vm = create_vm
vm.expects(:execute!).with(Vagrant::Actions::VM::Up).once
vm.expects(:start).never
@vms[:foo] = vm
@instance.execute
end end
end end
end end

View File

@ -161,6 +161,13 @@ class VMTest < Test::Unit::TestCase
end end
end end
context "upping" do
should "execute the up action" do
@vm.expects(:execute!).with(Vagrant::Actions::VM::Up).once
@vm.up
end
end
context "halting" do context "halting" do
should "execute the halt action" do should "execute the halt action" do
@vm.expects(:execute!).with(Vagrant::Actions::VM::Halt, false).once @vm.expects(:execute!).with(Vagrant::Actions::VM::Halt, false).once