Module: EnvironmentConfigTransmogrifier

Defined in:
lib/environment_config_transmogrifier.rb

Overview

EnvironmentConfigTransmogrifier uses environment variables to generate config values for specific keys.

Defined Under Namespace

Classes: NoEscapeMustache

Constant Summary collapse

@@memoize_mustache =
{}

Class Method Summary collapse

Class Method Details

.extendReplace(hash, path) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/environment_config_transmogrifier.rb', line 84

def self.extendReplace(hash, path)
  # This code assumes that 'path' points to a directory of files.
  # The name of each file is the key into the hash, and the contents
  # of the file are the value to enter, as-is. The order of the
  # files in the directory does not matter.
  Dir.glob(File.join(path, '*')).each do |file|
    key   = File.basename(file)
    value = File.read(file)

    # Write the collected information to the hash. This will
    # overwrite, i.e. replace an existing value for that name. This
    # means that the values found in 'path' have priority over the
    # values already in the 'hash'. This is what we want for
    # '/etc/secrets'
    hash[key.upcase.tr('-', '_')] = value
  end
end

.inject_value(hash, key_grams, value, original_key) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/environment_config_transmogrifier.rb', line 102

def self.inject_value(hash, key_grams, value, original_key)
  # if we only have 1 gram, we can set the value
  if key_grams.size == 1
    # If the new value is nil, and the old value was a hash, it's because
    # somebody didn't provide a full default value; ignore it.
    unless value.nil? && hash[key_grams[0]].is_a?(Hash)
      hash[key_grams[0]] = value
    end
    return
  end

  # if there's more than one gram, keep going

  # Initialize a hash if we're going deeper than what's currently available;
  # sometimes we want to override when the old value is nil (i.e. no default)
  # but the new value is a hash.
  hash[key_grams[0]] = {} if hash[key_grams[0]].nil?
  # error out if we're trying to override
  # an existing value that's not a hash
  unless hash[key_grams[0]].is_a?(Hash)
    raise NonHashValueOverride, \
          "Refusing to override non-hash value #{hash[key_grams[0]].inspect} " \
          "with #{value.inspect}: '#{key_grams.join('.')}'" \
          " - Complete key: '#{original_key}'"
  end
  # keep going deeper
  inject_value(hash[key_grams[0]], key_grams.drop(1), value, original_key)
end

.mustacheMessageFromError(e) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/environment_config_transmogrifier.rb', line 69

def self.mustacheMessageFromError(e)
  lines = e.message.split(/\n/)
  return 'No reason for failure given by mustache library' if lines.empty?
  return lines.first if lines.size < 4

  caret_line = lines[3]
  caret_pos = caret_line.index('^')
  return lines[0] + '.' unless caret_pos

  delimiter = '{{=(( ))=}}'
  actual_line = lines[2]
  leading_junk = actual_line.lstrip.start_with?(delimiter) ? actual_line.index(delimiter) : 0
  lines[0] + ": Error at or near position #{caret_pos - leading_junk} of template value."
end

.processMustacheTemplate(value, input_hash, key) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/environment_config_transmogrifier.rb', line 52

def self.processMustacheTemplate(value, input_hash, key)
  val = @@memoize_mustache.fetch(value, {})[input_hash]
  return val if val

  delimiter = '{{=(( ))=}}'
  begin
    mustache_value = NoEscapeMustache.render("#{delimiter}#{value}", input_hash)
    # replace new lines with double new lines for proper new-line YAML parsing
    mustache_value = mustache_value.to_s.gsub("\n", "\n\n")
    @@memoize_mustache[value] ||= {}
    @@memoize_mustache[value][input_hash] = YAML.safe_load(mustache_value)
  rescue => e
    msg = mustacheMessageFromError(e)
    raise LoadYamlFromMustacheError, "Could not load config key '#{key}': #{msg}"
  end
end

.transmogrify(base_config, environment_templates, secrets: nil) ⇒ Hash

Processes the mustache templates and injects new keys into the configuration

Returns:

  • (Hash)

    Hash containing the updated configuration



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
49
50
# File 'lib/environment_config_transmogrifier.rb', line 22

def self.transmogrify(base_config, environment_templates, secrets: nil)
  # build input hash for mustache
  input_hash = ENV.to_hash

  # load secrets
  extendReplace(input_hash, secrets) if secrets && File.directory?(secrets)

  # remove empty values
  input_hash.reject! { |_, v| v.nil? || v.empty? }

  # iterate through templates
  environment_templates.each do |key, value|
    # generate value from template

    # we need to process the template at least once, even if it doesn't
    # actually contain a mustache template, to get value types correctly;
    # by default, all values are strings, because they come from the environment
    # we need to unmarshall them using YAML.load
    loop do
      value = processMustacheTemplate(value, input_hash, key)
      break unless value.respond_to?(:include?) && value.include?('((')
    end

    # inject value in huge json
    inject_value(base_config, key.split('.'), value, key)
  end

  base_config
end