Class: MCollective::Agents

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/agents.rb

Overview

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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agents = {}) ⇒ Agents

Returns a new instance of Agents.



5
6
7
8
9
10
11
12
# File 'lib/mcollective/agents.rb', line 5

def initialize(agents = {})
  @config = Config.instance
  raise ("Configuration has not been loaded, can't load agents") unless @config.configured

  @@agents = agents

  loadagents
end

Class Method Details

.agentlistObject

Get a list of agents that we have



156
157
158
# File 'lib/mcollective/agents.rb', line 156

def self.agentlist
  @@agents.keys
end

Instance Method Details

#activate_agent?(agent) ⇒ Boolean

Checks if a plugin should be activated by calling #activate? on it if it responds to that method else always activate it

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mcollective/agents.rb', line 80

def activate_agent?(agent)
  klass = Kernel.const_get("MCollective").const_get("Agent").const_get(agent.capitalize)

  if klass.respond_to?("activate?")
    return klass.activate?
  else
    Log.debug("#{klass} does not have an activate? method, activating as default")
    return true
  end
rescue Exception => e
  Log.warn("Agent activation check for #{agent} failed: #{e.class}: #{e}")
  return false
end

#class_for_agent(agent) ⇒ Object

Builds a class name string given a Agent name



73
74
75
# File 'lib/mcollective/agents.rb', line 73

def class_for_agent(agent)
  "MCollective::Agent::#{agent.capitalize}"
end

#clear!Object

Deletes all agents



15
16
17
18
19
20
21
22
# File 'lib/mcollective/agents.rb', line 15

def clear!
  @@agents.each_key do |agent|
    PluginManager.delete "#{agent}_agent"
    Util.unsubscribe(Util.make_subscriptions(agent, :broadcast))
  end

  @@agents = {}
end

#dispatch(request, connection) ⇒ Object

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/mcollective/agents.rb', line 129

def dispatch(request, connection)
  Log.debug("Dispatching a message to agent #{request.agent}")

  Thread.new do
    begin
      agent = PluginManager["#{request.agent}_agent"]

      Timeout::timeout(agent.timeout) do
        replies = agent.handlemsg(request.payload, connection)

        # Agents can decide if they wish to reply or not,
        # returning nil will mean nothing goes back to the
        # requestor
        unless replies == nil
          yield(replies)
        end
      end
    rescue Timeout::Error => e
      Log.warn("Timeout while handling message for #{request.agent}")
    rescue Exception => e
      Log.error("Execution of #{request.agent} failed: #{e}")
      Log.error(e.backtrace.join("\n\t\t"))
    end
  end
end

#findagentfile(agentname) ⇒ Object

searches the libdirs for agents



95
96
97
98
99
100
101
102
103
104
# File 'lib/mcollective/agents.rb', line 95

def findagentfile(agentname)
  @config.libdir.each do |libdir|
    agentfile = File.join([libdir, "mcollective", "agent", "#{agentname}.rb"])
    if File.exist?(agentfile)
      Log.debug("Found #{agentname} at #{agentfile}")
      return agentfile
    end
  end
  return false
end

#help(agentname) ⇒ Object

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



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mcollective/agents.rb', line 113

def help(agentname)
  raise("No such agent") unless include?(agentname)

  body = PluginManager["#{agentname}_agent"].help.split("\n")

  if body.first =~ /^(\s+)\S/
    indent = $1

    body = body.map {|b| b.gsub(/^#{indent}/, "")}
  end

  body.join("\n")
end

#include?(agentname) ⇒ Boolean

Determines if we have an agent with a certain name

Returns:

  • (Boolean)


107
108
109
# File 'lib/mcollective/agents.rb', line 107

def include?(agentname)
  PluginManager.include?("#{agentname}_agent")
end

#loadagent(agentname) ⇒ Object

Loads a specified agent from disk if available



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mcollective/agents.rb', line 42

def loadagent(agentname)
  agentfile = findagentfile(agentname)
  return false unless agentfile
  classname = class_for_agent(agentname)

  PluginManager.delete("#{agentname}_agent")

  begin
    single_instance = ["registration", "discovery"].include?(agentname)

    PluginManager.loadclass(classname)

    if activate_agent?(agentname)
      PluginManager << {:type => "#{agentname}_agent", :class => classname, :single_instance => single_instance}

      Util.subscribe(Util.make_subscriptions(agentname, :broadcast)) unless @@agents.include?(agentname)

      @@agents[agentname] = {:file => agentfile}
      return true
    else
      Log.debug("Not activating agent #{agentname} due to agent policy in activate? method")
      return false
    end
  rescue Exception => e
    Log.error("Loading agent #{agentname} failed: #{e}")
    PluginManager.delete("#{agentname}_agent")
    return false
  end
end

#loadagentsObject

Loads all agents from disk



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mcollective/agents.rb', line 25

def loadagents
  Log.debug("Reloading all agents from disk")

  clear!

  @config.libdir.each do |libdir|
    agentdir = "#{libdir}/mcollective/agent"
    next unless File.directory?(agentdir)

    Dir.new(agentdir).grep(/\.rb$/).each do |agent|
      agentname = File.basename(agent, ".rb")
      loadagent(agentname) unless PluginManager.include?("#{agentname}_agent")
    end
  end
end