Class: MCollective::Discovery::Stdin

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

Class Method Summary collapse

Class Method Details

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



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mcollective/discovery/stdin.rb', line 9

def self.discover(filter, timeout, limit=0, client=nil)
  unless client.options[:discovery_options].empty?
    type = client.options[:discovery_options].first.downcase
  else
    type = 'auto'
  end

  discovered = []

  file = STDIN.read

  if file =~ /^\s*$/
    raise("data piped on STDIN contained only whitespace - could not discover hosts from it.")
  end

  if type == 'auto'
    if file =~ /^\s*\[/
      type = 'json'
    else
      type = 'text'
    end
  end

  Log.debug("Parsing STDIN input as type %s" % type)

  if type == 'json'
    hosts = RPC::Helpers.extract_hosts_from_json(file)
  elsif type == 'text'
    hosts = file.split("\n")
  else
    raise("stdin discovery plugin only knows the types auto/text/json, not \"#{type}\"")
  end

  hosts.map do |host|
    raise 'Identities can only match /\w\.\-/' unless host.match(/^[\w\.\-]+$/)
    host
  end

  # this plugin only supports identity filters, do regex matches etc against
  # the list found in the flatfile
  unless 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