Created
March 17, 2010 08:06
-
-
Save ohadlevy/335012 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'csv' | |
| module Puppet::Parser::Functions | |
| # we parse the precedence here because the best place to specify | |
| # it would be in site.pp but site.pp is only evaluated at startup | |
| # so $fqdn etc would have no meaning there, this way it gets evaluated | |
| # each run and has access to the right variables for that run | |
| newfunction(:lookup, :type => :rvalue) do |args| | |
| def var_to_fact str | |
| while str =~ /%\{(.+?)\}/ | |
| fact = lookupvar $1 | |
| raise(Puppet::ParseError, "Unable to found value for #{str}") if fact.nil? | |
| str.gsub!(/%\{#{$1}\}/, fact) | |
| end | |
| str | |
| end | |
| # what are we looking for? | |
| lookup_name = args[0] | |
| # hacky hacky line, but otherwise the value of lookup_order inside puppet will be changed!! | |
| order = lookupvar('lookup_order').join("!LoOKUpp!").split("!LoOKUpp!").map { |var| var_to_fact var } | |
| env = lookupvar('environment').to_sym | |
| # search through puppet module path and lookup order | |
| datafiles = Array.new | |
| begin | |
| Puppet.settings.instance_variable_get(:@values)[env][:modulepath].split(":").each do |module_path| | |
| # where our CSV file is located at | |
| order.each do |csv_file| | |
| file = "#{module_path}/#{csv_file}.csv" | |
| debug "scanning if #{file} exists" | |
| datafiles << file if File.exists?(file) | |
| end | |
| end | |
| rescue | |
| raise Puppet::ParseError, "Something went wrong while looking for a datafile for #{lookup_name} - $1" | |
| end | |
| debug "Found the following relevant data files: #{datafiles}" | |
| # parse our CSV files | |
| found = false | |
| result = "" | |
| datafiles.each do |file| | |
| begin | |
| parser.watch_file file | |
| next if found | |
| debug "scanning #{file}" | |
| result = CSV.read(file).find_all{ |r| r[0] == lookup_name } | |
| if result.size > 0 | |
| found = true | |
| result = result.first[1..-1] # result values are in a nested array, removing one layer. | |
| result.map { |r| var_to_fact r } # replace values to facts if required. | |
| end | |
| rescue | |
| raise Puppet::ParseError, "Something went wrong while parsing #{file} - $1" | |
| end | |
| end | |
| if not found or result.size == 0 | |
| raise Puppet::ParseError, "unable to find value for #{lookup_name}" | |
| else | |
| debug "Found: #{result}" | |
| return result | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment