Class | HostMap::Managers::PluginManager |
In: |
lib/plugin_manager.rb
|
Parent: | Object |
Handles the plugins. Loading and running jobs.
Creates a new plugin manager.
# 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
Enqueue a plugin to be runned.
# File lib/plugin_manager.rb, line 116 def enq(plugin, input) @queue << {plugin => input} end
Runs all plugins that depends from an enumerated domain.
# 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.
# 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.
# 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.
# 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.
# 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.
# 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
Load plugins from plugin directory
# 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