Class: MCollective::Discovery::Flatfile

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/discovery/flatfile.rb

Class Method Summary collapse

Class Method Details

.discover(filter, timeout, limit = 0, client = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mcollective/discovery/flatfile.rb', line 7

def self.discover(filter, timeout, limit=0, client=nil)
  unless client.options[:discovery_options].empty?
    file = client.options[:discovery_options].first
  else
    raise "The flatfile discovery method needs a path to a text file"
  end

  raise "Cannot read the file %s specified as discovery source" % file unless File.readable?(file)

  discovered = []
  hosts = []

  File.readlines(file).each do |host|
    host = host.chomp.strip
    if host.empty? || host.match(/^#/)
      next
    end
    raise 'Identities can only match /^[\w\.\-]+$/' unless host.match(/^[\w\.\-]+$/)
    hosts << host
  end

  # this plugin only supports identity filters, do regex matches etc against
  # the list found in the flatfile
  if !(filter["identity"].empty?)
    filter["identity"].each do |identity|
      identity = Regexp.new(identity.gsub("\/", "")) if identity.match("^/")

      if identity.is_a?(Regexp)
        discovered = hosts.grep(identity)
      elsif hosts.include?(identity)
        discovered << identity
      end
    end
  else
    discovered = hosts
  end

  discovered
end