Sha256: 834134f46d2c7153a0ec0ca323eceb39acb58a1dada8cfc5f07df34d759f8dec

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

require 'mcollective'
require 'motoko'
require 'optparse'

filters = []

oparser = Motoko::OptionParser.new

options = {}

oparser.parse do |parser|
  parser.separator ''
  parser.separator 'Host Filters'

  parser.on('-C', '--wc', '--with-class CLASS', 'Match hosts with a certain config management class') do |with_class|
    filters << Motoko::Utils::PuppetDB.class_filter(with_class)
  end

  parser.on('-F', '--wf', '--with-fact fact=val', 'Match hosts with a certain fact') do |with_fact|
    filters << Motoko::Utils::PuppetDB.fact_filter(with_fact)
  end

  parser.on('-I', '--wi', '--with-identity IDENT', 'Match hosts with a certain configured identity') do |with_ident|
    filters << Motoko::Utils::PuppetDB.identity_filter(with_ident)
  end

  Motoko::OptionParser.add_shortcut_options(parser, oparser.formatter, options)
end

(options[:with_class] || []).compact.each do |klass|
  filters << Motoko::Utils::PuppetDB.class_filter(klass)
end

(options[:with_fact] || []).compact.each do |fact|
  filters << Motoko::Utils::PuppetDB.fact_filter(fact)
end

config = MCollective::Config.instance
config.loadconfig(MCollective::Util.config_file_for_user)

client = MCollective::Util::Choria.new

response = client.pql_query("facts[certname, name, value] { #{filters.map { |f| "(#{f})" }.join(' and ')} }")

nodes = Hash.new { |hash, value| hash[value] = {} }

response.each do |fact|
  nodes[fact['certname']][fact['name']] = fact['value']
end

nodes.each do |sender, facts|
  oparser.formatter.nodes << Motoko::Node.new(sender, facts)
end

puts oparser.formatter.to_s

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
motoko-1.2.0 exe/pdb-inventory
motoko-1.1.0 exe/pdb-inventory
motoko-1.0.0 exe/pdb-inventory