7ce9db7b42dca6bdba8a83afe7e09c9b1c5afa0c
[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{|l| l.chomp}
19
20         # this plugin only supports identity filters, do regex matches etc against
21         # the list found in the flatfile
22         unless filter["identity"].empty?
23           filter["identity"].each do |identity|
24             identity = Regexp.new(identity.gsub("\/", "")) if identity.match("^/")
25
26             if identity.is_a?(Regexp)
27               discovered = hosts.grep(identity)
28             elsif hosts.include?(identity)
29               discovered << identity
30             end
31           end
32         else
33           discovered = hosts
34         end
35
36         discovered
37       end
38     end
39   end
40 end