Class: OctocatalogDiff::Facts
- Inherits:
-
Object
- Object
- OctocatalogDiff::Facts
- 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
Instance Method Summary collapse
- #dup ⇒ Object
-
#fact(key) ⇒ ?
Get the current value of a particular fact.
-
#facts(node = @node, timestamp = false) ⇒ Hash
Facts - returned the ‘cleansed’ facts.
-
#facts_to_yaml(node = @node) ⇒ String
Turn hash of facts into appropriate YAML for Puppet.
-
#fudge_timestamp ⇒ Object
Facts - Fudge the timestamp to right now and add include it in the facts when returned.
-
#initialize(options = {}, facts = nil) ⇒ Facts
constructor
Constructor.
-
#matching(regex) ⇒ Array<String>
Find all facts matching a particular pattern.
-
#node ⇒ String
Node - get the node name, either as set explicitly or as determined from the facts themselves.
-
#override(key, value) ⇒ Object
Override a particular fact.
-
#remove_fact_from_list(remove) ⇒ Object
Facts - remove a fact from the list.
-
#to_pson ⇒ String
Turn hash of facts into appropriate YAML for Puppet.
-
#without(remove) ⇒ Object
Facts - remove one or more facts from the list.
Constructor Details
#initialize(options = {}, facts = nil) ⇒ Facts
Constructor
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( = {}, facts = nil) @node = .fetch(:node, '') @timestamp = false @options = .dup if facts @facts = OctocatalogDiff::Util::Util.deep_dup(facts) else case [:backend] when :json @orig_facts = OctocatalogDiff::Facts::JSON.fact_retriever(, @node) when :yaml @orig_facts = OctocatalogDiff::Facts::Yaml.fact_retriever(, @node) when :puppetdb @orig_facts = OctocatalogDiff::Facts::PuppetDB.fact_retriever(, @node) else raise ArgumentError, 'Invalid fact source backend' end @facts = OctocatalogDiff::Util::Util.deep_dup(@orig_facts) end end |
Instance Method Details
#dup ⇒ Object
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
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.
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, = 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 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
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_timestamp ⇒ Object
Facts - Fudge the timestamp to right now and add include it in the facts when returned
74 75 76 77 |
# File 'lib/octocatalog-diff/facts.rb', line 74 def @timestamp = true self end |
#matching(regex) ⇒ Array<String>
Find all facts matching a particular pattern
135 136 137 |
# File 'lib/octocatalog-diff/facts.rb', line 135 def matching(regex) @facts['values'].keys.select { |fact| regex.match(fact) } end |
#node ⇒ String
Node - get the node name, either as set explicitly or as determined from the facts themselves.
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
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
91 92 93 |
# File 'lib/octocatalog-diff/facts.rb', line 91 def remove_fact_from_list(remove) @facts['values'].delete(remove) end |
#to_pson ⇒ String
Turn hash of facts into appropriate YAML for Puppet
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.
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 |