class MCollective::Discovery::Flatfile

Public Class Methods

discover(filter, timeout, limit=0, client=nil) click to toggle source
   # File lib/mcollective/discovery/flatfile.rb
 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   hosts = []
18 
19   File.readlines(file).each do |host|
20     host = host.chomp.strip
21     if host.empty? || host.match(/^#/)
22       next
23     end
24     raise 'Identities can only match /^[\w\.\-]+$/' unless host.match(/^[\w\.\-]+$/)
25     hosts << host
26   end
27 
28   # this plugin only supports identity filters, do regex matches etc against
29   # the list found in the flatfile
30   if !(filter["identity"].empty?)
31     filter["identity"].each do |identity|
32       identity = Regexp.new(identity.gsub("\/", "")) if identity.match("^/")
33 
34       if identity.is_a?(Regexp)
35         discovered = hosts.grep(identity)
36       elsif hosts.include?(identity)
37         discovered << identity
38       end
39     end
40   else
41     discovered = hosts
42   end
43 
44   discovered
45 end