From 455a44cc5d556eed2e96252ef2364171eb0d52c0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 26 May 2010 20:58:27 -0700 Subject: [PATCH] Add `--no-provision` flag to `vagrant up`. Refactor the way action options work a bit. [closes GH-87] --- lib/vagrant/actions/base.rb | 6 +++- lib/vagrant/actions/vm/halt.rb | 9 +----- lib/vagrant/actions/vm/package.rb | 16 +++++++---- lib/vagrant/actions/vm/start.rb | 4 +-- lib/vagrant/actions/vm/up.rb | 2 +- lib/vagrant/commands/halt.rb | 2 +- lib/vagrant/commands/package.rb | 2 +- lib/vagrant/commands/up.rb | 9 +++++- lib/vagrant/vm.rb | 14 ++++----- test/vagrant/actions/vm/package_test.rb | 38 ++++++++++++++++++++++--- test/vagrant/actions/vm/start_test.rb | 14 ++------- test/vagrant/actions/vm/up_test.rb | 2 +- test/vagrant/commands/halt_test.rb | 4 +-- test/vagrant/commands/package_test.rb | 2 +- test/vagrant/vm_test.rb | 17 ++++++----- vagrant.gemspec | 2 +- 16 files changed, 86 insertions(+), 57 deletions(-) diff --git a/lib/vagrant/actions/base.rb b/lib/vagrant/actions/base.rb index 5d35f43d3..65ad265a4 100644 --- a/lib/vagrant/actions/base.rb +++ b/lib/vagrant/actions/base.rb @@ -26,6 +26,9 @@ module Vagrant # The {Runner runner} which is executing the action attr_reader :runner + # Any options which are passed into the initializer as a hash. + attr_reader :options + # Included so subclasses don't need to include it themselves. include Vagrant::Util @@ -40,8 +43,9 @@ module Vagrant # Initialization of the action, passing any arguments which may have # been given to the {Runner runner}. This method can be used by subclasses # to save any of the configuration options which are passed in. - def initialize(runner, *args) + def initialize(runner, options=nil) @runner = runner + @options = options || {} end # This method is called once per action, allowing the action diff --git a/lib/vagrant/actions/vm/halt.rb b/lib/vagrant/actions/vm/halt.rb index bf8a46ef2..4a9a026cd 100644 --- a/lib/vagrant/actions/vm/halt.rb +++ b/lib/vagrant/actions/vm/halt.rb @@ -2,18 +2,11 @@ module Vagrant module Actions module VM class Halt < Base - attr_reader :force - - def initialize(vm, force=nil) - super - @force = force - end - def execute! raise ActionException.new(:vm_not_running) unless @runner.vm.running? @runner.invoke_around_callback(:halt) do - @runner.system.halt if !force + @runner.system.halt if !options[:force] if @runner.vm.state(true) != :powered_off logger.info "Forcing shutdown of VM..." diff --git a/lib/vagrant/actions/vm/package.rb b/lib/vagrant/actions/vm/package.rb index 894ae352d..b5d6ea9a9 100644 --- a/lib/vagrant/actions/vm/package.rb +++ b/lib/vagrant/actions/vm/package.rb @@ -2,20 +2,16 @@ module Vagrant module Actions module VM class Package < Base - attr_accessor :out_path - attr_accessor :include_files attr_reader :export_action - def initialize(vm, out_path = nil, include_files = nil, *args) + def initialize(*args) super - @out_path = out_path || "package" - @include_files = include_files || [] @temp_path = nil end def prepare # Verify the existance of all the additional files, if any - @include_files.each do |file| + include_files.each do |file| raise ActionException.new(:package_include_file_doesnt_exist, :filename => file) unless File.exists?(file) end @@ -28,6 +24,14 @@ module Vagrant compress end + def out_path + options[:output] || "package" + end + + def include_files + options[:include] || [] + end + def tar_path File.join(FileUtils.pwd, "#{out_path}#{@runner.env.config.package.extension}") end diff --git a/lib/vagrant/actions/vm/start.rb b/lib/vagrant/actions/vm/start.rb index 42011c968..f084d7215 100644 --- a/lib/vagrant/actions/vm/start.rb +++ b/lib/vagrant/actions/vm/start.rb @@ -8,11 +8,11 @@ module Vagrant steps = [Boot] if !@runner.vm || !@runner.vm.saved? steps.unshift([Customize, ForwardPorts, SharedFolders]) - steps << Provision if !@runner.env.config.vm.provisioner.nil? && !@runner.created? + steps << Provision if !@runner.env.config.vm.provisioner.nil? && options[:provision] end steps.flatten.each do |action_klass| - @runner.add_action(action_klass) + @runner.add_action(action_klass, options) end end end diff --git a/lib/vagrant/actions/vm/up.rb b/lib/vagrant/actions/vm/up.rb index 7922e4275..c117afc78 100644 --- a/lib/vagrant/actions/vm/up.rb +++ b/lib/vagrant/actions/vm/up.rb @@ -14,7 +14,7 @@ module Vagrant steps.insert(0, MoveHardDrive) if @runner.env.config.vm.hd_location steps.each do |action_klass| - @runner.add_action(action_klass) + @runner.add_action(action_klass, options) end end diff --git a/lib/vagrant/commands/halt.rb b/lib/vagrant/commands/halt.rb index a5f6c2d25..a61545e21 100644 --- a/lib/vagrant/commands/halt.rb +++ b/lib/vagrant/commands/halt.rb @@ -22,7 +22,7 @@ module Vagrant end if vm.created? - vm.halt(options[:force]) + vm.halt(options) else vm.env.logger.info "VM '#{name}' not created. Ignoring." end diff --git a/lib/vagrant/commands/package.rb b/lib/vagrant/commands/package.rb index e9ef16fc6..f934840f0 100644 --- a/lib/vagrant/commands/package.rb +++ b/lib/vagrant/commands/package.rb @@ -58,7 +58,7 @@ module Vagrant return # for tests end - vm.package(options[:output], options[:include]) + vm.package(options) end def options_spec(opts) diff --git a/lib/vagrant/commands/up.rb b/lib/vagrant/commands/up.rb index 3d7057e1e..2908adb4b 100644 --- a/lib/vagrant/commands/up.rb +++ b/lib/vagrant/commands/up.rb @@ -31,7 +31,14 @@ module Vagrant end def options_spec(opts) - opts.banner = "Usage: vagrant up" + opts.banner = "Usage: vagrant up [--no-provision]" + + # Defaults + options[:provision] = true + + opts.on("--no-provision", "Do not provision during this up.") do |v| + options[:provision] = false + end end end end diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index 01b0cee6e..4ab57930b 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -93,14 +93,14 @@ module Vagrant @vm = VirtualBox::VM.find(@vm.uuid) end - def package(out_path, include_files=[]) - add_action(Actions::VM::Export) - add_action(Actions::VM::Package, out_path, include_files) + def package(options=nil) + add_action(Actions::VM::Export, options) + add_action(Actions::VM::Package, options) execute! end - def up - execute!(Actions::VM::Up) + def up(options=nil) + execute!(Actions::VM::Up, options) end def start @@ -109,8 +109,8 @@ module Vagrant execute!(Actions::VM::Start) end - def halt(force=false) - execute!(Actions::VM::Halt, force) + def halt(options=nil) + execute!(Actions::VM::Halt, options) end def reload diff --git a/test/vagrant/actions/vm/package_test.rb b/test/vagrant/actions/vm/package_test.rb index 5be9faf4f..35fd64529 100644 --- a/test/vagrant/actions/vm/package_test.rb +++ b/test/vagrant/actions/vm/package_test.rb @@ -2,12 +2,15 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') class PackageActionTest < Test::Unit::TestCase setup do - @runner, @vm, @action = mock_action(Vagrant::Actions::VM::Package, "bing", []) + @runner, @vm, @action = mock_action(Vagrant::Actions::VM::Package) end context "initialization" do - def get_action(*args) - runner, vm, action = mock_action(Vagrant::Actions::VM::Package, *args) + def get_action(output, include_files) + runner, vm, action = mock_action(Vagrant::Actions::VM::Package, { + :output => output, + :include => include_files + }) return action end @@ -35,6 +38,31 @@ class PackageActionTest < Test::Unit::TestCase end end + context "out path" do + should "be the specified output file if given" do + result = mock("result") + @action.options[:output] = result + assert_equal result, @action.out_path + end + + should "default to 'package'" do + @action.options[:output] = nil + assert_equal "package", @action.out_path + end + end + + context "include files" do + should "specified array if given" do + @action.options[:include] = [1,2,3] + assert_equal @action.options[:include], @action.include_files + end + + should "be an empty array by default" do + @action.options[:include] = nil + assert_equal [], @action.include_files + end + end + context "tar path" do should "be the temporary directory with the name and extension attached" do pwd = "foo" @@ -185,7 +213,9 @@ class PackageActionTest < Test::Unit::TestCase context "checking include files" do setup do @include_files = ['fooiest', 'booiest'] - @runner, @vm, @action = mock_action(Vagrant::Actions::VM::Package, "bing", @include_files) + @runner, @vm, @action = mock_action(Vagrant::Actions::VM::Package, { + :include => @include_files + }) @runner.stubs(:find_action).returns("foo") end diff --git a/test/vagrant/actions/vm/start_test.rb b/test/vagrant/actions/vm/start_test.rb index eeff87950..dbbcb39e2 100644 --- a/test/vagrant/actions/vm/start_test.rb +++ b/test/vagrant/actions/vm/start_test.rb @@ -3,6 +3,8 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') class StartActionTest < Test::Unit::TestCase setup do @runner, @vm, @action = mock_action(Vagrant::Actions::VM::Start) + + @action.options[:provision] = true end context "sub-actions" do @@ -17,7 +19,7 @@ class StartActionTest < Test::Unit::TestCase def setup_action_expectations default_seq = sequence("default_seq") @default_order.flatten.each do |action| - @runner.expects(:add_action).with(action).once.in_sequence(default_seq) + @runner.expects(:add_action).with(action, @action.options).once.in_sequence(default_seq) end end @@ -49,15 +51,5 @@ class StartActionTest < Test::Unit::TestCase setup_action_expectations @action.prepare end - - should "not add provisioning if enabled but already created" do - @runner.stubs(:created?).returns(true) - @vm.env.config.vm.provisioner = :chef_solo - - @runner.stubs(:vm).returns(nil) - @default_order.unshift([Vagrant::Actions::VM::Customize, Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders]) - setup_action_expectations - @action.prepare - end end end diff --git a/test/vagrant/actions/vm/up_test.rb b/test/vagrant/actions/vm/up_test.rb index 115d90521..10056b2aa 100644 --- a/test/vagrant/actions/vm/up_test.rb +++ b/test/vagrant/actions/vm/up_test.rb @@ -20,7 +20,7 @@ class UpActionTest < Test::Unit::TestCase def setup_action_expectations default_seq = sequence("default_seq") @default_order.each do |action| - @runner.expects(:add_action).with(action).once.in_sequence(default_seq) + @runner.expects(:add_action).with(action, @action.options).once.in_sequence(default_seq) end end diff --git a/test/vagrant/commands/halt_test.rb b/test/vagrant/commands/halt_test.rb index 2e1ccf44b..46f8db7dd 100644 --- a/test/vagrant/commands/halt_test.rb +++ b/test/vagrant/commands/halt_test.rb @@ -31,13 +31,13 @@ class CommandsHaltTest < Test::Unit::TestCase should "halt if its created" do @foo_vm.stubs(:created?).returns(true) - @foo_vm.expects(:halt).with(false).once + @foo_vm.expects(:halt).with(:force => false).once @instance.execute(["foo"]) end should "halt and force if specified" do @foo_vm.stubs(:created?).returns(true) - @foo_vm.expects(:halt).with(true).once + @foo_vm.expects(:halt).with(:force => true).once @instance.execute(["foo", "--force"]) end diff --git a/test/vagrant/commands/package_test.rb b/test/vagrant/commands/package_test.rb index ebe0e0873..f10f98c76 100644 --- a/test/vagrant/commands/package_test.rb +++ b/test/vagrant/commands/package_test.rb @@ -97,7 +97,7 @@ class CommandsPackageTest < Test::Unit::TestCase @options[:output] = "foo.box" @options[:include] = :bar - @vm.expects(:package).with(@options[:output], @options[:include]).once + @vm.expects(:package).with(@options).once @instance.package_vm(@vm) end end diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index 339236d7c..5afcb9f8f 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -153,31 +153,30 @@ class VMTest < Test::Unit::TestCase context "packaging" do should "queue up the actions and execute" do - out_path = mock("out_path") - action_seq = sequence("actions") - @vm.expects(:add_action).with(Vagrant::Actions::VM::Export).once.in_sequence(action_seq) - @vm.expects(:add_action).with(Vagrant::Actions::VM::Package, out_path, []).once.in_sequence(action_seq) + action_seq = sequence("action_seq") + @vm.expects(:add_action).with(Vagrant::Actions::VM::Export, nil).once.in_sequence(action_seq) + @vm.expects(:add_action).with(Vagrant::Actions::VM::Package, nil).once.in_sequence(action_seq) @vm.expects(:execute!).in_sequence(action_seq) - @vm.package(out_path) + @vm.package end end context "upping" do should "execute the up action" do - @vm.expects(:execute!).with(Vagrant::Actions::VM::Up).once + @vm.expects(:execute!).with(Vagrant::Actions::VM::Up, nil).once @vm.up end end context "halting" 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, nil).once @vm.halt end should "force if specified" do - @vm.expects(:execute!).with(Vagrant::Actions::VM::Halt, true).once - @vm.halt(true) + @vm.expects(:execute!).with(Vagrant::Actions::VM::Halt, {:foo => :bar}).once + @vm.halt({:foo => :bar}) end end diff --git a/vagrant.gemspec b/vagrant.gemspec index 1f561b9c8..057256235 100644 --- a/vagrant.gemspec +++ b/vagrant.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version= s.authors = ["Mitchell Hashimoto", "John Bender"] - s.date = %q{2010-05-25} + s.date = %q{2010-05-26} s.default_executable = %q{vagrant} s.description = %q{Vagrant is a tool for building and distributing virtualized development environments.} s.email = ["mitchell.hashimoto@gmail.com", "john.m.bender@gmail.com"]