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.

Methods

Attributes

agent  [R] 
batch_size  [R] 
batch_sleep_time  [R] 
client  [R] 
config  [RW] 
ddl  [R] 
discovery_timeout  [RW] 
filter  [RW] 
limit_method  [R] 
limit_targets  [R] 
output_format  [R] 
progress  [RW] 
stats  [R] 
timeout  [RW] 
ttl  [RW] 
verbose  [RW] 

Public Class methods

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

[Source]

     # File lib/mcollective/rpc/client.rb, line 19
 19:       def initialize(agent, flags = {})
 20:         if flags.include?(:options)
 21:           initial_options = flags[:options]
 22: 
 23:         elsif @@initial_options
 24:           initial_options = Marshal.load(@@initial_options)
 25: 
 26:         else
 27:           oparser = MCollective::Optionparser.new({:verbose => false, :progress_bar => true, :mcollective_limit_targets => false, :batch_size => nil, :batch_sleep_time => 1}, "filter")
 28: 
 29:           initial_options = oparser.parse do |parser, opts|
 30:             if block_given?
 31:               yield(parser, opts)
 32:             end
 33: 
 34:             Helpers.add_simplerpc_options(parser, opts)
 35:           end
 36: 
 37:           @@initial_options = Marshal.dump(initial_options)
 38:         end
 39: 
 40:         @stats = Stats.new
 41:         @agent = agent
 42:         @discovery_timeout = initial_options[:disctimeout]
 43:         @timeout = initial_options[:timeout]
 44:         @verbose = initial_options[:verbose]
 45:         @filter = initial_options[:filter]
 46:         @config = initial_options[:config]
 47:         @discovered_agents = nil
 48:         @progress = initial_options[:progress_bar]
 49:         @limit_targets = initial_options[:mcollective_limit_targets]
 50:         @limit_method = Config.instance.rpclimitmethod
 51:         @output_format = initial_options[:output_format] || :console
 52:         @force_direct_request = false
 53:         @batch_size = initial_options[:batch_size]
 54:         @batch_sleep_time = Float(initial_options[:batch_sleep_time] || 1)
 55: 
 56:         agent_filter agent
 57: 
 58:         @client = MCollective::Client.new(@config)
 59:         @client.options = initial_options
 60: 
 61:         @collective = @client.collective
 62:         @ttl = initial_options[:ttl] || Config.instance.ttl
 63: 
 64:         # if we can find a DDL for the service override
 65:         # the timeout of the client so we always magically
 66:         # wait appropriate amounts of time.
 67:         #
 68:         # We add the discovery timeout to the ddl supplied
 69:         # timeout as the discovery timeout tends to be tuned
 70:         # for local network conditions and fact source speed
 71:         # which would other wise not be accounted for and
 72:         # some results might get missed.
 73:         #
 74:         # We do this only if the timeout is the default 5
 75:         # seconds, so that users cli overrides will still
 76:         # get applied
 77:         begin
 78:           @ddl = DDL.new(agent)
 79:           @timeout = @ddl.meta[:timeout] + @discovery_timeout if @timeout == 5
 80:         rescue Exception => e
 81:           Log.debug("Could not find DDL: #{e}")
 82:           @ddl = nil
 83:         end
 84: 
 85:         # allows stderr and stdout to be overridden for testing
 86:         # but also for web apps that might not want a bunch of stuff
 87:         # generated to actual file handles
 88:         if initial_options[:stderr]
 89:           @stderr = initial_options[:stderr]
 90:         else
 91:           @stderr = STDERR
 92:           @stderr.sync = true
 93:         end
 94: 
 95:         if initial_options[:stdout]
 96:           @stdout = initial_options[:stdout]
 97:         else
 98:           @stdout = STDOUT
 99:           @stdout.sync = true
100:         end
101:       end

Public Instance methods

Sets the agent filter

[Source]

     # File lib/mcollective/rpc/client.rb, line 336
336:       def agent_filter(agent)
337:         @filter["agent"] << agent
338:         @filter["agent"].compact!
339:         reset
340:       end

[Source]

     # File lib/mcollective/rpc/client.rb, line 499
499:       def batch_size=(limit)
500:         raise "Can only set batch size if direct addressing is supported" unless Config.instance.direct_addressing
501: 
502:         @batch_size = Integer(limit)
503:       end

[Source]

     # File lib/mcollective/rpc/client.rb, line 505
505:       def batch_sleep_time=(time)
506:         raise "Can only set batch sleep time if direct addressing is supported" unless Config.instance.direct_addressing
507: 
508:         @batch_sleep_time = Float(time)
509:       end

Sets the class filter

[Source]

     # File lib/mcollective/rpc/client.rb, line 312
312:       def class_filter(klass)
313:         @filter["cf_class"] << klass
314:         @filter["cf_class"].compact!
315:         reset
316:       end

Sets the collective we are communicating with

[Source]

     # File lib/mcollective/rpc/client.rb, line 468
468:       def collective=(c)
469:         @collective = c
470:         @client.options[:collective] = c
471:       end

Set a compound filter

[Source]

     # File lib/mcollective/rpc/client.rb, line 350
350:       def compound_filter(filter)
351:         @filter["compound"] = Matcher::Parser.new(filter).execution_stack
352:         reset
353:       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", {}, ["your.box.com"], {:identity => "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

If direct_addressing is enabled in the config file you can provide an empty hash as a filter, this will force that request to be a directly addressed request which technically does not need filters. If you try to use this mode with direct addressing disabled an exception will be raise

[Source]

     # File lib/mcollective/rpc/client.rb, line 266
266:       def custom_request(action, args, expected_agents, filter = {}, &block)
267:         @ddl.validate_request(action, args) if @ddl
268: 
269:         if filter == {} && !Config.instance.direct_addressing
270:           raise "Attempted to do a filterless custom_request without direct_addressing enabled, preventing unexpected call to all nodes"
271:         end
272: 
273: 
274:         @stats.reset
275: 
276:         custom_filter = Util.empty_filter
277:         custom_options = options.clone
278: 
279:         # merge the supplied filter with the standard empty one
280:         # we could just use the merge method but I want to be sure
281:         # we dont merge in stuff that isnt actually valid
282:         ["identity", "fact", "agent", "cf_class", "compound"].each do |ftype|
283:           if filter.include?(ftype)
284:             custom_filter[ftype] = [filter[ftype], custom_filter[ftype]].flatten
285:           end
286:         end
287: 
288:         # ensure that all filters at least restrict the call to the agent we're a proxy for
289:         custom_filter["agent"] << @agent unless custom_filter["agent"].include?(@agent)
290:         custom_options[:filter] = custom_filter
291: 
292:         # Fake out the stats discovery would have put there
293:         @stats.discovered_agents([expected_agents].flatten)
294: 
295:         # Handle fire and forget requests
296:         if args.include?(:process_results) && args[:process_results] == false
297:           return fire_and_forget_request(action, args, custom_filter)
298:         end
299: 
300:         # Now do a call pretty much exactly like in method_missing except with our own
301:         # options and discovery magic
302:         if block_given?
303:           call_agent(action, args, custom_options, [expected_agents].flatten) do |r|
304:             block.call(r)
305:           end
306:         else
307:           call_agent(action, args, custom_options, [expected_agents].flatten)
308:         end
309:       end

Disconnects cleanly from the middleware

[Source]

     # File lib/mcollective/rpc/client.rb, line 104
104:       def disconnect
105:         @client.disconnect
106:       end

Does discovery based on the filters set, if a discovery was previously done return that else do a new discovery.

Alternatively if identity filters are given and none of them are regular expressions then just use the provided data as discovered data, avoiding discovery

Discovery can be forece if direct_addressing is enabled by passing in an array of hosts with :hosts or JSON data like those produced by mcollective rpc JSON output

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

[Source]

     # File lib/mcollective/rpc/client.rb, line 382
382:       def discover(flags={})
383:         flags.include?(:verbose) ? verbose = flags[:verbose] : verbose = @verbose
384: 
385:         verbose = false unless @output_format == :console
386: 
387:         reset if flags[:hosts] || flags[:json]
388: 
389:         unless @discovered_agents
390:           # if either hosts or json is supplied try to figure out discovery data from there
391:           # if direct_addressing is not enabled this is a critical error as the user might
392:           # not have supplied filters so raise an exception
393:           if flags[:hosts] || flags[:json]
394:             raise "Can only supply discovery data if direct_addressing is enabled" unless Config.instance.direct_addressing
395: 
396:             hosts = []
397: 
398:             if flags[:hosts]
399:               hosts = Helpers.extract_hosts_from_array(flags[:hosts])
400:             elsif flags[:json]
401:               hosts = Helpers.extract_hosts_from_json(flags[:json])
402:             end
403: 
404:             raise "Could not find any hosts in discovery data provided" if hosts.empty?
405: 
406:             @discovered_agents = hosts
407:             @force_direct_request = true
408: 
409:           # if an identity filter is supplied and it is all strings no regex we can use that
410:           # as discovery data, technically the identity filter is then redundant if we are
411:           # in direct addressing mode and we could empty it out but this use case should
412:           # only really be for a few -I's on the cli
413:           #
414:           # For safety we leave the filter in place for now, that way we can support this
415:           # enhancement also in broadcast mode
416:           elsif options[:filter]["identity"].size > 0
417:             regex_filters = options[:filter]["identity"].select{|i| i.match("^\/")}.size
418: 
419:             if regex_filters == 0
420:               @discovered_agents = options[:filter]["identity"].clone
421:               @force_direct_request = true if Config.instance.direct_addressing
422:             end
423:           end
424:         end
425: 
426:         # All else fails we do it the hard way using a traditional broadcast
427:         unless @discovered_agents
428:           @stats.time_discovery :start
429: 
430:           @stderr.print("Determining the amount of hosts matching filter for #{discovery_timeout} seconds .... ") if verbose
431: 
432:           # if the requested limit is a pure number and not a percent
433:           # and if we're configured to use the first found hosts as the
434:           # limit method then pass in the limit thus minimizing the amount
435:           # of work we do in the discover phase and speeding it up significantly
436:           if @limit_method == :first and @limit_targets.is_a?(Fixnum)
437:             @discovered_agents = @client.discover(@filter, @discovery_timeout, @limit_targets)
438:           else
439:             @discovered_agents = @client.discover(@filter, @discovery_timeout)
440:           end
441: 
442:           @force_direct_request = false
443:           @stderr.puts(@discovered_agents.size) if verbose
444: 
445:           @stats.time_discovery :end
446:         end
447: 
448:         @stats.discovered_agents(@discovered_agents)
449:         RPC.discovered(@discovered_agents)
450: 
451:         @discovered_agents
452:       end

Sets the fact filter

[Source]

     # File lib/mcollective/rpc/client.rb, line 319
319:       def fact_filter(fact, value=nil, operator="=")
320:         return if fact.nil?
321:         return if fact == false
322: 
323:         if value.nil?
324:           parsed = Util.parse_fact_string(fact)
325:           @filter["fact"] << parsed unless parsed == false
326:         else
327:           parsed = Util.parse_fact_string("#{fact}#{operator}#{value}")
328:           @filter["fact"] << parsed unless parsed == false
329:         end
330: 
331:         @filter["fact"].compact!
332:         reset
333:       end

Returns help for an agent if a DDL was found

[Source]

     # File lib/mcollective/rpc/client.rb, line 109
109:       def help(template)
110:         if @ddl
111:           @ddl.help(template)
112:         else
113:           return "Can't find DDL for agent '#{@agent}'"
114:         end
115:       end

Sets the identity filter

[Source]

     # File lib/mcollective/rpc/client.rb, line 343
343:       def identity_filter(identity)
344:         @filter["identity"] << identity
345:         @filter["identity"].compact!
346:         reset
347:       end

Sets and sanity check the limit_method variable used to determine how to limit targets if limit_targets is set

[Source]

     # File lib/mcollective/rpc/client.rb, line 491
491:       def limit_method=(method)
492:         method = method.to_sym unless method.is_a?(Symbol)
493: 
494:         raise "Unknown limit method #{method} must be :random or :first" unless [:random, :first].include?(method)
495: 
496:         @limit_method = method
497:       end

Sets and sanity checks the limit_targets variable used to restrict how many nodes we‘ll target

[Source]

     # File lib/mcollective/rpc/client.rb, line 475
475:       def limit_targets=(limit)
476:         if limit.is_a?(String)
477:           raise "Invalid limit specified: #{limit} valid limits are /^\d+%*$/" unless limit =~ /^\d+%*$/
478: 
479:           begin
480:             @limit_targets = Integer(limit)
481:           rescue
482:             @limit_targets = limit
483:           end
484:         else
485:           @limit_targets = Integer(limit)
486:         end
487:       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.

Batched processing is supported:

  printrpc rpc.ping(:batch_size => 5)

This will do everything exactly as normal but communicate to only 5 agents at a time

[Source]

     # File lib/mcollective/rpc/client.rb, line 206
206:       def method_missing(method_name, *args, &block)
207:         # set args to an empty hash if nothings given
208:         args = args[0]
209:         args = {} if args.nil?
210: 
211:         action = method_name.to_s
212: 
213:         @stats.reset
214: 
215:         @ddl.validate_request(action, args) if @ddl
216: 
217:         # if a global batch size is set just use that else set it
218:         # in the case that it was passed as an argument
219:         batch_mode = args.include?(:batch_size) || @batch_size
220:         batch_size = args.delete(:batch_size) || @batch_size
221:         batch_sleep_time = args.delete(:batch_sleep_time) || @batch_sleep_time
222: 
223:         # Handle single target requests by doing discovery and picking
224:         # a random node.  Then do a custom request specifying a filter
225:         # that will only match the one node.
226:         if @limit_targets
227:           target_nodes = pick_nodes_from_discovered(@limit_targets)
228:           Log.debug("Picked #{target_nodes.join(',')} as limited target(s)")
229: 
230:           custom_request(action, args, target_nodes, {"identity" => /^(#{target_nodes.join('|')})$/}, &block)
231:         elsif batch_mode
232:           call_agent_batched(action, args, options, batch_size, batch_sleep_time, &block)
233:         else
234:           call_agent(action, args, options, :auto, &block)
235:         end
236:       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

[Source]

     # File lib/mcollective/rpc/client.rb, line 136
136:       def new_request(action, data)
137:         callerid = PluginManager["security_plugin"].callerid
138: 
139:         raise 'callerid received from security plugin is not valid' unless PluginManager["security_plugin"].valid_callerid?(callerid)
140: 
141:         {:agent  => @agent,
142:          :action => action,
143:          :caller => callerid,
144:          :data   => data}
145:       end

Provides a normal options hash like you would get from Optionparser

[Source]

     # File lib/mcollective/rpc/client.rb, line 456
456:       def options
457:         {:disctimeout => @discovery_timeout,
458:          :timeout => @timeout,
459:          :verbose => @verbose,
460:          :filter => @filter,
461:          :collective => @collective,
462:          :output_format => @output_format,
463:          :ttl => @ttl,
464:          :config => @config}
465:       end

Resets various internal parts of the class, most importantly it clears out the cached discovery

[Source]

     # File lib/mcollective/rpc/client.rb, line 357
357:       def reset
358:         @discovered_agents = nil
359:       end

Reet the filter to an empty one

[Source]

     # File lib/mcollective/rpc/client.rb, line 362
362:       def reset_filter
363:         @filter = Util.empty_filter
364:         agent_filter @agent
365:       end

[Validate]