Class: SaltHiera::SaltHiera
- Inherits:
-
Object
- Object
- SaltHiera::SaltHiera
- Defined in:
- lib/salt_hiera/salt_hiera.rb
Constant Summary collapse
- VERSION =
"0.3.1"
Instance Method Summary collapse
- #debug_dict(prefix, dict) ⇒ Object
- #find_plugin(plugin_name) ⇒ Object
-
#initialize(attributes) ⇒ SaltHiera
constructor
A new instance of SaltHiera.
- #to_yaml ⇒ Object
Constructor Details
#initialize(attributes) ⇒ SaltHiera
Returns a new instance of SaltHiera.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/salt_hiera/salt_hiera.rb', line 11 def initialize attributes raise "Config error (#{params})" unless attributes[:config_file] raise "Config file (#{attributes[:config_file]}) doesn't exist" unless File.file? attributes[:config_file] file_contents = File.read(attributes[:config_file]) begin @config = YAML::load(file_contents) rescue raise "Problem reading from config file #{attributes[:config_file]} (invalid YAML/permissions?)" end @config.each do |k, v| Configuration.set k, v end Logger.logfile(@config[:logfile]) Logger.loglevel(@config[:loglevel]) @params = attributes[:params] end |
Instance Method Details
#debug_dict(prefix, dict) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/salt_hiera/salt_hiera.rb', line 82 def debug_dict prefix, dict dict.each do |k, v| Logger.debug " -------> Found #{prefix}#{k}" if v.class == Hash debug_dict "#{prefix}#{k}.", v end end end |
#find_plugin(plugin_name) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/salt_hiera/salt_hiera.rb', line 32 def find_plugin plugin_name begin require "salt_hiera/plugins/#{plugin_name.downcase}" rescue Logger.critical "No plugin named #{plugin_name} exists" return Module.const_get("SaltHiera").const_get("Plugins").const_get("NoPlugin") end plugin_class = plugin_name.downcase.split("_").collect {|word| word.capitalize }.join("_") plugin = Module.const_get("SaltHiera").const_get("Plugins").const_get(plugin_class) raise "Could not find plugin #{plugin_name}" if plugin.nil? plugin end |
#to_yaml ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/salt_hiera/salt_hiera.rb', line 45 def to_yaml Logger.debug @config if @config["basedir"] Logger.debug "Setting basedir to \"#{@config['basedir']}\"" Dir.chdir @config["basedir"] end results = {} @config.keys.each do |key| if @config[key].class == String @config[key].gsub!(/\%\{(.*?)\}/) {|exp| @params[exp[2..-2]] || "!NOT_DEFINED!" } # Detokenize end end @config["hierarchy"].reverse.each do |hierarchy| Logger.debug "Analysing hierarchy element: #{hierarchy}" hierarchy_type, hierarchy_glob = hierarchy.split(':', 2) Logger.debug " type: #{hierarchy_type}" Logger.debug " glob: #{hierarchy_glob}" hierarchy_glob.gsub!(/\%\{(.*?)\}/) {|exp| @params[exp[2..-2]] || "!NOT_DEFINED!" } # Detokenize Logger.debug " reglob: #{hierarchy_glob}" next if hierarchy_glob.include? "!NOT_DEFINED!" files = Dir.glob(hierarchy_glob).collect {|x| x if File.file?(x) }.compact files.each do |file| Logger.debug " file: #{file} (#{hierarchy_type})" plugin = find_plugin hierarchy_type dict = plugin.process_file file debug_dict "", dict results.merge! dict end end if @config["hierarchy"] results.to_yaml end |