From 2e50a238fc1fa0cbc95fa33f2311b03970e361c8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 21 Mar 2013 17:57:31 -0700 Subject: [PATCH] Provider plugins can provide arbitrary options --- lib/vagrant/environment.rb | 10 +++++-- lib/vagrant/machine.rb | 10 ++++++- lib/vagrant/plugin/v2/components.rb | 8 ++++- lib/vagrant/plugin/v2/manager.rb | 2 +- lib/vagrant/plugin/v2/plugin.rb | 12 ++++---- test/unit/vagrant/machine_test.rb | 33 ++++++++------------- test/unit/vagrant/plugin/v2/manager_test.rb | 6 ++-- test/unit/vagrant/plugin/v2/plugin_test.rb | 12 ++++++-- 8 files changed, 54 insertions(+), 39 deletions(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 5f86306f3..ffffed50d 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -287,11 +287,15 @@ module Vagrant raise Errors::MachineNotFound, :name => name, :provider => provider end - provider_cls = Vagrant.plugin("2").manager.providers[provider] - if !provider_cls + provider_plugin = Vagrant.plugin("2").manager.providers[provider] + if !provider_plugin raise Errors::ProviderNotFound, :machine => name, :provider => provider end + # Extra the provider class and options from the plugin data + provider_cls = provider_plugin[0] + provider_options = provider_plugin[1] + # Build the machine configuration. This requires two passes: The first pass # loads in the machine sub-configuration. Since this can potentially # define a new box to base the machine from, we then make a second pass @@ -356,7 +360,7 @@ module Vagrant # 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, provider_cls, provider_config, - config, machine_data_path, box, self) + provider_options, config, machine_data_path, 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 401a695dd..51b3dfb2f 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -52,6 +52,11 @@ module Vagrant # @return [Symbol] attr_reader :provider_name + # The options given to the provider when registering the plugin. + # + # @return [Hash] + attr_reader :provider_options + # Initialize a new machine. # # @param [String] name Name of the virtual machine. @@ -59,13 +64,15 @@ module Vagrant # currently expected to be a V1 `provider` plugin. # @param [Object] provider_config The provider-specific configuration for # this machine. + # @param [Hash] provider_options The provider-specific options from the + # plugin definition. # @param [Object] config The configuration for this machine. # @param [Pathname] data_dir The directory where machine-specific data # can be stored. This directory is ensured to exist. # @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_name, provider_cls, provider_config, config, data_dir, box, env, base=false) + def initialize(name, provider_name, provider_cls, provider_config, provider_options, config, data_dir, box, env, base=false) @logger = Log4r::Logger.new("vagrant::machine") @logger.info("Initializing machine: #{name}") @logger.info(" - Provider: #{provider_cls}") @@ -79,6 +86,7 @@ module Vagrant @name = name @provider_config = provider_config @provider_name = provider_name + @provider_options = provider_options # Read the ID, which is usually in local storage @id = nil diff --git a/lib/vagrant/plugin/v2/components.rb b/lib/vagrant/plugin/v2/components.rb index f7035cb07..9510b0a87 100644 --- a/lib/vagrant/plugin/v2/components.rb +++ b/lib/vagrant/plugin/v2/components.rb @@ -16,12 +16,18 @@ module Vagrant # @return [Hash] attr_reader :configs + # This contains all the provider plugins by name, and returns + # the provider class and options. + # + # @return [Hash] + attr_reader :providers + def initialize # The action hooks hash defaults to [] @action_hooks = Hash.new { |h, k| h[k] = [] } - # Create the configs hash which defaults to a registry @configs = Hash.new { |h, k| h[k] = Registry.new } + @providers = Registry.new end end end diff --git a/lib/vagrant/plugin/v2/manager.rb b/lib/vagrant/plugin/v2/manager.rb index 572cea429..f7cfa5c4e 100644 --- a/lib/vagrant/plugin/v2/manager.rb +++ b/lib/vagrant/plugin/v2/manager.rb @@ -89,7 +89,7 @@ module Vagrant def providers Registry.new.tap do |result| @registered.each do |plugin| - result.merge!(plugin.provider) + result.merge!(plugin.components.providers) end end end diff --git a/lib/vagrant/plugin/v2/plugin.rb b/lib/vagrant/plugin/v2/plugin.rb index 77ece03fd..be2d11802 100644 --- a/lib/vagrant/plugin/v2/plugin.rb +++ b/lib/vagrant/plugin/v2/plugin.rb @@ -162,14 +162,12 @@ module Vagrant # Registers additional providers to be available. # # @param [Symbol] name Name of the provider. - def self.provider(name=UNSET_VALUE, &block) - data[:providers] ||= Registry.new + def self.provider(name=UNSET_VALUE, options=nil, &block) + components.providers.register(name.to_sym) do + [block.call, options || {}] + end - # Register a new provider class only if a name was given - data[:providers].register(name.to_sym, &block) if name != UNSET_VALUE - - # Return the registry - data[:providers] + nil end # Registers additional provisioners to be available. diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index e0b72f58a..7a24be9ab 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -14,6 +14,7 @@ describe Vagrant::Machine do end let(:provider_config) { Object.new } let(:provider_name) { :test } + let(:provider_options) { {} } let(:box) { Object.new } let(:config) { env.config_global } let(:data_dir) { Pathname.new(Tempdir.new.path) } @@ -30,10 +31,12 @@ describe Vagrant::Machine do let(:instance) { new_instance } + subject { instance } + # Returns a new instance with the test data def new_instance described_class.new(name, provider_name, provider_cls, provider_config, - config, data_dir, box, env) + provider_options, config, data_dir, box, env) end describe "initialization" do @@ -63,7 +66,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_name, provider_cls, provider_config, - config, data_dir, box, env) + provider_options, config, data_dir, box, env) received_machine.should eql(instance) end @@ -117,25 +120,13 @@ describe Vagrant::Machine do end describe "attributes" do - it "should provide access to the name" do - instance.name.should == name - end - - it "should provide access to the configuration" do - instance.config.should eql(config) - end - - it "should provide access to the box" do - instance.box.should eql(box) - end - - it "should provide access to the environment" do - instance.env.should eql(env) - end - - it "should provide access to the provider" do - instance.provider.should eql(provider) - end + its(:name) { should eq(name) } + its(:config) { should eql(config) } + its(:box) { should eql(box) } + its(:env) { should eql(env) } + its(:provider) { should eql(provider) } + its(:provider_config) { should eql(provider_config) } + its(:provider_options) { should eq(provider_options) } end describe "actions" do diff --git a/test/unit/vagrant/plugin/v2/manager_test.rb b/test/unit/vagrant/plugin/v2/manager_test.rb index 6d31ad5c7..7ed9d751e 100644 --- a/test/unit/vagrant/plugin/v2/manager_test.rb +++ b/test/unit/vagrant/plugin/v2/manager_test.rb @@ -126,15 +126,15 @@ describe Vagrant::Plugin::V2::Manager do end pB = plugin do |p| - p.provider("bar") { "baz" } + p.provider("bar", foo: "bar") { "baz" } end instance.register(pA) instance.register(pB) instance.providers.to_hash.length.should == 2 - instance.providers[:foo].should == "bar" - instance.providers[:bar].should == "baz" + instance.providers[:foo].should == ["bar", {}] + instance.providers[:bar].should == ["baz", { foo: "bar" }] end it "provides the collection of registered provider configs" do diff --git a/test/unit/vagrant/plugin/v2/plugin_test.rb b/test/unit/vagrant/plugin/v2/plugin_test.rb index bb64a393a..7e0b88d84 100644 --- a/test/unit/vagrant/plugin/v2/plugin_test.rb +++ b/test/unit/vagrant/plugin/v2/plugin_test.rb @@ -210,7 +210,15 @@ describe Vagrant::Plugin::V2::Plugin do provider("foo") { "bar" } end - plugin.provider[:foo].should == "bar" + plugin.components.providers[:foo].should == ["bar", {}] + end + + it "should register provider classes with options" do + plugin = Class.new(described_class) do + provider("foo", foo: "yep") { "bar" } + end + + plugin.components.providers[:foo].should == ["bar", { foo: "yep" }] end it "should lazily register provider classes" do @@ -227,7 +235,7 @@ describe Vagrant::Plugin::V2::Plugin do # Now verify when we actually get the configuration key that # a proper error is raised. expect { - plugin.provider[:foo] + plugin.components.providers[:foo] }.to raise_error(StandardError) end end