Drop explicit function requires
[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     host = args[0]
4     yamlfile = args[1]
5     begin
6       nodeinfo         = function_yamlinfo([host, yamlfile])
7       nodeinfo['ldap'] = function_ldapinfo([host, '*'])
8       unless nodeinfo['ldap']['ipHostNumber']
9         raise Puppet::ParseError, "Host #{host} does not have ipHostNumber values in ldap"
10       end
11       nodeinfo['hoster'] = function_whohosts([nodeinfo['ldap']['ipHostNumber']])
12       nodeinfo['buildd'] = (nodeinfo['ldap']['purpose'].respond_to?('include?') && nodeinfo['ldap']['purpose'].include?('buildd'))
13       nodeinfo['timeserver'] = (nodeinfo['ldap']['purpose'].respond_to?('include?') && nodeinfo['ldap']['purpose'].include?('timeserver'))
14       nodeinfo['porterbox'] = (nodeinfo['ldap']['purpose'].respond_to?('include?') && nodeinfo['ldap']['purpose'].include?('porterbox'))
15
16       if lookupvar('::mta') == 'exim4'
17         unless nodeinfo['heavy_exim']
18           nodeinfo['smarthost'] = 'mailout.debian.org'
19         end
20       end
21
22       nodeinfo['misc'] = {}
23       fqdn = lookupvar('::fqdn')
24       if fqdn and fqdn == host
25         v4ips = lookupvar('::v4ips')
26         if v4ips and v4ips.to_s != "" and v4ips.to_s != 'undefined'
27           nodeinfo['misc']['v4addrs'] = v4ips.split(',')
28
29           # find out if we are behind nat
30           intersection = nodeinfo['misc']['v4addrs'] & nodeinfo['ldap']['ipHostNumber']
31           nodeinfo['misc']['natted'] = intersection.empty?
32         end
33
34         v6ips = lookupvar('::v6ips')
35         if v6ips and v6ips.to_s != "" and v6ips.to_s != 'undefined'
36           nodeinfo['misc']['v6addrs'] = v6ips.split(',')
37         end
38       end
39
40       ns = call_function('hiera',['nameservers'])
41       allow_dns_q = call_function('hiera',['allow_dns_query'])
42       if ns.empty?
43         # no nameservers known for this hoster
44         nodeinfo['misc']['resolver-recursive'] = true
45
46         if not allow_dns_q.empty?
47           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}."
48         end
49       elsif (nodeinfo['misc']['v4addrs'] and (ns & nodeinfo['misc']['v4addrs']).size > 0) or
50             (nodeinfo['misc']['v6addrs'] and (ns & nodeinfo['misc']['v6addrs']).size > 0)
51         # this host is listed as a nameserver at this location
52         nodeinfo['misc']['resolver-recursive'] = true
53
54         if allow_dns_q.empty?
55           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"
56         end
57       else
58         nodeinfo['misc']['resolver-recursive'] = false
59       end
60
61       return(nodeinfo)
62     rescue => e
63       raise Puppet::ParseError, "Error in nodeinfo for node #{host}, yamlfile #{yamlfile}: #{e.message}\n#{e.backtrace}"
64     end
65   end
66 end
67
68 # vim: set fdm=marker ts=2 sw=2 et: