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.
Get a list of agents that we have
# File lib/mcollective/agents.rb, line 134 134: def self.agentlist 135: @@agents.keys 136: end
# 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
Dispatches a message to an agent, accepts a block that will get run if there are any replies to process from the agent
# File lib/mcollective/agents.rb, line 109 109: def dispatch(msg, target, connection) 110: Log.debug("Dispatching a message to agent #{target}") 111: 112: Thread.new do 113: begin 114: Timeout::timeout(timeout(target)) do 115: replies = send(target, msg, connection) 116: 117: # Agents can decide if they wish to reply or not, 118: # returning nil will mean nothing goes back to the 119: # requestor 120: unless replies == nil 121: yield(replies) 122: end 123: end 124: rescue Timeout::Error => e 125: Log.warn("Timeout while handling message for #{target}") 126: rescue Exception => e 127: Log.error("Execution of #{target} failed: #{e}") 128: Log.error(e.backtrace.join("\n\t\t")) 129: end 130: end 131: end
searches the libdirs for agents
# File lib/mcollective/agents.rb, line 61 61: def findagentfile(agentname) 62: @config.libdir.each do |libdir| 63: agentfile = "#{libdir}/mcollective/agent/#{agentname}.rb" 64: if File.exist?(agentfile) 65: Log.debug("Found #{agentname} at #{agentfile}") 66: return agentfile 67: end 68: end 69: return false 70: end
Returns the help for an agent after first trying to get rid of some indentation infront
# File lib/mcollective/agents.rb, line 86 86: def help(agentname) 87: raise("No such agent") unless include?(agentname) 88: 89: body = PluginManager["#{agentname}_agent"].help.split("\n") 90: 91: if body.first =~ /^(\s+)\S/ 92: indent = $1 93: 94: body = body.map {|b| b.gsub(/^#{indent}/, "")} 95: end 96: 97: body.join("\n") 98: end
Determines if we have an agent with a certain name
# File lib/mcollective/agents.rb, line 73 73: def include?(agentname) 74: PluginManager.include?("#{agentname}_agent") 75: end
Loads a specified agent from disk if available
# File lib/mcollective/agents.rb, line 39 39: def loadagent(agentname) 40: agentfile = findagentfile(agentname) 41: return false unless agentfile 42: classname = "MCollective::Agent::#{agentname.capitalize}" 43: 44: PluginManager.delete("#{agentname}_agent") 45: 46: begin 47: PluginManager.loadclass(classname) 48: PluginManager << {:type => "#{agentname}_agent", :class => classname} 49: 50: PluginManager["connector_plugin"].subscribe(Util.make_target(agentname, :command)) unless @@agents.include?(agentname) 51: 52: @@agents[agentname] = {:file => agentfile} 53: return true 54: rescue Exception => e 55: Log.error("Loading agent #{agentname} failed: #{e}") 56: PluginManager.delete("#{agentname}_agent") 57: end 58: end
Loads all agents from disk
# 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: connector = PluginManager["connector_plugin"] 20: @@agents.each_key do |agent| 21: PluginManager.delete "#{agent}_agent" 22: connector.unsubscribe(Util.make_target(agent, :command)) 23: end 24: 25: @@agents = {} 26: 27: @config.libdir.each do |libdir| 28: agentdir = "#{libdir}/mcollective/agent" 29: raise("Cannot find agents directory: '#{agentdir}'") unless File.directory?(agentdir) 30: 31: Dir.new(agentdir).grep(/\.rb$/).each do |agent| 32: agentname = File.basename(agent, ".rb") 33: loadagent(agentname) unless PluginManager.include?("#{agentname}_agent") 34: end 35: end 36: end
Sends a message to a specific agent
# File lib/mcollective/agents.rb, line 78 78: def send(agentname, msg, connection) 79: raise("No such agent") unless include?(agentname) 80: 81: PluginManager["#{agentname}_agent"].handlemsg(msg, connection) 82: end