sigh, totally broken
[mirror/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     #self.interp.newfile(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         next unless yaml[hoster].kind_of?(Hash) and yaml[hoster].has_key?('netrange')
18         netrange = yaml[hoster]['netrange']
19
20         netrange.each do |net|
21           begin
22             if IPAddr.new(net).include?(addr)
23               ans = yaml[hoster]
24               ans['name'] = hoster
25             end
26           rescue => e
27             raise Puppet::ParseError, "Error while trying to match addr #{addr} for net #{net}: #{e.message}\n#{e.backtrace}"
28           end
29         end
30       end
31     end
32     return ans
33   end
34 end
35 # vim:set ts=2:
36 # vim:set et:
37 # vim:set shiftwidth=2: