X-Git-Url: https://review.fuel-infra.org/gitweb?a=blobdiff_plain;f=plugins%2Fmcollective%2Fdiscovery%2Fstdin.rb;fp=plugins%2Fmcollective%2Fdiscovery%2Fstdin.rb;h=8a44ef93c878607feec7532bda0c947e48ba9e66;hb=d1f1649ba43c5cbc43c4beb2380096ba051d646a;hp=0000000000000000000000000000000000000000;hpb=8a3fe7daeecccf43dd71c59371c5005400d35101;p=packages%2Fprecise%2Fmcollective.git diff --git a/plugins/mcollective/discovery/stdin.rb b/plugins/mcollective/discovery/stdin.rb new file mode 100644 index 0000000..8a44ef9 --- /dev/null +++ b/plugins/mcollective/discovery/stdin.rb @@ -0,0 +1,66 @@ +# discovers against stdin instead of the traditional network discovery +# the input must be a flat file with a node name per line which should match identities as configured, +# or it should be a json string as output by the -j option of mco rpc +require 'mcollective/rpc/helpers' + +module MCollective + class Discovery + class Stdin + 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 + + if type == 'json' + hosts = MCollective::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 + end + end +end +