From eda286b476315b00d9e58acd9eef0afbbaf16d72 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 15 Dec 2011 20:32:33 -0800 Subject: [PATCH] Config classes are registered via a registry now --- lib/vagrant.rb | 21 +++++++++++++++ lib/vagrant/config.rb | 24 ++++++++--------- lib/vagrant/config/base.rb | 13 --------- lib/vagrant/config/nfs.rb | 2 -- lib/vagrant/config/package.rb | 2 -- lib/vagrant/config/ssh.rb | 2 -- lib/vagrant/config/top.rb | 41 +++++++++++++---------------- lib/vagrant/config/vagrant.rb | 2 -- lib/vagrant/config/vm.rb | 2 -- lib/vagrant/systems/freebsd.rb | 2 -- lib/vagrant/systems/linux/config.rb | 2 -- lib/vagrant/systems/solaris.rb | 2 -- test/unit/vagrant_test.rb | 4 +++ 13 files changed, 54 insertions(+), 65 deletions(-) diff --git a/lib/vagrant.rb b/lib/vagrant.rb index 884c198ff..376f8900f 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -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' diff --git a/lib/vagrant/config.rb b/lib/vagrant/config.rb index 6ff90bfb5..74665edc1 100644 --- a/lib/vagrant/config.rb +++ b/lib/vagrant/config.rb @@ -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 diff --git a/lib/vagrant/config/base.rb b/lib/vagrant/config/base.rb index 5a079969d..c51f2766a 100644 --- a/lib/vagrant/config/base.rb +++ b/lib/vagrant/config/base.rb @@ -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 diff --git a/lib/vagrant/config/nfs.rb b/lib/vagrant/config/nfs.rb index beaf63e4f..f7aed1f42 100644 --- a/lib/vagrant/config/nfs.rb +++ b/lib/vagrant/config/nfs.rb @@ -1,8 +1,6 @@ module Vagrant module Config class NFSConfig < Base - configures :nfs - attr_accessor :map_uid attr_accessor :map_gid end diff --git a/lib/vagrant/config/package.rb b/lib/vagrant/config/package.rb index 85871c434..112e20a61 100644 --- a/lib/vagrant/config/package.rb +++ b/lib/vagrant/config/package.rb @@ -1,8 +1,6 @@ module Vagrant module Config class PackageConfig < Base - configures :package - attr_accessor :name end end diff --git a/lib/vagrant/config/ssh.rb b/lib/vagrant/config/ssh.rb index 9adaff5d5..166a01f93 100644 --- a/lib/vagrant/config/ssh.rb +++ b/lib/vagrant/config/ssh.rb @@ -1,8 +1,6 @@ module Vagrant module Config class SSHConfig < Base - configures :ssh - attr_accessor :username attr_accessor :host attr_accessor :forwarded_port_key diff --git a/lib/vagrant/config/top.rb b/lib/vagrant/config/top.rb index b80ed5083..4d49c290a 100644 --- a/lib/vagrant/config/top.rb +++ b/lib/vagrant/config/top.rb @@ -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 diff --git a/lib/vagrant/config/vagrant.rb b/lib/vagrant/config/vagrant.rb index 4ba66db92..9e1da1f77 100644 --- a/lib/vagrant/config/vagrant.rb +++ b/lib/vagrant/config/vagrant.rb @@ -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 diff --git a/lib/vagrant/config/vm.rb b/lib/vagrant/config/vm.rb index 573b80a66..9751faa06 100644 --- a/lib/vagrant/config/vm.rb +++ b/lib/vagrant/config/vm.rb @@ -4,8 +4,6 @@ require 'vagrant/config/vm/provisioner' module Vagrant module Config class VMConfig < Base - configures :vm - include Util::StackedProcRunner attr_accessor :name diff --git a/lib/vagrant/systems/freebsd.rb b/lib/vagrant/systems/freebsd.rb index af21d079a..2944bc094 100644 --- a/lib/vagrant/systems/freebsd.rb +++ b/lib/vagrant/systems/freebsd.rb @@ -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 diff --git a/lib/vagrant/systems/linux/config.rb b/lib/vagrant/systems/linux/config.rb index a98a7f4ad..76e029199 100644 --- a/lib/vagrant/systems/linux/config.rb +++ b/lib/vagrant/systems/linux/config.rb @@ -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 diff --git a/lib/vagrant/systems/solaris.rb b/lib/vagrant/systems/solaris.rb index 5f11cb208..86936fb0d 100644 --- a/lib/vagrant/systems/solaris.rb +++ b/lib/vagrant/systems/solaris.rb @@ -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 diff --git a/test/unit/vagrant_test.rb b/test/unit/vagrant_test.rb index 03824f94e..46ab37d63 100644 --- a/test/unit/vagrant_test.rb +++ b/test/unit/vagrant_test.rb @@ -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