Class: OctocatalogDiff::Facts

Inherits:
Object
  • Object
show all
Defined in:
lib/octocatalog-diff/facts.rb,
lib/octocatalog-diff/facts/json.rb,
lib/octocatalog-diff/facts/yaml.rb,
lib/octocatalog-diff/facts/puppetdb.rb

Overview

Deal with facts in all forms, including:

  • In existing YAML files

  • In existing JSON files

  • Retrieved dynamically from PuppetDB

Defined Under Namespace

Classes: JSON, PuppetDB, Yaml

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, facts = nil) ⇒ Facts

Constructor

Parameters:

  • options (Hash) (defaults to: {})

    Initialization options, varies per backend



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Method Details

#dupObject



39
40
41
# File 'lib/octocatalog-diff/facts.rb', line 39

def dup
  self.class.new(@options, @orig_facts)
end

#fact(key) ⇒ ?

Get the current value of a particular fact

Parameters:

  • key (String)

    Fact key to override

Returns:

  • (?)

    Value for fact



117
118
119
# File 'lib/octocatalog-diff/facts.rb', line 117

def fact(key)
  @facts['values'][key]
end

#facts(node = @node, timestamp = false) ⇒ Hash

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.

Parameters:

  • node (String) (defaults to: @node)

    Node name to override returned facts

Returns:

  • (Hash)

    Facts hash { ‘name’ => ‘…’, ‘values’ => { … } }



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ String

Turn hash of facts into appropriate YAML for Puppet

Parameters:

  • node (String) (defaults to: @node)

    Node name to override returned facts

Returns:

  • (String)

    Puppet-compatible YAML facts



98
99
100
101
102
103
104
105
# 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_timestampObject

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

Returns:

  • self



74
75
76
77
# File 'lib/octocatalog-diff/facts.rb', line 74

def fudge_timestamp
  @timestamp = true
  self
end

#matching(regex) ⇒ Array<String>

Find all facts matching a particular pattern

Parameters:

  • regex (Regexp)

    Regular expression to match

Returns:

  • (Array<String>)

    Facts that match the regexp



135
136
137
# File 'lib/octocatalog-diff/facts.rb', line 135

def matching(regex)
  @facts['values'].keys.select { |fact| regex.match(fact) }
end

#nodeString

Node - get the node name, either as set explicitly or as determined from the facts themselves.

Returns:

  • (String)

    Node name, explicit or guessed



45
46
47
48
49
50
# 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) ⇒ Object

Override a particular fact

Parameters:

  • key (String)

    Fact key to override

  • value (?)

    Value for fact



124
125
126
127
128
129
130
# 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) ⇒ Object

Facts - remove a fact from the list

Parameters:

  • remove (String)

    Fact to remove



91
92
93
# File 'lib/octocatalog-diff/facts.rb', line 91

def remove_fact_from_list(remove)
  @facts['values'].delete(remove)
end

#to_psonString

Turn hash of facts into appropriate YAML for Puppet

Parameters:

  • node (String)

    Node name to override returned facts

Returns:

  • (String)

    Puppet-compatible YAML facts



110
111
112
# File 'lib/octocatalog-diff/facts.rb', line 110

def to_pson
  PSON.generate(facts)
end

#without(remove) ⇒ Object

Facts - remove one or more facts from the list.

Parameters:

Returns:

  • self



82
83
84
85
86
87
# 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