Update version according to OSCI-856
[packages/precise/mcollective.git] / plugins / mcollective / discovery / flatfile.rb
1 # discovers against a flatfile instead of the traditional network discovery
2 # the flat file must have a node name per line which should match identities
3 # as configured
4 module MCollective
5   class Discovery
6     class Flatfile
7       def self.discover(filter, timeout, limit=0, client=nil)
8         unless client.options[:discovery_options].empty?
9           file = client.options[:discovery_options].first
10         else
11           raise "The flatfile discovery method needs a path to a text file"
12         end
13
14         raise "Cannot read the file %s specified as discovery source" % file unless File.readable?(file)
15
16         discovered = []
17
18         hosts = File.readlines(file).map do |host|
19           host = host.chomp
20           raise 'Identities can only match /\w\.\-/' unless host.match(/^[\w\.\-]+$/)
21           host
22         end
23
24         # this plugin only supports identity filters, do regex matches etc against
25         # the list found in the flatfile
26         unless filter["identity"].empty?
27           filter["identity"].each do |identity|
28             identity = Regexp.new(identity.gsub("\/", "")) if identity.match("^/")
29
30             if identity.is_a?(Regexp)
31               discovered = hosts.grep(identity)
32             elsif hosts.include?(identity)
33               discovered << identity
34             end
35           end
36         else
37           discovered = hosts
38         end
39
40         discovered
41       end
42     end
43   end
44 end