class OctocatalogDiff::Facts

Deal with facts in all forms, including:

Public Class Methods

new(options = {}, facts = nil) click to toggle source

Constructor @param options [Hash] Initialization options, varies per backend

# File lib/octocatalog-diff/facts.rb, line 18
def initialize(options = {}, facts = nil)
  @node = options.fetch(:node, '')
  @timestamp = false
  @options = options.dup
  if facts
    @facts = OctocatalogDiff::Util::Util.deep_dup(facts)
  else
    case options[:backend]
    when :json
      @orig_facts = OctocatalogDiff::Facts::JSON.fact_retriever(options, @node)
    when :yaml
      @orig_facts = OctocatalogDiff::Facts::Yaml.fact_retriever(options, @node)
    when :puppetdb
      @orig_facts = OctocatalogDiff::Facts::PuppetDB.fact_retriever(options, @node)
    else
      raise ArgumentError, 'Invalid fact source backend'
    end
    @facts = OctocatalogDiff::Util::Util.deep_dup(@orig_facts)
  end
end

Public Instance Methods

dup() click to toggle source
# File lib/octocatalog-diff/facts.rb, line 39
def dup
  self.class.new(@options, @orig_facts)
end
fact(key) click to toggle source

Get the current value of a particular fact @param key [String] Fact key to override @return [?] Value for fact

# File lib/octocatalog-diff/facts.rb, line 117
def fact(key)
  @facts['values'][key]
end
facts(node = @node, timestamp = false) click to toggle source

Facts - returned the 'cleansed' facts. Clean up facts by setting 'name' to the node if given, and deleting _timestamp and expiration which may cause Puppet catalog compilation to fail if the facts are old. @param node [String] Node name to override returned facts @return [Hash] Facts hash { 'name' => '…', 'values' => { … } }

# File lib/octocatalog-diff/facts.rb, line 57
def facts(node = @node, timestamp = false)
  raise "Expected @facts to be a hash but it is a #{@facts.class}" unless @facts.is_a?(Hash)
  raise "Expected @facts['values'] to be a hash but it is a #{@facts['values'].class}" unless @facts['values'].is_a?(Hash)
  f = @facts.dup
  f['name'] = node unless node.nil? || node.empty?
  f['values'].delete('_timestamp')
  f.delete('expiration')
  if timestamp
    f['timestamp'] = Time.now.to_s
    f['values']['timestamp'] = f['timestamp']
    f['expiration'] = (Time.now + (24 * 60 * 60)).to_s
  end
  f
end
facts_to_yaml(node = @node) click to toggle source

Turn hash of facts into appropriate YAML for Puppet @param node [String] Node name to override returned facts @return [String] Puppet-compatible YAML facts

# File lib/octocatalog-diff/facts.rb, line 98
def facts_to_yaml(node = @node)
  # Add the header that Puppet needs to treat this as facts. Save the results
  # as a string in the option.
  f = facts(node)
  fact_file = f.to_yaml.split(/\n/)
  fact_file[0] = '--- !ruby/object:Puppet::Node::Facts' if fact_file[0] =~ /^---/
  fact_file.join("\n")
end
fudge_timestamp() click to toggle source

Facts - Fudge the timestamp to right now and add include it in the facts when returned @return self

# File lib/octocatalog-diff/facts.rb, line 74
def fudge_timestamp
  @timestamp = true
  self
end
matching(regex) click to toggle source

Find all facts matching a particular pattern @param regex [Regexp] Regular expression to match @return [Array<String>] Facts that match the regexp

# File lib/octocatalog-diff/facts.rb, line 135
def matching(regex)
  @facts['values'].keys.select { |fact| regex.match(fact) }
end
node() click to toggle source

Node - get the node name, either as set explicitly or as determined from the facts themselves. @return [String] Node name, explicit or guessed

# File lib/octocatalog-diff/facts.rb, line 45
def node
  return @node unless @node.nil? || @node.empty?
  return facts['name'] if facts.key?('name')
  return facts['values']['fqdn'] if facts.key?('values') && facts['values'].key?('fqdn')
  ''
end
override(key, value) click to toggle source

Override a particular fact @param key [String] Fact key to override @param value [?] Value for fact

# File lib/octocatalog-diff/facts.rb, line 124
def override(key, value)
  if value.nil?
    @facts['values'].delete(key)
  else
    @facts['values'][key] = value
  end
end
remove_fact_from_list(remove) click to toggle source

Facts - remove a fact from the list @param remove [String] Fact to remove

# File lib/octocatalog-diff/facts.rb, line 91
def remove_fact_from_list(remove)
  @facts['values'].delete(remove)
end
to_pson() click to toggle source

Turn hash of facts into appropriate YAML for Puppet @param node [String] Node name to override returned facts @return [String] Puppet-compatible YAML facts

# File lib/octocatalog-diff/facts.rb, line 110
def to_pson
  PSON.generate(facts)
end
without(remove) click to toggle source

Facts - remove one or more facts from the list. @param remove [String|Array<String>] Fact(s) to remove @return self

# File lib/octocatalog-diff/facts.rb, line 82
def without(remove)
  r = remove.is_a?(Array) ? remove : [remove]
  obj = dup
  r.each { |fact| obj.remove_fact_from_list(fact) }
  obj
end