Remove old configuration classes

This commit is contained in:
Mitchell Hashimoto 2012-04-18 17:16:03 -07:00
parent a62e859231
commit a23fee4848
13 changed files with 1 additions and 529 deletions

View File

@ -8,12 +8,6 @@ module Vagrant
autoload :V1, 'vagrant/config/v1'
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
# This is the method which is called by all Vagrantfiles to configure Vagrant.

View File

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

View File

@ -1,7 +0,0 @@
module Vagrant
module Config
class PackageConfig < Base
attr_accessor :name
end
end
end

View File

@ -1,27 +0,0 @@
module Vagrant
module Config
class SSHConfig < Base
attr_accessor :username
attr_accessor :password
attr_accessor :host
attr_accessor :port
attr_accessor :guest_port
attr_accessor :max_tries
attr_accessor :timeout
attr_accessor :private_key_path
attr_accessor :forward_agent
attr_accessor :forward_x11
attr_accessor :shell
def validate(env, errors)
[:username, :host, :max_tries, :timeout].each do |field|
errors.add(I18n.t("vagrant.config.common.error_empty", :field => field)) if !instance_variable_get("@#{field}".to_sym)
end
if private_key_path && !File.file?(File.expand_path(private_key_path, env.root_path))
errors.add(I18n.t("vagrant.config.ssh.private_key_missing", :path => private_key_path))
end
end
end
end
end

View File

@ -1,72 +0,0 @@
module Vagrant
module Config
# This class is the "top" configure class, which handles registering
# other configuration classes as well as validation of all configured
# classes. This is the object which is returned by {Environment#config}
# and has accessors to all other configuration classes.
#
# If you're looking to create your own configuration class, see {Base}.
class Top < Base
attr_reader :keys
def initialize(registry=nil)
@keys = {}
@registry = registry || Vagrant.config_keys
end
# 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 = @registry.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
# Custom implementation to merge each key separately.
def merge(other)
result = self.class.new
@keys.each do |key, value|
result.keys[key] = value.merge(other.send(key))
end
other.keys.each do |key, value|
if !@keys.has_key?(key)
# This is a key that the other configuration class has
# that we don't, so just copy it in.
result.keys[key] = value.dup
end
end
result
end
# Validates the configuration classes of this instance and raises an
# exception if they are invalid. If you are implementing a custom configuration
# class, the method you want to implement is {Base#validate}. This is
# the method that checks all the validation, not one which defines
# validation rules.
def validate!(env)
# Validate each of the configured classes and store the results into
# a hash.
errors = @keys.inject({}) do |container, data|
key, instance = data
recorder = ErrorRecorder.new
instance.validate(env, recorder)
container[key.to_sym] = recorder if !recorder.errors.empty?
container
end
return if errors.empty?
raise Errors::ConfigValidationFailed, :messages => Util::TemplateRenderer.render("config/validation_failed", :errors => errors)
end
end
end
end

View File

@ -1,14 +0,0 @@
module Vagrant
module Config
class VagrantConfig < Base
attr_accessor :dotfile_name
attr_accessor :host
def validate(env, errors)
[:dotfile_name, :host].each do |field|
errors.add(I18n.t("vagrant.config.common.error_empty", :field => field)) if !instance_variable_get("@#{field}".to_sym)
end
end
end
end
end

View File

@ -1,168 +0,0 @@
require 'pathname'
require 'vagrant/config/vm/sub_vm'
require 'vagrant/config/vm/provisioner'
module Vagrant
module Config
class VMConfig < Base
attr_accessor :name
attr_accessor :auto_port_range
attr_accessor :box
attr_accessor :box_url
attr_accessor :base_mac
attr_accessor :boot_mode
attr_accessor :host_name
attr_reader :forwarded_ports
attr_reader :shared_folders
attr_reader :networks
attr_reader :provisioners
attr_reader :customizations
attr_accessor :guest
def initialize
@forwarded_ports = []
@shared_folders = {}
@networks = []
@provisioners = []
@customizations = []
end
# Custom merge method since some keys here are merged differently.
def merge(other)
result = super
result.instance_variable_set(:@forwarded_ports, @forwarded_ports + other.forwarded_ports)
result.instance_variable_set(:@shared_folders, @shared_folders.merge(other.shared_folders))
result.instance_variable_set(:@networks, @networks + other.networks)
result.instance_variable_set(:@provisioners, @provisioners + other.provisioners)
result.instance_variable_set(:@customizations, @customizations + other.customizations)
result
end
def forward_port(guestport, hostport, options=nil)
@forwarded_ports << {
:name => "#{guestport.to_s(32)}-#{hostport.to_s(32)}",
:guestport => guestport,
:hostport => hostport,
:protocol => :tcp,
:adapter => 1,
:auto => false
}.merge(options || {})
end
def share_folder(name, guestpath, hostpath, opts=nil)
@shared_folders[name] = {
:guestpath => guestpath.to_s,
:hostpath => hostpath.to_s,
:create => false,
:owner => nil,
:group => nil,
:nfs => false,
:transient => false,
:extra => nil
}.merge(opts || {})
end
def network(type, *args)
@networks << [type, args]
end
def provision(name, options=nil, &block)
@provisioners << Provisioner.new(name, options, &block)
end
# TODO: This argument should not be `nil` in the future.
# It is only defaulted to nil so that the deprecation error
# can be properly shown.
def customize(command=nil)
@customizations << command if command
end
def defined_vms
@defined_vms ||= {}
end
# This returns the keys of the sub-vms in the order they were
# defined.
def defined_vm_keys
@defined_vm_keys ||= []
end
def define(name, options=nil, &block)
name = name.to_sym
options ||= {}
# Add the name to the array of VM keys. This array is used to
# preserve the order in which VMs are defined.
defined_vm_keys << name
# Add the SubVM to the hash of defined VMs
defined_vms[name] ||= SubVM.new
defined_vms[name].options.merge!(options)
defined_vms[name].push_proc(&block) if block
end
def validate(env, errors)
errors.add(I18n.t("vagrant.config.vm.box_missing")) if !box
errors.add(I18n.t("vagrant.config.vm.box_not_found", :name => box)) if box && !box_url && !env.boxes.find(box)
errors.add(I18n.t("vagrant.config.vm.boot_mode_invalid")) if ![:headless, :gui].include?(boot_mode.to_sym)
errors.add(I18n.t("vagrant.config.vm.base_mac_invalid")) if env.boxes.find(box) && !base_mac
shared_folders.each do |name, options|
hostpath = Pathname.new(options[:hostpath]).expand_path(env.root_path)
if !hostpath.directory? && !options[:create]
errors.add(I18n.t("vagrant.config.vm.shared_folder_hostpath_missing",
:name => name,
:path => options[:hostpath]))
end
if options[:nfs] && (options[:owner] || options[:group])
# Owner/group don't work with NFS
errors.add(I18n.t("vagrant.config.vm.shared_folder_nfs_owner_group",
:name => name))
end
end
# Validate some basic networking
#
# TODO: One day we need to abstract this out, since in the future
# providers other than VirtualBox will not be able to satisfy
# all types of networks.
networks.each do |type, args|
if type == :hostonly && args[0] == :dhcp
# Valid. There is no real way this can be invalid at the moment.
elsif type == :hostonly
# Validate the host-only network
ip = args[0]
options = args[1] || {}
if !ip
errors.add(I18n.t("vagrant.config.vm.network_ip_required"))
else
ip_parts = ip.split(".")
if ip_parts.length != 4
errors.add(I18n.t("vagrant.config.vm.network_ip_invalid",
:ip => ip))
elsif ip_parts.last == "1"
errors.add(I18n.t("vagrant.config.vm.network_ip_ends_one",
:ip => ip))
end
end
elsif type == :bridged
else
# Invalid network type
errors.add(I18n.t("vagrant.config.vm.network_invalid",
:type => type.to_s))
end
end
# Each provisioner can validate itself
provisioners.each do |prov|
prov.validate(env, errors)
end
end
end
end
end

View File

@ -1,52 +0,0 @@
require 'log4r'
module Vagrant
module Config
class VMConfig < Base
# Represents a single configured provisioner for a VM.
class Provisioner
attr_reader :shortcut
attr_reader :provisioner
attr_reader :config
def initialize(shortcut, options=nil, &block)
@logger = Log4r::Logger.new("vagrant::config::vm::provisioner")
@logger.debug("Provisioner config: #{shortcut}")
@shortcut = shortcut
@provisioner = shortcut
@provisioner = Vagrant.provisioners.get(shortcut) if shortcut.is_a?(Symbol)
@config = nil
@logger.info("Provisioner class: #{provisioner}")
configure(options, &block) if @provisioner
end
# Configures the provisioner if it can (if it is valid).
def configure(options=nil, &block)
config_class = @provisioner.config_class
return if !config_class
@logger.debug("Configuring provisioner with: #{config_class}")
@config = config_class.new
@config.set_options(options) if options
block.call(@config) if block
end
def validate(env, errors)
if !provisioner
# If we don't have a provisioner then the whole thing is invalid.
errors.add(I18n.t("vagrant.config.vm.provisioner_not_found", :shortcut => shortcut))
return
end
if !(provisioner <= Provisioners::Base)
errors.add(I18n.t("vagrant.config.vm.provisioner_invalid_class", :shortcut => shortcut))
end
# Pass on validation to the provisioner config
config.validate(env, errors) if config
end
end
end
end
end

View File

@ -1,17 +0,0 @@
module Vagrant
module Config
class VMConfig < Base
# Represents a single sub-VM in a multi-VM environment.
class SubVM
include Util::StackedProcRunner
attr_reader :options
def initialize
@options = {}
end
end
end
end
end

View File

@ -402,7 +402,7 @@ module Vagrant
# to simply be our configuration.
if defined_vm_keys.empty?
defined_vm_keys << DEFAULT_VM
defined_vms[DEFAULT_VM] = Config::VMConfig::SubVM.new
defined_vms[DEFAULT_VM] = VagrantPlugins::Kernel::VagrantConfigSubVM.new
end
vm_configs = defined_vm_keys.map do |vm_name|

View File

@ -1,17 +0,0 @@
require File.expand_path("../../../base", __FILE__)
describe Vagrant::Config::SSHConfig do
include_context "unit"
[:forward_agent, :forward_x11].each do |bool_setting|
it "merges boolean #{bool_setting} properly" do
a = described_class.new
a.send("#{bool_setting}=", true)
b = described_class.new
c = a.merge(b)
c.send(bool_setting).should be
end
end
end

View File

@ -1,69 +0,0 @@
require File.expand_path("../../../base", __FILE__)
require "vagrant/registry"
describe Vagrant::Config::Top do
include_context "unit"
let(:registry) { Vagrant::Registry.new }
let(:instance) { described_class.new(registry) }
it "should load in the proper config class" do
registry.register(:foo, Object)
instance.foo.should be_kind_of(Object)
end
it "should load the proper config class only once" do
registry.register(:foo, Object)
obj = instance.foo
instance.foo.should eql(obj)
end
it "still raises a method missing error if invalid key" do
expect { instance.foo }.to raise_error(NoMethodError)
end
describe "merging" do
let(:foo_class) do
Class.new do
attr_accessor :one
attr_accessor :two
def merge(other)
result = self.class.new
result.one = other.one || one
result.two = other.two || two
result
end
end
end
it "merges each key by calling `merge` on the class" do
registry.register(:foo, foo_class)
instance.foo.one = 1
instance.foo.two = 2
another = described_class.new(registry)
another.foo.one = 2
result = instance.merge(another)
result.foo.one.should == 2
result.foo.two.should == 2
end
it "merges keys that aren't in the source instance" do
reg = Vagrant::Registry.new
reg.register(:foo, foo_class)
another = described_class.new(reg)
another.foo.one = 2
result = instance.merge(another)
result.foo.one.should == 2
result.foo.two.should be_nil
end
end
end

View File

@ -1,71 +0,0 @@
require File.expand_path("../../../base", __FILE__)
describe Vagrant::Config::VMConfig do
include_context "unit"
it "merges by appending forwarded ports" do
a = described_class.new
a.forward_port 80, 8080
b = described_class.new
b.forward_port 100, 1000
c = a.merge(b)
c.forwarded_ports.length.should == 2
c.forwarded_ports[0][:guestport].should == 80
c.forwarded_ports[0][:hostport].should == 8080
c.forwarded_ports[1][:guestport].should == 100
c.forwarded_ports[1][:hostport].should == 1000
end
it "merges by merging shared folders" do
a = described_class.new
a.share_folder "a", "/guest", "/host"
a.share_folder "b", "/guest", "/host"
b = described_class.new
b.share_folder "c", "/guest", "/host"
c = a.merge(b)
c.shared_folders.has_key?("a").should be
c.shared_folders.has_key?("b").should be
c.shared_folders.has_key?("c").should be
end
it "merges by appending networks" do
a = described_class.new
a.network :hostonly, "192.168.33.10"
b = described_class.new
b.network :hostonly, "192.168.33.11"
c = a.merge(b)
c.networks.length.should == 2
c.networks[0].should == [:hostonly, ["192.168.33.10"]]
c.networks[1].should == [:hostonly, ["192.168.33.11"]]
end
it "merges by appending provisioners" do
a = described_class.new
a.provision :foo
b = described_class.new
b.provision :bar
c = a.merge(b)
c.provisioners.length.should == 2
c.provisioners[0].shortcut.should == :foo
c.provisioners[1].shortcut.should == :bar
end
it "merges by appending customizations" do
a = described_class.new
a.customize "a"
b = described_class.new
b.customize "b"
c = a.merge(b)
c.customizations.should == ["a", "b"]
end
end