Distro dispatch for Host classes

This commit is contained in:
Mitchell Hashimoto 2011-08-27 23:39:02 -07:00
parent 5f62231bac
commit 1c2f80fab4
4 changed files with 44 additions and 39 deletions

View File

@ -8,41 +8,39 @@ module Vagrant
# The {Environment} which this host belongs to.
attr_reader :env
class << self
# Loads the proper host for the given value. If the value is nil
# or is the symbol `:detect`, then the host class will be detected
# using the `RUBY_PLATFORM` constant.
#
# @param [Environment] env
# @param [String] klass
# @return [Base]
def load(env, klass)
klass = detect if klass.nil? || klass == :detect
return nil if !klass
return klass.new(env)
# Loads the proper host for the given value. If the value is nil
# or is the symbol `:detect`, then the host class will be detected
# using the `RUBY_PLATFORM` constant.
#
# @param [Environment] env
# @param [String] klass
# @return [Base]
def self.load(env, klass)
klass = detect if klass.nil? || klass == :detect
return nil if !klass
return klass.new(env)
end
# Detects the proper host class for current platform and returns
# the class.
#
# @return [Class]
def self.detect
[BSD, Linux].each do |type|
result = type.distro_dispatch
return result if result
end
# Detects the proper host class for current platform and returns
# the class.
#
# @return [Class]
def detect
# More coming soon
classes = {
:darwin => BSD,
:bsd => BSD,
:arch => Arch,
:linux => Linux
}
nil
rescue Exception
nil
end
classes.each do |type, klass|
return klass if Util::Platform.send("#{type}?")
end
nil
rescue Exception
nil
end
# This must be implemented by subclasses to dispatch to the proper
# distro-specific class for the host. If this returns nil then it is
# an invalid host class.
def self.distro_dispatch
nil
end
# Initialzes a new host. This method shouldn't be called directly,

View File

@ -1,3 +1,5 @@
require 'vagrant/util/platform'
module Vagrant
module Hosts
# Represents a BSD host, such as FreeBSD and Darwin (Mac OS X).
@ -5,6 +7,10 @@ module Vagrant
include Util
include Util::Retryable
def self.distro_dispatch
return self if Util::Platform.darwin? || Util::Platform.bsd?
end
def nfs?
retryable(:tries => 10, :on => TypeError) do
system("which nfsd > /dev/null 2>&1")

View File

@ -1,3 +1,5 @@
require 'vagrant/util/platform'
module Vagrant
module Hosts
# Represents a Linux based host, such as Ubuntu.
@ -5,6 +7,12 @@ module Vagrant
include Util
include Util::Retryable
def self.distro_dispatch
return nil if !Util::Platform.linux?
return Arch if File.exist?("/etc/rc.conf") && File.exist?("/etc/pacman.conf")
return self
end
def nfs?
retryable(:tries => 10, :on => TypeError) do
# Check procfs to see if NFSd is a supported filesystem

View File

@ -27,13 +27,6 @@ module Vagrant
false
end
def arch?
linux? &&
File.exist?('/etc/rc.conf') &&
File.exist?('/etc/pacman.conf') &&
File.exist?('/etc/rc.d/')
end
# Returns boolean noting whether this is a 64-bit CPU. This
# is not 100% accurate and there could easily be false negatives.
#