From c7f040f14ca4f78e8d7c36775e8bd9a13f674ea7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 2 Mar 2010 22:07:04 -0800 Subject: [PATCH] vagrant-up now starts the VM if its already created, rather than giving an error. --- bin/vagrant-up | 4 +++- lib/vagrant/commands.rb | 14 +++++--------- lib/vagrant/vm.rb | 9 +++++++++ test/vagrant/commands_test.rb | 12 ++++++------ test/vagrant/vm_test.rb | 14 ++++++++++++++ 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/bin/vagrant-up b/bin/vagrant-up index 4b19bdbbb..e0fe3f37b 100755 --- a/bin/vagrant-up +++ b/bin/vagrant-up @@ -18,7 +18,9 @@ GitStyleBinary.command do banner <<-EOS Usage: #{command.full_name} #{all_options_string} -Create the vagrant environment. +Create the vagrant environment if it doesn't exist, +otherwise start the vagrant environment if its not +already running. EOS diff --git a/lib/vagrant/commands.rb b/lib/vagrant/commands.rb index d61511a59..4e1cd4c92 100644 --- a/lib/vagrant/commands.rb +++ b/lib/vagrant/commands.rb @@ -32,16 +32,12 @@ error Env.load! if Env.persisted_vm - error_and_exit(<<-error) -The task you're trying to run requires that the vagrant environment -not exist yet, but it appears you already have an instance running -or available. If you really want to rebuild this instance, please -run `vagrant down` first. -error + logger.info "VM already created. Starting VM if its not already running..." + Env.persisted_vm.start + else + Env.require_box + VM.execute!(Actions::VM::Up) end - - Env.require_box - VM.execute!(Actions::VM::Up) end # Tear down a vagrant instance. This not only shuts down the instance diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index b71c4ecf2..49b5403ae 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -25,6 +25,15 @@ module Vagrant execute! end + def start + actions = [Actions::VM::ForwardPorts, Actions::VM::SharedFolders, Actions::VM::Start] + actions.each do |action| + add_action(action) + end + + execute! + end + def destroy execute!(Actions::VM::Halt) if @vm.running? diff --git a/test/vagrant/commands_test.rb b/test/vagrant/commands_test.rb index 4c6f1c672..3e3dd5389 100644 --- a/test/vagrant/commands_test.rb +++ b/test/vagrant/commands_test.rb @@ -46,15 +46,15 @@ class CommandsTest < Test::Unit::TestCase Vagrant::Commands.up end - should "error if a persisted VM already exists" do - Vagrant::Env.expects(:persisted_vm).returns(true) - Vagrant::Commands.expects(:error_and_exit).once - Vagrant::VM.expects(:up).never + should "call the up action on VM if it doesn't exist" do + Vagrant::VM.expects(:execute!).with(Vagrant::Actions::VM::Up).once Vagrant::Commands.up end - should "call the up action on VM" do - Vagrant::VM.expects(:execute!).with(Vagrant::Actions::VM::Up).once + should "call start on the persisted vm if it exists" do + Vagrant::Env.stubs(:persisted_vm).returns(@persisted_vm) + @persisted_vm.expects(:start).once + Vagrant::VM.expects(:execute!).never Vagrant::Commands.up end end diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index 5cd0e57ff..d954076bb 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -78,5 +78,19 @@ class VMTest < Test::Unit::TestCase @vm.save_state end end + + context "starting" do + should "add and execute the proper actions" do + actions = [Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Start] + + action_seq = sequence("action_seq") + actions.each do |action| + @vm.expects(:add_action).with(action).in_sequence(action_seq) + end + + @vm.expects(:execute!).once.in_sequence(action_seq) + @vm.start + end + end end end