core: Modify plugin interfaces and API for hosts

This commit is contained in:
Mitchell Hashimoto 2014-01-07 18:38:49 -08:00
parent 04a5e6bcd2
commit 5922241fcb
5 changed files with 80 additions and 14 deletions

View File

@ -26,6 +26,16 @@ module Vagrant
# @return [Hash<Symbol, Registry>] # @return [Hash<Symbol, Registry>]
attr_reader :guest_capabilities attr_reader :guest_capabilities
# This contains all the hosts and their parents.
#
# @return [Registry<Symbol, Array<Class, Symbol>>]
attr_reader :hosts
# This contains all the registered host capabilities.
#
# @return [Hash<Symbol, Registry>]
attr_reader :host_capabilities
# This contains all the provider plugins by name, and returns # This contains all the provider plugins by name, and returns
# the provider class and options. # the provider class and options.
# #
@ -44,6 +54,8 @@ module Vagrant
@configs = Hash.new { |h, k| h[k] = Registry.new } @configs = Hash.new { |h, k| h[k] = Registry.new }
@guests = Registry.new @guests = Registry.new
@guest_capabilities = Hash.new { |h, k| h[k] = Registry.new } @guest_capabilities = Hash.new { |h, k| h[k] = Registry.new }
@hosts = Registry.new
@host_capabilities = Hash.new { |h, k| h[k] = Registry.new }
@providers = Registry.new @providers = Registry.new
@synced_folders = Registry.new @synced_folders = Registry.new
end end

View File

@ -88,18 +88,33 @@ module Vagrant
results results
end end
# This returns all the registered guests.
# This returns all registered host classes.
# #
# @return [Hash] # @return [Hash]
def hosts def hosts
Registry.new.tap do |result| Registry.new.tap do |result|
@registered.each do |plugin| @registered.each do |plugin|
result.merge!(plugin.host) result.merge!(plugin.components.hosts)
end end
end end
end end
# This returns all the registered host capabilities.
#
# @return [Hash]
def host_capabilities
results = Hash.new { |h, k| h[k] = Registry.new }
@registered.each do |plugin|
plugin.components.host_capabilities.each do |host, caps|
results[host].merge!(caps)
end
end
results
end
# This returns all registered providers. # This returns all registered providers.
# #
# @return [Hash] # @return [Hash]

View File

@ -135,7 +135,7 @@ module Vagrant
# #
# @param [String] name Name of the guest. # @param [String] name Name of the guest.
# @param [String] parent Name of the parent guest (if any) # @param [String] parent Name of the parent guest (if any)
def self.guest(name=UNSET_VALUE, parent=nil, &block) def self.guest(name, parent=nil, &block)
components.guests.register(name.to_sym) do components.guests.register(name.to_sym) do
parent = parent.to_sym if parent parent = parent.to_sym if parent
@ -160,14 +160,26 @@ module Vagrant
# the given key. # the given key.
# #
# @param [String] name Name of the host. # @param [String] name Name of the host.
def self.host(name=UNSET_VALUE, &block) # @param [String] parent Name of the parent host (if any)
data[:hosts] ||= Registry.new def self.host(name, parent=nil, &block)
components.hosts.register(name.to_sym) do
parent = parent.to_sym if parent
# Register a new host class only if a name was given [block.call, parent]
data[:hosts].register(name.to_sym, &block) if name != UNSET_VALUE end
nil
end
# Return the registry # Defines a capability for the given host. The block should return
data[:hosts] # a class/module that has a method with the capability name, ready
# to be executed. This means that if it is an instance method,
# the block should return an instance of the class.
#
# @param [String] host The name of the host
# @param [String] cap The name of the capability
def self.host_capability(host, cap, &block)
components.host_capabilities[host.to_sym].register(cap.to_sym, &block)
nil
end end
# Registers additional providers to be available. # Registers additional providers to be available.

View File

@ -126,15 +126,32 @@ describe Vagrant::Plugin::V2::Manager do
end end
pB = plugin do |p| pB = plugin do |p|
p.host("bar") { "baz" } p.host("bar", "foo") { "baz" }
end end
instance.register(pA) instance.register(pA)
instance.register(pB) instance.register(pB)
instance.hosts.to_hash.length.should == 2 instance.hosts.to_hash.length.should == 2
instance.hosts[:foo].should == "bar" instance.hosts[:foo].should == ["bar", nil]
instance.hosts[:bar].should == "baz" instance.hosts[:bar].should == ["baz", :foo]
end
it "should enumerate registered host capabilities" do
pA = plugin do |p|
p.host_capability("foo", "foo") { "bar" }
end
pB = plugin do |p|
p.host_capability("bar", "foo") { "baz" }
end
instance.register(pA)
instance.register(pB)
instance.host_capabilities.length.should == 2
instance.host_capabilities[:foo][:foo].should == "bar"
instance.host_capabilities[:bar][:foo].should == "baz"
end end
it "should enumerate registered provider classes" do it "should enumerate registered provider classes" do

View File

@ -192,7 +192,7 @@ describe Vagrant::Plugin::V2::Plugin do
host("foo") { "bar" } host("foo") { "bar" }
end end
plugin.host[:foo].should == "bar" plugin.components.hosts[:foo].should == ["bar", nil]
end end
it "should lazily register host classes" do it "should lazily register host classes" do
@ -214,6 +214,16 @@ describe Vagrant::Plugin::V2::Plugin do
end end
end end
describe "host capabilities" do
it "should register host capabilities" do
plugin = Class.new(described_class) do
host_capability("foo", "bar") { "baz" }
end
plugin.components.host_capabilities[:foo][:bar].should == "baz"
end
end
describe "providers" do describe "providers" do
it "should register provider classes" do it "should register provider classes" do
plugin = Class.new(described_class) do plugin = Class.new(described_class) do