Config classes are registered via a registry now

This commit is contained in:
Mitchell Hashimoto 2011-12-15 20:32:33 -08:00
parent 7ab6ab31f8
commit eda286b476
13 changed files with 54 additions and 65 deletions

View File

@ -44,6 +44,15 @@ module Vagrant
@source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
end
# Global registry of config keys that are available.
#
# This registry is used to look up the keys for `config` objects.
# For example, `config.vagrant` looks up the `:vagrant` config key
# for the configuration class to use.
def self.config_keys
@config_keys ||= Registry.new
end
# Global registry of available host classes and shortcut symbols
# associated with them.
#
@ -74,6 +83,13 @@ end
# # Default I18n to load the en locale
I18n.load_path << File.expand_path("templates/locales/en.yml", Vagrant.source_root)
# Registry the build-in config keys
Vagrant.config_keys.register(:vagrant) { Vagrant::Config::VagrantConfig }
Vagrant.config_keys.register(:ssh) { Vagrant::Config::SSHConfig }
Vagrant.config_keys.register(:nfs) { Vagrant::Config::NFSConfig }
Vagrant.config_keys.register(:vm) { Vagrant::Config::VMConfig }
Vagrant.config_keys.register(:package) { Vagrant::Config::PackageConfig }
# Register the built-in hosts
Vagrant.hosts.register(:arch) { Vagrant::Hosts::Arch }
Vagrant.hosts.register(:freebsd) { Vagrant::Hosts::FreeBSD }
@ -99,6 +115,11 @@ Vagrant.provisioners.register(:puppet) { Vagrant::Provisioners::Puppet }
Vagrant.provisioners.register(:puppet_server) { Vagrant::Provisioners::PuppetServer }
Vagrant.provisioners.register(:shell) { Vagrant::Provisioners::Shell }
# Register the built-in systems
Vagrant.config_keys.register(:freebsd) { Vagrant::Provisioners::FreeBSD::FreeBSDConfig }
Vagrant.config_keys.register(:linux) { Vagrant::Provisioners::Linux::LinuxConfig }
Vagrant.config_keys.register(:solaris) { Vagrant::Provisioners::Solaris::SolarisConfig }
# Load the things which must be loaded before anything else.
require 'vagrant/command'
require 'vagrant/systems'

View File

@ -1,18 +1,16 @@
require 'vagrant/config/base'
require 'vagrant/config/loader'
require 'vagrant/config/error_recorder'
require 'vagrant/config/top'
# # The built-in configuration classes
require 'vagrant/config/vagrant'
require 'vagrant/config/ssh'
require 'vagrant/config/nfs'
require 'vagrant/config/vm'
require 'vagrant/config/package'
module Vagrant
module Config
autoload :Container, 'vagrant/config/container'
autoload :Base, 'vagrant/config/base'
autoload :Container, 'vagrant/config/container'
autoload :ErrorRecorder, 'vagrant/config/error_recorder'
autoload :Loader, 'vagrant/config/loader'
autoload :Top, 'vagrant/config/top'
autoload :NFSConfig, 'vagrant/config/nfs'
autoload :PackageConfig, 'vagrant/config/package'
autoload :SSHConfig, 'vagrant/config/ssh'
autoload :VagrantConfig, 'vagrant/config/vagrant'
autoload :VMConfig, 'vagrant/config/vm'
CONFIGURE_MUTEX = Mutex.new

View File

@ -4,19 +4,6 @@ module Vagrant
# basic things such as the environment instance variable which all
# config classes need as well as a basic `to_json` implementation.
class Base
# {Top} of this configuration stack
attr_accessor :top
# Registers a subclass with the Vagrant configuration system so
# that it can then be used in Vagrantfiles.
#
# @param [Symbol] accessor The accessor on the main config object
# that is used to access the configuration class.
#
def self.configures(accessor, klass=self)
Top.configures(accessor, klass)
end
# Loads configuration values from JSON back into the proper
# configuration classes. By default, this is done by simply
# iterating over all values in the JSON hash and assigning them

View File

@ -1,8 +1,6 @@
module Vagrant
module Config
class NFSConfig < Base
configures :nfs
attr_accessor :map_uid
attr_accessor :map_gid
end

View File

@ -1,8 +1,6 @@
module Vagrant
module Config
class PackageConfig < Base
configures :package
attr_accessor :name
end
end

View File

@ -1,8 +1,6 @@
module Vagrant
module Config
class SSHConfig < Base
configures :ssh
attr_accessor :username
attr_accessor :host
attr_accessor :forwarded_port_key

View File

@ -7,28 +7,23 @@ module Vagrant
#
# If you're looking to create your own configuration class, see {Base}.
class Top < Base
@@configures = {} if !defined?(@@configures)
class << self
# The list of registered configuration classes as well as the key
# they're registered under.
def configures_list
@@configures ||= {}
end
# Registers a configuration class with the given key. This method shouldn't
# be called. Instead, inherit from {Base} and call {Base.configures}.
def configures(key, klass)
configures_list[key] = klass
attr_reader key.to_sym
end
def initialize
@keys = {}
end
def initialize
self.class.configures_list.each do |key, klass|
config = klass.new
config.top = self
instance_variable_set("@#{key}".to_sym, config)
# We use method_missing as a way to get the configuration that is used
# for Vagrant and load the proper configuration classes for each.
def method_missing(name, *args)
return @keys[name] if @keys.has_key?(name)
config_klass = Vagrant.config_keys.get(name.to_sym)
if config_klass
# Instantiate the class and return the instance
@keys[name] = config_klass.new
return @keys[name]
else
# Super it up to probably raise a NoMethodError
super
end
end
@ -40,10 +35,10 @@ module Vagrant
def validate!(env)
# Validate each of the configured classes and store the results into
# a hash.
errors = self.class.configures_list.inject({}) do |container, data|
key, _ = data
errors = @keys.inject({}) do |container, data|
key, instance = data
recorder = ErrorRecorder.new
send(key.to_sym).validate(env, recorder)
instance.validate(env, recorder)
container[key.to_sym] = recorder if !recorder.errors.empty?
container
end

View File

@ -1,8 +1,6 @@
module Vagrant
module Config
class VagrantConfig < Base
configures :vagrant
attr_accessor :dotfile_name
attr_accessor :host
attr_accessor :ssh_session_cache

View File

@ -4,8 +4,6 @@ require 'vagrant/config/vm/provisioner'
module Vagrant
module Config
class VMConfig < Base
configures :vm
include Util::StackedProcRunner
attr_accessor :name

View File

@ -9,8 +9,6 @@ module Vagrant
# generally, Vagrant tries to make almost every aspect of its execution
# configurable, and this assists that goal.
class FreeBSDConfig < Vagrant::Config::Base
configures :freebsd
attr_accessor :halt_timeout
attr_accessor :halt_check_interval

View File

@ -6,8 +6,6 @@ module Vagrant
# generally, Vagrant tries to make almost every aspect of its execution
# configurable, and this assists that goal.
class LinuxConfig < Vagrant::Config::Base
configures :linux
attr_accessor :halt_timeout
attr_accessor :halt_check_interval

View File

@ -9,8 +9,6 @@ module Vagrant
# generally, Vagrant tries to make almost every aspect of its execution
# configurable, and this assists that goal.
class SolarisConfig < Vagrant::Config::Base
configures :solaris
attr_accessor :halt_timeout
attr_accessor :halt_check_interval
# This sets the command to use to execute items as a superuser. sudo is default

View File

@ -5,6 +5,10 @@ describe Vagrant do
described_class.source_root.should == Pathname.new(File.expand_path("../../../", __FILE__))
end
it "has a registry for config keys" do
described_class.config_keys.should be_a(Vagrant::Registry)
end
it "has a registry for hosts" do
described_class.hosts.should be_a(Vagrant::Registry)
end