Class | MCollective::RPC::Client |
In: |
lib/mcollective/rpc/client.rb
|
Parent: | Object |
The main component of the Simple RPC client system, this wraps around MCollective::Client and just brings in a lot of convention and standard approached.
agent | [R] | |
client | [R] | |
config | [RW] | |
ddl | [R] | |
discovery_timeout | [RW] | |
filter | [RW] | |
limit_targets | [R] | |
progress | [RW] | |
stats | [R] | |
timeout | [RW] | |
verbose | [RW] |
Creates a stub for a remote agent, you can pass in an options array in the flags which will then be used else it will just create a default options array with filtering enabled based on the standard command line use.
rpc = RPC::Client.new("rpctest", :configfile => "client.cfg", :options => options)
You typically would not call this directly you‘d use MCollective::RPC#rpcclient instead which is a wrapper around this that can be used as a Mixin
# File lib/mcollective/rpc/client.rb, line 19 19: def initialize(agent, flags = {}) 20: if flags.include?(:options) 21: options = flags[:options] 22: 23: elsif @@initial_options 24: options = Marshal.load(@@initial_options) 25: 26: else 27: oparser = MCollective::Optionparser.new({:verbose => false, :progress_bar => true, :mcollective_limit_targets => false}, "filter") 28: 29: options = oparser.parse do |parser, options| 30: if block_given? 31: yield(parser, options) 32: end 33: 34: Helpers.add_simplerpc_options(parser, options) 35: end 36: 37: @@initial_options = Marshal.dump(options) 38: end 39: 40: @stats = Stats.new 41: @agent = agent 42: @discovery_timeout = options[:disctimeout] 43: @timeout = options[:timeout] 44: @verbose = options[:verbose] 45: @filter = options[:filter] 46: @config = options[:config] 47: @discovered_agents = nil 48: @progress = options[:progress_bar] 49: @limit_targets = options[:mcollective_limit_targets] 50: 51: agent_filter agent 52: 53: @client = client = MCollective::Client.new(@config) 54: @client.options = options 55: 56: # if we can find a DDL for the service override 57: # the timeout of the client so we always magically 58: # wait appropriate amounts of time. 59: # 60: # We do this only if the timeout is the default 5 61: # seconds, so that users cli overrides will still 62: # get applied 63: begin 64: @ddl = DDL.new(agent) 65: @timeout = @ddl.meta[:timeout] if @timeout == 5 66: rescue Exception => e 67: Log.instance.debug("Could not find DDL: #{e}") 68: @ddl = nil 69: end 70: 71: STDERR.sync = true 72: STDOUT.sync = true 73: end
Sets the agent filter
# File lib/mcollective/rpc/client.rb, line 274 274: def agent_filter(agent) 275: @filter["agent"] << agent 276: @filter["agent"].compact! 277: reset 278: end
Sets the class filter
# File lib/mcollective/rpc/client.rb, line 260 260: def class_filter(klass) 261: @filter["cf_class"] << klass 262: @filter["cf_class"].compact! 263: reset 264: end
Constructs custom requests with custom filters and discovery data the idea is that this would be used in web applications where you might be using a cached copy of data provided by a registration agent to figure out on your own what nodes will be responding and what your filter would be.
This will help you essentially short circuit the traditional cycle of:
mc discover / call / wait for discovered nodes
by doing discovery however you like, contructing a filter and a list of nodes you expect responses from.
Other than that it will work exactly like a normal call, blocks will behave the same way, stats will be handled the same way etcetc
If you just wanted to contact one machine for example with a client that already has other filter options setup you can do:
puppet.custom_request("runonce", {}, {:identity => "your.box.com"},
["your.box.com"])
This will do runonce action on just ‘your.box.com’, no discovery will be done and after receiving just one response it will stop waiting for responses
# File lib/mcollective/rpc/client.rb, line 218 218: def custom_request(action, args, expected_agents, filter = {}, &block) 219: @ddl.validate_request(action, args) if @ddl 220: 221: @stats.reset 222: 223: custom_filter = Util.empty_filter 224: custom_options = options.clone 225: 226: # merge the supplied filter with the standard empty one 227: # we could just use the merge method but I want to be sure 228: # we dont merge in stuff that isnt actually valid 229: ["identity", "fact", "agent", "cf_class"].each do |ftype| 230: if filter.include?(ftype) 231: custom_filter[ftype] = [filter[ftype], custom_filter[ftype]].flatten 232: end 233: end 234: 235: # ensure that all filters at least restrict the call to the agent we're a proxy for 236: custom_filter["agent"] << @agent unless custom_filter["agent"].include?(@agent) 237: custom_options[:filter] = custom_filter 238: 239: # Fake out the stats discovery would have put there 240: @stats.discovered_agents([expected_agents].flatten) 241: 242: # Handle fire and forget requests 243: if args.include?(:process_results) && args[:process_results] == false 244: @filter = custom_filter 245: return fire_and_forget_request(action, args) 246: end 247: 248: # Now do a call pretty much exactly like in method_missing except with our own 249: # options and discovery magic 250: if block_given? 251: call_agent(action, args, custom_options, [expected_agents].flatten) do |r| 252: block.call(r) 253: end 254: else 255: call_agent(action, args, custom_options, [expected_agents].flatten) 256: end 257: end
Disconnects cleanly from the middleware
# File lib/mcollective/rpc/client.rb, line 76 76: def disconnect 77: @client.disconnect 78: end
Does discovery based on the filters set, i a discovery was previously done return that else do a new discovery.
Will show a message indicating its doing discovery if running verbose or if the :verbose flag is passed in.
Use reset to force a new discovery
# File lib/mcollective/rpc/client.rb, line 306 306: def discover(flags={}) 307: flags.include?(:verbose) ? verbose = flags[:verbose] : verbose = @verbose 308: 309: if @discovered_agents == nil 310: @stats.time_discovery :start 311: 312: STDERR.print("Determining the amount of hosts matching filter for #{discovery_timeout} seconds .... ") if verbose 313: @discovered_agents = @client.discover(@filter, @discovery_timeout) 314: STDERR.puts(@discovered_agents.size) if verbose 315: 316: @stats.time_discovery :end 317: 318: end 319: 320: @stats.discovered_agents(@discovered_agents) 321: RPC.discovered(@discovered_agents) 322: 323: @discovered_agents 324: end
Sets the fact filter
# File lib/mcollective/rpc/client.rb, line 267 267: def fact_filter(fact, value) 268: @filter["fact"] << {:fact => fact, :value => value} 269: @filter["fact"].compact! 270: reset 271: end
Sets the identity filter
# File lib/mcollective/rpc/client.rb, line 281 281: def identity_filter(identity) 282: @filter["identity"] << identity 283: @filter["identity"].compact! 284: reset 285: end
Sets and sanity checks the limit_targets variable used to restrict how many nodes we‘ll target
# File lib/mcollective/rpc/client.rb, line 338 338: def limit_targets=(limit) 339: if limit.is_a?(String) 340: raise "Invalid limit specified: #{limit} valid limits are /^\d+%*$/" unless limit =~ /^\d+%*$/ 341: @limit_targets = limit 342: elsif limit.respond_to?(:to_i) 343: limit = limit.to_i 344: limit = 1 if limit == 0 345: @limit_targets = limit 346: else 347: raise "Don't know how to handle limit of type #{limit.class}" 348: end 349: end
Magic handler to invoke remote methods
Once the stub is created using the constructor or the RPC#rpcclient helper you can call remote actions easily:
ret = rpc.echo(:msg => "hello world")
This will call the ‘echo’ action of the ‘rpctest’ agent and return the result as an array, the array will be a simplified result set from the usual full MCollective::Client#req with additional error codes and error text:
{
:sender => "remote.box.com", :statuscode => 0, :statusmsg => "OK", :data => "hello world"
}
If :statuscode is 0 then everything went find, if it‘s 1 then you supplied the correct arguments etc but the request could not be completed, you‘ll find a human parsable reason in :statusmsg then.
Codes 2 to 5 maps directly to UnknownRPCAction, MissingRPCData, InvalidRPCData and UnknownRPCError see below for a description of those, in each case :statusmsg would be the reason for failure.
To get access to the full result of the MCollective::Client#req calls you can pass in a block:
rpc.echo(:msg => "hello world") do |resp| pp resp end
In this case resp will the result from MCollective::Client#req. Instead of returning simple text and codes as above you‘ll also need to handle the following exceptions:
UnknownRPCAction - There is no matching action on the agent MissingRPCData - You did not supply all the needed parameters for the action InvalidRPCData - The data you did supply did not pass validation UnknownRPCError - Some other error prevented the agent from running
During calls a progress indicator will be shown of how many results we‘ve received against how many nodes were discovered, you can disable this by setting progress to false:
rpc.progress = false
This supports a 2nd mode where it will send the SimpleRPC request and never handle the responses. It‘s a bit like UDP, it sends the request with the filter attached and you only get back the requestid, you have no indication about results.
You can invoke this using:
puts rpc.echo(:process_results => false)
This will output just the request id.
# File lib/mcollective/rpc/client.rb, line 169 169: def method_missing(method_name, *args, &block) 170: # set args to an empty hash if nothings given 171: args = args[0] 172: args = {} if args.nil? 173: 174: action = method_name.to_s 175: 176: @stats.reset 177: 178: @ddl.validate_request(action, args) if @ddl 179: 180: # Handle single target requests by doing discovery and picking 181: # a random node. Then do a custom request specifying a filter 182: # that will only match the one node. 183: if @limit_targets 184: target_nodes = pick_nodes_from_discovered(@limit_targets) 185: Log.instance.debug("Picked #{target_nodes.join(',')} as limited target(s)") 186: 187: custom_request(action, args, target_nodes, {"identity" => /^(#{target_nodes.join('|')})$/}, &block) 188: else 189: # Normal agent requests as per client.action(args) 190: call_agent(action, args, options, &block) 191: end 192: end
Creates a suitable request hash for the SimpleRPC agent.
You‘d use this if you ever wanted to take care of sending requests on your own - perhaps via Client#sendreq if you didn‘t care for responses.
In that case you can just do:
msg = your_rpc.new_request("some_action", :foo => :bar) filter = your_rpc.filter your_rpc.client.sendreq(msg, msg[:agent], filter)
This will send a SimpleRPC request to the action some_action with arguments :foo = :bar, it will return immediately and you will have no indication at all if the request was receieved or not
Clearly the use of this technique should be limited and done only if your code requires such a thing
# File lib/mcollective/rpc/client.rb, line 108 108: def new_request(action, data) 109: callerid = PluginManager["security_plugin"].callerid 110: 111: {:agent => @agent, 112: :action => action, 113: :caller => callerid, 114: :data => data} 115: end
Provides a normal options hash like you would get from Optionparser
# File lib/mcollective/rpc/client.rb, line 328 328: def options 329: {:disctimeout => @discovery_timeout, 330: :timeout => @timeout, 331: :verbose => @verbose, 332: :filter => @filter, 333: :config => @config} 334: end
Resets various internal parts of the class, most importantly it clears out the cached discovery
# File lib/mcollective/rpc/client.rb, line 289 289: def reset 290: @discovered_agents = nil 291: end