lib/hieracles/node.rb in hieracles-0.2.0 vs lib/hieracles/node.rb in hieracles-0.2.1

- old
+ new

@@ -6,34 +6,36 @@ module Hieracles class Node include Hieracles::Utils include Hieracles::Interpolate - attr_reader :hiera_params, :hiera + attr_reader :hiera_params, :hiera, :facts, :notifications def initialize(fqdn, options) + @fqdn = fqdn Config.load(options) @hiera = Hieracles::Hiera.new - @hiera_params = { fqdn: fqdn }. - merge(get_hiera_params(fqdn)). - merge(Config.scope). + @hiera_params = { fqdn: @fqdn }. + merge(get_hiera_params(@fqdn)). merge(Config.extraparams) - @fqdn = fqdn + @facts = deep_sort(@hiera_params. + merge(Config.scope). + merge(puppet_facts)) end def get_hiera_params(fqdn) - if File.exist?(File.join(Config.path('encpath'), "#{fqdn}.yaml")) - load = YAML.load_file(File.join(Config.path('encpath'), "#{fqdn}.yaml")) + @__hiera_params ||= if File.exist?(File.join(Config.encpath, "#{fqdn}.yaml")) + load = YAML.load_file(File.join(Config.encpath, "#{fqdn}.yaml")) sym_keys(load['parameters']).merge({ classes: load['classes']}) else raise "Node not found" end end def files(without_common = true) @__files ||= @hiera.hierarchy.reduce([]) do |a, f| - file = parse("#{f}.yaml", @hiera_params, Config.interactive) + file = parse("#{f}.yaml", @facts, Config.interactive) if file && File.exist?(File.join(@hiera.datapath, file)) && (!without_common || !file[/common/]) a << File.join(@hiera.datadir, file) @@ -70,10 +72,14 @@ end deep_sort(params) end def modules + @_modules ||= _get_modules + end + + def _get_modules modules = {} classfiles.each do |c| if File.exist?(c) f = File.open(c, "r") f.each_line do |line| @@ -86,16 +92,24 @@ end modules end def info - @hiera_params + @_info ||= _get_info end + def _get_info + extra = {} + if Config.usedb + extra = puppetdb_info + end + @hiera_params.merge extra + end + def classfiles @hiera_params[:classes].map do |cl| - format(Config.path('classpath'), cl) + format(Config.classpath, cl) end end def modulepath(path) File.join(Config.modulepath, path) @@ -112,10 +126,33 @@ end end modules end + def puppetdb_info + request_db.node_info(@fqdn).data + end + + def puppet_facts + if Config.usedb + resp = request_db.node_facts(@fqdn) + @notifications = resp.notifications + if resp.total_records > 0 + resp.data + else + error "not found in puppetdb." + {} + end + else + {} + end + end + + def request_db + @_request ||= Hieracles::Puppetdb::Request.new Config.puppetdb + end + def merge_trees(left, right) case @hiera.merge_behavior when :deeper left.deep_merge!(right) when :deep @@ -141,9 +178,17 @@ deep_sort(value) end end else value + end + end + + def error(message) + if @notifications + @notifications << Notification.new('node', message, 'error') + else + @notifications = [ Notification.new('node', message, 'error') ] end end end end