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 92 92: def self.discovered(discovered) 93: @@discovered = discovered 94: end
Factory for RPC::Reply messages, only really here to make agents a bit easier to understand
# File lib/mcollective/rpc.rb, line 165 165: def self.reply 166: RPC::Reply.new 167: end
Factory for RPC::Request messages, only really here to make agents a bit easier to understand
# File lib/mcollective/rpc.rb, line 159 159: def self.request(msg) 160: RPC::Request.new(msg) 161: 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 84 84: def self.stats(stats) 85: @@stats = stats 86: end
Wrapper for MCollective::Util.empty_filter? to make clients less fugly to write - ticket 18
# File lib/mcollective/rpc.rb, line 149 149: def empty_filter?(options) 150: if options.include?(:filter) 151: Util.empty_filter?(options[:filter]) 152: else 153: Util.empty_filter?(options) 154: end 155: 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 130 130: def printrpc(result, flags = {}) 131: verbose = @options[:verbose] rescue verbose = false 132: verbose = flags[:verbose] || verbose 133: flatten = flags[:flatten] || false 134: 135: result_text = Helpers.rpcresults(result, {:verbose => verbose, :flatten => flatten}) 136: 137: if result.is_a?(Array) 138: puts "\n%s\n" % [ result_text ] 139: else 140: # when we get just one result to print dont pad them all with 141: # blank spaces etc, just print the individual result with no 142: # padding 143: puts result_text unless result_text == "" 144: end 145: 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 109 109: def printrpcstats(flags={}) 110: verbose = @options[:verbose] rescue verbose = false 111: caption = flags[:caption] || "rpc stats" 112: 113: begin 114: stats = @@stats 115: rescue 116: puts("no stats to display") 117: return 118: end 119: 120: puts 121: puts stats.report(caption, verbose) 122: 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 56 56: def rpcclient(agent, flags = {}) 57: configfile = flags[:configfile] || "/etc/mcollective/client.cfg" 58: options = flags[:options] || nil 59: 60: begin 61: if options 62: rpc = Client.new(agent, :configfile => options[:config], :options => options) 63: @options = rpc.options 64: else 65: rpc = Client.new(agent, :configfile => configfile) 66: @options = rpc.options 67: end 68: rescue Exception => e 69: puts("Could not create RPC client: #{e}") 70: exit! 71: end 72: 73: if block_given? 74: yield(rpc) 75: else 76: return rpc 77: end 78: end
Creates a standard options hash, pass in a block to add extra headings etc see Optionparser
# File lib/mcollective/rpc.rb, line 22 22: def rpcoptions 23: oparser = MCollective::Optionparser.new({:verbose => false, :progress_bar => true}, "filter") 24: 25: options = oparser.parse do |parser, options| 26: if block_given? 27: yield(parser, options) 28: end 29: 30: Helpers.add_simplerpc_options(parser, options) 31: end 32: 33: return options 34: end