Class MCollective::Agents
In: lib/mcollective/agents.rb
Parent: Object

A collection of agents, loads them, reloads them and dispatches messages to them. It uses the PluginManager to store, load and manage instances of plugins.

Methods

Public Class methods

Get a list of agents that we have

[Source]

     # File lib/mcollective/agents.rb, line 123
123:         def self.agentlist
124:             @@agents.keys
125:         end

[Source]

    # File lib/mcollective/agents.rb, line 5
 5:         def initialize
 6:             @config = Config.instance
 7:             raise ("Configuration has not been loaded, can't load agents") unless @config.configured
 8: 
 9:             @@agents = {}
10: 
11:             loadagents
12:         end

Public Instance methods

Dispatches a message to an agent, accepts a block that will get run if there are any replies to process from the agent

[Source]

     # File lib/mcollective/agents.rb, line 96
 96:         def dispatch(msg, target, connection)
 97:             Log.debug("Dispatching a message to agent #{target}")
 98: 
 99:             Thread.new do
100:                 begin
101:                     agent = PluginManager["#{target}_agent"]
102: 
103:                     Timeout::timeout(agent.timeout) do
104:                         replies = agent.handlemsg(msg, connection)
105: 
106:                         # Agents can decide if they wish to reply or not,
107:                         # returning nil will mean nothing goes back to the
108:                         # requestor
109:                         unless replies == nil
110:                             yield(replies)
111:                         end
112:                     end
113:                 rescue Timeout::Error => e
114:                     Log.warn("Timeout while handling message for #{target}")
115:                 rescue Exception => e
116:                     Log.error("Execution of #{target} failed: #{e}")
117:                     Log.error(e.backtrace.join("\n\t\t"))
118:                 end
119:             end
120:         end

searches the libdirs for agents

[Source]

    # File lib/mcollective/agents.rb, line 62
62:         def findagentfile(agentname)
63:             @config.libdir.each do |libdir|
64:                 agentfile = "#{libdir}/mcollective/agent/#{agentname}.rb"
65:                 if File.exist?(agentfile)
66:                     Log.debug("Found #{agentname} at #{agentfile}")
67:                     return agentfile
68:                 end
69:             end
70:             return false
71:         end

Returns the help for an agent after first trying to get rid of some indentation infront

[Source]

    # File lib/mcollective/agents.rb, line 80
80:         def help(agentname)
81:             raise("No such agent") unless include?(agentname)
82: 
83:             body = PluginManager["#{agentname}_agent"].help.split("\n")
84: 
85:             if body.first =~ /^(\s+)\S/
86:                 indent = $1
87: 
88:                 body = body.map {|b| b.gsub(/^#{indent}/, "")}
89:             end
90: 
91:             body.join("\n")
92:         end

Determines if we have an agent with a certain name

[Source]

    # File lib/mcollective/agents.rb, line 74
74:         def include?(agentname)
75:             PluginManager.include?("#{agentname}_agent")
76:         end

Loads a specified agent from disk if available

[Source]

    # File lib/mcollective/agents.rb, line 38
38:         def loadagent(agentname)
39:             agentfile = findagentfile(agentname)
40:             return false unless agentfile
41:             classname = "MCollective::Agent::#{agentname.capitalize}"
42: 
43:             PluginManager.delete("#{agentname}_agent")
44: 
45:             begin
46:                 single_instance = ["registration", "discovery"].include?(agentname)
47: 
48:                 PluginManager.loadclass(classname)
49:                 PluginManager << {:type => "#{agentname}_agent", :class => classname, :single_instance => single_instance}
50: 
51:                 Util.subscribe(Util.make_target(agentname, :command)) unless @@agents.include?(agentname)
52: 
53:                 @@agents[agentname] = {:file => agentfile}
54:                 return true
55:             rescue Exception => e
56:                 Log.error("Loading agent #{agentname} failed: #{e}")
57:                 PluginManager.delete("#{agentname}_agent")
58:             end
59:         end

Loads all agents from disk

[Source]

    # File lib/mcollective/agents.rb, line 15
15:         def loadagents
16:             Log.debug("Reloading all agents from disk")
17: 
18:             # We're loading all agents so just nuke all the old agents and unsubscribe
19:             @@agents.each_key do |agent|
20:                 PluginManager.delete "#{agent}_agent"
21:                 Util.unsubscribe(Util.make_target(agent, :command))
22:             end
23: 
24:             @@agents = {}
25: 
26:             @config.libdir.each do |libdir|
27:                 agentdir = "#{libdir}/mcollective/agent"
28:                 next unless File.directory?(agentdir)
29: 
30:                 Dir.new(agentdir).grep(/\.rb$/).each do |agent|
31:                     agentname = File.basename(agent, ".rb")
32:                     loadagent(agentname) unless PluginManager.include?("#{agentname}_agent")
33:                 end
34:             end
35:         end

[Validate]