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 108
108: def self.discovered(discovered)
109: @@discovered = discovered
110: end
Factory for RPC::Reply messages, only really here to make agents a bit easier to understand
# File lib/mcollective/rpc.rb, line 184
184: def self.reply
185: RPC::Reply.new
186: end
Factory for RPC::Request messages, only really here to make agents a bit easier to understand
# File lib/mcollective/rpc.rb, line 178
178: def self.request(msg)
179: RPC::Request.new(msg)
180: 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 100
100: def self.stats(stats)
101: @@stats = stats
102: end
Wrapper for MCollective::Util.empty_filter? to make clients less fugly to write - ticket 18
# File lib/mcollective/rpc.rb, line 168
168: def empty_filter?(options)
169: if options.include?(:filter)
170: Util.empty_filter?(options[:filter])
171: else
172: Util.empty_filter?(options)
173: end
174: 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 148
148: def printrpc(result, flags = {})
149: verbose = @options[:verbose] rescue verbose = false
150: verbose = flags[:verbose] || verbose
151: flatten = flags[:flatten] || false
152: format = @options[:output_format]
153:
154: result_text = Helpers.rpcresults(result, {:verbose => verbose, :flatten => flatten, :format => format})
155:
156: if result.is_a?(Array) && format == :console
157: puts "\n%s\n" % [ result_text ]
158: else
159: # when we get just one result to print dont pad them all with
160: # blank spaces etc, just print the individual result with no
161: # padding
162: puts result_text unless result_text == ""
163: end
164: 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 125
125: def printrpcstats(flags={})
126: return unless @options[:output_format] == :console
127:
128: verbose = @options[:verbose] rescue verbose = false
129: caption = flags[:caption] || "rpc stats"
130:
131: begin
132: stats = @@stats
133: rescue
134: puts("no stats to display")
135: return
136: end
137:
138: puts
139: puts stats.report(caption, verbose)
140: 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 61
61: def rpcclient(agent, flags = {})
62: configfile = flags[:configfile] || "/etc/mcollective/client.cfg"
63: options = flags[:options] || nil
64:
65: if flags.key?(:exit_on_failure)
66: exit_on_failure = flags[:exit_on_failure]
67: else
68: # We exit on failure by default for CLI-friendliness
69: exit_on_failure = true
70: end
71:
72: begin
73: if options
74: rpc = Client.new(agent, :configfile => options[:config], :options => options)
75: @options = rpc.options
76: else
77: rpc = Client.new(agent, :configfile => configfile)
78: @options = rpc.options
79: end
80: rescue Exception => e
81: if exit_on_failure
82: puts("Could not create RPC client: #{e}")
83: exit!
84: else
85: raise e
86: end
87: end
88:
89: if block_given?
90: yield(rpc)
91: else
92: return rpc
93: end
94: 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