Configure the V2 kernel.

This commit is contained in:
Mitchell Hashimoto 2012-11-06 21:28:44 -08:00
parent 2de124e296
commit d254d6f718
10 changed files with 385 additions and 2 deletions

View File

@ -98,8 +98,8 @@ module Vagrant
# otherwise we load only the upgrade safe ones, since we're
# obviously being loaded for an upgrade.
config_map = nil
plugin_manager = Vagrant.plugin("1").manager
if Config::CURRENT_VERSION == "1"
plugin_manager = Vagrant.plugin("2").manager
if Config::CURRENT_VERSION == "2"
config_map = plugin_manager.config
else
config_map = plugin_manager.config_upgrade_safe

View File

@ -113,6 +113,19 @@ module Vagrant
providers
end
# This returns all registered provisioners.
#
# @return [Hash]
def provisioners
results = {}
@registered.each do |plugin|
results.merge!(plugin.provisioner.to_hash)
end
results
end
# This registers a plugin. This should _NEVER_ be called by the public
# and should only be called from within Vagrant. Vagrant will
# automatically register V2 plugins when a name is set on the

View File

@ -0,0 +1,10 @@
require "vagrant"
module VagrantPlugins
module Kernel_V2
class NFSConfig < Vagrant.plugin("2", :config)
attr_accessor :map_uid
attr_accessor :map_gid
end
end
end

View File

@ -0,0 +1,9 @@
require "vagrant"
module VagrantPlugins
module Kernel_V2
class PackageConfig < Vagrant.plugin("2", :config)
attr_accessor :name
end
end
end

View File

@ -0,0 +1,30 @@
require "vagrant"
module VagrantPlugins
module Kernel_V2
class SSHConfig < Vagrant.plugin("2", :config)
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|
value = instance_variable_get("@#{field}".to_sym)
errors.add(I18n.t("vagrant.config.common.error_empty", :field => field)) if !value
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,10 @@
require "vagrant"
module VagrantPlugins
module Kernel_V2
class VagrantConfig < Vagrant.plugin("2", :config)
attr_accessor :dotfile_name
attr_accessor :host
end
end
end

View File

@ -0,0 +1,186 @@
require "pathname"
require "vagrant"
require File.expand_path("../vm_provisioner", __FILE__)
require File.expand_path("../vm_subvm", __FILE__)
module VagrantPlugins
module Kernel_V2
class VMConfig < Vagrant.plugin("2", :config)
DEFAULT_VM_NAME = :default
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
if !defined_vms[name]
# If it hasn't been defined before, then create the sub-VM configuration
# and configure it so that it has the proper name.
defined_vms[name] ||= VagrantConfigSubVM.new
defined_vms[name].push_proc do |config|
config.vm.name = name
end
end
defined_vms[name].options.merge!(options)
defined_vms[name].push_proc(&block) if block
end
def finalize!
# If we haven't defined a single VM, then we need to define a
# default VM which just inherits the rest of the configuration.
define(DEFAULT_VM_NAME) if defined_vm_keys.empty?
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, :virtualbox)
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, :virtualbox) && !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,55 @@
require 'log4r'
module VagrantPlugins
module Kernel_V2
# 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
@config = nil
# If the shorcut is a symbol, we look through the registered
# plugins to see if any define a provisioner we want.
if shortcut.is_a?(Symbol)
@provisioner = Vagrant.plugin("2").manager.provisioners[shortcut]
end
@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.plugin("1", :provisioner))
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,26 @@
require "vagrant/util/stacked_proc_runner"
module VagrantPlugins
module Kernel_V2
# Represents a single sub-VM in a multi-VM environment.
class VagrantConfigSubVM
include Vagrant::Util::StackedProcRunner
attr_reader :options
def initialize
@options = {}
end
# This returns an array of the procs to configure this VM, with
# the proper version pre-pended for the configuration loader.
#
# @return [Array]
def config_procs
proc_stack.map do |proc|
["2", proc]
end
end
end
end
end

View File

@ -0,0 +1,44 @@
require "vagrant"
module VagrantPlugins
module Kernel_V2
# This is the "kernel" of Vagrant and contains the configuration classes
# that make up the core of Vagrant for V2.
class Plugin < Vagrant.plugin("2")
name "kernel"
description <<-DESC
The kernel of Vagrant. This plugin contains required items for even
basic functionality of Vagrant version 2.
DESC
# Core configuration keys provided by the kernel. Note that all
# the kernel configuration classes are marked as _upgrade safe_ (the
# true 2nd param). This means that these can be loaded in ANY version
# of the core of Vagrant.
config("ssh", true) do
require File.expand_path("../config/ssh", __FILE__)
SSHConfig
end
config("nfs", true) do
require File.expand_path("../config/nfs", __FILE__)
NFSConfig
end
config("package", true) do
require File.expand_path("../config/package", __FILE__)
PackageConfig
end
config("vagrant", true) do
require File.expand_path("../config/vagrant", __FILE__)
VagrantConfig
end
config("vm", true) do
require File.expand_path("../config/vm", __FILE__)
VMConfig
end
end
end
end