Class: Enviera::Subcommands::Hierarchy

Inherits:
Enviera::Subcommand show all
Defined in:
lib/enviera/subcommands/hierarchy.rb

Class Method Summary collapse

Methods inherited from Enviera::Subcommand

all_options, attach_option, find, hidden?, load_config_file, parse, prettyname

Class Method Details

.descriptionObject



38
39
40
# File 'lib/enviera/subcommands/hierarchy.rb', line 38

def self.description
  "display the hierarchy in use with the given environment"
end

.executeObject



59
60
61
62
63
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
# File 'lib/enviera/subcommands/hierarchy.rb', line 59

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
  hiera_config = Hiera::Config.load(Enviera::Options[:hiera_config])
  hiera= Hiera.new(:config=>hiera_config)
  sources = []
  hiera.config[:backends].each do |backend|
    sources.push("backend: "+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
      sources.push(sourcefile)
    }
  end
  sources = {'hierarchy' => sources}
  case Enviera::Options[:output]
  when "block"
    sources
  when "json"
    sources.to_json
  when "yaml"
    sources.to_yaml
  else
    sources.to_yaml
  end
end

.optionsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/enviera/subcommands/hierarchy.rb', line 13

def self.options
  [{: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 => :output,
    :description => "What format to output in. [json|yaml|block]",
    :short => 'o',
    :type => :string,
    :default => 'yaml'}
  ]
end

.validate(options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/enviera/subcommands/hierarchy.rb', line 42

def self.validate options
  sources = [:string, :file, :stdin].collect {|x| x if options[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
  options[:source] = sources.first

  options[:input_data] = case options[:source]
  when :stdin
    STDIN.read
  when :string
    options[:string]
  when :file
    File.read options[:file]
  end
  options
end