let's try this
[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     parser = Puppet::Parser::Parser.new(environment)
9     parser.watch_file(yamlfile)
10
11     $KCODE = 'utf-8'
12
13     ans = {"name" => "unknown"}
14     yaml = YAML.load_file(yamlfile)
15
16     ipAddrs.each do |addr|
17       yaml.keys.each do |hoster|
18         next unless yaml[hoster].kind_of?(Hash) and yaml[hoster].has_key?('netrange')
19         netrange = yaml[hoster]['netrange']
20
21         netrange.each do |net|
22           begin
23             if IPAddr.new(net).include?(addr)
24               ans = yaml[hoster]
25               ans['name'] = hoster
26             end
27           rescue => e
28             raise Puppet::ParseError, "Error while trying to match addr #{addr} for net #{net}: #{e.message}\n#{e.backtrace}"
29           end
30         end
31       end
32     end
33     return ans
34   end
35 end
36 # vim:set ts=2:
37 # vim:set et:
38 # vim:set shiftwidth=2: