Note that exim contains tracker-specific configuration
[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['timeserver'] = (nodeinfo['ldap']['purpose'].respond_to?('include?') && nodeinfo['ldap']['purpose'].include?('timeserver'))
15       nodeinfo['ganeti'] = (nodeinfo['ldap']['purpose'].respond_to?('include?') && nodeinfo['ldap']['purpose'].include?('ganeti/kvm host'))
16
17       nodeinfo['misc'] = {}
18       fqdn = lookupvar('::fqdn')
19       if fqdn and fqdn == host
20         v4ips = lookupvar('::v4ips')
21         if v4ips and v4ips.to_s != "" and v4ips.to_s != 'undefined'
22           nodeinfo['misc']['v4addrs'] = v4ips.split(',').uniq()
23
24           # find out if we are behind nat
25           intersection = nodeinfo['misc']['v4addrs'] & nodeinfo['ldap']['ipHostNumber']
26           nodeinfo['misc']['natted'] = intersection.empty?
27         else
28           nodeinfo['misc']['v4addrs'] = []
29         end
30
31         v6ips = lookupvar('::v6ips')
32         if v6ips and v6ips.to_s != "" and v6ips.to_s != 'undefined'
33           nodeinfo['misc']['v6addrs'] = v6ips.split(',').uniq()
34         else
35           nodeinfo['misc']['v6addrs'] = []
36         end
37
38         # find out if we have an ipv4 and/or an ipv6 address for our host in ldap.
39         nodeinfo['misc']['has_v4_ldap'] = nodeinfo['ldap']['ipHostNumber'].any? { |x| IPAddr.new(x).ipv4? }
40         nodeinfo['misc']['has_v6_ldap'] = nodeinfo['ldap']['ipHostNumber'].any? { |x| IPAddr.new(x).ipv6? }
41         nodeinfo['misc']['v4_ldap'] = nodeinfo['ldap']['ipHostNumber'].select { |x| IPAddr.new(x).ipv4? }
42         nodeinfo['misc']['v6_ldap'] = nodeinfo['ldap']['ipHostNumber'].select { |x| IPAddr.new(x).ipv6? }
43       end
44
45       ns = call_function('hiera',['resolv::nameservers'])
46       allow_dns_q = call_function('hiera',['allow_dns_query'])
47       if ns.empty?
48         # no nameservers known for this hoster
49         nodeinfo['misc']['resolver-recursive'] = true
50
51         if not allow_dns_q.empty?
52           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}."
53         end
54       elsif (nodeinfo['misc']['v4addrs'] and (ns & nodeinfo['misc']['v4addrs']).size > 0) or
55             (nodeinfo['misc']['v6addrs'] and (ns & nodeinfo['misc']['v6addrs']).size > 0)
56         # this host is listed as a nameserver at this location
57         nodeinfo['misc']['resolver-recursive'] = true
58
59         if allow_dns_q.empty?
60           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"
61         end
62       else
63         nodeinfo['misc']['resolver-recursive'] = false
64       end
65
66       return(nodeinfo)
67     rescue => e
68       raise Puppet::ParseError, "Error in nodeinfo for node #{host}, yamlfile #{yamlfile}: #{e.message}\n#{e.backtrace}"
69     end
70   end
71 end
72
73 # vim: set fdm=marker ts=2 sw=2 et: