~ubuntu-branches/ubuntu/oneiric/mcollective/oneiric-proposed

« back to all changes in this revision

Viewing changes to lib/mcollective/agents.rb

  • Committer: Bazaar Package Importer
  • Author(s): Marc Cluet, Marc Cluet, Chuck Short
  • Date: 2011-05-05 07:37:54 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110505073754-klk1jkz8afi4fomx
Tags: 1.2.0-0ubuntu1
[Marc Cluet]
* Update to 1.2.0
* Build for Oneiric.

[Chuck Short]
* Drop ruby from mcollective, mcollective-middleware, mcollective-client
  since its a dependency of mcollective-common.
* Bump standards to 3.9.2.
* Fix up lintian warnings.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
            @config = Config.instance
7
7
            raise ("Configuration has not been loaded, can't load agents") unless @config.configured
8
8
 
9
 
            @log = Log.instance
10
9
            @@agents = {}
11
10
 
12
11
            loadagents
14
13
 
15
14
        # Loads all agents from disk
16
15
        def loadagents
17
 
            @log.debug("Reloading all agents from disk")
 
16
            Log.debug("Reloading all agents from disk")
18
17
 
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))
23
22
            end
24
23
 
25
24
            @@agents = {}
26
25
 
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)
29
29
 
30
 
            Dir.new(agentdir).grep(/\.rb$/).each do |agent|
31
 
                agentname = File.basename(agent, ".rb")
32
 
                loadagent(agentname)
 
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
33
34
            end
34
35
        end
35
36
 
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}"
40
42
 
41
 
            return false unless File.exist?(agentfile)
42
 
 
43
43
            PluginManager.delete("#{agentname}_agent")
44
44
 
45
45
            begin
 
46
                single_instance = ["registration", "discovery"].include?(agentname)
 
47
 
46
48
                PluginManager.loadclass(classname)
47
 
                PluginManager << {:type => "#{agentname}_agent", :class => classname}
 
49
                PluginManager << {:type => "#{agentname}_agent", :class => classname, :single_instance => single_instance}
48
50
 
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)
50
52
 
51
53
                @@agents[agentname] = {:file => agentfile}
52
54
                return true
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")
56
58
            end
57
59
        end
58
60
 
 
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}")
 
67
                    return agentfile
 
68
                end
 
69
            end
 
70
            return false
 
71
        end
 
72
 
59
73
        # Determines if we have an agent with a certain name
60
74
        def include?(agentname)
61
75
            PluginManager.include?("#{agentname}_agent")
62
76
        end
63
77
 
64
 
        # Sends a message to a specific agent
65
 
        def send(agentname, msg, connection)
66
 
            raise("No such agent") unless include?(agentname)
67
 
 
68
 
            PluginManager["#{agentname}_agent"].handlemsg(msg, connection)
69
 
        end
70
 
 
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)
84
91
            body.join("\n")
85
92
        end
86
93
 
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)
90
 
 
91
 
            PluginManager["#{agentname}_agent"].timeout
92
 
        end
93
 
 
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}")
98
98
 
99
99
            Thread.new do
100
100
                begin
101
 
                    Timeout::timeout(timeout(target)) do
102
 
                        replies = send(target, msg, connection)
 
101
                    agent = PluginManager["#{target}_agent"]
 
102
 
 
103
                    Timeout::timeout(agent.timeout) do
 
104
                        replies = agent.handlemsg(msg, connection)
103
105
 
104
106
                        # Agents can decide if they wish to reply or not,
105
107
                        # returning nil will mean nothing goes back to the
109
111
                        end
110
112
                    end
111
113
                rescue Timeout::Error => e
112
 
                    @log.warn("Timeout while handling message for #{target}")
 
114
                    Log.warn("Timeout while handling message for #{target}")
113
115
                rescue Exception => e
114
 
                    @log.error("Execution of #{target} failed: #{e}")
115
 
                    @log.error(e.backtrace.join("\n\t\t"))
 
116
                    Log.error("Execution of #{target} failed: #{e}")
 
117
                    Log.error(e.backtrace.join("\n\t\t"))
116
118
                end
117
119
            end
118
120
        end