Class: Vagrant::Config::V2::Loader

Inherits:
Vagrant::Config::VersionBase show all
Defined in:
lib/vagrant/config/v2/loader.rb

Overview

This is the loader that handles configuration loading for V2 configurations.

Class Method Summary collapse

Class Method Details

.finalize(config) ⇒ Object

Finalizes the configuration by making sure there is at least one VM defined in it.


18
19
20
21
22
23
24
25
# File 'lib/vagrant/config/v2/loader.rb', line 18

def self.finalize(config)
  # Call the `#finalize` method on each of the configuration keys.
  # They're expected to modify themselves in our case.
  config.finalize!

  # Return the object
  config
end

.initV2::Root

Returns a bare empty configuration object.

Returns:


12
13
14
# File 'lib/vagrant/config/v2/loader.rb', line 12

def self.init
  new_root_object
end

.load(config_proc) ⇒ Object

Loads the configuration for the given proc and returns a configuration object.

Parameters:

  • config_proc (Proc)

Returns:

  • (Object)

32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vagrant/config/v2/loader.rb', line 32

def self.load(config_proc)
  # Create a root configuration object
  root = new_root_object

  # Call the proc with the root
  config_proc.call(root)

  # Return the root object, which doubles as the configuration object
  # we actually use for accessing as well.
  root
end

.merge(old, new) ⇒ V2::Root

Merges two configuration objects.

Parameters:

  • old (V2::Root)

    The older root config.

  • new (V2::Root)

    The newer root config.

Returns:


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/vagrant/config/v2/loader.rb', line 49

def self.merge(old, new)
  # Grab the internal states, we use these heavily throughout the process
  old_state = old.__internal_state
  new_state = new.__internal_state

  # Make sure we instantiate every key in the config so that we
  # merge every key. This avoids issues with the same reference
  # being part of the config.
  old_state["config_map"].each do |k, _|
    old.public_send(k)
  end
  new_state["config_map"].each do |k, _|
    new.public_send(k)
  end

  # The config map for the new object is the old one merged with the
  # new one.
  config_map = old_state["config_map"].merge(new_state["config_map"])

  # Merge the keys.
  old_keys = old_state["keys"]
  new_keys = new_state["keys"]
  keys     = {}
  old_keys.each do |key, old_value|
    if new_keys.key?(key)
      # We need to do a merge, which we expect to be available
      # on the config class itself.
      keys[key] = old_value.merge(new_keys[key])
    else
      # We just take the old value, but dup it so that we can modify.
      keys[key] = old_value.dup
    end
  end

  new_keys.each do |key, new_value|
    # Add in the keys that the new class has that we haven't merged.
    if !keys.key?(key)
      keys[key] = new_value.dup
    end
  end

  # Merge the missing keys
  new_missing_key_calls =
    old_state["missing_key_calls"] + new_state["missing_key_calls"]

  # Return the final root object
  V2::Root.new(config_map).tap do |result|
    result.__set_internal_state({
      "config_map"        => config_map,
      "keys"              => keys,
      "missing_key_calls" => new_missing_key_calls
    })
  end
end

.upgrade(old) ⇒ Array

Upgrade a V1 configuration to a V2 configuration. We do this by creating a V2 configuration, and calling "upgrade" on each of the V1 configurations, expecting them to set the right settings on the new root.

Parameters:

Returns:

  • (Array)

    A 3-tuple result.


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/vagrant/config/v2/loader.rb', line 111

def self.upgrade(old)
  # Get a new root
  root = new_root_object

  # Store the warnings/errors
  warnings = []
  errors   = []

  # Go through the old keys and upgrade them if they can be
  old.__internal_state["keys"].each do |_, old_value|
    if old_value.respond_to?(:upgrade)
      result = old_value.upgrade(root)

      # Sanity check to guard against random return values
      if result.is_a?(Array)
        warnings += result[0]
        errors   += result[1]
      end
    end
  end

  old.__internal_state["missing_key_calls"].to_a.sort.each do |key|
    warnings << I18n.t("vagrant.config.loader.bad_v1_key", key: key)
  end

  [root, warnings, errors]
end