From dcbfe709f3e5a2f97cddb06ef3299dd424de0b57 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 15 Mar 2010 15:41:53 -0700 Subject: [PATCH] VM customization through Vagrantfile (modifying RAM, name, etc.) --- lib/vagrant/actions/vm/customize.rb | 15 +++++++++ lib/vagrant/actions/vm/reload.rb | 2 +- lib/vagrant/actions/vm/up.rb | 2 +- lib/vagrant/config.rb | 6 ++++ test/vagrant/actions/vm/customize_test.rb | 16 ++++++++++ test/vagrant/actions/vm/reload_test.rb | 2 +- test/vagrant/actions/vm/up_test.rb | 2 +- test/vagrant/config_test.rb | 38 +++++++++++++++-------- 8 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 lib/vagrant/actions/vm/customize.rb create mode 100644 test/vagrant/actions/vm/customize_test.rb diff --git a/lib/vagrant/actions/vm/customize.rb b/lib/vagrant/actions/vm/customize.rb new file mode 100644 index 000000000..076038c51 --- /dev/null +++ b/lib/vagrant/actions/vm/customize.rb @@ -0,0 +1,15 @@ +module Vagrant + module Actions + module VM + class Customize < Base + def execute! + # Run the customization procs over the VM + Vagrant.config.vm.run_procs!(@runner.vm) + + # Save the vm + @runner.vm.save(true) + end + end + end + end +end diff --git a/lib/vagrant/actions/vm/reload.rb b/lib/vagrant/actions/vm/reload.rb index 940cf183b..1c3bb08e2 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 = [ForwardPorts, SharedFolders, Boot] + steps = [Customize, ForwardPorts, SharedFolders, Boot] steps.unshift(Halt) if @runner.vm.running? steps << Provision if !Vagrant.config.vm.provisioner.nil? diff --git a/lib/vagrant/actions/vm/up.rb b/lib/vagrant/actions/vm/up.rb index a6532a75e..05d861b17 100644 --- a/lib/vagrant/actions/vm/up.rb +++ b/lib/vagrant/actions/vm/up.rb @@ -20,7 +20,7 @@ msg # Up is a "meta-action" so it really just queues up a bunch # of other actions in its place: - steps = [Import, ForwardPorts, SharedFolders, Boot] + steps = [Import, Customize, ForwardPorts, SharedFolders, Boot] steps << Provision if !Vagrant.config.vm.provisioner.nil? steps.insert(0, MoveHardDrive) if Vagrant.config.vm.hd_location diff --git a/lib/vagrant/config.rb b/lib/vagrant/config.rb index 983a9196c..f0ddbc884 100644 --- a/lib/vagrant/config.rb +++ b/lib/vagrant/config.rb @@ -66,6 +66,8 @@ module Vagrant end class VMConfig < Base + include StackedProcRunner + attr_accessor :box attr_accessor :box_ovf attr_accessor :base_mac @@ -111,6 +113,10 @@ module Vagrant def shared_folder_gid @shared_folder_gid || Vagrant.config.ssh.username end + + def customize(&block) + push_proc(&block) + end end class PackageConfig < Base diff --git a/test/vagrant/actions/vm/customize_test.rb b/test/vagrant/actions/vm/customize_test.rb new file mode 100644 index 000000000..986593719 --- /dev/null +++ b/test/vagrant/actions/vm/customize_test.rb @@ -0,0 +1,16 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') + +class CustomizeActionTest < Test::Unit::TestCase + setup do + @runner, @vm, @action = mock_action(Vagrant::Actions::VM::Customize) + mock_config + end + + context "executing" do + should "run the VM customization procs then save the VM" do + Vagrant.config.vm.expects(:run_procs!).with(@vm) + @vm.expects(:save).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 4bd912d71..20a039e3f 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::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Boot] + @default_order = [Vagrant::Actions::VM::Customize, Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Boot] @vm.stubs(:running?).returns(false) end diff --git a/test/vagrant/actions/vm/up_test.rb b/test/vagrant/actions/vm/up_test.rb index eab488ccf..9fa7a42b4 100644 --- a/test/vagrant/actions/vm/up_test.rb +++ b/test/vagrant/actions/vm/up_test.rb @@ -10,7 +10,7 @@ class UpActionTest < Test::Unit::TestCase setup do File.stubs(:file?).returns(true) File.stubs(:exist?).returns(true) - @default_order = [Vagrant::Actions::VM::Import, Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Boot] + @default_order = [Vagrant::Actions::VM::Import, Vagrant::Actions::VM::Customize, Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Boot] end def setup_action_expectations diff --git a/test/vagrant/config_test.rb b/test/vagrant/config_test.rb index c93ee3e44..bf5b519bc 100644 --- a/test/vagrant/config_test.rb +++ b/test/vagrant/config_test.rb @@ -204,24 +204,36 @@ class ConfigTest < Test::Unit::TestCase end end - should "return the shared folder UID if set" do - @config.shared_folder_uid = "foo" - assert_equal "foo", @config.shared_folder_uid + should "include the stacked proc runner module" do + assert @config.class.included_modules.include?(Vagrant::StackedProcRunner) end - should "return the SSH username if UID not set" do - @config.shared_folder_uid = nil - assert_equal @username, @config.shared_folder_uid + should "add the customize proc to the proc stack" do + proc = Proc.new {} + @config.customize(&proc) + assert_equal [proc], @config.proc_stack end - should "return the shared folder GID if set" do - @config.shared_folder_gid = "foo" - assert_equal "foo", @config.shared_folder_gid - end + context "uid/gid" do + should "return the shared folder UID if set" do + @config.shared_folder_uid = "foo" + assert_equal "foo", @config.shared_folder_uid + end - should "return the SSH username if GID not set" do - @config.shared_folder_gid = nil - assert_equal @username, @config.shared_folder_gid + should "return the SSH username if UID not set" do + @config.shared_folder_uid = nil + assert_equal @username, @config.shared_folder_uid + end + + should "return the shared folder GID if set" do + @config.shared_folder_gid = "foo" + assert_equal "foo", @config.shared_folder_gid + end + + should "return the SSH username if GID not set" do + @config.shared_folder_gid = nil + assert_equal @username, @config.shared_folder_gid + end end end end