Move host base class to a plugin component

This commit is contained in:
Mitchell Hashimoto 2012-06-27 09:26:03 -07:00
parent 214b29888a
commit 70bdd9f56e
11 changed files with 84 additions and 73 deletions

View File

@ -90,6 +90,7 @@ module Vagrant
c.register([:"1", :command]) { Plugin::V1::Command }
c.register([:"1", :config]) { Plugin::V1::Config }
c.register([:"1", :guest]) { Plugin::V1::Guest }
c.register([:"1", :host]) { Plugin::V1::Host }
c.register([:"1", :provisioner]) { Plugin::V1::Provisioner }
# Returns a `Vagrant::Registry` object that contains all the built-in

View File

@ -176,7 +176,7 @@ module Vagrant
# Returns the host object associated with this environment.
#
# @return [Hosts::Base]
# @return [Class]
def host
return @host if defined?(@host)
@ -194,10 +194,9 @@ module Vagrant
# Get the flattened list of available hosts
host_klass = Hosts.detect(hosts)
end
host_klass = Vagrant.hosts.get(host_klass) if host_klass.is_a?(Symbol)
# If no host class is detected, we use the base class.
host_klass ||= Hosts::Base
host_klass ||= Vagrant.plugin("1", :host)
@host ||= host_klass.new(@ui)
end

View File

@ -24,68 +24,5 @@ module Vagrant
# No matches found...
return nil
end
# Interface for classes which house behavior that is specific
# to the host OS that is running Vagrant.
#
# By default, Vagrant will attempt to choose the best option
# for your machine, but the host may also be explicitly set
# via the `config.vagrant.host` parameter.
class Base
# This returns true/false depending on if the current running system
# 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
end
# Initializes a new host class.
#
# The only required parameter is a UI object so that the host
# objects have some way to communicate with the outside world.
#
# @param [UI] ui UI for the hosts to output to.
def initialize(ui)
@ui = ui
end
# Returns true of false denoting whether or not this host supports
# NFS shared folder setup. This method ideally should verify that
# NFS is installed.
#
# @return [Boolean]
def nfs?
false
end
# Exports the given hash of folders via NFS.
#
# @param [String] id A unique ID that is guaranteed to be unique to
# match these sets of folders.
# @param [String] ip IP of the guest machine.
# @param [Hash] folders Shared folders to sync.
def nfs_export(id, ip, folders)
end
# Prunes any NFS exports made by Vagrant which aren't in the set
# of valid ids given.
#
# @param [Array<String>] valid_ids Valid IDs that should not be
# pruned.
def nfs_prune(valid_ids)
end
end
end
end

View File

@ -8,6 +8,7 @@ module Vagrant
autoload :Command, "vagrant/plugin/v1/command"
autoload :Config, "vagrant/plugin/v1/config"
autoload :Guest, "vagrant/plugin/v1/guest"
autoload :Host, "vagrant/plugin/v1/host"
autoload :Plugin, "vagrant/plugin/v1/plugin"
autoload :Provisioner, "vagrant/plugin/v1/provisioner"
end

View File

@ -0,0 +1,66 @@
module Vagrant
module Plugin
module V1
# Base class for a host in Vagrant. A host class contains functionality
# that is specific to a specific OS that is running Vagrant. This
# abstraction is done becauase there is some host-specific logic that
# Vagrant must do in some cases.
class Host
# This returns true/false depending on if the current running system
# 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
end
# Initializes a new host class.
#
# The only required parameter is a UI object so that the host
# objects have some way to communicate with the outside world.
#
# @param [UI] ui UI for the hosts to output to.
def initialize(ui)
@ui = ui
end
# Returns true of false denoting whether or not this host supports
# NFS shared folder setup. This method ideally should verify that
# NFS is installed.
#
# @return [Boolean]
def nfs?
false
end
# Exports the given hash of folders via NFS.
#
# @param [String] id A unique ID that is guaranteed to be unique to
# match these sets of folders.
# @param [String] ip IP of the guest machine.
# @param [Hash] folders Shared folders to sync.
def nfs_export(id, ip, folders)
end
# Prunes any NFS exports made by Vagrant which aren't in the set
# of valid ids given.
#
# @param [Array<String>] valid_ids Valid IDs that should not be
# pruned.
def nfs_prune(valid_ids)
end
end
end
end
end

View File

@ -6,7 +6,7 @@ require 'vagrant/util/platform'
module VagrantPlugins
module HostBSD
# Represents a BSD host, such as FreeBSD and Darwin (Mac OS X).
class Host < Vagrant::Hosts::Base
class Host < Vagrant.plugin("1", :host)
include Vagrant::Util
include Vagrant::Util::Retryable

View File

@ -6,7 +6,7 @@ require 'vagrant/util/platform'
module VagrantPlugins
module HostLinux
# Represents a Linux based host, such as Ubuntu.
class Host < Vagrant::Hosts::Base
class Host < Vagrant.plugin("1", :host)
include Vagrant::Util
include Vagrant::Util::Retryable

View File

@ -3,7 +3,7 @@ require 'vagrant/util/platform'
module VagrantPlugins
module HostWindows
class Host < Vagrant::Hosts::Base
class Host < Vagrant.plugin("1", :host)
def self.match?
Vagrant::Util::Platform.windows?
end

View File

@ -2,13 +2,14 @@ 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(Vagrant::Hosts::Base) do
foo_klass = Class.new(base_class) do
def self.match?; false; end
end
bar_klass = Class.new(Vagrant::Hosts::Base) do
bar_klass = Class.new(base_class) do
def self.match?; true; end
end
@ -19,11 +20,11 @@ describe Vagrant::Hosts do
end
it "detects the host that matches true with the highest precedence first" do
foo_klass = Class.new(Vagrant::Hosts::Base) do
foo_klass = Class.new(base_class) do
def self.match?; true; end
end
bar_klass = Class.new(Vagrant::Hosts::Base) do
bar_klass = Class.new(base_class) do
def self.match?; true; end
def self.precedence; 9; end
end

View File

@ -0,0 +1,5 @@
require File.expand_path("../../../../base", __FILE__)
describe Vagrant::Plugin::V1::Host do
# No tests.
end

View File

@ -15,6 +15,7 @@ describe Vagrant do
described_class.plugin("1", :command).should == Vagrant::Plugin::V1::Command
described_class.plugin("1", :config).should == Vagrant::Plugin::V1::Config
described_class.plugin("1", :guest).should == Vagrant::Plugin::V1::Guest
described_class.plugin("1", :host).should == Vagrant::Plugin::V1::Host
described_class.plugin("1", :provisioner).should == Vagrant::Plugin::V1::Provisioner
end
end