From 1c2f80fab4f4e3aafce96cae8f5bb6f7a688f642 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 27 Aug 2011 23:39:02 -0700 Subject: [PATCH] Distro dispatch for Host classes --- lib/vagrant/hosts/base.rb | 62 +++++++++++++++++------------------- lib/vagrant/hosts/bsd.rb | 6 ++++ lib/vagrant/hosts/linux.rb | 8 +++++ lib/vagrant/util/platform.rb | 7 ---- 4 files changed, 44 insertions(+), 39 deletions(-) diff --git a/lib/vagrant/hosts/base.rb b/lib/vagrant/hosts/base.rb index 10932ccbf..5b7f5f39b 100644 --- a/lib/vagrant/hosts/base.rb +++ b/lib/vagrant/hosts/base.rb @@ -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, diff --git a/lib/vagrant/hosts/bsd.rb b/lib/vagrant/hosts/bsd.rb index f81dae20a..d84711482 100644 --- a/lib/vagrant/hosts/bsd.rb +++ b/lib/vagrant/hosts/bsd.rb @@ -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") diff --git a/lib/vagrant/hosts/linux.rb b/lib/vagrant/hosts/linux.rb index 68466491e..b6ec7bed4 100644 --- a/lib/vagrant/hosts/linux.rb +++ b/lib/vagrant/hosts/linux.rb @@ -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 diff --git a/lib/vagrant/util/platform.rb b/lib/vagrant/util/platform.rb index cbe3a3709..76be1a665 100644 --- a/lib/vagrant/util/platform.rb +++ b/lib/vagrant/util/platform.rb @@ -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. #