Concepts of "Hosts" introduced, preparing for host-specific behavior.

This commit is contained in:
Mitchell Hashimoto 2010-07-10 22:07:10 -07:00
parent 9679000820
commit e2badeb9e1
10 changed files with 151 additions and 1 deletions

View File

@ -3,6 +3,7 @@ Vagrant::Config.run do |config|
config.vagrant.log_output = STDOUT
config.vagrant.dotfile_name = ".vagrant"
config.vagrant.home = "~/.vagrant"
config.vagrant.host = :detect
config.ssh.username = "vagrant"
config.ssh.host = "localhost"

View File

@ -13,7 +13,7 @@ require File.expand_path("util/glob_loader", libdir)
# Load them up
Vagrant::GlobLoader.glob_require(libdir, %w{util/stacked_proc_runner
downloaders/base config provisioners/base provisioners/chef systems/base
commands/base commands/box action/exception_catcher})
commands/base commands/box action/exception_catcher hosts/base})
# Initialize the built-in actions
Vagrant::Action.builtin!

View File

@ -193,6 +193,7 @@ module Vagrant
attr_accessor :dotfile_name
attr_accessor :log_output
attr_accessor :home
attr_accessor :host
def home
@home ? File.expand_path(@home) : nil

View File

@ -15,6 +15,7 @@ module Vagrant
attr_accessor :cwd
attr_reader :root_path
attr_reader :config
attr_reader :host
attr_reader :box
attr_accessor :vm
attr_reader :vms
@ -137,6 +138,7 @@ module Vagrant
load_root_path!
load_config!
load_home_directory!
load_host!
load_box!
load_config!
self.class.check_virtualbox!
@ -237,6 +239,11 @@ module Vagrant
end
end
# Loads the host class for this environment.
def load_host!
@host = Hosts::Base.load(self, config.vagrant.host)
end
# Loads the specified box for this environment.
def load_box!
return unless root_path

55
lib/vagrant/hosts/base.rb Normal file
View File

@ -0,0 +1,55 @@
module Vagrant
module Hosts
# Base class representing a host machine. These classes
# define methods which may have host-specific (Mac OS X, Windows,
# Linux, etc) behavior. The class is automatically determined by
# default but may be explicitly set via `config.vagrant.host`.
class Base
# 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)
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
}
classes.each do |type, klass|
return klass if Util::Platform.send("#{type}?")
end
nil
rescue Exception
nil
end
end
# Initialzes a new host. This method shouldn't be called directly,
# typically, since it will be called by {Environment#load_host!}
#
# @param [Environment] env
def initialize(env)
@env = env
end
end
end
end

7
lib/vagrant/hosts/bsd.rb Normal file
View File

@ -0,0 +1,7 @@
module Vagrant
module Hosts
# Represents a BSD host, such as FreeBSD and Darwin (Mac OS X).
class BSD < Base
end
end
end

View File

@ -7,10 +7,28 @@ module Vagrant
RUBY_PLATFORM.downcase.include?("darwin9")
end
[:darwin, :bsd, :linux].each do |type|
define_method("#{type}?") do
platform.include?(type.to_s)
end
end
def windows?
%W[mingw mswin].each do |text|
return true if platform.include?(text)
end
false
end
def tar_file_options
# create, write only, fail if the file exists, binary if windows
File::WRONLY|File::EXCL|File::CREAT|(Mario::Platform.windows? ? File::BINARY : 0)
end
def platform
RUBY_PLATFORM.to_s.downcase
end
end
end
end

View File

@ -26,6 +26,7 @@ class Test::Unit::TestCase
config.vagrant.home = '~/.home'
config.vagrant.dotfile_name = ".vagrant"
config.vagrant.log_output = nil
config.vagrant.host = :detect
config.ssh.username = "foo"
config.ssh.host = "baz"

View File

@ -220,6 +220,7 @@ class EnvironmentTest < Test::Unit::TestCase
@env.expects(:load_root_path!).once.in_sequence(call_seq)
@env.expects(:load_config!).once.in_sequence(call_seq)
@env.expects(:load_home_directory!).once.in_sequence(call_seq)
@env.expects(:load_host!).once.in_sequence(call_seq)
@env.expects(:load_box!).once.in_sequence(call_seq)
@env.expects(:load_config!).once.in_sequence(call_seq)
Vagrant::Environment.expects(:check_virtualbox!).once.in_sequence(call_seq)
@ -447,6 +448,19 @@ class EnvironmentTest < Test::Unit::TestCase
end
end
context "loading host" do
setup do
@env = mock_environment
end
should "load the host by calling the load method on Host::Base" do
result = mock("result")
Vagrant::Hosts::Base.expects(:load).with(@env, @env.config.vagrant.host).once.returns(result)
@env.load_host!
assert_equal result, @env.host
end
end
context "loading box" do
setup do
@box = mock("box")

View File

@ -0,0 +1,46 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class BaseHostTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Hosts::Base
end
context "class methods" do
context "loading" do
setup do
@env = mock_environment
end
should "return detected class if klass is nil" do
Vagrant::Util::Platform.stubs(:platform).returns("darwin")
result = @klass.load(@env, nil)
assert result.is_a?(Vagrant::Hosts::BSD)
end
should "instantiate the given class" do
result = @klass.load(@env, Vagrant::Hosts::BSD)
assert result.is_a?(Vagrant::Hosts::BSD)
assert_equal @env, result.env
end
end
context "detecting class" do
should "return the proper class" do
Vagrant::Util::Platform.stubs(:platform).returns("darwin10")
assert_equal Vagrant::Hosts::BSD, @klass.detect
end
should "return nil if no class is detected" do
Vagrant::Util::Platform.stubs(:platform).returns("boo")
assert_nil @klass.detect
end
should "return nil if an exception is raised" do
Vagrant::Util::Platform.expects(:darwin?).raises(Exception)
assert_nothing_raised {
assert_nil @klass.detect
}
end
end
end
end