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] 
client  [R] 
config  [RW] 
ddl  [R] 
discovery_timeout  [RW] 
filter  [RW] 
limit_targets  [R] 
progress  [RW] 
stats  [R] 
timeout  [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:                     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 = MCollective::Client.new(@config)
54:                 @client.options = options
55: 
56:                 @collective = @client.collective
57: 
58:                 # if we can find a DDL for the service override
59:                 # the timeout of the client so we always magically
60:                 # wait appropriate amounts of time.
61:                 #
62:                 # We add the discovery timeout to the ddl supplied
63:                 # timeout as the discovery timeout tends to be tuned
64:                 # for local network conditions and fact source speed
65:                 # which would other wise not be accounted for and
66:                 # some results might get missed.
67:                 #
68:                 # We do this only if the timeout is the default 5
69:                 # seconds, so that users cli overrides will still
70:                 # get applied
71:                 begin
72:                     @ddl = DDL.new(agent)
73:                     @timeout = @ddl.meta[:timeout] + @discovery_timeout if @timeout == 5
74:                 rescue Exception => e
75:                     Log.debug("Could not find DDL: #{e}")
76:                     @ddl = nil
77:                 end
78: 
79:                 STDERR.sync = true
80:                 STDOUT.sync = true
81:             end

Public Instance methods

Sets the agent filter

[Source]

     # File lib/mcollective/rpc/client.rb, line 292
292:             def agent_filter(agent)
293:                 @filter["agent"] << agent
294:                 @filter["agent"].compact!
295:                 reset
296:             end

Sets the class filter

[Source]

     # File lib/mcollective/rpc/client.rb, line 268
268:             def class_filter(klass)
269:                 @filter["cf_class"] << klass
270:                 @filter["cf_class"].compact!
271:                 reset
272:             end

Sets the collective we are communicating with

[Source]

     # File lib/mcollective/rpc/client.rb, line 356
356:             def collective=(c)
357:                 @collective = c
358:                 @client.options[:collective] = c
359:             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

[Source]

     # File lib/mcollective/rpc/client.rb, line 226
226:             def custom_request(action, args, expected_agents, filter = {}, &block)
227:                 @ddl.validate_request(action, args) if @ddl
228: 
229:                 @stats.reset
230: 
231:                 custom_filter = Util.empty_filter
232:                 custom_options = options.clone
233: 
234:                 # merge the supplied filter with the standard empty one
235:                 # we could just use the merge method but I want to be sure
236:                 # we dont merge in stuff that isnt actually valid
237:                 ["identity", "fact", "agent", "cf_class"].each do |ftype|
238:                     if filter.include?(ftype)
239:                         custom_filter[ftype] = [filter[ftype], custom_filter[ftype]].flatten
240:                     end
241:                 end
242: 
243:                 # ensure that all filters at least restrict the call to the agent we're a proxy for
244:                 custom_filter["agent"] << @agent unless custom_filter["agent"].include?(@agent)
245:                 custom_options[:filter] = custom_filter
246: 
247:                 # Fake out the stats discovery would have put there
248:                 @stats.discovered_agents([expected_agents].flatten)
249: 
250:                 # Handle fire and forget requests
251:                 if args.include?(:process_results) && args[:process_results] == false
252:                     @filter = custom_filter
253:                     return fire_and_forget_request(action, args)
254:                 end
255: 
256:                 # Now do a call pretty much exactly like in method_missing except with our own
257:                 # options and discovery magic
258:                 if block_given?
259:                     call_agent(action, args, custom_options, [expected_agents].flatten) do |r|
260:                         block.call(r)
261:                     end
262:                 else
263:                     call_agent(action, args, custom_options, [expected_agents].flatten)
264:                 end
265:             end

Disconnects cleanly from the middleware

[Source]

    # File lib/mcollective/rpc/client.rb, line 84
84:             def disconnect
85:                 @client.disconnect
86:             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

[Source]

     # File lib/mcollective/rpc/client.rb, line 324
324:             def discover(flags={})
325:                 flags.include?(:verbose) ? verbose = flags[:verbose] : verbose = @verbose
326: 
327:                 if @discovered_agents == nil
328:                     @stats.time_discovery :start
329: 
330:                     STDERR.print("Determining the amount of hosts matching filter for #{discovery_timeout} seconds .... ") if verbose
331:                     @discovered_agents = @client.discover(@filter, @discovery_timeout)
332:                     STDERR.puts(@discovered_agents.size) if verbose
333: 
334:                     @stats.time_discovery :end
335: 
336:                 end
337: 
338:                 @stats.discovered_agents(@discovered_agents)
339:                 RPC.discovered(@discovered_agents)
340: 
341:                 @discovered_agents
342:             end

Sets the fact filter

[Source]

     # File lib/mcollective/rpc/client.rb, line 275
275:             def fact_filter(fact, value=nil, operator="=")
276:                 return if fact.nil?
277:                 return if fact == false
278: 
279:                 if value.nil?
280:                     parsed = Util.parse_fact_string(fact)
281:                     @filter["fact"] << parsed unless parsed == false
282:                 else
283:                     parsed = Util.parse_fact_string("#{fact}#{operator}#{value}")
284:                     @filter["fact"] << parsed unless parsed == false
285:                 end
286: 
287:                 @filter["fact"].compact!
288:                 reset
289:             end

Returns help for an agent if a DDL was found

[Source]

    # File lib/mcollective/rpc/client.rb, line 89
89:             def help(template)
90:                 if @ddl
91:                     @ddl.help(template)
92:                 else
93:                     return "Can't find DDL for agent '#{@agent}'"
94:                 end
95:             end

Sets the identity filter

[Source]

     # File lib/mcollective/rpc/client.rb, line 299
299:             def identity_filter(identity)
300:                 @filter["identity"] << identity
301:                 @filter["identity"].compact!
302:                 reset
303:             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 363
363:             def limit_targets=(limit)
364:                 if limit.is_a?(String)
365:                     raise "Invalid limit specified: #{limit} valid limits are /^\d+%*$/" unless limit =~ /^\d+%*$/
366:                     @limit_targets = limit
367:                 elsif limit.respond_to?(:to_i)
368:                     limit = limit.to_i
369:                     limit = 1 if limit == 0
370:                     @limit_targets = limit
371:                 else
372:                     raise "Don't know how to handle limit of type #{limit.class}"
373:                 end
374:             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.

[Source]

     # File lib/mcollective/rpc/client.rb, line 177
177:             def method_missing(method_name, *args, &block)
178:                 # set args to an empty hash if nothings given
179:                 args = args[0]
180:                 args = {} if args.nil?
181: 
182:                 action = method_name.to_s
183: 
184:                 @stats.reset
185: 
186:                 @ddl.validate_request(action, args) if @ddl
187: 
188:                 # Handle single target requests by doing discovery and picking
189:                 # a random node.  Then do a custom request specifying a filter
190:                 # that will only match the one node.
191:                 if @limit_targets
192:                     target_nodes = pick_nodes_from_discovered(@limit_targets)
193:                     Log.debug("Picked #{target_nodes.join(',')} as limited target(s)")
194: 
195:                     custom_request(action, args, target_nodes, {"identity" => /^(#{target_nodes.join('|')})$/}, &block)
196:                 else
197:                     # Normal agent requests as per client.action(args)
198:                     call_agent(action, args, options, &block)
199:                 end
200:             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 116
116:             def new_request(action, data)
117:                 callerid = PluginManager["security_plugin"].callerid
118: 
119:                 {:agent  => @agent,
120:                  :action => action,
121:                  :caller => callerid,
122:                  :data   => data}
123:             end

Provides a normal options hash like you would get from Optionparser

[Source]

     # File lib/mcollective/rpc/client.rb, line 346
346:             def options
347:                 {:disctimeout => @discovery_timeout,
348:                  :timeout => @timeout,
349:                  :verbose => @verbose,
350:                  :filter => @filter,
351:                  :collective => @collective,
352:                  :config => @config}
353:             end

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

[Source]

     # File lib/mcollective/rpc/client.rb, line 307
307:             def reset
308:                 @discovered_agents = nil
309:             end

Reet the filter to an empty one

[Source]

     # File lib/mcollective/rpc/client.rb, line 312
312:             def reset_filter
313:                 @filter = Util.empty_filter
314:                 agent_filter @agent
315:             end

[Validate]