Sha256: f1f5cd37d70f22ade706c1901db3d96ccb63a22ae496c0ce90e68a85a59b86c9

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

# discovers against a flatfile instead of the traditional network discovery
# the flat file must have a node name per line which should match identities
# as configured
module MCollective
  class Discovery
    class Flatfile
      def self.discover(filter, timeout, limit=0, client=nil)
        if client.options[:discovery_options].empty?
          raise "The flatfile discovery method needs a path to a text file"
        else
          file = client.options[:discovery_options].first
        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
          next if host.empty? || host.match(/^#/)
          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
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
choria-mcorpc-support-2.23.3 lib/mcollective/discovery/flatfile.rb
choria-mcorpc-support-2.23.2 lib/mcollective/discovery/flatfile.rb
choria-mcorpc-support-2.23.1 lib/mcollective/discovery/flatfile.rb
choria-mcorpc-support-2.23.0 lib/mcollective/discovery/flatfile.rb
choria-mcorpc-support-2.23.0.pre lib/mcollective/discovery/flatfile.rb