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
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 # Create the machine and cache it for future calls. This will also
# return the machine from this method. # 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 end
# This returns a list of the configured machines for this environment. # This returns a list of the configured machines for this environment.

View File

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

View File

@ -23,7 +23,6 @@ module VagrantPlugins
attr_reader :networks attr_reader :networks
attr_reader :providers attr_reader :providers
attr_reader :provisioners attr_reader :provisioners
attr_reader :customizations
def initialize def initialize
@forwarded_ports = [] @forwarded_ports = []
@ -31,7 +30,6 @@ module VagrantPlugins
@networks = [] @networks = []
@providers = {} @providers = {}
@provisioners = [] @provisioners = []
@customizations = []
end end
# Custom merge method since some keys here are merged differently. # 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(:@shared_folders, @shared_folders.merge(other.shared_folders))
result.instance_variable_set(:@networks, @networks + other.networks) result.instance_variable_set(:@networks, @networks + other.networks)
result.instance_variable_set(:@provisioners, @provisioners + other.provisioners) result.instance_variable_set(:@provisioners, @provisioners + other.provisioners)
result.instance_variable_set(:@customizations, @customizations + other.customizations)
result result
end end
@ -85,13 +82,6 @@ module VagrantPlugins
@provisioners << VagrantConfigProvisioner.new(name, options, &block) @provisioners << VagrantConfigProvisioner.new(name, options, &block)
end 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 def defined_vms
@defined_vms ||= {} @defined_vms ||= {}
end end

View File

@ -7,7 +7,7 @@ module VagrantPlugins
end end
def call(env) def call(env)
customizations = env[:machine].config.vm.customizations customizations = env[:machine].provider_config.customizations
if !customizations.empty? if !customizations.empty?
env[:ui].info I18n.t("vagrant.actions.vm.customize.running") 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) result = env[:machine].provider.driver.execute_command(processed_command)
if result.exit_code != 0 if result.exit_code != 0
raise Errors::VMCustomizationFailed, { raise Vagrant::Errors::VMCustomizationFailed, {
:command => processed_command.inspect, :command => processed_command.inspect,
:error => result.stderr :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. VirtualBox-based virtual machines.
EOF EOF
provider("virtualbox") do provider(:virtualbox) do
require File.expand_path("../provider", __FILE__) require File.expand_path("../provider", __FILE__)
Provider Provider
end end
config(:virtualbox, :provider => :virtualbox) do
require File.expand_path("../config", __FILE__)
Config
end
end end
autoload :Action, File.expand_path("../action", __FILE__) autoload :Action, File.expand_path("../action", __FILE__)

View File

@ -177,11 +177,15 @@ VF
describe "getting a machine" do describe "getting a machine" do
# A helper to register a provider for use in tests. # 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)) provider_cls = Class.new(Vagrant.plugin("2", :provider))
register_plugin("2") do |p| register_plugin("2") do |p|
p.provider(name) { provider_cls } p.provider(name) { provider_cls }
if config_class
p.config(name, :provider => name) { config_class }
end
end end
provider_cls provider_cls
@ -209,6 +213,40 @@ VF
machine.should be_kind_of(Vagrant::Machine) machine.should be_kind_of(Vagrant::Machine)
machine.name.should == :foo machine.name.should == :foo
machine.provider.should be_kind_of(foo_provider) 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 end
it "should cache the machine objects by name and provider" do 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.stub(:new => provider)
obj obj
end end
let(:provider_config) { Object.new }
let(:box) { Object.new } let(:box) { Object.new }
let(:config) { env.config_global } let(:config) { env.config_global }
let(:env) do let(:env) do
@ -27,7 +28,7 @@ describe Vagrant::Machine do
# Returns a new instance with the test data # Returns a new instance with the test data
def new_instance def new_instance
described_class.new(name, provider_cls, config, box, env) described_class.new(name, provider_cls, provider_config, config, box, env)
end end
describe "initialization" do describe "initialization" do
@ -56,7 +57,7 @@ describe Vagrant::Machine do
# Initialize a new machine and verify that we properly receive # Initialize a new machine and verify that we properly receive
# the machine we expect. # 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) received_machine.should eql(instance)
end end