Class: Vcloud::Core::ConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/vcloud/core/config_loader.rb

Instance Method Summary collapse

Instance Method Details

#load_config(config_file, schema = nil, vars_file = nil) ⇒ Hash

Loads the configuration from config_file, optionally rendering config_file as a Mustache template using vars in vars_file and optionally validating config against schema supplied.

Parameters:

  • config_file (String)

    Location of the YAML config file

  • schema (String, nil) (defaults to: nil)

    Location of the validation schema if nil, no validation takes place.

  • vars_file (String, nil) (defaults to: nil)

    Location of the vars_file (YAML), if nil, config_file is not rendered by Mustache

Returns:

  • (Hash)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vcloud/core/config_loader.rb', line 19

def load_config(config_file, schema = nil, vars_file = nil)
  if vars_file
    rendered_config = Mustache.render(
      File.read(config_file),
      YAML::load_file(vars_file)
    )
    input_config = YAML::load(rendered_config)
  else
    input_config = YAML::load_file(config_file)
  end

  # There is no way in YAML or Ruby to symbolize keys in a hash
  json_string = JSON.generate(input_config)
  config = JSON.parse(json_string, :symbolize_names => true)

  if schema
    validation = Core::ConfigValidator.validate(:base, config, schema)
    validation.warnings.each do |warning|
      Vcloud::Core.logger.warn(warning)
    end
    unless validation.valid?
      validation.errors.each do |error|
        Vcloud::Core.logger.fatal(error)
      end
      raise("Supplied configuration does not match supplied schema")
    end
  end

  config
end