Class: Enviera::Subcommands::Lookup
- Inherits:
-
Enviera::Subcommand
- Object
- Enviera::Subcommand
- Enviera::Subcommands::Lookup
- Defined in:
- lib/enviera/subcommands/lookup.rb
Class Method Summary collapse
Methods inherited from Enviera::Subcommand
all_options, attach_option, find, hidden?, load_config_file, parse, prettyname
Class Method Details
.description ⇒ Object
43 44 45 |
# File 'lib/enviera/subcommands/lookup.rb', line 43 def self.description "lookup hiera data for environment" end |
.execute ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/enviera/subcommands/lookup.rb', line 64 def self.execute begin environment = JSON.parse(Enviera::Options[:input_data]) rescue raise "Unable to parse input as valid JSON. Please ensure structure is proper." end keys_to_merge = Enviera::Options[:merged_keys].split(',') hiera_config = Hiera::Config.load(Enviera::Options[:hiera_config]) hiera= Hiera.new(:config=>hiera_config) keys = {} hiera.config[:backends].each do |backend| Hiera::Backend.datasources(environment) { |source| # Presently I'm only handling 'yaml' extensions. I hope someday this works for other stuff # But I haven't figured out a smart way to do this with the tools available # And it's not urgent for me sourcefile = Hiera::Backend.datafile(backend,environment,source,"yaml") or next source = YAML.load_file(sourcefile) source.each_key do |key| if !keys.has_key?(key) then keys[key] = source[key] elsif keys_to_merge.include?(key) then keys[key] = (keys[key] + source[key]).uniq end end } end keys = Hash[ keys.sort{ |v0, v1| v0[0] <=> v1[0] } ] case Enviera::Options[:output] when "block" keys when "json" keys.to_json when "yaml" keys.to_yaml else keys.to_yaml end end |
.options ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/enviera/subcommands/lookup.rb', line 13 def self. [{:name => :string, :description => "Source input is a json string provided as an argument", :short => 's', :type => :string}, {:name => :file, :description => "Source input is a regular json file", :short => 'f', :type => :string}, {:name => :stdin, :description => "Source input is taken from stdin (json string)", :short => :none}, {:name => :hiera_config, :description => "Path to your hiera config yaml file", :short => 'c', :type => :string, :default => '/etc/puppet/hiera.yaml'}, {:name => :merged_keys, :description => "Keys that should be merged (i.e. hiera_include style) separated by commas", :short => 'm', :type => :string, :default => ''}, {:name => :output, :description => "What format to output in. [json|yaml|block]", :short => 'o', :type => :string, :default => 'yaml'} ] end |
.validate(options) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/enviera/subcommands/lookup.rb', line 47 def self.validate sources = [:string, :file, :stdin].collect {|x| x if [x]}.compact Trollop::die "You must specify a source (I.E. -s '{}')" if sources.count.zero? Trollop::die "You can only specify one of (#{sources.join(', ')})" if sources.count > 1 [:source] = sources.first [:input_data] = case [:source] when :stdin STDIN.read when :string [:string] when :file File.read [:file] end end |