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
# File lib/mcollective/rpc.rb, line 187 187: def self.const_missing(const_name) 188: super unless const_name == :DDL 189: 190: Log.warn("MCollective::RPC::DDL is deprecatd, please use MCollective::DDL instead") 191: MCollective::DDL 192: end
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 107 107: def self.discovered(discovered) 108: @@discovered = discovered 109: end
Factory for RPC::Reply messages, only really here to make agents a bit easier to understand
# File lib/mcollective/rpc.rb, line 183 183: def self.reply 184: RPC::Reply.new 185: end
Factory for RPC::Request messages, only really here to make agents a bit easier to understand
# File lib/mcollective/rpc.rb, line 177 177: def self.request(msg) 178: RPC::Request.new(msg) 179: 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 99 99: def self.stats(stats) 100: @@stats = stats 101: end
Wrapper for MCollective::Util.empty_filter? to make clients less fugly to write - ticket 18
# File lib/mcollective/rpc.rb, line 167 167: def empty_filter?(options) 168: if options.include?(:filter) 169: Util.empty_filter?(options[:filter]) 170: else 171: Util.empty_filter?(options) 172: end 173: 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 147 147: def printrpc(result, flags = {}) 148: verbose = @options[:verbose] rescue verbose = false 149: verbose = flags[:verbose] || verbose 150: flatten = flags[:flatten] || false 151: format = @options[:output_format] 152: 153: result_text = Helpers.rpcresults(result, {:verbose => verbose, :flatten => flatten, :format => format}) 154: 155: if result.is_a?(Array) && format == :console 156: puts "\n%s\n" % [ result_text ] 157: else 158: # when we get just one result to print dont pad them all with 159: # blank spaces etc, just print the individual result with no 160: # padding 161: puts result_text unless result_text == "" 162: end 163: 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 124 124: def printrpcstats(flags={}) 125: return unless @options[:output_format] == :console 126: 127: verbose = @options[:verbose] rescue verbose = false 128: caption = flags[:caption] || "rpc stats" 129: 130: begin 131: stats = @@stats 132: rescue 133: puts("no stats to display") 134: return 135: end 136: 137: puts 138: puts stats.report(caption, verbose) 139: 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 :exit_on_failure => true
Options would be a build up options hash from the Optionparser you can use the rpcoptions helper to create this
:exit_on_failure is true by default, and causes the application to exit if there is a failure constructing the RPC client. Set this flag to false to cause an Exception to be raised instead.
# File lib/mcollective/rpc.rb, line 60 60: def rpcclient(agent, flags = {}) 61: configfile = flags[:configfile] || "/etc/mcollective/client.cfg" 62: options = flags[:options] || nil 63: 64: if flags.key?(:exit_on_failure) 65: exit_on_failure = flags[:exit_on_failure] 66: else 67: # We exit on failure by default for CLI-friendliness 68: exit_on_failure = true 69: end 70: 71: begin 72: if options 73: rpc = Client.new(agent, :configfile => options[:config], :options => options) 74: @options = rpc.options 75: else 76: rpc = Client.new(agent, :configfile => configfile) 77: @options = rpc.options 78: end 79: rescue Exception => e 80: if exit_on_failure 81: puts("Could not create RPC client: #{e}") 82: exit! 83: else 84: raise e 85: end 86: end 87: 88: if block_given? 89: yield(rpc) 90: else 91: return rpc 92: end 93: 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