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