]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/puppetmaster/lib/puppet/parser/functions/whohosts.rb
4b4371db132612fe9617695305367981bf81abc2
[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     nodeinfo = 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     if (nodeinfo['ldap'].has_key?('ipHostNumber'))
16       nodeinfo['ldap']['ipHostNumber'].each do |addr|
17         yaml.keys.each do |hoster|
18           if yaml[hoster].kind_of?(Hash) and yaml[hoster].has_key?('netrange')
19             netrange = yaml[hoster]['netrange']
20           else
21             next
22           end
23
24           netrange.each do |net|
25             begin
26               if IPAddr.new(net).include?(addr)
27                 ans = yaml[hoster]
28                 ans['name'] = hoster
29               end
30             rescue => e
31               raise Puppet::ParseError, "Error while trying to match addr #{addr} for net #{net}: #{e.message}\n#{e.backtrace}"
32             end
33           end
34         end
35       end
36     end
37     return ans
38   end
39 end
40 # vim:set ts=2:
41 # vim:set et:
42 # vim:set shiftwidth=2: