module DopCommon::Node::Config

Public Instance Methods

config(variable) click to toggle source
# File lib/dop_common/node/config.rb, line 17
def config(variable)
  resolve_external(variable) || resolve_internal(variable)
end
config_includes?(variable, pattern) click to toggle source
# File lib/dop_common/node/config.rb, line 25
def config_includes?(variable, pattern)
  [config(variable)].flatten.any?{|v| pattern_match?(v, pattern)}
end
fact(variable) click to toggle source
# File lib/dop_common/node/config.rb, line 29
def fact(variable)
  scope[ensure_global_namespace(variable)]
end
has_config?(variable, pattern) click to toggle source
# File lib/dop_common/node/config.rb, line 21
def has_config?(variable, pattern)
  pattern_match?(config(variable), pattern)
end
has_fact?(variable, pattern) click to toggle source
# File lib/dop_common/node/config.rb, line 33
def has_fact?(variable, pattern)
  pattern_match?(fact(variable), pattern)
end
has_name?(pattern) click to toggle source
# File lib/dop_common/node/config.rb, line 13
def has_name?(pattern)
  pattern_match?(name, pattern)
end
has_role?(pattern) click to toggle source
# File lib/dop_common/node/config.rb, line 41
def has_role?(pattern)
  pattern_match?(role, pattern)
end
role() click to toggle source
# File lib/dop_common/node/config.rb, line 37
def role
  config(DopCommon.config.role_variable) || role_default
end

Private Instance Methods

basic_scope() click to toggle source
# File lib/dop_common/node/config.rb, line 54
def basic_scope
  @basic_scope ||= {
    '::fqdn'       => fqdn,
    '::clientcert' => fqdn,
    '::hostname'   => hostname,
    '::domain'     => domainname
  }
end
ensure_global_namespace(fact) click to toggle source
# File lib/dop_common/node/config.rb, line 74
def ensure_global_namespace(fact)
  fact =~ /^::/ ? fact : '::' + fact
end
facts() click to toggle source
# File lib/dop_common/node/config.rb, line 63
def facts
  return {} unless DopCommon.config.load_facts
  facts_yaml = File.join(DopCommon.config.facts_dir, fqdn + '.yaml')
  if File.exists? facts_yaml
    YAML.load_file(facts_yaml).values
  else
    DopCommon.log.warn("No facts found for node #{name} at #{facts_yaml}")
    {}
  end
end
hiera() click to toggle source
# File lib/dop_common/node/config.rb, line 83
def hiera
  @@mutex_hiera.synchronize do
    # Create a new Hiera object if the config has changed
    unless DopCommon.config.hiera_yaml == @@hiera_config
      DopCommon.log.debug("Hiera config location changed from #{@@hiera_config.to_s} to #{DopCommon.config.hiera_yaml.to_s}")
      @@hiera_config = DopCommon.config.hiera_yaml
      config = {}
      if File.exists?(@@hiera_config)
        config = YAML.load_file(@@hiera_config)
      else
        DopCommon.log.error("Hiera config #{@@hiera_config} not found! Using empty config")
      end
      # set the plan_store defaults
      config[:dop] ||= { }
      unless config[:dop].has_key?(:plan_store_dir)
        config[:dop][:plan_store_dir] = DopCommon.config.plan_store_dir
      end
      config[:logger] = 'dop'
      @@hiera = Hiera.new(:config => config)
    end
  end
  @@hiera
end
pattern_match?(value, pattern) click to toggle source
# File lib/dop_common/node/config.rb, line 47
def pattern_match?(value, pattern)
  case pattern
  when Regexp then value =~ pattern
  else value == pattern
  end
end
resolve_external(variable) click to toggle source

this will try to resolve the variable over hiera directly

# File lib/dop_common/node/config.rb, line 146
def resolve_external(variable)
  return nil unless DopCommon.config.use_hiera
  @@mutex_lookup.synchronize do
    begin hiera.lookup(variable, nil, scope)
    rescue Psych::SyntaxError => e
      DopCommon.log.error("YAML parsing error in hiera data. Make sure you hiera yaml files are valid")
      nil
    end
  end
end
resolve_internal(variable) click to toggle source

This will try to resolve the config variable from the plan configuration hash. This is needed in case the plan is not yet added to the plan cache (in case of validation) and hiera can't resolve it over the plugin, but we still need the information about the node config.

# File lib/dop_common/node/config.rb, line 120
def resolve_internal(variable)
  return nil unless DopCommon.config.use_hiera
  @@mutex_lookup.synchronize do
    begin
      hiera # make sure hiera is initialized
      answer = nil
      Hiera::Backend.datasources(scope) do |source|
        DopCommon.log.debug("Hiera internal: Looking for data source #{source}")
        data = nil
        begin
          data = @parsed_configuration.lookup(source, variable, scope)
        rescue DopCommon::ConfigurationValueNotFound
          next
        else
          break if answer = Hiera::Backend.parse_answer(data, scope)
        end
      end
    rescue StandardError => e
      DopCommon.log.debug(e.message)
    end
    DopCommon.log.debug("Hiera internal: answer for variable #{variable} : #{answer}")
    return answer
  end
end
role_default() click to toggle source
# File lib/dop_common/node/config.rb, line 107
def role_default
  if DopCommon.config.role_default
    DopCommon.config.role_default
  else
    DopCommon.log.warn("No role found for #{name} and no default role defined.")
    '-'
  end
end
scope() click to toggle source
# File lib/dop_common/node/config.rb, line 78
def scope
  merged_scope = basic_scope.merge(facts)
  Hash[merged_scope.map {|fact,value| [ensure_global_namespace(fact), value ]}]
end