Class: Vagrant::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/config.rb,
lib/vagrant/config/vm.rb,
lib/vagrant/config/nfs.rb,
lib/vagrant/config/ssh.rb,
lib/vagrant/config/top.rb,
lib/vagrant/config/base.rb,
lib/vagrant/config/package.rb,
lib/vagrant/config/vagrant.rb,
lib/vagrant/config/vm/sub_vm.rb,
lib/vagrant/config/error_recorder.rb,
lib/vagrant/config/vm/provisioner.rb

Overview

The config class is responsible for loading Vagrant configurations, which are usually found in Vagrantfiles but may also be procs. The loading is done by specifying a queue of files or procs that are for configuration, and then executing them. The config loader will run each item in the queue, so that configuration from later items overwrite that from earlier items. This is how Vagrant "scoping" of Vagranfiles is implemented.

If you're looking to create your own configuration classes, see Base.

Loading Configuration Files

If you are in fact looking to load configuration files, then this is the class you are looking for. Loading configuration is quite easy. The following example assumes env is already a loaded instance of Environment:

config = Vagrant::Config.new
config.set(:first, "/path/to/some/Vagrantfile")
config.set(:second, "/path/to/another/Vagrantfile")
config.load_order = [:first, :second]
result = config.load(env)

p "Your box is: #{result.vm.box}"

The load order determines what order the config files specified are loaded. If a key is not mentioned (for example if above the load order was set to [:first], therefore :second was not mentioned), then that config file won't be loaded.

Defined Under Namespace

Classes: Base, ErrorRecorder, NFSConfig, PackageConfig, SSHConfig, Top, VMConfig, VagrantConfig

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Config

Returns a new instance of Config.



68
69
70
71
72
73
74
75
76
77
# File 'lib/vagrant/config.rb', line 68

def initialize(parent=nil)
  @procs = {}
  @load_order = []

  if parent
    # Shallow copy the procs and load order from parent if given
    @procs = parent.procs.dup
    @load_order = parent.load_order.dup
  end
end

Instance Attribute Details

#load_orderObject

An array of symbols specifying the load order for the procs.



42
43
44
# File 'lib/vagrant/config.rb', line 42

def load_order
  @load_order
end

#procsObject (readonly)

Returns the value of attribute procs.



43
44
45
# File 'lib/vagrant/config.rb', line 43

def procs
  @procs
end

Class Method Details

.last_procProc

Returns the last proc which was activated for the class via run. This also sets the last proc to nil so that calling this method multiple times will not return duplicates.

Returns:

  • (Proc)


62
63
64
65
66
# File 'lib/vagrant/config.rb', line 62

def self.last_proc
  value = @last_procs
  @last_procs = nil
  value
end

.run(&block) ⇒ Object

This is the method which is called by all Vagrantfiles to configure Vagrant. This method expects a block which accepts a single argument representing an instance of the Top class.

Note that the block is not run immediately. Instead, it's proc is stored away for execution later.



51
52
53
54
55
# File 'lib/vagrant/config.rb', line 51

def self.run(&block)
  # Store it for later
  @last_procs ||= []
  @last_procs << block
end

Instance Method Details

#load(env) ⇒ Object

Loads the added procs using the set load_order attribute and returns the Top object result. The configuration is loaded for the given Environment object.

Parameters:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/vagrant/config.rb', line 92

def load(env)
  config = Top.new(env)

  # Only run the procs specified in the load order, in the order
  # specified.
  load_order.each do |key|
    if @procs[key]
      @procs[key].each do |proc|
        proc.call(config) if proc
      end
    end
  end

  config
end

#set(key, path) ⇒ Object

Adds a Vagrantfile to be loaded to the queue of config procs. Note that this causes the Vagrantfile file to be loaded at this point, and it will never be loaded again.



82
83
84
85
# File 'lib/vagrant/config.rb', line 82

def set(key, path)
  return if @procs.has_key?(key)
  @procs[key] = [path].flatten.map(&method(:proc_for)).flatten
end