let's try this
[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     parser = Puppet::Parser::Parser.new(environment)
7     parser.watch_file(yamlfile)
8
9     def read_yaml(yaml, host)
10       results = {}
11
12       ['nameinfo', 'footer'].each do |detail|
13         if yaml.has_key?(detail)
14           if yaml[detail].has_key?(host)
15             results[detail] = yaml[detail][host]
16           end
17         end
18       end
19       
20       if yaml.has_key?('services')
21         yaml['services'].each_pair do |service, hostlist|
22           hostlist=[hostlist] unless hostlist.kind_of?(Array)
23           results[service] = hostlist.include?(host)
24         end
25       end
26
27       results['mail_port']      = ''
28       results['smarthost']      = ''
29       results['heavy_exim']     = ''
30       results['smarthost_port'] = 587
31       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'
32     
33       if yaml['host_settings'].kind_of?(Hash)
34         yaml['host_settings'].each_pair do |property, values|
35           if values.kind_of?(Hash)
36             results[property] = values[host] if values.has_key?(host)
37           elsif values.kind_of?(Array)
38             results[property] = values.include?(host)
39           end
40         end
41       end
42       return(results)
43     end
44
45     require 'yaml'
46     $KCODE = 'utf-8'
47
48     yaml = YAML.load_file(yamlfile)
49     ret = {}
50
51     if host == '*'
52       Dir.entries('/var/lib/puppet/yaml/node/').each do |fname|
53         next unless fname =~ /(.*)\.yaml$/
54         host_name = $1
55         ret[host_name] = read_yaml(yaml, host_name)
56       end
57     else
58       ret = read_yaml(yaml, host)
59     end
60
61     return(ret)
62   end
63 end
64