| 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
action "foo" do
implemented_by "/some/script.sh"
end
end
end
end
If you wish to implement the logic for an action using an external script use the implemented_by method that will cause your script to be run with 2 arguments.
The first argument is a file containing JSON with the request and the 2nd argument is where the script should save its output as a JSON hash.
We also currently have the validation code in here, this will be moved to plugins soon.
| config | [R] | |
| ddl | [R] | |
| logger | [R] | |
| meta | [RW] | |
| reply | [RW] | |
| request | [RW] | |
| timeout | [R] |
# File lib/mcollective/rpc/agent.rb, line 44
44: def initialize
45: # Default meta data unset
46: @meta = {:timeout => 10,
47: :name => "Unknown",
48: :description => "Unknown",
49: :author => "Unknown",
50: :license => "Unknown",
51: :version => "Unknown",
52: :url => "Unknown"}
53:
54: @timeout = meta[:timeout] || 10
55: @logger = Log.instance
56: @config = Config.instance
57: @agent_name = self.class.to_s.split("::").last.downcase
58:
59: # Loads the DDL so we can later use it for validation
60: # and help generation
61: begin
62: @ddl = DDL.new(@agent_name)
63: rescue
64: @ddl = nil
65: end
66:
67: # if we have a global authorization provider enable it
68: # plugins can still override it per plugin
69: self.class.authorized_by(@config.rpcauthprovider) if @config.rpcauthorization
70:
71: startup_hook
72: end
# File lib/mcollective/rpc/agent.rb, line 74
74: def handlemsg(msg, connection)
75: @request = RPC.request(msg)
76: @reply = RPC.reply
77:
78: begin
79: # Calls the authorization plugin if any is defined
80: # if this raises an exception we wil just skip processing this
81: # message
82: authorization_hook(@request) if respond_to?("authorization_hook")
83:
84:
85: # Audits the request, currently continues processing the message
86: # we should make this a configurable so that an audit failure means
87: # a message wont be processed by this node depending on config
88: audit_request(@request, connection)
89:
90: before_processing_hook(msg, connection)
91:
92: if respond_to?("#{@request.action}_action")
93: send("#{@request.action}_action")
94: else
95: raise UnknownRPCAction, "Unknown action: #{@request.action}"
96: end
97: rescue RPCAborted => e
98: @reply.fail e.to_s, 1
99:
100: rescue UnknownRPCAction => e
101: @reply.fail e.to_s, 2
102:
103: rescue MissingRPCData => e
104: @reply.fail e.to_s, 3
105:
106: rescue InvalidRPCData => e
107: @reply.fail e.to_s, 4
108:
109: rescue UnknownRPCError => e
110: @reply.fail e.to_s, 5
111:
112: rescue Exception => e
113: @reply.fail e.to_s, 5
114:
115: end
116:
117: after_processing_hook
118:
119: if @request.should_respond?
120: return @reply.to_hash
121: else
122: Log.debug("Client did not request a response, surpressing reply")
123: return nil
124: end
125: end