15
14
# Loads all agents from disk
17
@log.debug("Reloading all agents from disk")
16
Log.debug("Reloading all agents from disk")
19
18
# We're loading all agents so just nuke all the old agents and unsubscribe
20
connector = PluginManager["connector_plugin"]
21
19
@@agents.each_key do |agent|
22
connector.unsubscribe(Util.make_target(agent, :command))
20
PluginManager.delete "#{agent}_agent"
21
Util.unsubscribe(Util.make_target(agent, :command))
27
agentdir = "#{@config.libdir}/mcollective/agent"
28
raise("Cannot find agents directory") unless File.directory?(agentdir)
26
@config.libdir.each do |libdir|
27
agentdir = "#{libdir}/mcollective/agent"
28
next unless File.directory?(agentdir)
30
Dir.new(agentdir).grep(/\.rb$/).each do |agent|
31
agentname = File.basename(agent, ".rb")
30
Dir.new(agentdir).grep(/\.rb$/).each do |agent|
31
agentname = File.basename(agent, ".rb")
32
loadagent(agentname) unless PluginManager.include?("#{agentname}_agent")
36
37
# Loads a specified agent from disk if available
37
38
def loadagent(agentname)
38
agentfile = "#{@config.libdir}/mcollective/agent/#{agentname}.rb"
39
agentfile = findagentfile(agentname)
40
return false unless agentfile
39
41
classname = "MCollective::Agent::#{agentname.capitalize}"
41
return false unless File.exist?(agentfile)
43
43
PluginManager.delete("#{agentname}_agent")
46
single_instance = ["registration", "discovery"].include?(agentname)
46
48
PluginManager.loadclass(classname)
47
PluginManager << {:type => "#{agentname}_agent", :class => classname}
49
PluginManager << {:type => "#{agentname}_agent", :class => classname, :single_instance => single_instance}
49
PluginManager["connector_plugin"].subscribe(Util.make_target(agentname, :command)) unless @@agents.include?(agentname)
51
Util.subscribe(Util.make_target(agentname, :command)) unless @@agents.include?(agentname)
51
53
@@agents[agentname] = {:file => agentfile}
53
55
rescue Exception => e
54
@log.error("Loading agent #{agentname} failed: #{e}")
56
Log.error("Loading agent #{agentname} failed: #{e}")
55
57
PluginManager.delete("#{agentname}_agent")
61
# searches the libdirs for agents
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}")
59
73
# Determines if we have an agent with a certain name
60
74
def include?(agentname)
61
75
PluginManager.include?("#{agentname}_agent")
64
# Sends a message to a specific agent
65
def send(agentname, msg, connection)
66
raise("No such agent") unless include?(agentname)
68
PluginManager["#{agentname}_agent"].handlemsg(msg, connection)
71
78
# Returns the help for an agent after first trying to get
72
79
# rid of some indentation infront
73
80
def help(agentname)
87
# Determine the max amount of time a specific agent should be running
88
def timeout(agentname)
89
raise("No such agent") unless include?(agentname)
91
PluginManager["#{agentname}_agent"].timeout
94
94
# Dispatches a message to an agent, accepts a block that will get run if there are
95
95
# any replies to process from the agent
96
96
def dispatch(msg, target, connection)
97
@log.debug("Dispatching a message to agent #{target}")
97
Log.debug("Dispatching a message to agent #{target}")
101
Timeout::timeout(timeout(target)) do
102
replies = send(target, msg, connection)
101
agent = PluginManager["#{target}_agent"]
103
Timeout::timeout(agent.timeout) do
104
replies = agent.handlemsg(msg, connection)
104
106
# Agents can decide if they wish to reply or not,
105
107
# returning nil will mean nothing goes back to the