f53014dee3a22c2f7dcaeda9fd746fa9be329605
[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     read_yaml = lambda { |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
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     }
43
44     require 'yaml'
45
46     yaml = YAML.load_file(yamlfile)
47     ret = {}
48
49     if host == '*'
50       Dir.entries('/var/lib/puppet/yaml/node/').each do |fname|
51         next unless fname =~ /(.*)\.yaml$/
52         host_name = $1
53         ret[host_name] = read_yaml.call(yaml, host_name)
54       end
55     else
56       ret = read_yaml.call(yaml, host)
57     end
58
59     return(ret)
60   end
61 end
62