Toolset to create a standard interface of client and agent using an RPC metaphor, standard compliant agents will make it easier to create generic clients like web interfaces etc
means for other classes to drop discovered hosts into this module its a bit hacky but needed so that the mixin methods like printrpcstats can easily get access to it without users having to pass it around in params.
# File lib/mcollective/rpc.rb, line 91 91: def self.discovered(discovered) 92: @@discovered = discovered 93: end
Factory for RPC::Reply messages, only really here to make agents a bit easier to understand
# File lib/mcollective/rpc.rb, line 156 156: def self.reply 157: RPC::Reply.new 158: end
Factory for RPC::Request messages, only really here to make agents a bit easier to understand
# File lib/mcollective/rpc.rb, line 150 150: def self.request(msg) 151: RPC::Request.new(msg) 152: end
means for other classes to drop stats into this module its a bit hacky but needed so that the mixin methods like printrpcstats can easily get access to it without users having to pass it around in params.
# File lib/mcollective/rpc.rb, line 83 83: def self.stats(stats) 84: @@stats = stats 85: end
Wrapper for MCollective::Util.empty_filter? to make clients less fugly to write - ticket 18
# File lib/mcollective/rpc.rb, line 140 140: def empty_filter?(options) 141: if options.include?(:filter) 142: Util.empty_filter?(options[:filter]) 143: else 144: Util.empty_filter?(options) 145: end 146: end
Prints the result of an RPC call.
In the default quiet mode - no flattening or verbose - only results that produce an error will be printed
To get details of each result run with the -v command line option.
# File lib/mcollective/rpc.rb, line 129 129: def printrpc(result, flags = {}) 130: verbose = @options[:verbose] rescue verbose = false 131: verbose = flags[:verbose] || verbose 132: flatten = flags[:flatten] || false 133: 134: puts 135: puts Helpers.rpcresults(result, {:verbose => verbose, :flatten => flatten}) 136: end
Prints stats, requires stats to be saved from elsewhere using the MCollective::RPC.stats method.
If you‘ve passed -v on the command line a detailed stat block will be printed, else just a one liner.
You can pass flags into it, at the moment only one flag is supported:
printrpcstats :caption => "Foo"
This will use "Foo" as the caption to the stats in verbose mode
# File lib/mcollective/rpc.rb, line 108 108: def printrpcstats(flags={}) 109: verbose = @options[:verbose] rescue verbose = false 110: caption = flags[:caption] || "rpc stats" 111: 112: begin 113: stats = @@stats 114: rescue 115: puts("no stats to display") 116: return 117: end 118: 119: puts 120: puts stats.report(caption, verbose) 121: end
Wrapper to create clients, supposed to be used as a mixin:
include MCollective::RPC
exim = rpcclient("exim") printrpc exim.mailq
or
rpcclient("exim") do |exim|
printrpc exim.mailq
end
It will take a few flags:
:configfile => "etc/client.cfg" :options => options
Options would be a build up options hash from the Optionparser you can use the rpcoptions helper to create this
# File lib/mcollective/rpc.rb, line 55 55: def rpcclient(agent, flags = {}) 56: configfile = flags[:configfile] || "/etc/mcollective/client.cfg" 57: options = flags[:options] || nil 58: 59: begin 60: if options 61: rpc = Client.new(agent, :configfile => options[:config], :options => options) 62: @options = rpc.options 63: else 64: rpc = Client.new(agent, :configfile => configfile) 65: @options = rpc.options 66: end 67: rescue Exception => e 68: puts("Could not create RPC client: #{e}") 69: exit! 70: end 71: 72: if block_given? 73: yield(rpc) 74: else 75: return rpc 76: end 77: end
Creates a standard options hash, pass in a block to add extra headings etc see Optionparser
# File lib/mcollective/rpc.rb, line 21 21: def rpcoptions 22: oparser = MCollective::Optionparser.new({:verbose => false, :progress_bar => true}, "filter") 23: 24: options = oparser.parse do |parser, options| 25: if block_given? 26: yield(parser, options) 27: end 28: 29: Helpers.add_simplerpc_options(parser, options) 30: end 31: 32: return options 33: end