core: Remove Vagrant::Hosts and add Vagrant::Host, a CapabilityHost

This commit is contained in:
Mitchell Hashimoto 2014-01-07 18:34:19 -08:00
parent a83498e32a
commit 04a5e6bcd2
6 changed files with 36 additions and 79 deletions

View File

@ -93,7 +93,7 @@ module Vagrant
autoload :Environment, 'vagrant/environment'
autoload :Errors, 'vagrant/errors'
autoload :Guest, 'vagrant/guest'
autoload :Hosts, 'vagrant/hosts'
autoload :Host, 'vagrant/host'
autoload :Machine, 'vagrant/machine'
autoload :MachineState, 'vagrant/machine_state'
autoload :Plugin, 'vagrant/plugin'

16
lib/vagrant/host.rb Normal file
View File

@ -0,0 +1,16 @@
require "vagrant/capability_host"
module Vagrant
# This class handles host-OS specific interations. It is responsible for
# detecting the proper host OS implementation and delegating capabilities
# to plugins.
#
# See {Guest} for more information on capabilities.
class Host
include CapabilityHost
def initialize(host, hosts, capabilities)
initialize_capabilities!(host, hosts, capabilities)
end
end
end

View File

@ -1,28 +0,0 @@
require 'log4r'
module Vagrant
module Hosts
# This method detects the correct host based on the `match?` methods
# implemented in the registered hosts.
#
# @param [Hash] registry Hash mapping key to host class
def self.detect(registry)
logger = Log4r::Logger.new("vagrant::hosts")
# Sort the hosts by their precedence
host_klasses = registry.values.sort_by { |a| a.precedence }.reverse
logger.debug("Host path search classes: #{host_klasses.inspect}")
# Test for matches and return the host class that matches
host_klasses.each do |klass|
if klass.match?
logger.info("Host class: #{klass}")
return klass
end
end
# No matches found...
return nil
end
end
end

View File

@ -10,19 +10,8 @@ module Vagrant
# matches the host class.
#
# @return [Boolean]
def self.match?
nil
end
# The precedence of the host when checking for matches. This is to
# allow certain host such as generic OS's ("Linux", "BSD", etc.)
# to be specified last.
#
# The hosts with the higher numbers will be checked first.
#
# If you're implementing a basic host, you can probably ignore this.
def self.precedence
5
def detect?
false
end
# Initializes a new host class.

View File

@ -0,0 +1,17 @@
require "pathname"
require File.expand_path("../../base", __FILE__)
describe Vagrant::Host do
include_context "capability_helpers"
let(:capabilities) { {} }
let(:hosts) { {} }
it "initializes the capabilities" do
described_class.any_instance.should_receive(:initialize_capabilities!).
with(:foo, hosts, capabilities)
described_class.new(:foo, hosts, capabilities)
end
end

View File

@ -1,37 +0,0 @@
require File.expand_path("../../base", __FILE__)
describe Vagrant::Hosts do
let(:registry) { Hash.new }
let(:base_class) { Vagrant::Plugin::V1::Host }
it "detects the host that matches true" do
foo_klass = Class.new(base_class) do
def self.match?; false; end
end
bar_klass = Class.new(base_class) do
def self.match?; true; end
end
registry[:foo] = foo_klass
registry[:bar] = bar_klass
described_class.detect(registry).should == bar_klass
end
it "detects the host that matches true with the highest precedence first" do
foo_klass = Class.new(base_class) do
def self.match?; true; end
end
bar_klass = Class.new(base_class) do
def self.match?; true; end
def self.precedence; 9; end
end
registry[:foo] = foo_klass
registry[:bar] = bar_klass
described_class.detect(registry).should == bar_klass
end
end