1 # discovers against stdin instead of the traditional network discovery
2 # the input must be a flat file with a node name per line which should match identities as configured,
3 # or it should be a json string as output by the -j option of mco rpc
4 require 'mcollective/rpc/helpers'
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
21 raise("data piped on STDIN contained only whitespace - could not discover hosts from it.")
33 hosts = MCollective::RPC::Helpers.extract_hosts_from_json(file)
35 hosts = file.split("\n")
37 raise("stdin discovery plugin only knows the types auto/text/json, not \"#{type}\"")
41 raise 'Identities can only match /\w\.\-/' unless host.match(/^[\w\.\-]+$/)
45 # this plugin only supports identity filters, do regex matches etc against
46 # the list found in the flatfile
47 unless filter["identity"].empty?
48 filter["identity"].each do |identity|
49 identity = Regexp.new(identity.gsub("\/", "")) if identity.match("^/")
51 if identity.is_a?(Regexp)
52 discovered = hosts.grep(identity)
53 elsif hosts.include?(identity)
54 discovered << identity