Module: PuppetSpecFacts

Defined in:
lib/puppet_spec_facts.rb,
lib/puppet_spec_facts/version.rb

Overview

you can generate appropriate json files with: facter –json > filename.json then dump the file in the facts directory with the .json extension. If you’re cool you’ll use facter –json | jq . > filename.json to prettyprint it

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.fact_style(fact_hash) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/puppet_spec_facts.rb', line 134

def self.fact_style(fact_hash)
  if SemVer.new(fact_hash['facterversion']) > SemVer.new('2.0.0')
    return 'structured'  if fact_hash['system_uptime'].is_a?(Hash)
    return 'stringified' if fact_hash['system_uptime'] == nil
  elsif SemVer.new(fact_hash['facterversion']) < SemVer.new('2.0.0')
    return 'stringified' if fact_hash['system_uptime'] == nil
  end
  raise('cannot determine whether fact hash is structured or stringified')
end

.facts_for_platform_by_fact(select_facts = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/puppet_spec_facts.rb', line 47

def self.facts_for_platform_by_fact(select_facts={})
  puppet_platforms.select { |platform, facts|
    match = true
    select_facts.each { |query_fact, query_value|
      match = false if facts[query_fact] != query_value
    }
    match
  }
end

.facts_for_platform_by_name(platforms) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/puppet_spec_facts.rb', line 57

def self.facts_for_platform_by_name(platforms)
  platforms = [platforms] if platforms.is_a?(String)
  results = {}
  platforms.each do |name|
    results[:"#{name}"] = puppet_platforms[name]
  end
  results
end

.get_jsonfile(filename) ⇒ Object



144
145
146
# File 'lib/puppet_spec_facts.rb', line 144

def self.get_jsonfile(filename)
  JSON.parse(File.read(filename))
end

.is_pe(fact_hash) ⇒ Object



114
115
116
117
118
# File 'lib/puppet_spec_facts.rb', line 114

def self.is_pe(fact_hash)
  return false unless fact_hash['puppetversion']
  return true if fact_hash['puppetversion'].downcase.include?('puppet enterprise')
  false
end

.make_sane_name(fact_hash) ⇒ Object

private



103
104
105
106
107
108
109
110
111
112
# File 'lib/puppet_spec_facts.rb', line 103

def self.make_sane_name(fact_hash)
  osname = name_osname(fact_hash)
  codename = name_codename(fact_hash)
  version = name_version(fact_hash)
  architecture = fact_hash['architecture']
  puppet_version = puppet_name(fact_hash)
  fact_style = fact_style(fact_hash)

  return [osname, codename, version, architecture, puppet_version, fact_style].compact.join('_')
end

.name_codename(fact_hash) ⇒ Object



97
98
99
100
# File 'lib/puppet_spec_facts.rb', line 97

def self.name_codename(fact_hash)
  return fact_hash['lsbdistcodename'] unless ['RedHat','Gentoo'].include?(fact_hash['osfamily'])
  nil
end

.name_osname(fact_hash) ⇒ Object



84
85
86
87
88
89
# File 'lib/puppet_spec_facts.rb', line 84

def self.name_osname(fact_hash)
  osname ||= fact_hash['lsbdistid']
  osname ||= fact_hash['operatingsystem']
  osname ||= fact_hash['kernel']
  osname ||= fact_hash['osfamily']
end

.name_version(fact_hash) ⇒ Object



91
92
93
94
95
# File 'lib/puppet_spec_facts.rb', line 91

def self.name_version(fact_hash)
  version = fact_hash['lsbdistrelease']
  version ||= fact_hash['macosx_productversion']
  version ||= fact_hash['operatingsystemrelease']
end

.overload_facts(fact_hash) ⇒ Object

inject some additional facts to simplify lookups from rspec



31
32
33
34
35
36
37
# File 'lib/puppet_spec_facts.rb', line 31

def self.overload_facts(fact_hash)
  fact_hash['fact_style']     = fact_style(fact_hash)
  fact_hash['is_pe']          = 'true' if is_pe(fact_hash)
  fact_hash['concat_basedir'] = '/fake_concat_basedir'
  fact_hash['path']           = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
  return fact_hash
end

.puppet_name(fact_hash) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/puppet_spec_facts.rb', line 120

def self.puppet_name(fact_hash)
  if is_pe(fact_hash)
    version = fact_hash['puppetversion'].delete('()').split[3]
    return "PE-#{version}"
  else
    return fact_hash['puppetversion']
  end
end

.puppet_platform_namesObject



39
40
41
42
43
44
45
# File 'lib/puppet_spec_facts.rb', line 39

def self.puppet_platform_names
  platform_names = []
  puppet_platforms.each do |name, value|
    platform_names << name
  end
  platform_names
end

.puppet_platformsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/puppet_spec_facts.rb', line 15

def self.puppet_platforms
  platforms = {}
  file_list = Dir.glob("#{@proj_root}/**/*.json")
  file_list.each do |filename|
    fact_hash = get_jsonfile(filename)
    platform_name = make_sane_name(fact_hash)

    if platforms[platform_name] != nil
      next
    end
    platforms[platform_name] = overload_facts(fact_hash)
  end
  platforms
end

.resave_all_factsObject



66
67
68
69
70
# File 'lib/puppet_spec_facts.rb', line 66

def self.resave_all_facts
  puppet_platforms.each do |name, hash|
    save_reorganized_facts_to_json(hash)
  end
end

.save_reorganized_facts_to_json(fact_hash = nil, dir = 'facts') ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/puppet_spec_facts.rb', line 72

def self.save_reorganized_facts_to_json(fact_hash=nil, dir='facts')
  namepath = name_osname(fact_hash)
  save_path = "#{@proj_root}/#{dir}/#{namepath}"
  filename = make_sane_name(fact_hash) + '.json'
  json_output = JSON.pretty_generate(fact_hash)
  FileUtils.mkdir_p save_path

  File.open("#{save_path}/#{filename}","w") do |f|
    f.write(json_output)
  end
end