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 24
      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 116
      def enq(plugin, input)
        @queue << {plugin => input}
      end

Runs all plugins that depends from an enumerated domain.

[Source]

# File lib/plugin_manager.rb, line 47
      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 61
      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 40
      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 54
      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 68
      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
                begin
                  Timeout::timeout(self.engine.opts['timeout'].to_i) {
                    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)}"
                  }    
                rescue Timeout::Error
                  out = k.timeout
                  self.engine.host_discovery.report(out)
                  $LOG.warn "Plugin #{k.name.inspect} execution expired. Output: #{set2txt(out)}"
                rescue
                  $LOG.debug "Plugin #{k.name.inspect} got a unhandled exception #{$!}"
                end
              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 123
      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 136
      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 149
      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]