From 6478139cee01a63c1e09dcfa619ed05284523a94 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 22 Dec 2012 22:47:06 -0800 Subject: [PATCH] Move config.vm.customize to VirtualBox specific option --- lib/vagrant/environment.rb | 8 +++- lib/vagrant/machine.rb | 12 +++++- plugins/kernel_v2/config/vm.rb | 10 ----- .../providers/virtualbox/action/customize.rb | 4 +- plugins/providers/virtualbox/config.rb | 27 +++++++++++++ plugins/providers/virtualbox/plugin.rb | 7 +++- test/unit/vagrant/environment_test.rb | 40 ++++++++++++++++++- test/unit/vagrant/machine_test.rb | 5 ++- 8 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 plugins/providers/virtualbox/config.rb diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index b3494b573..4bd11f672 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -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. diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index 5f3d66098..e66e8e684 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -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 diff --git a/plugins/kernel_v2/config/vm.rb b/plugins/kernel_v2/config/vm.rb index 9e3c1379f..d39baf643 100644 --- a/plugins/kernel_v2/config/vm.rb +++ b/plugins/kernel_v2/config/vm.rb @@ -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 diff --git a/plugins/providers/virtualbox/action/customize.rb b/plugins/providers/virtualbox/action/customize.rb index ea699ac33..eb39ab962 100644 --- a/plugins/providers/virtualbox/action/customize.rb +++ b/plugins/providers/virtualbox/action/customize.rb @@ -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 } diff --git a/plugins/providers/virtualbox/config.rb b/plugins/providers/virtualbox/config.rb new file mode 100644 index 000000000..bc7f3a5bc --- /dev/null +++ b/plugins/providers/virtualbox/config.rb @@ -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 diff --git a/plugins/providers/virtualbox/plugin.rb b/plugins/providers/virtualbox/plugin.rb index 77341f61b..3c876c340 100644 --- a/plugins/providers/virtualbox/plugin.rb +++ b/plugins/providers/virtualbox/plugin.rb @@ -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__) diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index cddfd9858..6ab1e29be 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -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 diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index b4cefa2a8..7c98acfc7 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -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