Move config.vm.customize to VirtualBox specific option

This commit is contained in:
Mitchell Hashimoto 2012-12-22 22:47:06 -08:00
parent 66849fda20
commit 6478139cee
8 changed files with 94 additions and 19 deletions

View File

@ -212,9 +212,15 @@ module Vagrant
end
end
# Get the provider configuration from the final loaded configuration
vm_provider = config.vm.providers[provider]
provider_config = nil
provider_config = vm_provider.config if vm_provider
# Create the machine and cache it for future calls. This will also
# return the machine from this method.
@machines[cache_key] = Machine.new(name, provider_cls, config, box, self)
@machines[cache_key] = Machine.new(name, provider_cls, provider_config,
config, box, self)
end
# This returns a list of the configured machines for this environment.

View File

@ -37,25 +37,33 @@ module Vagrant
# @return [Object]
attr_reader :provider
# The provider-specific configuration for this machine.
#
# @return [Object]
attr_reader :provider_config
# Initialize a new machine.
#
# @param [String] name Name of the virtual machine.
# @param [Class] provider The provider backing this machine. This is
# currently expected to be a V1 `provider` plugin.
# @param [Object] provider_config The provider-specific configuration for
# this machine.
# @param [Object] config The configuration for this machine.
# @param [Box] box The box that is backing this virtual machine.
# @param [Environment] env The environment that this machine is a
# part of.
def initialize(name, provider_cls, config, box, env, base=false)
def initialize(name, provider_cls, provider_config, config, box, env, base=false)
@logger = Log4r::Logger.new("vagrant::machine")
@logger.info("Initializing machine: #{name}")
@logger.info(" - Provider: #{provider_cls}")
@logger.info(" - Box: #{box}")
@name = name
@box = box
@config = config
@env = env
@name = name
@provider_config = provider_config
# Read the ID, which is usually in local storage
@id = nil

View File

@ -23,7 +23,6 @@ module VagrantPlugins
attr_reader :networks
attr_reader :providers
attr_reader :provisioners
attr_reader :customizations
def initialize
@forwarded_ports = []
@ -31,7 +30,6 @@ module VagrantPlugins
@networks = []
@providers = {}
@provisioners = []
@customizations = []
end
# Custom merge method since some keys here are merged differently.
@ -41,7 +39,6 @@ module VagrantPlugins
result.instance_variable_set(:@shared_folders, @shared_folders.merge(other.shared_folders))
result.instance_variable_set(:@networks, @networks + other.networks)
result.instance_variable_set(:@provisioners, @provisioners + other.provisioners)
result.instance_variable_set(:@customizations, @customizations + other.customizations)
result
end
@ -85,13 +82,6 @@ module VagrantPlugins
@provisioners << VagrantConfigProvisioner.new(name, options, &block)
end
# TODO: This argument should not be `nil` in the future.
# It is only defaulted to nil so that the deprecation error
# can be properly shown.
def customize(command=nil)
@customizations << command if command
end
def defined_vms
@defined_vms ||= {}
end

View File

@ -7,7 +7,7 @@ module VagrantPlugins
end
def call(env)
customizations = env[:machine].config.vm.customizations
customizations = env[:machine].provider_config.customizations
if !customizations.empty?
env[:ui].info I18n.t("vagrant.actions.vm.customize.running")
@ -20,7 +20,7 @@ module VagrantPlugins
result = env[:machine].provider.driver.execute_command(processed_command)
if result.exit_code != 0
raise Errors::VMCustomizationFailed, {
raise Vagrant::Errors::VMCustomizationFailed, {
:command => processed_command.inspect,
:error => result.stderr
}

View File

@ -0,0 +1,27 @@
module VagrantPlugins
module ProviderVirtualBox
class Config < Vagrant.plugin("2", :config)
attr_reader :customizations
def initialize
@customizations = []
end
# Customize the VM by calling `VBoxManage` with the given
# arguments.
#
# When called multiple times, the customizations will be applied
# in the order given.
#
# The special `:name` parameter in the command will be replaced with
# the unique ID or name of the virtual machine. This is useful for
# parameters to `modifyvm` and the like.
#
# @param [Array] command An array of arguments to pass to
# VBoxManage.
def customize(command)
@customizations << command
end
end
end
end

View File

@ -9,10 +9,15 @@ module VagrantPlugins
VirtualBox-based virtual machines.
EOF
provider("virtualbox") do
provider(:virtualbox) do
require File.expand_path("../provider", __FILE__)
Provider
end
config(:virtualbox, :provider => :virtualbox) do
require File.expand_path("../config", __FILE__)
Config
end
end
autoload :Action, File.expand_path("../action", __FILE__)

View File

@ -177,11 +177,15 @@ VF
describe "getting a machine" do
# A helper to register a provider for use in tests.
def register_provider(name)
def register_provider(name, config_class=nil)
provider_cls = Class.new(Vagrant.plugin("2", :provider))
register_plugin("2") do |p|
p.provider(name) { provider_cls }
if config_class
p.config(name, :provider => name) { config_class }
end
end
provider_cls
@ -209,6 +213,40 @@ VF
machine.should be_kind_of(Vagrant::Machine)
machine.name.should == :foo
machine.provider.should be_kind_of(foo_provider)
machine.provider_config.should be_nil
end
it "should return a machine object with the machine configuration" do
# Create a provider
foo_config = Class.new do
attr_accessor :value
end
foo_provider = register_provider("foo", foo_config)
# Create the configuration
isolated_env = isolated_environment do |e|
e.vagrantfile(<<-VF)
Vagrant.configure("2") do |config|
config.vm.box = "base"
config.vm.define "foo"
config.vm.provider :foo do |fooconfig|
fooconfig.value = 100
end
end
VF
e.box2("base", :foo)
end
# Verify that we can get the machine
env = isolated_env.create_vagrant_env
machine = env.machine(:foo, :foo)
machine.should be_kind_of(Vagrant::Machine)
machine.name.should == :foo
machine.provider.should be_kind_of(foo_provider)
machine.provider_config.value.should == 100
end
it "should cache the machine objects by name and provider" do

View File

@ -10,6 +10,7 @@ describe Vagrant::Machine do
obj.stub(:new => provider)
obj
end
let(:provider_config) { Object.new }
let(:box) { Object.new }
let(:config) { env.config_global }
let(:env) do
@ -27,7 +28,7 @@ describe Vagrant::Machine do
# Returns a new instance with the test data
def new_instance
described_class.new(name, provider_cls, config, box, env)
described_class.new(name, provider_cls, provider_config, config, box, env)
end
describe "initialization" do
@ -56,7 +57,7 @@ describe Vagrant::Machine do
# Initialize a new machine and verify that we properly receive
# the machine we expect.
instance = described_class.new(name, provider_cls, config, box, env)
instance = described_class.new(name, provider_cls, provider_config, config, box, env)
received_machine.should eql(instance)
end