Class: HieraSimulator::FactSource::Util
- Inherits:
-
Object
- Object
- HieraSimulator::FactSource::Util
- Defined in:
- lib/hiera-simulator/fact_source/util.rb
Overview
Some handy methods that may be used by multiple fact sources
Class Method Summary collapse
-
.modify_and_load_jsonfile(filepath) ⇒ Hash
A typical fact file (e.g., from PuppetDB) looks like this: { “key1”: “value1”, “key2”: “value2” } This reads the fact file and returns a hash with the facts in the proper format.
-
.modify_and_load_yamlfile(filepath) ⇒ Hash
A typical Puppet YAML fact file looks like this: “‘ — !ruby/object:Puppet::Node::Facts name: your.fqdn.com values: key1: val1 key2: val2 “` To avoid loading in all of Puppet just to parse what is basically a plain YAML file, this method strips off the first line and un-indents all of the remaining lines to make it a plain YAML file, and then parses that.
-
.stringify(name, value, prior = '::' + name) ⇒ Array<[name, value]>
Stringify key-value, needed for some older versions of Puppet and hiera.
Class Method Details
.modify_and_load_jsonfile(filepath) ⇒ Hash
A typical fact file (e.g., from PuppetDB) looks like this:
"key1": "value1",
"key2": "value2"
This reads the fact file and returns a hash with the facts in the proper format.
44 45 46 47 48 49 |
# File 'lib/hiera-simulator/fact_source/util.rb', line 44 def self.modify_and_load_jsonfile(filepath) data = JSON.parse(File.read(filepath)) result = {} data.each { |k, v| result['::' + k] = v } result end |
.modify_and_load_yamlfile(filepath) ⇒ Hash
A typical Puppet YAML fact file looks like this: “‘ — !ruby/object:Puppet::Node::Facts
name: your.fqdn.com
values:
key1: val1
key2: val2
“‘ To avoid loading in all of Puppet just to parse what is basically a plain YAML file, this method strips off the first line and un-indents all of the remaining lines to make it a plain YAML file, and then parses that.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/hiera-simulator/fact_source/util.rb', line 21 def self.modify_and_load_yamlfile(filepath) content = File.read(filepath).split(/[\r\n]+/) first_line = content.shift unless first_line == '--- !ruby/object:Puppet::Node::Facts' raise HieraSimulator::FactSourceError, "#{filepath} was not in expected Puppet::Node::Facts format" end content.unshift '---' fixed_yaml = content.map { |line| line.sub(/^ /, '') } data = YAML.load(fixed_yaml.join("\n")) raise HieraSimulator::FactLookupError, "Invalid content in node fact file #{filepath}" unless data.key?('values') result = {} data['values'].each { |k, v| result['::' + k] = v } result end |
.stringify(name, value, prior = '::' + name) ⇒ Array<[name, value]>
Stringify key-value, needed for some older versions of Puppet and hiera
56 57 58 59 60 61 62 63 |
# File 'lib/hiera-simulator/fact_source/util.rb', line 56 def self.stringify(name, value, prior = '::' + name) return [[ prior.sub(/::$/, ''), value ]] unless value.is_a?(Hash) result = [] value.each do |k, v| result.concat stringify(k, v, prior + '::' + k) end result end |