VM customization through Vagrantfile (modifying RAM, name, etc.)

This commit is contained in:
Mitchell Hashimoto 2010-03-15 15:41:53 -07:00
parent 83ddfa6695
commit dcbfe709f3
8 changed files with 66 additions and 17 deletions

View File

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

View File

@ -3,7 +3,7 @@ module Vagrant
module VM module VM
class Reload < Base class Reload < Base
def prepare def prepare
steps = [ForwardPorts, SharedFolders, Boot] steps = [Customize, ForwardPorts, SharedFolders, Boot]
steps.unshift(Halt) if @runner.vm.running? steps.unshift(Halt) if @runner.vm.running?
steps << Provision if !Vagrant.config.vm.provisioner.nil? steps << Provision if !Vagrant.config.vm.provisioner.nil?

View File

@ -20,7 +20,7 @@ msg
# Up is a "meta-action" so it really just queues up a bunch # Up is a "meta-action" so it really just queues up a bunch
# of other actions in its place: # 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 << Provision if !Vagrant.config.vm.provisioner.nil?
steps.insert(0, MoveHardDrive) if Vagrant.config.vm.hd_location steps.insert(0, MoveHardDrive) if Vagrant.config.vm.hd_location

View File

@ -66,6 +66,8 @@ module Vagrant
end end
class VMConfig < Base class VMConfig < Base
include StackedProcRunner
attr_accessor :box attr_accessor :box
attr_accessor :box_ovf attr_accessor :box_ovf
attr_accessor :base_mac attr_accessor :base_mac
@ -111,6 +113,10 @@ module Vagrant
def shared_folder_gid def shared_folder_gid
@shared_folder_gid || Vagrant.config.ssh.username @shared_folder_gid || Vagrant.config.ssh.username
end end
def customize(&block)
push_proc(&block)
end
end end
class PackageConfig < Base class PackageConfig < Base

View File

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

View File

@ -8,7 +8,7 @@ class ReloadActionTest < Test::Unit::TestCase
context "sub-actions" do context "sub-actions" do
setup 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) @vm.stubs(:running?).returns(false)
end end

View File

@ -10,7 +10,7 @@ class UpActionTest < Test::Unit::TestCase
setup do setup do
File.stubs(:file?).returns(true) File.stubs(:file?).returns(true)
File.stubs(:exist?).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 end
def setup_action_expectations def setup_action_expectations

View File

@ -204,6 +204,17 @@ class ConfigTest < Test::Unit::TestCase
end end
end end
should "include the stacked proc runner module" do
assert @config.class.included_modules.include?(Vagrant::StackedProcRunner)
end
should "add the customize proc to the proc stack" do
proc = Proc.new {}
@config.customize(&proc)
assert_equal [proc], @config.proc_stack
end
context "uid/gid" do
should "return the shared folder UID if set" do should "return the shared folder UID if set" do
@config.shared_folder_uid = "foo" @config.shared_folder_uid = "foo"
assert_equal "foo", @config.shared_folder_uid assert_equal "foo", @config.shared_folder_uid
@ -224,4 +235,5 @@ class ConfigTest < Test::Unit::TestCase
assert_equal @username, @config.shared_folder_gid assert_equal @username, @config.shared_folder_gid
end end
end end
end
end end