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 121 121: def self.agentlist 122: @@agents.keys 123: 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: @log = Log.instance 10: @@agents = {} 11: 12: loadagents 13: 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 96 96: def dispatch(msg, target, connection) 97: @log.debug("Dispatching a message to agent #{target}") 98: 99: Thread.new do 100: begin 101: Timeout::timeout(timeout(target)) do 102: replies = send(target, msg, connection) 103: 104: # Agents can decide if they wish to reply or not, 105: # returning nil will mean nothing goes back to the 106: # requestor 107: unless replies == nil 108: yield(replies) 109: end 110: end 111: rescue Timeout::Error => e 112: @log.warn("Timeout while handling message for #{target}") 113: rescue Exception => e 114: @log.error("Execution of #{target} failed: #{e}") 115: @log.error(e.backtrace.join("\n\t\t")) 116: end 117: end 118: end
Returns the help for an agent after first trying to get rid of some indentation infront
# File lib/mcollective/agents.rb, line 73 73: def help(agentname) 74: raise("No such agent") unless include?(agentname) 75: 76: body = PluginManager["#{agentname}_agent"].help.split("\n") 77: 78: if body.first =~ /^(\s+)\S/ 79: indent = $1 80: 81: body = body.map {|b| b.gsub(/^#{indent}/, "")} 82: end 83: 84: body.join("\n") 85: end
Determines if we have an agent with a certain name
# File lib/mcollective/agents.rb, line 60 60: def include?(agentname) 61: PluginManager.include?("#{agentname}_agent") 62: end
Loads a specified agent from disk if available
# File lib/mcollective/agents.rb, line 37 37: def loadagent(agentname) 38: agentfile = "#{@config.libdir}/mcollective/agent/#{agentname}.rb" 39: classname = "MCollective::Agent::#{agentname.capitalize}" 40: 41: return false unless File.exist?(agentfile) 42: 43: PluginManager.delete("#{agentname}_agent") 44: 45: begin 46: PluginManager.loadclass(classname) 47: PluginManager << {:type => "#{agentname}_agent", :class => classname} 48: 49: PluginManager["connector_plugin"].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 16 16: def loadagents 17: @log.debug("Reloading all agents from disk") 18: 19: # We're loading all agents so just nuke all the old agents and unsubscribe 20: connector = PluginManager["connector_plugin"] 21: @@agents.each_key do |agent| 22: connector.unsubscribe(Util.make_target(agent, :command)) 23: end 24: 25: @@agents = {} 26: 27: agentdir = "#{@config.libdir}/mcollective/agent" 28: raise("Cannot find agents directory") unless File.directory?(agentdir) 29: 30: Dir.new(agentdir).grep(/\.rb$/).each do |agent| 31: agentname = File.basename(agent, ".rb") 32: loadagent(agentname) 33: end 34: end
Sends a message to a specific agent
# File lib/mcollective/agents.rb, line 65 65: def send(agentname, msg, connection) 66: raise("No such agent") unless include?(agentname) 67: 68: PluginManager["#{agentname}_agent"].handlemsg(msg, connection) 69: end