#!/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