3bfaa5c27b74487a49ab6f7f28c04848211170cf
[mirror/dsa-puppet.git] / modules / puppetmaster / lib / puppet / parser / functions / nodeinfo.rb
1 module Puppet::Parser::Functions
2   newfunction(:nodeinfo, :type => :rvalue) do |args|
3     require 'ipaddr'
4
5     host = args[0]
6     yamlfile = args[1]
7     begin
8       nodeinfo         = function_yamlinfo([host, yamlfile])
9       nodeinfo['ldap'] = function_ldapinfo([host, '*'])
10       unless nodeinfo['ldap']['ipHostNumber']
11         raise Puppet::ParseError, "Host #{host} does not have ipHostNumber values in ldap"
12       end
13       nodeinfo['hoster'] = function_whohosts([nodeinfo['ldap']['ipHostNumber']])
14       nodeinfo['buildd'] = (nodeinfo['ldap']['purpose'].respond_to?('include?') && nodeinfo['ldap']['purpose'].include?('buildd'))
15       nodeinfo['timeserver'] = (nodeinfo['ldap']['purpose'].respond_to?('include?') && nodeinfo['ldap']['purpose'].include?('timeserver'))
16       nodeinfo['porterbox'] = (nodeinfo['ldap']['purpose'].respond_to?('include?') && nodeinfo['ldap']['purpose'].include?('porterbox'))
17
18       if lookupvar('::mta') == 'exim4'
19         unless nodeinfo['heavy_exim']
20           nodeinfo['smarthost'] = 'mailout.debian.org'
21         end
22       elsif lookupvar('::mta') == 'postfix'
23         unless nodeinfo['heavy_postfix']
24           nodeinfo['smarthost'] = 'mailout.debian.org'
25         end
26       end
27
28       nodeinfo['misc'] = {}
29       fqdn = lookupvar('::fqdn')
30       if fqdn and fqdn == host
31         v4ips = lookupvar('::v4ips')
32         if v4ips and v4ips.to_s != "" and v4ips.to_s != 'undefined'
33           nodeinfo['misc']['v4addrs'] = v4ips.split(',').uniq()
34
35           # find out if we are behind nat
36           intersection = nodeinfo['misc']['v4addrs'] & nodeinfo['ldap']['ipHostNumber']
37           nodeinfo['misc']['natted'] = intersection.empty?
38         else
39           nodeinfo['misc']['v4addrs'] = []
40         end
41
42         v6ips = lookupvar('::v6ips')
43         if v6ips and v6ips.to_s != "" and v6ips.to_s != 'undefined'
44           nodeinfo['misc']['v6addrs'] = v6ips.split(',').uniq()
45         else
46           nodeinfo['misc']['v6addrs'] = []
47         end
48
49         # find out if we have an ipv4 and/or an ipv6 address for our host in ldap.
50         nodeinfo['misc']['has_v4_ldap'] = nodeinfo['ldap']['ipHostNumber'].any? { |x| IPAddr.new(x).ipv4? }
51         nodeinfo['misc']['has_v6_ldap'] = nodeinfo['ldap']['ipHostNumber'].any? { |x| IPAddr.new(x).ipv6? }
52         nodeinfo['misc']['v4_ldap'] = nodeinfo['ldap']['ipHostNumber'].select { |x| IPAddr.new(x).ipv4? }
53         nodeinfo['misc']['v6_ldap'] = nodeinfo['ldap']['ipHostNumber'].select { |x| IPAddr.new(x).ipv6? }
54       end
55
56       ns = call_function('hiera',['nameservers'])
57       allow_dns_q = call_function('hiera',['allow_dns_query'])
58       if ns.empty?
59         # no nameservers known for this hoster
60         nodeinfo['misc']['resolver-recursive'] = true
61
62         if not allow_dns_q.empty?
63           raise Puppet::ParseError, "No nameservers listed for #{nodeinfo['hoster']['name']} yet we should answer somebody's queries?  That makes no sense.  allow_dns_q: #{allow_dns_q}."
64         end
65       elsif (nodeinfo['misc']['v4addrs'] and (ns & nodeinfo['misc']['v4addrs']).size > 0) or
66             (nodeinfo['misc']['v6addrs'] and (ns & nodeinfo['misc']['v6addrs']).size > 0)
67         # this host is listed as a nameserver at this location
68         nodeinfo['misc']['resolver-recursive'] = true
69
70         if allow_dns_q.empty?
71           raise Puppet::ParseError, "Host #{host} is listed as a nameserver for #{nodeinfo['hoster']['name']} but no allow_dns_query networks are defined for this location"
72         end
73       else
74         nodeinfo['misc']['resolver-recursive'] = false
75       end
76
77       return(nodeinfo)
78     rescue => e
79       raise Puppet::ParseError, "Error in nodeinfo for node #{host}, yamlfile #{yamlfile}: #{e.message}\n#{e.backtrace}"
80     end
81   end
82 end
83
84 # vim: set fdm=marker ts=2 sw=2 et: