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 133 133: def self.agentlist 134: @@agents.keys 135: 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 108 108: def dispatch(msg, target, connection) 109: Log.debug("Dispatching a message to agent #{target}") 110: 111: Thread.new do 112: begin 113: Timeout::timeout(timeout(target)) do 114: replies = send(target, msg, connection) 115: 116: # Agents can decide if they wish to reply or not, 117: # returning nil will mean nothing goes back to the 118: # requestor 119: unless replies == nil 120: yield(replies) 121: end 122: end 123: rescue Timeout::Error => e 124: Log.warn("Timeout while handling message for #{target}") 125: rescue Exception => e 126: Log.error("Execution of #{target} failed: #{e}") 127: Log.error(e.backtrace.join("\n\t\t")) 128: end 129: end 130: end
searches the libdirs for agents
# File lib/mcollective/agents.rb, line 60 60: def findagentfile(agentname) 61: @config.libdir.each do |libdir| 62: agentfile = "#{libdir}/mcollective/agent/#{agentname}.rb" 63: if File.exist?(agentfile) 64: Log.debug("Found #{agentname} at #{agentfile}") 65: return agentfile 66: end 67: end 68: return false 69: end
Returns the help for an agent after first trying to get rid of some indentation infront
# File lib/mcollective/agents.rb, line 85 85: def help(agentname) 86: raise("No such agent") unless include?(agentname) 87: 88: body = PluginManager["#{agentname}_agent"].help.split("\n") 89: 90: if body.first =~ /^(\s+)\S/ 91: indent = $1 92: 93: body = body.map {|b| b.gsub(/^#{indent}/, "")} 94: end 95: 96: body.join("\n") 97: end
Determines if we have an agent with a certain name
# File lib/mcollective/agents.rb, line 72 72: def include?(agentname) 73: PluginManager.include?("#{agentname}_agent") 74: end
Loads a specified agent from disk if available
# 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: PluginManager.loadclass(classname) 47: PluginManager << {:type => "#{agentname}_agent", :class => classname} 48: 49: Util.subscribe(Util.make_target(agentname, :command)) unless @@agents.include?(agentname) 50: 51: @@agents[agentname] = {:file => agentfile} 52: return true 53: rescue Exception => e 54: Log.error("Loading agent #{agentname} failed: #{e}") 55: PluginManager.delete("#{agentname}_agent") 56: end 57: 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: @@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
Sends a message to a specific agent
# File lib/mcollective/agents.rb, line 77 77: def send(agentname, msg, connection) 78: raise("No such agent") unless include?(agentname) 79: 80: PluginManager["#{agentname}_agent"].handlemsg(msg, connection) 81: end