6590d62530d4bb5383ae0ca65a27c0ff26b45109
[mirror/dsa-puppet.git] / modules / puppetmaster / lib / puppet / parser / functions / yamlinfo.rb
1 require 'puppet/file_system'
2
3 module Puppet::Parser::Functions
4   newfunction(:yamlinfo, :type => :rvalue) do |args|
5
6     host = args[0]
7
8     read_yaml = lambda { |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       if yaml['host_settings'].kind_of?(Hash)
27         yaml['host_settings'].each_pair do |property, values|
28           if values.kind_of?(Hash)
29             results[property] = values[host] if values.has_key?(host)
30           elsif values.kind_of?(Array)
31             results[property] = values.include?(host)
32           end
33         end
34       end
35       return(results)
36     }
37
38     require 'yaml'
39
40     yamlfile = Puppet::Parser::Files.find_file('debian_org/misc/local.yaml', compiler.environment)
41     yaml = YAML.load_file(yamlfile)
42     ret = {}
43
44     if host == '*'
45       Dir.entries('/var/lib/puppet/yaml/facts/').each do |fname|
46         next unless fname =~ /(.*)\.yaml$/
47         host_name = $1
48         ret[host_name] = read_yaml.call(yaml, host_name)
49       end
50     else
51       ret = read_yaml.call(yaml, host)
52     end
53
54     return(ret)
55   end
56 end
57