class MCollective::Discovery::Stdin

Public Class Methods

discover(filter, timeout, limit=0, client=nil) click to toggle source
   # File lib/mcollective/discovery/stdin.rb
 9 def self.discover(filter, timeout, limit=0, client=nil)
10   unless client.options[:discovery_options].empty?
11     type = client.options[:discovery_options].first.downcase
12   else
13     type = 'auto'
14   end
15 
16   discovered = []
17 
18   file = STDIN.read
19 
20   if file =~ /^\s*$/
21     raise("data piped on STDIN contained only whitespace - could not discover hosts from it.")
22   end
23 
24   if type == 'auto'
25     if file =~ /^\s*\[/
26       type = 'json'
27     else
28       type = 'text'
29     end
30   end
31 
32   Log.debug("Parsing STDIN input as type %s" % type)
33 
34   if type == 'json'
35     hosts = RPC::Helpers.extract_hosts_from_json(file)
36   elsif type == 'text'
37     hosts = file.split("\n")
38   else
39     raise("stdin discovery plugin only knows the types auto/text/json, not \"#{type}\"")
40   end
41 
42   hosts.map do |host|
43     raise 'Identities can only match /\w\.\-/' unless host.match(/^[\w\.\-]+$/)
44     host
45   end
46 
47   # this plugin only supports identity filters, do regex matches etc against
48   # the list found in the flatfile
49   unless filter["identity"].empty?
50     filter["identity"].each do |identity|
51       identity = Regexp.new(identity.gsub("\/", "")) if identity.match("^/")
52 
53       if identity.is_a?(Regexp)
54         discovered = hosts.grep(identity)
55       elsif hosts.include?(identity)
56         discovered << identity
57       end
58     end
59   else
60     discovered = hosts
61   end
62 
63   discovered
64 end