0d12c661433ef26a59468f623d701d4c9aa4caf6
[mirror/dsa-puppet.git] / modules / puppetmaster / lib / puppet / parser / functions / yamlinfo.rb
1 module Puppet::Parser::Functions
2   newfunction(:yamlinfo, :type => :rvalue) do |args|
3
4     host = args[0]
5     yamlfile = args[1]
6     self.interp.newfile(yamlfile)
7
8     def read_yaml(yaml, host)
9       results = {}
10
11       ['nameinfo', 'footer'].each do |detail|
12         if yaml.has_key?(detail)
13           if yaml[detail].has_key?(host)
14             results[detail] = yaml[detail][host]
15           end
16         end
17       end
18       
19       if yaml.has_key?('services')
20         yaml['services'].each_pair do |service, hostlist|
21           hostlist=[hostlist] unless hostlist.kind_of?(Array)
22           results[service] = hostlist.include?(host)
23         end
24       end
25
26       results['mail_port']      = ''
27       results['smarthost']      = ''
28       results['heavy_exim']     = ''
29       results['smarthost_port'] = 587
30       results['reservedaddrs']  = '0.0.0.0/8 : 127.0.0.0/8 : 10.0.0.0/8 : 169.254.0.0/16 : 172.16.0.0/12 : 192.0.0.0/17 : 192.168.0.0/16 : 224.0.0.0/4 : 240.0.0.0/5 : 248.0.0.0/5'
31     
32       if yaml['host_settings'].kind_of?(Hash)
33         yaml['host_settings'].each_pair do |property, values|
34           if values.kind_of?(Hash)
35             results[property] = values[host] if values.has_key?(host)
36           elsif values.kind_of?(Array)
37             results[property] = values.include?(host)
38           end
39         end
40       end
41       return(results)
42     end
43
44     require 'yaml'
45     $KCODE = 'utf-8'
46
47     yaml = YAML.load_file(yamlfile)
48     ret = {}
49
50     if host == '*'
51       Dir.entries('/var/lib/puppet/yaml/node/').each do |fname|
52         next unless fname =~ /(.*)\.yaml$/
53         host_name = $1
54         ret[host_name] = read_yaml(yaml, host_name)
55       end
56     else
57       ret = read_yaml(yaml, host)
58     end
59
60     return(ret)
61   end
62 end
63