hiera-examiner 0.3.11 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/hiera-examiner +198 -12
  3. data/lib/hiera-examiner.rb +21 -14
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 27cfbdf5d39f17c19a04c2148e0c265265cff6c2
4
- data.tar.gz: 88f7b9160086c98d044ebff2cca705894e7db9a6
3
+ metadata.gz: 4c29d2f21e94a81e69792ddad6a67a760e91cb2e
4
+ data.tar.gz: 4ac6a723906532cac3a686178d97fa35cd51cc41
5
5
  SHA512:
6
- metadata.gz: da7165f4654e54e73c51c58c451abf754f4c3e62027802df246e91add7d12b633554df47612edc396537ee4e41178a410d10db21ea3c0a4d68ce8c299f69f2a5
7
- data.tar.gz: e6f5b6b525c490e94c921d0e3e8ca980edee6805152eba6e1dd923a0b43a8220b7229bbc6dac14571bb9d128f3cfcdedc292714913013e7611f770cdf2dabeea
6
+ metadata.gz: 8609583d93fdfc0af31d0cc8686e9cd670813b978d280223ce925ccf0be39f0e107dacca70bad4824aa2f5d28b6a2eaa2cca7e5cb8ab8620d224ceb19f8749cf
7
+ data.tar.gz: a2654674b75df2206b95fe28ef85763a8222a7390ecef7800f3ce3065fc35442743492b34873eb80040815f5544653f8b3e3756b4a9d23f125939a7806258468
data/bin/hiera-examiner CHANGED
@@ -1,17 +1,203 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ $LOAD_PATH.delete '.'
4
+
5
+ # CLI client for Hiera Examiner.
3
6
  require 'hiera-examiner'
7
+ require 'hiera/util'
8
+ require 'optparse'
9
+ require 'pp'
10
+
11
+ options = {
12
+ :default => nil,
13
+ :config => '/vagrant/puppet/hiera/hiera.yaml',
14
+ :scope => {},
15
+ :key => nil,
16
+ :verbose => false,
17
+ :resolution_type => :priority
18
+ }
19
+
20
+ # Loads the scope from YAML or JSON files
21
+ def load_scope(source, type=:yaml)
22
+ case type
23
+ when :mcollective
24
+ begin
25
+ require 'mcollective'
26
+
27
+ include MCollective::RPC
28
+
29
+ util = rpcclient("rpcutil")
30
+ util.progress = false
31
+ nodestats = util.custom_request("inventory", {}, source, {"identity" => source}).first
32
+
33
+ raise "Failed to retrieve facts for node #{source}: #{nodestats[:statusmsg]}" unless nodestats[:statuscode] == 0
34
+
35
+ scope = nodestats[:data][:facts]
36
+ rescue Exception => e
37
+ STDERR.puts "MCollective lookup failed: #{e.class}: #{e}"
38
+ exit 1
39
+ end
40
+
41
+ when :yaml
42
+ raise "Cannot find scope #{type} file #{source}" unless File.exist?(source)
43
+
44
+ require 'yaml'
45
+
46
+ # Attempt to load puppet in case we're going to be fed
47
+ # Puppet yaml files
48
+ begin
49
+ require 'puppet'
50
+ rescue
51
+ end
52
+
53
+ scope = YAML.load_file(source)
54
+
55
+ # Puppet makes dumb yaml files that do not promote data reuse.
56
+ scope = scope.values if scope.is_a?(Puppet::Node::Facts)
57
+
58
+ when :json
59
+ raise "Cannot find scope #{type} file #{source}" unless File.exist?(source)
60
+
61
+ require 'json'
62
+
63
+ scope = JSON.load(File.read(source))
64
+
65
+ when :inventory_service
66
+ # For this to work the machine running the hiera command needs access to
67
+ # /facts REST endpoint on your inventory server. This access is
68
+ # controlled in auth.conf and identification is by the certname of the
69
+ # machine running hiera commands.
70
+ #
71
+ # Another caveat is that if your inventory server isn't at the short dns
72
+ # name of 'puppet' you will need to set the inventory_sever option in
73
+ # your puppet.conf. Set it in either the master or main sections. It
74
+ # is fine to have the inventory_server option set even if the config
75
+ # doesn't have the fact_terminus set to rest.
76
+ begin
77
+ require 'puppet/util/run_mode'
78
+ $puppet_application_mode = Puppet::Util::RunMode[:master]
79
+ require 'puppet'
80
+ Puppet.settings.parse
81
+ Puppet::Node::Facts.indirection.terminus_class = :rest
82
+ scope = YAML.load(Puppet::Node::Facts.indirection.find(source).to_yaml)
83
+ # Puppet makes dumb yaml files that do not promote data reuse.
84
+ scope = scope.values if scope.is_a?(Puppet::Node::Facts)
85
+ rescue Exception => e
86
+ STDERR.puts "Puppet inventory service lookup failed: #{e.class}: #{e}"
87
+ exit 1
88
+ end
89
+ else
90
+ raise "Don't know how to load data type #{type}"
91
+ end
4
92
 
5
- if (ARGV[0])
6
- examiner = HieraExaminer.new()
7
- if (ARGV[1])
8
- examiner = HieraExaminer.new(ARGV[1])
9
- end
10
-
11
- examiner.explain(ARGV[0])
93
+ raise "Scope from #{type} file #{source} should be a Hash" unless scope.is_a?(Hash)
94
+
95
+ scope
96
+ end
97
+
98
+ OptionParser.new do |opts|
99
+ opts.banner = "Usage: hiera [options] key [default value] [variable='text'...]\n\nThe default value will be used if no value is found for the key. Scope variables\nwill be interpolated into %{variable} placeholders in the hierarchy and in\nreturned values.\n\n"
100
+
101
+ #opts.on("--version", "-V", "Version information") do
102
+ # puts Hiera.version
103
+ # exit
104
+ #end
105
+
106
+ opts.on("--debug", "-d", "Show debugging information") do
107
+ options[:verbose] = true
108
+ end
109
+
110
+ opts.on("--array", "-a", "Return all values as an array") do
111
+ options[:resolution_type] = :array
112
+ end
113
+
114
+ opts.on("--hash", "-h", "Return all values as a hash") do
115
+ options[:resolution_type] = :hash
116
+ end
117
+
118
+ opts.on("--config CONFIG", "-c", "Configuration file") do |v|
119
+ if File.exist?(v)
120
+ options[:config] = v
121
+ else
122
+ STDERR.puts "Cannot find config file: #{v}"
123
+ exit 1
124
+ end
125
+ end
126
+
127
+ opts.on("--json SCOPE", "-j", "JSON format file to load scope from") do |v|
128
+ begin
129
+ options[:scope] = load_scope(v, :json)
130
+ rescue Exception => e
131
+ STDERR.puts "Could not load JSON scope: #{e.class}: #{e}"
132
+ exit 1
133
+ end
134
+ end
135
+
136
+ opts.on("--yaml SCOPE", "-y", "YAML format file to load scope from") do |v|
137
+ begin
138
+ options[:scope] = load_scope(v)
139
+ rescue Exception => e
140
+ STDERR.puts "Could not load YAML scope: #{e.class}: #{e}"
141
+ exit 1
142
+ end
143
+ end
144
+
145
+ opts.on("--mcollective IDENTITY", "-m", "Use facts from a node (via mcollective) as scope") do |v|
146
+ begin
147
+ options[:scope] = load_scope(v, :mcollective)
148
+ rescue Exception => e
149
+ STDERR.puts "Could not load MCollective scope: #{e.class}: #{e}"
150
+ exit 1
151
+ end
152
+ end
153
+
154
+ opts.on("--inventory_service IDENTITY", "-i", "Use facts from a node (via Puppet's inventory service) as scope") do |v|
155
+ begin
156
+ options[:scope] = load_scope(v, :inventory_service)
157
+ rescue Exception => e
158
+ STDERR.puts "Could not load Puppet inventory service scope: #{e.class}: #{e}"
159
+ exit 1
160
+ end
161
+ end
162
+ end.parse!
163
+
164
+ # arguments can be:
165
+ #
166
+ # key default var=val another=val
167
+ #
168
+ # The var=val's assign scope
169
+ unless ARGV.empty?
170
+ options[:key] = ARGV.delete_at(0)
171
+
172
+ ARGV.each do |arg|
173
+ if arg =~ /^(.+?)=(.+?)$/
174
+ options[:scope][$1] = $2
175
+ else
176
+ unless options[:default]
177
+ options[:default] = arg.dup
178
+ else
179
+ STDERR.puts "Don't know how to parse scope argument: #{arg}"
180
+ end
181
+ end
182
+ end
12
183
  else
13
- puts 'hiera-examiner Usage:'
14
- puts 'hiera-examiner <key> [path]'
15
- puts '\tkey:\tHiera key to examine'
16
- puts '\tpath:\tPath to hiera config file to use. Default: /vagrant/puppet/hiera/hiera.yaml'
17
- end
184
+ STDERR.puts "Please supply a data item to look up"
185
+ exit 1
186
+ end
187
+
188
+ begin
189
+ hieraExaminer = HieraExaminer.new(options[:config])
190
+ rescue Exception => e
191
+ if options[:verbose]
192
+ raise
193
+ else
194
+ STDERR.puts "Failed to start HieraExaminer: #{e.class}: #{e}"
195
+ exit 1
196
+ end
197
+ end
198
+
199
+ unless options[:verbose]
200
+ #HieraExaminer.logger = "noop"
201
+ end
202
+
203
+ hieraExaminer.explain(options[:key], options[:scope])
@@ -5,14 +5,14 @@ class HieraExaminer
5
5
  require 'puppet'
6
6
  require 'colorize'
7
7
 
8
- def initialize(conf = "/vagrant/puppet/hiera/hiera.yaml")
9
- puts 'Conf = ' + conf
8
+ def initialize(conf)
9
+ puts conf
10
10
  @conf = conf
11
11
  end
12
12
 
13
- def yamlLookup(key)
14
- scope = YAML.load("")
15
- scope = {}
13
+ def yamlLookup(key, scope)
14
+ # scope = YAML.load("")
15
+ # scope = {}
16
16
  hiera = Hiera.new(:config => @conf)
17
17
  value = {val: nil, file: nil}
18
18
  Hiera::Backend.datasourcefiles('yaml', scope, 'yaml') { |source, file|
@@ -24,19 +24,26 @@ class HieraExaminer
24
24
  return value
25
25
  end
26
26
 
27
- def hieraLookup(key)
28
- scope = YAML.load("")
29
- scope = {}
27
+ def hieraLookup(key, scope)
28
+ # scope = YAML.load("")
29
+ # scope = {}
30
30
  hiera = Hiera.new(:config => @conf)
31
31
  puts "Looking up '#{key}' in '#{@conf}' with '#{scope}'"
32
32
  return hiera.lookup(key, @conf, scope)
33
33
  end
34
34
 
35
- def explain(key)
36
- puts "#{key.red}\n#{explainDetail(key, '', true)}"
35
+ def explain(key, scope)
36
+ puts 'key ' + (key or "UNKNOWN")
37
+ print 'scope '
38
+ puts scope
39
+
40
+ puts "#{key.red}\n#{explainDetail(key, scope, '', true)}"
37
41
  end
42
+ # def explain1(key)
43
+ # puts "#{key.red}\n#{explainDetail(key, '', true)}"
44
+ # end
38
45
 
39
- def explainDetail(key, prefix = '', showSource = false, yamlFileParam = '')
46
+ def explainDetail(key, scope, prefix = '', showSource = false, yamlFileParam = '')
40
47
  startExp = /%{hiera\('/
41
48
  endExp = /'\)}/
42
49
 
@@ -46,7 +53,7 @@ class HieraExaminer
46
53
  newIndex = firstIndex + 9
47
54
  endIndex = key.index(endExp) - 1
48
55
  padding = "#{prefix}#{' '.rjust((firstIndex + 2) - prefix.length)}"
49
- yamlMap = yamlLookup(key[newIndex..endIndex])
56
+ yamlMap = yamlLookup(key[newIndex..endIndex], scope)
50
57
  yamlVal = yamlMap[:val]
51
58
  yamlFile = yamlMap[:file]
52
59
 
@@ -81,8 +88,8 @@ class HieraExaminer
81
88
 
82
89
  return res
83
90
  else
84
- hieraVal = hieraLookup(key)
85
- yamlMap = yamlLookup(key)
91
+ hieraVal = hieraLookup(key, scope)
92
+ yamlMap = yamlLookup(key, scope)
86
93
  yamlVal = yamlMap[:val]
87
94
  yamlFile = yamlMap[:file]
88
95
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiera-examiner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.11
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Stortz