1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6 <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
8 <title>Class: MCollective::RPC::Agent</title>
10 <link rel="stylesheet" href="../../rdoc.css" type="text/css" media="screen" />
12 <script src="../../js/jquery.js" type="text/javascript"
13 charset="utf-8"></script>
14 <script src="../../js/thickbox-compressed.js" type="text/javascript"
15 charset="utf-8"></script>
16 <script src="../../js/quicksearch.js" type="text/javascript"
17 charset="utf-8"></script>
18 <script src="../../js/darkfish.js" type="text/javascript"
19 charset="utf-8"></script>
25 <div id="file-metadata">
26 <div id="file-list-section" class="section">
27 <h3 class="section-header">In Files</h3>
28 <div class="section-body">
31 <li><a href="../../lib/mcollective/rpc/agent_rb.html?TB_iframe=true&height=550&width=785"
32 class="thickbox" title="lib/mcollective/rpc/agent.rb">lib/mcollective/rpc/agent.rb</a></li>
41 <div id="class-metadata">
45 <div id="parent-class-section" class="section">
46 <h3 class="section-header">Parent</h3>
48 <p class="link"><a href="../../Object.html">Object</a></p>
53 <!-- Namespace Contents -->
56 <!-- Method Quickref -->
58 <div id="method-list-section" class="section">
59 <h3 class="section-header">Methods</h3>
60 <ul class="link-list">
62 <li><a href="#M000411">::action</a></li>
64 <li><a href="#M000405">::actions</a></li>
66 <li><a href="#M000404">::activate?</a></li>
68 <li><a href="#M000410">::activate_when</a></li>
70 <li><a href="#M000412">::authorized_by</a></li>
72 <li><a href="#M000407">::metadata</a></li>
74 <li><a href="#M000393">::new</a></li>
76 <li><a href="#M000418">#after_processing_hook</a></li>
78 <li><a href="#M000419">#audit_request</a></li>
80 <li><a href="#M000417">#before_processing_hook</a></li>
82 <li><a href="#M000395">#handlemsg</a></li>
84 <li><a href="#M000415">#implemented_by</a></li>
86 <li><a href="#M000394">#load_ddl</a></li>
88 <li><a href="#M000406">#run</a></li>
90 <li><a href="#M000414">#shellescape</a></li>
92 <li><a href="#M000416">#startup_hook</a></li>
94 <li><a href="#M000413">#validate</a></li>
100 <!-- Included Modules -->
102 <div id="includes-section" class="section">
103 <h3 class="section-header">Included Modules</h3>
104 <ul class="link-list">
107 <li><a class="include" href="../Validator.html">MCollective::Validator</a></li>
115 <div id="project-metadata">
118 <div id="fileindex-section" class="section project-section">
119 <h3 class="section-header">Files</h3>
122 <li class="file"><a href="../../COPYING.html">COPYING</a></li>
124 <li class="file"><a href="../../Gemfile.html">Gemfile</a></li>
126 <li class="file"><a href="../../README.html">README</a></li>
128 <li class="file"><a href="../../Rakefile.html">Rakefile</a></li>
130 <li class="file"><a href="../../etc/client_cfg_dist.html">client.cfg.dist</a></li>
132 <li class="file"><a href="../../etc/data-help_erb.html">data-help.erb</a></li>
134 <li class="file"><a href="../../etc/discovery-help_erb.html">discovery-help.erb</a></li>
136 <li class="file"><a href="../../etc/facts_yaml_dist.html">facts.yaml.dist</a></li>
138 <li class="file"><a href="../../etc/metadata-help_erb.html">metadata-help.erb</a></li>
140 <li class="file"><a href="../../etc/msg-help_erb.html">msg-help.erb</a></li>
142 <li class="file"><a href="../../etc/rpc-help_erb.html">rpc-help.erb</a></li>
144 <li class="file"><a href="../../etc/server_cfg_dist.html">server.cfg.dist</a></li>
146 <li class="file"><a href="../../etc/ssl/PLACEHOLDER.html">PLACEHOLDER</a></li>
148 <li class="file"><a href="../../etc/ssl/clients/PLACEHOLDER.html">PLACEHOLDER</a></li>
150 <li class="file"><a href="../../lib/mcollective/generators/templates/action_snippet_erb.html">action_snippet.erb</a></li>
152 <li class="file"><a href="../../lib/mcollective/generators/templates/data_input_snippet_erb.html">data_input_snippet.erb</a></li>
154 <li class="file"><a href="../../lib/mcollective/generators/templates/ddl_erb.html">ddl.erb</a></li>
156 <li class="file"><a href="../../lib/mcollective/generators/templates/plugin_erb.html">plugin.erb</a></li>
158 <li class="file"><a href="../../lib/mcollective/locales/en_yml.html">en.yml</a></li>
160 <li class="file"><a href="../../mcollective_init.html">mcollective.init</a></li>
166 <div id="classindex-section" class="section project-section">
167 <h3 class="section-header">Class Index
168 <span class="search-toggle"><img src="../../images/find.png"
169 height="16" width="16" alt="[+]"
170 title="show/hide quicksearch" /></span></h3>
171 <form action="#" method="get" accept-charset="utf-8" class="initially-hidden">
173 <legend>Quicksearch</legend>
174 <input type="text" name="quicksearch" value=""
175 class="quicksearch-field" />
179 <ul class="link-list">
181 <li><a href="../../MCollective.html">MCollective</a></li>
183 <li><a href="../../MCollective/Agent.html">MCollective::Agent</a></li>
185 <li><a href="../../MCollective/Agents.html">MCollective::Agents</a></li>
187 <li><a href="../../MCollective/Aggregate.html">MCollective::Aggregate</a></li>
189 <li><a href="../../MCollective/Aggregate/Base.html">MCollective::Aggregate::Base</a></li>
191 <li><a href="../../MCollective/Aggregate/Result.html">MCollective::Aggregate::Result</a></li>
193 <li><a href="../../MCollective/Aggregate/Result/Base.html">MCollective::Aggregate::Result::Base</a></li>
195 <li><a href="../../MCollective/Aggregate/Result/CollectionResult.html">MCollective::Aggregate::Result::CollectionResult</a></li>
197 <li><a href="../../MCollective/Aggregate/Result/NumericResult.html">MCollective::Aggregate::Result::NumericResult</a></li>
199 <li><a href="../../MCollective/Application.html">MCollective::Application</a></li>
201 <li><a href="../../MCollective/Applications.html">MCollective::Applications</a></li>
203 <li><a href="../../MCollective/Cache.html">MCollective::Cache</a></li>
205 <li><a href="../../MCollective/Client.html">MCollective::Client</a></li>
207 <li><a href="../../MCollective/CodedError.html">MCollective::CodedError</a></li>
209 <li><a href="../../MCollective/Config.html">MCollective::Config</a></li>
211 <li><a href="../../MCollective/Connector.html">MCollective::Connector</a></li>
213 <li><a href="../../MCollective/Connector/Base.html">MCollective::Connector::Base</a></li>
215 <li><a href="../../MCollective/DDL.html">MCollective::DDL</a></li>
217 <li><a href="../../MCollective/DDL/AgentDDL.html">MCollective::DDL::AgentDDL</a></li>
219 <li><a href="../../MCollective/DDL/Base.html">MCollective::DDL::Base</a></li>
221 <li><a href="../../MCollective/DDL/DataDDL.html">MCollective::DDL::DataDDL</a></li>
223 <li><a href="../../MCollective/DDL/DiscoveryDDL.html">MCollective::DDL::DiscoveryDDL</a></li>
225 <li><a href="../../MCollective/DDL/ValidatorDDL.html">MCollective::DDL::ValidatorDDL</a></li>
227 <li><a href="../../MCollective/DDLValidationError.html">MCollective::DDLValidationError</a></li>
229 <li><a href="../../MCollective/Data.html">MCollective::Data</a></li>
231 <li><a href="../../MCollective/Data/Base.html">MCollective::Data::Base</a></li>
233 <li><a href="../../MCollective/Data/Result.html">MCollective::Data::Result</a></li>
235 <li><a href="../../MCollective/Discovery.html">MCollective::Discovery</a></li>
237 <li><a href="../../MCollective/Facts.html">MCollective::Facts</a></li>
239 <li><a href="../../MCollective/Facts/Base.html">MCollective::Facts::Base</a></li>
241 <li><a href="../../MCollective/Generators.html">MCollective::Generators</a></li>
243 <li><a href="../../MCollective/Generators/AgentGenerator.html">MCollective::Generators::AgentGenerator</a></li>
245 <li><a href="../../MCollective/Generators/Base.html">MCollective::Generators::Base</a></li>
247 <li><a href="../../MCollective/Generators/DataGenerator.html">MCollective::Generators::DataGenerator</a></li>
249 <li><a href="../../MCollective/InvalidRPCData.html">MCollective::InvalidRPCData</a></li>
251 <li><a href="../../MCollective/Log.html">MCollective::Log</a></li>
253 <li><a href="../../MCollective/Logger.html">MCollective::Logger</a></li>
255 <li><a href="../../MCollective/Logger/Base.html">MCollective::Logger::Base</a></li>
257 <li><a href="../../MCollective/Logger/Console_logger.html">MCollective::Logger::Console_logger</a></li>
259 <li><a href="../../MCollective/Logger/File_logger.html">MCollective::Logger::File_logger</a></li>
261 <li><a href="../../MCollective/Logger/Syslog_logger.html">MCollective::Logger::Syslog_logger</a></li>
263 <li><a href="../../MCollective/Matcher.html">MCollective::Matcher</a></li>
265 <li><a href="../../MCollective/Matcher/Parser.html">MCollective::Matcher::Parser</a></li>
267 <li><a href="../../MCollective/Matcher/Scanner.html">MCollective::Matcher::Scanner</a></li>
269 <li><a href="../../MCollective/Message.html">MCollective::Message</a></li>
271 <li><a href="../../MCollective/MissingRPCData.html">MCollective::MissingRPCData</a></li>
273 <li><a href="../../MCollective/MsgDoesNotMatchRequestID.html">MCollective::MsgDoesNotMatchRequestID</a></li>
275 <li><a href="../../MCollective/MsgTTLExpired.html">MCollective::MsgTTLExpired</a></li>
277 <li><a href="../../MCollective/NotTargettedAtUs.html">MCollective::NotTargettedAtUs</a></li>
279 <li><a href="../../MCollective/Optionparser.html">MCollective::Optionparser</a></li>
281 <li><a href="../../MCollective/PluginManager.html">MCollective::PluginManager</a></li>
283 <li><a href="../../MCollective/PluginPackager.html">MCollective::PluginPackager</a></li>
285 <li><a href="../../MCollective/PluginPackager/AgentDefinition.html">MCollective::PluginPackager::AgentDefinition</a></li>
287 <li><a href="../../MCollective/PluginPackager/StandardDefinition.html">MCollective::PluginPackager::StandardDefinition</a></li>
289 <li><a href="../../MCollective/RPC.html">MCollective::RPC</a></li>
291 <li><a href="../../MCollective/RPC/ActionRunner.html">MCollective::RPC::ActionRunner</a></li>
293 <li><a href="../../MCollective/RPC/Agent.html">MCollective::RPC::Agent</a></li>
295 <li><a href="../../MCollective/RPC/Audit.html">MCollective::RPC::Audit</a></li>
297 <li><a href="../../MCollective/RPC/Client.html">MCollective::RPC::Client</a></li>
299 <li><a href="../../MCollective/RPC/Helpers.html">MCollective::RPC::Helpers</a></li>
301 <li><a href="../../MCollective/RPC/Progress.html">MCollective::RPC::Progress</a></li>
303 <li><a href="../../MCollective/RPC/Reply.html">MCollective::RPC::Reply</a></li>
305 <li><a href="../../MCollective/RPC/Request.html">MCollective::RPC::Request</a></li>
307 <li><a href="../../MCollective/RPC/Result.html">MCollective::RPC::Result</a></li>
309 <li><a href="../../MCollective/RPC/Stats.html">MCollective::RPC::Stats</a></li>
311 <li><a href="../../MCollective/RPCAborted.html">MCollective::RPCAborted</a></li>
313 <li><a href="../../MCollective/RPCError.html">MCollective::RPCError</a></li>
315 <li><a href="../../MCollective/Registration.html">MCollective::Registration</a></li>
317 <li><a href="../../MCollective/Registration/Base.html">MCollective::Registration::Base</a></li>
319 <li><a href="../../MCollective/Runner.html">MCollective::Runner</a></li>
321 <li><a href="../../MCollective/RunnerStats.html">MCollective::RunnerStats</a></li>
323 <li><a href="../../MCollective/SSL.html">MCollective::SSL</a></li>
325 <li><a href="../../MCollective/Security.html">MCollective::Security</a></li>
327 <li><a href="../../MCollective/Security/Base.html">MCollective::Security::Base</a></li>
329 <li><a href="../../MCollective/SecurityValidationFailed.html">MCollective::SecurityValidationFailed</a></li>
331 <li><a href="../../MCollective/Shell.html">MCollective::Shell</a></li>
333 <li><a href="../../MCollective/Translatable.html">MCollective::Translatable</a></li>
335 <li><a href="../../MCollective/UnixDaemon.html">MCollective::UnixDaemon</a></li>
337 <li><a href="../../MCollective/UnknownRPCAction.html">MCollective::UnknownRPCAction</a></li>
339 <li><a href="../../MCollective/UnknownRPCError.html">MCollective::UnknownRPCError</a></li>
341 <li><a href="../../MCollective/Util.html">MCollective::Util</a></li>
343 <li><a href="../../MCollective/Validator.html">MCollective::Validator</a></li>
345 <li><a href="../../MCollective/ValidatorError.html">MCollective::ValidatorError</a></li>
347 <li><a href="../../MCollective/WindowsDaemon.html">MCollective::WindowsDaemon</a></li>
349 <li><a href="../../Array.html">Array</a></li>
351 <li><a href="../../Dir.html">Dir</a></li>
353 <li><a href="../../Object.html">Object</a></li>
355 <li><a href="../../String.html">String</a></li>
357 <li><a href="../../Symbol.html">Symbol</a></li>
360 <div id="no-class-search-results" style="display: none;">No matching classes.</div>
367 <div id="documentation">
368 <h1 class="class">MCollective::RPC::Agent</h1>
370 <div id="description">
372 A wrapper around the traditional agent, it takes care of a lot of the
373 tedious setup you would do for each agent allowing you to just create
374 methods following a naming standard leaving the heavy lifting up to this
379 href="http://marionette-collective.org/simplerpc/agents.html">marionette-collective.org/simplerpc/agents.html</a>
382 It only really makes sense to use this with a Simple <a
383 href="../RPC.html">RPC</a> client on the other end, basic usage would be:
388 class Helloworld<RPC::Agent
389 action "hello" do
390 reply[:msg] = "Hello #{request[:name]}"
393 action "foo" do
394 implemented_by "/some/script.sh"
401 If you wish to implement the logic for an action using an external script
402 use the <a href="Agent.html#M000415">implemented_by</a> method that will
403 cause your script to be run with 2 arguments.
406 The first argument is a file containing JSON with the request and the 2nd
407 argument is where the script should save its output as a JSON hash.
410 We also currently have the validation code in here, this will be moved to
421 <div id="attribute-method-details" class="method-section section">
422 <h3 class="section-header">Attributes</h3>
425 <div id="reply-attribute-method" class="method-detail">
428 <a name="reply="></a>
430 <div class="method-heading attribute-method-heading">
431 <span class="method-name">reply</span><span
432 class="attribute-access-type">[RW]</span>
435 <div class="method-description">
437 <p class="missing-docs">(Not documented)</p>
442 <div id="request-attribute-method" class="method-detail">
443 <a name="request"></a>
445 <a name="request="></a>
447 <div class="method-heading attribute-method-heading">
448 <span class="method-name">request</span><span
449 class="attribute-access-type">[RW]</span>
452 <div class="method-description">
454 <p class="missing-docs">(Not documented)</p>
459 <div id="agent-name-attribute-method" class="method-detail">
460 <a name="agent_name"></a>
462 <a name="agent_name="></a>
464 <div class="method-heading attribute-method-heading">
465 <span class="method-name">agent_name</span><span
466 class="attribute-access-type">[RW]</span>
469 <div class="method-description">
471 <p class="missing-docs">(Not documented)</p>
476 <div id="logger-attribute-method" class="method-detail">
477 <a name="logger"></a>
479 <div class="method-heading attribute-method-heading">
480 <span class="method-name">logger</span><span
481 class="attribute-access-type">[R]</span>
484 <div class="method-description">
486 <p class="missing-docs">(Not documented)</p>
491 <div id="config-attribute-method" class="method-detail">
492 <a name="config"></a>
494 <div class="method-heading attribute-method-heading">
495 <span class="method-name">config</span><span
496 class="attribute-access-type">[R]</span>
499 <div class="method-description">
501 <p class="missing-docs">(Not documented)</p>
506 <div id="timeout-attribute-method" class="method-detail">
507 <a name="timeout"></a>
509 <div class="method-heading attribute-method-heading">
510 <span class="method-name">timeout</span><span
511 class="attribute-access-type">[R]</span>
514 <div class="method-description">
516 <p class="missing-docs">(Not documented)</p>
521 <div id="ddl-attribute-method" class="method-detail">
524 <div class="method-heading attribute-method-heading">
525 <span class="method-name">ddl</span><span
526 class="attribute-access-type">[R]</span>
529 <div class="method-description">
531 <p class="missing-docs">(Not documented)</p>
536 <div id="meta-attribute-method" class="method-detail">
539 <div class="method-heading attribute-method-heading">
540 <span class="method-name">meta</span><span
541 class="attribute-access-type">[R]</span>
544 <div class="method-description">
546 <p class="missing-docs">(Not documented)</p>
556 <div id="public-class-method-details" class="method-section section">
557 <h3 class="section-header">Public Class Methods</h3>
560 <div id="actions-method" class="method-detail ">
561 <a name="M000405"></a>
563 <div class="method-heading">
565 <span class="method-name">actions</span><span
566 class="method-args">()</span>
567 <span class="method-click-advice">click to toggle source</span>
571 <div class="method-description">
574 Returns an array of actions this agent support
579 <div class="method-source-code"
582 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 157</span>
583 157: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">actions</span>
584 158: <span class="ruby-identifier">public_instance_methods</span>.<span class="ruby-identifier">sort</span>.<span class="ruby-identifier">grep</span>(<span class="ruby-regexp re">/_action$/</span>).<span class="ruby-identifier">map</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">method</span><span class="ruby-operator">|</span>
585 159: <span class="ruby-identifier">$1</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">method</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp re">/(.+)_action$/</span>
586 160: <span class="ruby-keyword kw">end</span>
587 161: <span class="ruby-keyword kw">end</span></pre>
596 <div id="activate--method" class="method-detail ">
597 <a name="M000404"></a>
599 <div class="method-heading">
601 <span class="method-name">activate?</span><span
602 class="method-args">()</span>
603 <span class="method-click-advice">click to toggle source</span>
607 <div class="method-description">
610 By default <a href="../RPC.html">RPC</a> <a
611 href="../Agents.html">Agents</a> support a toggle in the configuration that
612 can enable and disable them based on the agent name
615 Example an agent called Foo can have:
618 plugin.foo.activate_agent = false
621 and this will prevent the agent from loading on this particular machine.
624 <a href="../Agents.html">Agents</a> can use the <a
625 href="Agent.html#M000410">activate_when</a> helper to override this for
629 <a href="Agent.html#M000410">activate_when</a> do
632 File.exist?("/usr/bin/puppet")
640 <div class="method-source-code"
641 id="activate--source">
643 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 142</span>
644 142: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">activate?</span>
645 143: <span class="ruby-identifier">agent_name</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">split</span>(<span class="ruby-value str">"::"</span>).<span class="ruby-identifier">last</span>.<span class="ruby-identifier">downcase</span>
647 145: <span class="ruby-identifier">log_code</span>(<span class="ruby-identifier">:PLMC37</span>, <span class="ruby-value str">"Starting default activation checks for the '%{agent}' agent"</span>, <span class="ruby-identifier">:debug</span>, <span class="ruby-identifier">:agent</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">agent_name</span>)
649 147: <span class="ruby-identifier">should_activate</span> = <span class="ruby-constant">Util</span>.<span class="ruby-identifier">str_to_bool</span>(<span class="ruby-constant">Config</span>.<span class="ruby-identifier">instance</span>.<span class="ruby-identifier">pluginconf</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-node">"#{agent_name}.activate_agent"</span>, <span class="ruby-keyword kw">true</span>))
651 149: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">should_activate</span>
652 150: <span class="ruby-identifier">log_code</span>(<span class="ruby-identifier">:PLMC38</span>, <span class="ruby-value str">"Found plugin configuration '%{agent}.activate_agent' with value '%{should_activate}'"</span>, <span class="ruby-identifier">:debug</span>, <span class="ruby-identifier">:agent</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">agent_name</span>, <span class="ruby-identifier">:should_activate</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">should_activate</span>)
653 151: <span class="ruby-keyword kw">end</span>
655 153: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">should_activate</span>
656 154: <span class="ruby-keyword kw">end</span></pre>
665 <div id="new-method" class="method-detail ">
666 <a name="M000393"></a>
668 <div class="method-heading">
670 <span class="method-name">new</span><span
671 class="method-args">()</span>
672 <span class="method-click-advice">click to toggle source</span>
676 <div class="method-description">
678 <p class="missing-docs">(Not documented)</p>
682 <div class="method-source-code"
685 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 40</span>
686 40: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>
687 41: <span class="ruby-ivar">@agent_name</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">split</span>(<span class="ruby-value str">"::"</span>).<span class="ruby-identifier">last</span>.<span class="ruby-identifier">downcase</span>
689 43: <span class="ruby-identifier">load_ddl</span>
691 45: <span class="ruby-ivar">@logger</span> = <span class="ruby-constant">Log</span>.<span class="ruby-identifier">instance</span>
692 46: <span class="ruby-ivar">@config</span> = <span class="ruby-constant">Config</span>.<span class="ruby-identifier">instance</span>
694 48: <span class="ruby-comment cmt"># if we have a global authorization provider enable it</span>
695 49: <span class="ruby-comment cmt"># plugins can still override it per plugin</span>
696 50: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">authorized_by</span>(<span class="ruby-ivar">@config</span>.<span class="ruby-identifier">rpcauthprovider</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@config</span>.<span class="ruby-identifier">rpcauthorization</span>
698 52: <span class="ruby-identifier">startup_hook</span>
699 53: <span class="ruby-keyword kw">end</span></pre>
710 <div id="private-class-method-details" class="method-section section">
711 <h3 class="section-header">Private Class Methods</h3>
714 <div id="action-method" class="method-detail ">
715 <a name="M000411"></a>
717 <div class="method-heading">
719 <span class="method-name">action</span><span
720 class="method-args">(name, &block)</span>
721 <span class="method-click-advice">click to toggle source</span>
725 <div class="method-description">
728 Creates a new action with the block passed and sets some defaults
731 action “status” do
734 # logic here to restart service
742 <div class="method-source-code"
745 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 260</span>
746 260: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">action</span>(<span class="ruby-identifier">name</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
747 261: <span class="ruby-identifier">raise</span> <span class="ruby-value str">"Need to pass a body for the action"</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">block_given?</span>
749 263: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">module_eval</span> { <span class="ruby-identifier">define_method</span>(<span class="ruby-node">"#{name}_action"</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) }
750 264: <span class="ruby-keyword kw">end</span></pre>
759 <div id="activate-when-method" class="method-detail ">
760 <a name="M000410"></a>
762 <div class="method-heading">
764 <span class="method-name">activate_when</span><span
765 class="method-args">(&block)</span>
766 <span class="method-click-advice">click to toggle source</span>
770 <div class="method-description">
773 Creates the needed activate? class in a manner similar to the other helpers
774 like action, <a href="Agent.html#M000412">authorized_by</a> etc
777 <a href="Agent.html#M000410">activate_when</a> do
780 File.exist?("/usr/bin/puppet")
788 <div class="method-source-code"
789 id="activate-when-source">
791 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 249</span>
792 249: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">activate_when</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
793 250: (<span class="ruby-keyword kw">class</span> <span class="ruby-operator"><<</span> <span class="ruby-keyword kw">self</span>; <span class="ruby-keyword kw">self</span>; <span class="ruby-keyword kw">end</span>).<span class="ruby-identifier">instance_eval</span> <span class="ruby-keyword kw">do</span>
794 251: <span class="ruby-identifier">define_method</span>(<span class="ruby-value str">"activate?"</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
795 252: <span class="ruby-keyword kw">end</span>
796 253: <span class="ruby-keyword kw">end</span></pre>
805 <div id="authorized-by-method" class="method-detail ">
806 <a name="M000412"></a>
808 <div class="method-heading">
810 <span class="method-name">authorized_by</span><span
811 class="method-args">(plugin)</span>
812 <span class="method-click-advice">click to toggle source</span>
816 <div class="method-description">
819 Helper that creates a method on the class that will call your authorization
820 plugin. If your plugin raises an exception that will abort the request
825 <div class="method-source-code"
826 id="authorized-by-source">
828 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 268</span>
829 268: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">authorized_by</span>(<span class="ruby-identifier">plugin</span>)
830 269: <span class="ruby-identifier">plugin</span> = <span class="ruby-identifier">plugin</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">capitalize</span>
832 271: <span class="ruby-comment cmt"># turns foo_bar into FooBar</span>
833 272: <span class="ruby-identifier">plugin</span> = <span class="ruby-identifier">plugin</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">split</span>(<span class="ruby-value str">"_"</span>).<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">v</span>.<span class="ruby-identifier">capitalize</span>}.<span class="ruby-identifier">join</span>
834 273: <span class="ruby-identifier">pluginname</span> = <span class="ruby-node">"MCollective::Util::#{plugin}"</span>
836 275: <span class="ruby-constant">PluginManager</span>.<span class="ruby-identifier">loadclass</span>(<span class="ruby-identifier">pluginname</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-constant">MCollective</span><span class="ruby-operator">::</span><span class="ruby-constant">Util</span>.<span class="ruby-identifier">constants</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">plugin</span>)
838 277: <span class="ruby-identifier">class_eval</span>(<span class="ruby-node">"
839 278: def authorization_hook(request)
840 279: #{pluginname}.authorize(request)
843 282: <span class="ruby-keyword kw">end</span></pre>
852 <div id="metadata-method" class="method-detail ">
853 <a name="M000407"></a>
855 <div class="method-heading">
857 <span class="method-name">metadata</span><span
858 class="method-args">(data)</span>
859 <span class="method-click-advice">click to toggle source</span>
863 <div class="method-description">
866 Registers meta data for the introspection hash
871 <div class="method-source-code"
872 id="metadata-source">
874 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 237</span>
875 237: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">metadata</span>(<span class="ruby-identifier">data</span>)
876 238: <span class="ruby-identifier">agent</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">basename</span>(<span class="ruby-identifier">caller</span>.<span class="ruby-identifier">first</span>).<span class="ruby-identifier">split</span>(<span class="ruby-value str">":"</span>).<span class="ruby-identifier">first</span>
878 240: <span class="ruby-identifier">log_code</span>(<span class="ruby-identifier">:PLMC34</span>, <span class="ruby-value str">"setting meta data in agents have been deprecated, DDL files are now being used for this information. Please update the '%{agent}' agent"</span>, <span class="ruby-identifier">:warn</span>, <span class="ruby-identifier">:agent</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">agent</span>)
879 241: <span class="ruby-keyword kw">end</span></pre>
890 <div id="public-instance-method-details" class="method-section section">
891 <h3 class="section-header">Public Instance Methods</h3>
894 <div id="handlemsg-method" class="method-detail ">
895 <a name="M000395"></a>
897 <div class="method-heading">
899 <span class="method-name">handlemsg</span><span
900 class="method-args">(msg, connection)</span>
901 <span class="method-click-advice">click to toggle source</span>
905 <div class="method-description">
907 <p class="missing-docs">(Not documented)</p>
911 <div class="method-source-code"
912 id="handlemsg-source">
914 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 64</span>
915 64: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">handlemsg</span>(<span class="ruby-identifier">msg</span>, <span class="ruby-identifier">connection</span>)
916 65: <span class="ruby-ivar">@request</span> = <span class="ruby-constant">RPC</span><span class="ruby-operator">::</span><span class="ruby-constant">Request</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">msg</span>, <span class="ruby-ivar">@ddl</span>)
917 66: <span class="ruby-ivar">@reply</span> = <span class="ruby-constant">RPC</span><span class="ruby-operator">::</span><span class="ruby-constant">Reply</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@request</span>.<span class="ruby-identifier">action</span>, <span class="ruby-ivar">@ddl</span>)
919 68: <span class="ruby-keyword kw">begin</span>
920 69: <span class="ruby-comment cmt"># Incoming requests need to be validated against the DDL thus reusing</span>
921 70: <span class="ruby-comment cmt"># all the work users put into creating DDLs and creating a consistent</span>
922 71: <span class="ruby-comment cmt"># quality of input validation everywhere with the a simple once off</span>
923 72: <span class="ruby-comment cmt"># investment of writing a DDL</span>
924 73: <span class="ruby-ivar">@request</span>.<span class="ruby-identifier">validate!</span>
926 75: <span class="ruby-comment cmt"># Calls the authorization plugin if any is defined</span>
927 76: <span class="ruby-comment cmt"># if this raises an exception we wil just skip processing this</span>
928 77: <span class="ruby-comment cmt"># message</span>
929 78: <span class="ruby-identifier">authorization_hook</span>(<span class="ruby-ivar">@request</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">respond_to?</span>(<span class="ruby-value str">"authorization_hook"</span>)
931 80: <span class="ruby-comment cmt"># Audits the request, currently continues processing the message</span>
932 81: <span class="ruby-comment cmt"># we should make this a configurable so that an audit failure means</span>
933 82: <span class="ruby-comment cmt"># a message wont be processed by this node depending on config</span>
934 83: <span class="ruby-identifier">audit_request</span>(<span class="ruby-ivar">@request</span>, <span class="ruby-identifier">connection</span>)
936 85: <span class="ruby-identifier">before_processing_hook</span>(<span class="ruby-identifier">msg</span>, <span class="ruby-identifier">connection</span>)
938 87: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">respond_to?</span>(<span class="ruby-node">"#{@request.action}_action"</span>)
939 88: <span class="ruby-identifier">send</span>(<span class="ruby-node">"#{@request.action}_action"</span>)
940 89: <span class="ruby-keyword kw">else</span>
941 90: <span class="ruby-identifier">log_code</span>(<span class="ruby-identifier">:PLMC36</span>, <span class="ruby-value str">"Unknown action '%{action}' for agent '%{agent}'"</span>, <span class="ruby-identifier">:warn</span>, <span class="ruby-identifier">:action</span> =<span class="ruby-operator">></span> <span class="ruby-ivar">@request</span>.<span class="ruby-identifier">action</span>, <span class="ruby-identifier">:agent</span> =<span class="ruby-operator">></span> <span class="ruby-ivar">@request</span>.<span class="ruby-identifier">agent</span>)
942 91: <span class="ruby-identifier">raise</span> <span class="ruby-constant">UnknownRPCAction</span>, <span class="ruby-node">"Unknown action '#{@request.action}' for agent '#{@request.agent}'"</span>
943 92: <span class="ruby-keyword kw">end</span>
944 93: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">RPCAborted</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
945 94: <span class="ruby-ivar">@reply</span>.<span class="ruby-identifier">fail</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-value">1</span>
947 96: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">UnknownRPCAction</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
948 97: <span class="ruby-ivar">@reply</span>.<span class="ruby-identifier">fail</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-value">2</span>
950 99: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">MissingRPCData</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
951 100: <span class="ruby-ivar">@reply</span>.<span class="ruby-identifier">fail</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-value">3</span>
953 102: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">InvalidRPCData</span>, <span class="ruby-constant">DDLValidationError</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
954 103: <span class="ruby-ivar">@reply</span>.<span class="ruby-identifier">fail</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-value">4</span>
956 105: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">UnknownRPCError</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
957 106: <span class="ruby-constant">Log</span>.<span class="ruby-identifier">error</span>(<span class="ruby-value str">"%s#%s failed: %s: %s"</span> <span class="ruby-operator">%</span> [<span class="ruby-ivar">@agent_name</span>, <span class="ruby-ivar">@request</span>.<span class="ruby-identifier">action</span>, <span class="ruby-identifier">e</span>.<span class="ruby-identifier">class</span>, <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_s</span>])
958 107: <span class="ruby-constant">Log</span>.<span class="ruby-identifier">error</span>(<span class="ruby-identifier">e</span>.<span class="ruby-identifier">backtrace</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">"\n\t"</span>))
959 108: <span class="ruby-ivar">@reply</span>.<span class="ruby-identifier">fail</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-value">5</span>
961 110: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
962 111: <span class="ruby-constant">Log</span>.<span class="ruby-identifier">error</span>(<span class="ruby-value str">"%s#%s failed: %s: %s"</span> <span class="ruby-operator">%</span> [<span class="ruby-ivar">@agent_name</span>, <span class="ruby-ivar">@request</span>.<span class="ruby-identifier">action</span>, <span class="ruby-identifier">e</span>.<span class="ruby-identifier">class</span>, <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_s</span>])
963 112: <span class="ruby-constant">Log</span>.<span class="ruby-identifier">error</span>(<span class="ruby-identifier">e</span>.<span class="ruby-identifier">backtrace</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">"\n\t"</span>))
964 113: <span class="ruby-ivar">@reply</span>.<span class="ruby-identifier">fail</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-value">5</span>
966 115: <span class="ruby-keyword kw">end</span>
968 117: <span class="ruby-identifier">after_processing_hook</span>
970 119: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@request</span>.<span class="ruby-identifier">should_respond?</span>
971 120: <span class="ruby-keyword kw">return</span> <span class="ruby-ivar">@reply</span>.<span class="ruby-identifier">to_hash</span>
972 121: <span class="ruby-keyword kw">else</span>
973 122: <span class="ruby-identifier">log_code</span>(<span class="ruby-identifier">:PLMC35</span>, <span class="ruby-value str">"Client did not request a response, surpressing reply"</span>, <span class="ruby-identifier">:debug</span>)
974 123: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span>
975 124: <span class="ruby-keyword kw">end</span>
976 125: <span class="ruby-keyword kw">end</span></pre>
985 <div id="load-ddl-method" class="method-detail ">
986 <a name="M000394"></a>
988 <div class="method-heading">
990 <span class="method-name">load_ddl</span><span
991 class="method-args">()</span>
992 <span class="method-click-advice">click to toggle source</span>
996 <div class="method-description">
998 <p class="missing-docs">(Not documented)</p>
1002 <div class="method-source-code"
1003 id="load-ddl-source">
1005 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 55</span>
1006 55: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">load_ddl</span>
1007 56: <span class="ruby-ivar">@ddl</span> = <span class="ruby-constant">DDL</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@agent_name</span>, <span class="ruby-identifier">:agent</span>)
1008 57: <span class="ruby-ivar">@meta</span> = <span class="ruby-ivar">@ddl</span>.<span class="ruby-identifier">meta</span>
1009 58: <span class="ruby-ivar">@timeout</span> = <span class="ruby-ivar">@meta</span>[<span class="ruby-identifier">:timeout</span>] <span class="ruby-operator">||</span> <span class="ruby-value">10</span>
1011 60: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
1012 61: <span class="ruby-constant">DDL</span>.<span class="ruby-identifier">validation_fail!</span>(<span class="ruby-identifier">:PLMC24</span>, <span class="ruby-value str">"Failed to load DDL for the '%{agent}' agent, DDLs are required: %{error_class}: %{error}"</span>, <span class="ruby-identifier">:error</span>, <span class="ruby-identifier">:agent</span> =<span class="ruby-operator">></span> <span class="ruby-ivar">@agent_name</span>, <span class="ruby-identifier">:error_class</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">class</span>, <span class="ruby-identifier">:error</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_s</span>)
1013 62: <span class="ruby-keyword kw">end</span></pre>
1024 <div id="private-instance-method-details" class="method-section section">
1025 <h3 class="section-header">Private Instance Methods</h3>
1028 <div id="after-processing-hook-method" class="method-detail ">
1029 <a name="M000418"></a>
1031 <div class="method-heading">
1033 <span class="method-name">after_processing_hook</span><span
1034 class="method-args">()</span>
1035 <span class="method-click-advice">click to toggle source</span>
1039 <div class="method-description">
1042 Called at the end of processing just before the response gets sent to the
1046 This gets run outside of the main exception handling block of the agent so
1047 you should handle any exceptions you could raise yourself. The reason it is
1048 outside of the block is so you’ll have access to even status codes
1049 set by the exception handlers. If you do raise an exception it will just be
1050 passed onto the runner and processing will fail.
1055 <div class="method-source-code"
1056 id="after-processing-hook-source">
1058 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 356</span>
1059 356: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">after_processing_hook</span>
1060 357: <span class="ruby-keyword kw">end</span></pre>
1069 <div id="audit-request-method" class="method-detail ">
1070 <a name="M000419"></a>
1072 <div class="method-heading">
1074 <span class="method-name">audit_request</span><span
1075 class="method-args">(msg, connection)</span>
1076 <span class="method-click-advice">click to toggle source</span>
1080 <div class="method-description">
1083 Gets called right after a request was received and calls audit plugins
1086 <a href="../Agents.html">Agents</a> can disable auditing by just overriding
1087 this method with a noop one this might be useful for agents that gets a lot
1088 of requests or simply if you do not care for the auditing in a specific
1094 <div class="method-source-code"
1095 id="audit-request-source">
1097 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 364</span>
1098 364: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">audit_request</span>(<span class="ruby-identifier">msg</span>, <span class="ruby-identifier">connection</span>)
1099 365: <span class="ruby-constant">PluginManager</span>[<span class="ruby-value str">"rpcaudit_plugin"</span>].<span class="ruby-identifier">audit_request</span>(<span class="ruby-identifier">msg</span>, <span class="ruby-identifier">connection</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@config</span>.<span class="ruby-identifier">rpcaudit</span>
1100 366: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
1101 367: <span class="ruby-identifier">logexception</span>(<span class="ruby-identifier">:PLMC39</span>, <span class="ruby-value str">"Audit failed with an error, processing the request will continue."</span>, <span class="ruby-identifier">:warn</span>, <span class="ruby-identifier">e</span>)
1102 368: <span class="ruby-keyword kw">end</span></pre>
1111 <div id="before-processing-hook-method" class="method-detail ">
1112 <a name="M000417"></a>
1114 <div class="method-heading">
1116 <span class="method-name">before_processing_hook</span><span
1117 class="method-args">(msg, connection)</span>
1118 <span class="method-click-advice">click to toggle source</span>
1122 <div class="method-description">
1125 Called just after a message was received from the middleware before it gets
1126 passed to the handlers. @request and @reply will already be set, the msg
1127 passed is the message as received from the normal mcollective runner and
1128 the connection is the actual connector.
1133 <div class="method-source-code"
1134 id="before-processing-hook-source">
1136 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 345</span>
1137 345: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">before_processing_hook</span>(<span class="ruby-identifier">msg</span>, <span class="ruby-identifier">connection</span>)
1138 346: <span class="ruby-keyword kw">end</span></pre>
1147 <div id="implemented-by-method" class="method-detail ">
1148 <a name="M000415"></a>
1150 <div class="method-heading">
1152 <span class="method-name">implemented_by</span><span
1153 class="method-args">(command, type=:json)</span>
1154 <span class="method-click-advice">click to toggle source</span>
1158 <div class="method-description">
1161 handles external actions
1166 <div class="method-source-code"
1167 id="implemented-by-source">
1169 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 313</span>
1170 313: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">implemented_by</span>(<span class="ruby-identifier">command</span>, <span class="ruby-identifier">type</span>=<span class="ruby-identifier">:json</span>)
1171 314: <span class="ruby-identifier">runner</span> = <span class="ruby-constant">ActionRunner</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">command</span>, <span class="ruby-identifier">request</span>, <span class="ruby-identifier">type</span>)
1173 316: <span class="ruby-identifier">res</span> = <span class="ruby-identifier">runner</span>.<span class="ruby-identifier">run</span>
1175 318: <span class="ruby-identifier">reply</span>.<span class="ruby-identifier">fail!</span> <span class="ruby-node">"Did not receive data from #{command}"</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">res</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">:data</span>)
1176 319: <span class="ruby-identifier">reply</span>.<span class="ruby-identifier">fail!</span> <span class="ruby-node">"Reply data from #{command} is not a Hash"</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">res</span>[<span class="ruby-identifier">:data</span>].<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>)
1178 321: <span class="ruby-identifier">reply</span>.<span class="ruby-identifier">data</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">res</span>[<span class="ruby-identifier">:data</span>])
1180 323: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">res</span>[<span class="ruby-identifier">:exitstatus</span>] <span class="ruby-operator">></span> <span class="ruby-value">0</span>
1181 324: <span class="ruby-identifier">reply</span>.<span class="ruby-identifier">fail</span> <span class="ruby-node">"Failed to run #{command}: #{res[:stderr]}"</span>, <span class="ruby-identifier">res</span>[<span class="ruby-identifier">:exitstatus</span>]
1182 325: <span class="ruby-keyword kw">end</span>
1183 326: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
1184 327: <span class="ruby-constant">Log</span>.<span class="ruby-identifier">warn</span>(<span class="ruby-node">"Unhandled #{e.class} exception during #{request.agent}##{request.action}: #{e}"</span>)
1185 328: <span class="ruby-identifier">reply</span>.<span class="ruby-identifier">fail!</span> <span class="ruby-node">"Unexpected failure calling #{command}: #{e.class}: #{e}"</span>
1186 329: <span class="ruby-keyword kw">end</span></pre>
1195 <div id="run-method" class="method-detail ">
1196 <a name="M000406"></a>
1198 <div class="method-heading">
1200 <span class="method-name">run</span><span
1201 class="method-args">(command, options={})</span>
1202 <span class="method-click-advice">click to toggle source</span>
1206 <div class="method-description">
1209 Runs a command via the MC::Shell wrapper, options are as per MC::Shell
1212 The simplest use is:
1217 status = run("echo 1", :stdout => out, :stderr => err)
1221 reply[:exitstatus] = status
1224 This can be simplified as:
1227 reply[:exitstatus] = run("echo 1", :stdout => :out, :stderr => :error)
1230 You can set a command specific environment and cwd:
1233 run("echo 1", :cwd => "/tmp", :environment => {"FOO" => "BAR"})
1236 This will run ‘echo 1’ from /tmp with FOO=BAR in addition to a
1237 setting forcing LC_ALL = C. To prevent LC_ALL from being set either set it
1241 run("echo 1", :cwd => "/tmp", :environment => nil)
1244 Exceptions here will be handled by the usual agent exception handler or any
1245 specific one you create, if you dont it will just fall through and be sent
1249 If the shell handler fails to return a Process::Status instance for exit
1250 status this method will return -1 as the exit status
1255 <div class="method-source-code"
1258 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 195</span>
1259 195: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">run</span>(<span class="ruby-identifier">command</span>, <span class="ruby-identifier">options</span>={})
1260 196: <span class="ruby-identifier">shellopts</span> = {}
1262 198: <span class="ruby-comment cmt"># force stderr and stdout to be strings as the library</span>
1263 199: <span class="ruby-comment cmt"># will append data to them if given using the << method.</span>
1264 200: <span class="ruby-comment cmt">#</span>
1265 201: <span class="ruby-comment cmt"># if the data pased to :stderr or :stdin is a Symbol</span>
1266 202: <span class="ruby-comment cmt"># add that into the reply hash with that Symbol</span>
1267 203: [<span class="ruby-identifier">:stderr</span>, <span class="ruby-identifier">:stdout</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span><span class="ruby-operator">|</span>
1268 204: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">k</span>)
1269 205: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">k</span>].<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>)
1270 206: <span class="ruby-identifier">reply</span>[ <span class="ruby-identifier">options</span>[<span class="ruby-identifier">k</span>] ] = <span class="ruby-value str">""</span>
1271 207: <span class="ruby-identifier">shellopts</span>[<span class="ruby-identifier">k</span>] = <span class="ruby-identifier">reply</span>[ <span class="ruby-identifier">options</span>[<span class="ruby-identifier">k</span>] ]
1272 208: <span class="ruby-keyword kw">else</span>
1273 209: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">k</span>].<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value str">"<<"</span>)
1274 210: <span class="ruby-identifier">shellopts</span>[<span class="ruby-identifier">k</span>] = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">k</span>]
1275 211: <span class="ruby-keyword kw">else</span>
1276 212: <span class="ruby-identifier">reply</span>.<span class="ruby-identifier">fail!</span> <span class="ruby-node">"#{k} should support << while calling run(#{command})"</span>
1277 213: <span class="ruby-keyword kw">end</span>
1278 214: <span class="ruby-keyword kw">end</span>
1279 215: <span class="ruby-keyword kw">end</span>
1280 216: <span class="ruby-keyword kw">end</span>
1282 218: [<span class="ruby-identifier">:stdin</span>, <span class="ruby-identifier">:cwd</span>, <span class="ruby-identifier">:environment</span>, <span class="ruby-identifier">:timeout</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span><span class="ruby-operator">|</span>
1283 219: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">k</span>)
1284 220: <span class="ruby-identifier">shellopts</span>[<span class="ruby-identifier">k</span>] = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">k</span>]
1285 221: <span class="ruby-keyword kw">end</span>
1286 222: <span class="ruby-keyword kw">end</span>
1288 224: <span class="ruby-identifier">shell</span> = <span class="ruby-constant">Shell</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">command</span>, <span class="ruby-identifier">shellopts</span>)
1290 226: <span class="ruby-identifier">shell</span>.<span class="ruby-identifier">runcommand</span>
1292 228: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:chomp</span>]
1293 229: <span class="ruby-identifier">shellopts</span>[<span class="ruby-identifier">:stdout</span>].<span class="ruby-identifier">chomp!</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">shellopts</span>[<span class="ruby-identifier">:stdout</span>].<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">String</span>)
1294 230: <span class="ruby-identifier">shellopts</span>[<span class="ruby-identifier">:stderr</span>].<span class="ruby-identifier">chomp!</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">shellopts</span>[<span class="ruby-identifier">:stderr</span>].<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">String</span>)
1295 231: <span class="ruby-keyword kw">end</span>
1297 233: <span class="ruby-identifier">shell</span>.<span class="ruby-identifier">status</span>.<span class="ruby-identifier">exitstatus</span> <span class="ruby-keyword kw">rescue</span> <span class="ruby-value">-1</span>
1298 234: <span class="ruby-keyword kw">end</span></pre>
1307 <div id="shellescape-method" class="method-detail ">
1308 <a name="M000414"></a>
1310 <div class="method-heading">
1312 <span class="method-name">shellescape</span><span
1313 class="method-args">(str)</span>
1314 <span class="method-click-advice">click to toggle source</span>
1318 <div class="method-description">
1321 convenience wrapper around <a
1322 href="../Util.html#M000311">Util#shellescape</a>
1327 <div class="method-source-code"
1328 id="shellescape-source">
1330 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 308</span>
1331 308: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">shellescape</span>(<span class="ruby-identifier">str</span>)
1332 309: <span class="ruby-constant">Util</span>.<span class="ruby-identifier">shellescape</span>(<span class="ruby-identifier">str</span>)
1333 310: <span class="ruby-keyword kw">end</span></pre>
1342 <div id="startup-hook-method" class="method-detail ">
1343 <a name="M000416"></a>
1345 <div class="method-heading">
1347 <span class="method-name">startup_hook</span><span
1348 class="method-args">()</span>
1349 <span class="method-click-advice">click to toggle source</span>
1353 <div class="method-description">
1356 Called at the end of the <a href="Agent.html">RPC::Agent</a> standard
1357 initialize method use this to adjust meta parameters, timeouts and any
1358 setup you need to do.
1361 This will not be called right when the daemon starts up, we use lazy
1362 loading and initialization so it will only be called the first time a
1363 request for this agent arrives.
1368 <div class="method-source-code"
1369 id="startup-hook-source">
1371 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 338</span>
1372 338: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">startup_hook</span>
1373 339: <span class="ruby-keyword kw">end</span></pre>
1382 <div id="validate-method" class="method-detail ">
1383 <a name="M000413"></a>
1385 <div class="method-heading">
1387 <span class="method-name">validate</span><span
1388 class="method-args">(key, validation)</span>
1389 <span class="method-click-advice">click to toggle source</span>
1393 <div class="method-description">
1396 Validates a data member, if validation is a regex then it will try to match
1397 it else it supports testing object types only:
1400 validate :msg, <a href="../../String.html">String</a> validate :msg,
1404 There are also some special helper validators:
1407 validate :command, :shellsafe validate :command, :ipv6address validate
1408 :command, :ipv4address validate :command, :boolean validate :command,
1409 [“start”, “stop”]
1412 It will raise appropriate exceptions that the <a href="../RPC.html">RPC</a>
1418 <div class="method-source-code"
1419 id="validate-source">
1421 <span class="ruby-comment cmt"># File lib/mcollective/rpc/agent.rb, line 299</span>
1422 299: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">validate</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">validation</span>)
1423 300: <span class="ruby-identifier">raise</span> <span class="ruby-constant">MissingRPCData</span>, <span class="ruby-node">"please supply a #{key} argument"</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@request</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">key</span>)
1425 302: <span class="ruby-constant">Validator</span>.<span class="ruby-identifier">validate</span>(<span class="ruby-ivar">@request</span>[<span class="ruby-identifier">key</span>], <span class="ruby-identifier">validation</span>)
1426 303: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">ValidatorError</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
1427 304: <span class="ruby-identifier">raise</span> <span class="ruby-constant">InvalidRPCData</span>, <span class="ruby-value str">"Input %s did not pass validation: %s"</span> <span class="ruby-operator">%</span> [ <span class="ruby-identifier">key</span>, <span class="ruby-identifier">e</span>.<span class="ruby-identifier">message</span> ]
1428 305: <span class="ruby-keyword kw">end</span></pre>
1443 <div id="rdoc-debugging-section-dump" class="debugging-section">
1445 <p>Disabled; run with --debug to generate this.</p>
1449 <div id="validator-badges">
1450 <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
1451 <p><small>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish
1452 Rdoc Generator</a> 1.1.6</small>.</p>