Read misc files from environment
[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       results['mail_port']      = ''
27       results['smarthost']      = ''
28       results['heavy_exim']     = ''
29       results['smarthost_port'] = 587
30
31       if yaml['host_settings'].kind_of?(Hash)
32         yaml['host_settings'].each_pair do |property, values|
33           if values.kind_of?(Hash)
34             results[property] = values[host] if values.has_key?(host)
35           elsif values.kind_of?(Array)
36             results[property] = values.include?(host)
37           end
38         end
39       end
40       return(results)
41     }
42
43     require 'yaml'
44
45     yamlfile = Puppet::Parser::Files.find_file('debian_org/misc/local.yaml', compiler.environment)
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