bin/hiera in hiera-0.1.1 vs bin/hiera in hiera-0.2.0

- old
+ new

@@ -13,48 +13,67 @@ require 'rubygems' require 'hiera' require 'optparse' +options = {:default => nil, :config => "/etc/hiera.yaml", :scope => {}, :key => nil, :verbose => false, :resolution_type => :priority} -options = {:default => nil, :config => "/etc/hiera.yaml", :scope => {}, :key => nil, :verbose => false} - class Hiera::Noop_logger class << self def warn(msg);end def debug(msg);end end end # Loads the scope from YAML or JSON files -def load_scope(file, type=:yaml) - raise "Cannot find scope #{type} file #{file}" unless File.exist?(file) - +def load_scope(source, type=:yaml) case type + when :mcollective + begin + require 'mcollective' + + include MCollective::RPC + + util = rpcclient("rpcutil") + util.progress = false + nodestats = util.custom_request("inventory", {}, source, {"identity" => source}).first + + raise "Failed to retrieve facts for node #{source}: #{nodestats[:statusmsg]}" unless nodestats[:statuscode] == 0 + + scope = nodestats[:data][:facts] + rescue Exception => e + STDERR.puts "MCollective lookup failed: #{e.class}: #{e}" + exit 1 + end + when :yaml + raise "Cannot find scope #{type} file #{source}" unless File.exist?(source) + require 'yaml' # Attempt to load puppet in case we're going to be fed # Puppet yaml files begin require 'puppet' rescue end - scope = YAML.load_file(file) + scope = YAML.load_file(source) # Puppet makes dumb yaml files that do not promote data reuse. scope = scope.values if scope.is_a?(Puppet::Node::Facts) when :json + raise "Cannot find scope #{type} file #{source}" unless File.exist?(source) + require 'json' - scope = JSON.load(File.read(file)) + scope = JSON.load(File.read(source)) else raise "Don't know how to load data type #{type}" end - raise "Scope from #{type} file #{file} should be a Hash" unless scope.is_a?(Hash) + raise "Scope from #{type} file #{source} should be a Hash" unless scope.is_a?(Hash) scope end OptionParser.new do |opts| @@ -65,10 +84,14 @@ opts.on("--debug", "-d", "Show debugging information") do options[:verbose] = true end + opts.on("--array", "-a", "Array search") do + options[:resolution_type] = :array + end + opts.on("--config CONFIG", "-c", "Configuration file") do |v| if File.exist?(v) options[:config] = v else STDERR.puts "Cannot find config file: #{v}" @@ -85,16 +108,25 @@ end end opts.on("--yaml SCOPE", "-y", "YAML format file to load scope from") do |v| begin - options[:scope] = load_scope(v) + options[:scope] = load_scope(v, :json) rescue Exception => e STDERR.puts "Could not load YAML scope: #{e.class}: #{e}" exit 1 end end + + opts.on("--mcollective IDENTITY", "-m", "Retrieve facts from a node via mcollective as scope") do |v| + begin + options[:scope] = load_scope(v, :mcollective) + rescue Exception => e + STDERR.puts "Could not load MCollective scope: #{e.class}: #{e}" + exit 1 + end + end end.parse! # arguments can be: # # key default var=val another=val @@ -132,10 +164,10 @@ unless options[:verbose] Hiera.logger = "noop" end -ans = hiera.lookup(options[:key], options[:default], options[:scope]) +ans = hiera.lookup(options[:key], options[:default], options[:scope], nil, options[:resolution_type]) if ans.is_a?(String) puts ans else p ans