]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/puppetmaster/lib/puppet/parser/functions/whohosts.rb
Move hosterinfo into nodeinfo
[dsa-puppet.git] / modules / puppetmaster / lib / puppet / parser / functions / whohosts.rb
1 module Puppet::Parser::Functions
2   newfunction(:whohosts, :type => :rvalue) do |args|
3     require 'ipaddr'
4     require 'yaml'
5
6     ipAddrs = args[0]
7     yamlfile = args[1]
8     parser.watch_file(yamlfile)
9
10     $KCODE = 'utf-8'
11
12     ans = {"name" => "unknown"}
13     yaml = YAML.load_file(yamlfile)
14
15     ipAddrs.each do |addr|
16       yaml.keys.each do |hoster|
17         if yaml[hoster].kind_of?(Hash) and yaml[hoster].has_key?('netrange')
18           netrange = yaml[hoster]['netrange']
19         else
20           next
21         end
22
23         netrange.each do |net|
24           begin
25             if IPAddr.new(net).include?(addr)
26               ans = yaml[hoster]
27               ans['name'] = hoster
28             end
29           rescue => e
30             raise Puppet::ParseError, "Error while trying to match addr #{addr} for net #{net}: #{e.message}\n#{e.backtrace}"
31           end
32         end
33       end
34     end
35     return ans
36   end
37 end
38 # vim:set ts=2:
39 # vim:set et:
40 # vim:set shiftwidth=2: