Move core config into a plugin

This commit is contained in:
Mitchell Hashimoto 2012-04-17 22:12:27 -07:00
parent c0ee3b06ff
commit 98d6bf958e
11 changed files with 379 additions and 0 deletions

View File

@ -154,6 +154,19 @@ end
# # Default I18n to load the en locale # # Default I18n to load the en locale
I18n.load_path << File.expand_path("templates/locales/en.yml", Vagrant.source_root) I18n.load_path << File.expand_path("templates/locales/en.yml", Vagrant.source_root)
# Load the core plugins that ship with Vagrant
Vagrant.source_root.join("plugins").each_child do |directory|
# We only care about directories
next if !directory.directory?
# We only care if there is a plugin file within the directory
plugin_file = directory.join("plugin.rb")
next if !plugin_file.file?
# Load the plugin!
load(plugin_file)
end
# Register the built-in commands # Register the built-in commands
Vagrant.commands.register(:box) { Vagrant::Command::Box } Vagrant.commands.register(:box) { Vagrant::Command::Box }
Vagrant.commands.register(:destroy) { Vagrant::Command::Destroy } Vagrant.commands.register(:destroy) { Vagrant::Command::Destroy }

View File

@ -1,3 +1,4 @@
require "vagrant/config/v1/base"
require "vagrant/config/v1/root" require "vagrant/config/v1/root"
module Vagrant module Vagrant

View File

@ -0,0 +1,15 @@
module Vagrant
module Config
class V1
# Base class for configuration keys. It is not required to inherit
# from this class but this class provides useful helpers that config
# classes may wish to use.
class Base
# This is a useful default to use for attributes, and the built-in
# merge for this class will use this as a marker that a value is
# unset (versus just being explicitly set to `nil`)
UNSET_VALUE = Object.new
end
end
end
end

View File

@ -0,0 +1,15 @@
require "vagrant"
module VagrantPlugins
module Kernel
class NFSConfig < Vagrant::Config::V1::Base
attr_accessor :map_uid
attr_accessor :map_gid
def initialize
@map_uid = UNSET_VALUE
@map_gid = UNSET_VALUE
end
end
end
end

View File

@ -0,0 +1,13 @@
require "vagrant"
module VagrantPlugins
module Kernel
class PackageConfig < Vagrant::Config::V1::Base
attr_accessor :name
def initialize
@name = UNSET_VALUE
end
end
end
end

View File

@ -0,0 +1,43 @@
require "vagrant"
module VagrantPlugins
module Kernel
class SSHConfig < Vagrant::Config::V1::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 initialize
@username = UNSET_VALUE
@password = UNSET_VALUE
@host = UNSET_VALUE
@port = UNSET_VALUE
@guest_port = UNSET_VALUE
@max_tries = UNSET_VALUE
@timeout = UNSET_VALUE
@private_key_path = UNSET_VALUE
@forward_agent = UNSET_VALUE
@forward_x11 = UNSET_VALUE
@shell = UNSET_VALUE
end
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

@ -0,0 +1,15 @@
require "vagrant"
module VagrantPlugins
module Kernel
class VagrantConfig < Vagrant::Config::V1::Base
attr_accessor :dotfile_name
attr_accessor :host
def initialize
@dotfile_name = UNSET_VALUE
@host = UNSET_VALUE
end
end
end
end

170
plugins/kernel/config/vm.rb Normal file
View File

@ -0,0 +1,170 @@
require "pathname"
require "vagrant"
require File.expand_path("../vm_provisioner", __FILE__)
require File.expand_path("../vm_subvm", __FILE__)
module VagrantPlugins
module Kernel
class VMConfig < Vagrant::Config::V1::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 << VagrantConfigProvisioner.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] ||= VagrantConfigSubVM.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

@ -0,0 +1,50 @@
require 'log4r'
module VagrantPlugins
module Kernel
# Represents a single configured provisioner for a VM.
class VagrantConfigProvisioner
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 <= Vagrant::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

View File

@ -0,0 +1,16 @@
require "vagrant/util/stacked_proc_runner"
module VagrantPlugins
module Kernel
# Represents a single sub-VM in a multi-VM environment.
class VagrantConfigSubVM
include Vagrant::Util::StackedProcRunner
attr_reader :options
def initialize
@options = {}
end
end
end
end

28
plugins/kernel/plugin.rb Normal file
View File

@ -0,0 +1,28 @@
require "vagrant"
module VagrantPlugins
module Kernel
autoload :SSHConfig, File.expand_path("../config/ssh", __FILE__)
autoload :NFSConfig, File.expand_path("../config/nfs", __FILE__)
autoload :PackageConfig, File.expand_path("../config/package", __FILE__)
autoload :VagrantConfig, File.expand_path("../config/vagrant", __FILE__)
autoload :VMConfig, File.expand_path("../config/vm", __FILE__)
# This is the "kernel" of Vagrant and contains the configuration classes
# that make up the core of Vagrant.
class Plugin < Vagrant.plugin("1")
name "kernel"
description <<-DESC
The kernel of Vagrant. This plugin contains required items for even
basic functionality of Vagrant version 1.
DESC
# Core configuration keys provided by the kernel.
config("ssh") { SSHConfig }
config("nfs") { NFSConfig }
config("package") { PackageConfig }
config("vagrant") { VagrantConfig }
config("vm") { VMConfig }
end
end
end