Class MCollective::RPC::Agent
In: lib/mcollective/rpc/agent.rb
Parent: Object

A wrapper around the traditional agent, it takes care of a lot of the tedious setup you would do for each agent allowing you to just create methods following a naming standard leaving the heavy lifting up to this clas.

See marionette-collective.org/simplerpc/agents.html

It only really makes sense to use this with a Simple RPC client on the other end, basic usage would be:

   module MCollective
      module Agent
         class Helloworld<RPC::Agent
            matadata :name        => "Test SimpleRPC Agent",
                     :description => "A simple test",
                     :author      => "You",
                     :license     => "1.1",
                     :url         => "http://your.com/,
                     :timeout     => 60

            action "hello" do
                reply[:msg] = "Hello #{request[:name]}"
            end
         end
      end
   end

We also currently have the validation code in here, this will be moved to plugins soon.

Methods

actions   handlemsg   help   help   new  

Attributes

config  [R] 
ddl  [R] 
logger  [R] 
meta  [RW] 
reply  [RW] 
request  [RW] 
timeout  [R] 

Public Class methods

Returns an array of actions this agent support

[Source]

     # File lib/mcollective/rpc/agent.rb, line 128
128:             def self.actions
129:                 public_instance_methods.sort.grep(/_action$/).map do |method|
130:                     $1 if method =~ /(.+)_action$/
131:                 end
132:             end

Generates help using the template based on the data created with metadata and input

[Source]

     # File lib/mcollective/rpc/agent.rb, line 114
114:             def self.help(template)
115:                 if @ddl
116:                     @ddl.help(template)
117:                 else
118:                     "No DDL defined"
119:                 end
120:             end

[Source]

    # File lib/mcollective/rpc/agent.rb, line 34
34:             def initialize
35:                 # Default meta data unset
36:                 @meta = {:timeout     => 10,
37:                          :name        => "Unknown",
38:                          :description => "Unknown",
39:                          :author      => "Unknown",
40:                          :license     => "Unknown",
41:                          :version     => "Unknown",
42:                          :url         => "Unknown"}
43: 
44:                 @timeout = meta[:timeout] || 10
45:                 @logger = Log.instance
46:                 @config = Config.instance
47:                 @agent_name = self.class.to_s.split("::").last.downcase
48: 
49:                 # Loads the DDL so we can later use it for validation
50:                 # and help generation
51:                 begin
52:                     @ddl = DDL.new(@agent_name)
53:                 rescue
54:                     @ddl = nil
55:                 end
56: 
57:                 # if we have a global authorization provider enable it
58:                 # plugins can still override it per plugin
59:                 self.class.authorized_by(@config.rpcauthprovider) if @config.rpcauthorization
60:                 
61:                 startup_hook
62:             end

Public Instance methods

[Source]

     # File lib/mcollective/rpc/agent.rb, line 64
 64:             def handlemsg(msg, connection)
 65:                 @request = RPC.request(msg)
 66:                 @reply = RPC.reply
 67: 
 68:                 begin
 69:                     # Calls the authorization plugin if any is defined
 70:                     # if this raises an exception we wil just skip processing this 
 71:                     # message
 72:                     authorization_hook(@request) if respond_to?("authorization_hook")
 73: 
 74: 
 75:                     # Audits the request, currently continues processing the message
 76:                     # we should make this a configurable so that an audit failure means
 77:                     # a message wont be processed by this node depending on config
 78:                     audit_request(@request, connection)
 79: 
 80:                     before_processing_hook(msg, connection)
 81: 
 82:                     if respond_to?("#{@request.action}_action")
 83:                         send("#{@request.action}_action")
 84:                     else
 85:                         raise UnknownRPCAction, "Unknown action: #{@request.action}"
 86:                     end
 87:                 rescue RPCAborted => e
 88:                     @reply.fail e.to_s, 1
 89: 
 90:                 rescue UnknownRPCAction => e
 91:                     @reply.fail e.to_s, 2
 92: 
 93:                 rescue MissingRPCData => e
 94:                     @reply.fail e.to_s, 3
 95: 
 96:                 rescue InvalidRPCData => e
 97:                     @reply.fail e.to_s, 4
 98: 
 99:                 rescue UnknownRPCError => e
100:                     @reply.fail e.to_s, 5
101: 
102:                 rescue Exception => e
103:                     @reply.fail e.to_s, 5
104: 
105:                 end
106: 
107:                 after_processing_hook
108: 
109:                 @reply.to_hash
110:             end

to auto generate help

[Source]

     # File lib/mcollective/rpc/agent.rb, line 123
123:             def help
124:                 self.help("#{@config[:configdir]}/rpc-help.erb")
125:             end

[Validate]