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 168 168: def self.reply 169: RPC::Reply.new 170: end
Factory for RPC::Request messages, only really here to make agents a bit easier to understand
# File lib/mcollective/rpc.rb, line 162 162: def self.request(msg) 163: RPC::Request.new(msg) 164: 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 152 152: def empty_filter?(options) 153: if options.include?(:filter) 154: Util.empty_filter?(options[:filter]) 155: else 156: Util.empty_filter?(options) 157: end 158: 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 132 132: def printrpc(result, flags = {}) 133: verbose = @options[:verbose] rescue verbose = false 134: verbose = flags[:verbose] || verbose 135: flatten = flags[:flatten] || false 136: format = @options[:output_format] 137: 138: result_text = Helpers.rpcresults(result, {:verbose => verbose, :flatten => flatten, :format => format}) 139: 140: if result.is_a?(Array) && format == :console 141: puts "\n%s\n" % [ result_text ] 142: else 143: # when we get just one result to print dont pad them all with 144: # blank spaces etc, just print the individual result with no 145: # padding 146: puts result_text unless result_text == "" 147: end 148: 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: return unless @options[:output_format] == :console 111: 112: verbose = @options[:verbose] rescue verbose = false 113: caption = flags[:caption] || "rpc stats" 114: 115: begin 116: stats = @@stats 117: rescue 118: puts("no stats to display") 119: return 120: end 121: 122: puts 123: puts stats.report(caption, verbose) 124: 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