class Patch::Hub

The main application object

Attributes

log[R]
patches[R]

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options @option options [IO] :log @option options [Array<Patch>] :patches

# File lib/patch/hub.rb, line 11
def initialize(options = {})
  @log = Log.new(options[:log]) unless options[:log].nil?
  populate_patches(options[:patches] || options[:patch])
end

Public Instance Methods

ips() click to toggle source

Collected IP addresses for the nodes @return [Array<String>]

# File lib/patch/hub.rb, line 18
def ips
  regex = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/
  all_ips = Socket.ip_address_list.map(&:inspect_sockaddr)
  all_ips.select { |ip| !!ip.match(regex) }
end
listen(options = {}) click to toggle source

Start the hub @param [Hash] options @option options [Boolean] :background Run in a background thread (default: false) @return [Hub] self

# File lib/patch/hub.rb, line 28
def listen(options = {})
  begin
    enable_nodes
    @thread.join unless !!options[:background]
    self
  rescue SystemExit, Interrupt => exception
    exit 0
  end
end
nodes() click to toggle source

All of the nodes used by the patches @return [Node::Container]

# File lib/patch/hub.rb, line 40
def nodes
  nodes = @patches.map { |patch| patch.maps.map(&:nodes) }.flatten.compact.uniq
  Node::Container.new(nodes)
end

Private Instance Methods

enable_nodes() click to toggle source

Enable the nodes @return [Thread]

# File lib/patch/hub.rb, line 49
def enable_nodes
  @thread = ::Patch::Thread.new do
    EM.epoll
    EM.run {
      @patches.each(&:enable)
      nodes.enable
    }
    !nodes.empty?
  end
end
populate_patches(patches) click to toggle source

Populate the patches given various arg formats @param [Array<Patch>, Patch] patches @return [Array<Patch>]

# File lib/patch/hub.rb, line 63
def populate_patches(patches)
  @patches = [patches].flatten.compact
end