Class HostMap::Managers::PluginManager
In: lib/plugin_manager.rb
Parent: Object

Handles the plugins. Loading and running jobs.

Methods

enq   load   new   run_domain   run_hostname   run_ip   run_ns   set2txt   start   stop_all  

Included Modules

HostMap::Engine::Shared

Public Class methods

Creates a new plugin manager.

[Source]

# File lib/plugin_manager.rb, line 23
      def initialize(engine)
        self.engine = engine
        # Load plugins.
        self.load
        # Store plugin queue
        @queue = Queue.new
        @pool = []
        # Throw exceptions in threads
        Thread.abort_on_exception = true
        # Callback to call when execution is done
        @callback = nil
      end

Public Instance methods

Enqueue a plugin to be runned.

[Source]

# File lib/plugin_manager.rb, line 105
      def enq(plugin, input)
        @queue << {plugin => input}
      end

Runs all plugins that depends from an enumerated domain.

[Source]

# File lib/plugin_manager.rb, line 46
      def run_domain(name)
        PlugMan.registered_plugins[:main].do_group(:domain, name, self)
      end

Runs all plugins that depends from an enumerated hostname.

[Source]

# File lib/plugin_manager.rb, line 60
      def run_hostname(name)
        PlugMan.registered_plugins[:main].do_group(:hostname, name, self)
      end

Runs all plugins that depends from an enumerated ip.

[Source]

# File lib/plugin_manager.rb, line 39
      def run_ip(ip)
        PlugMan.registered_plugins[:main].do_group(:ip, ip, self)
      end

Runs all plugins that depends from an enumerated nameserver.

[Source]

# File lib/plugin_manager.rb, line 53
      def run_ns(name)
        PlugMan.registered_plugins[:main].do_group(:ns, name, self)
      end

Start the engine suppling the parameters to start the first plugin.

[Source]

# File lib/plugin_manager.rb, line 67
      def start(type, value, callback)
        # Set callback to later user
        @callback = callback

        # Select the right plugin to start
        case type
          when :ip then self.run_ip(value)
        end

        # Plugins loop
        loop do
          until @queue.empty?  do
            # Dequeue a plugin and run it
            @queue.deq.each { |k,v|
              # Creating a thread, running the plugin inside
              job = Thread.new do
                out = k.run(v, self.engine.opts)
                # Reports the result
                self.engine.host_discovery.report(out)
                $LOG.debug "Plugin: #{k.name.inspect} Output: #{set2txt(out)}"
              end
            @pool << job
            }
          end

          # Consume long threads
          @pool.each { |th| th.join }
          # Break loop if no more plugins
          break if @queue.empty?
        end

        # Stop plugin manager
        stop_all
      end

Stops all the plugins.

[Source]

# File lib/plugin_manager.rb, line 112
      def stop_all
        $LOG.debug "Stopping all plugins."
        @pool.each { |th| th.kill }
        PlugMan.stop_all_plugins()
        # Callback to discovery
        @callback.call
      end

Protected Instance methods

Load plugins from plugin directory

[Source]

# File lib/plugin_manager.rb, line 125
      def load
        $LOG.debug "Loading plugins."

        # Load all the plugins
        PlugMan.load_plugins PLUGINDIR

        # Start all the pluins
        PlugMan.start_all_plugins
      end

Converts a results Set to a printable string

[Source]

# File lib/plugin_manager.rb, line 138
      def set2txt(out)
        txt = ''

        out.each { |result|
          result.each { |key, val|
            txt << "#{val} "
          }
        }

        # If no results
        if out.length == 0
          txt = "None"
        end

        # Return text string
        txt
      end

[Validate]