Sha256: 559b21a1cccee30455f55d3464dc5c391f31c18eea463880f2a8bda051741d67

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

require 'rb-inotify'
require 'ipaddr'

module Ring
class SQA

  class Nodes
    FILE   = '/etc/hosts'
    attr_reader :list

    def run
      Thread.new { @inotify.run }
    end

    private

    def initialize
      @list = get_list
      @inotify = INotify::Notifier.new
      @inotify.watch(File.dirname(FILE), :modify, :create) do |event|
        @list = get_list if event.name == FILE.split('/').last
      end
      run
    end

    def get_list
      list = []
      File.read(FILE).lines.each do |line|
        entry = line.split(/\s+/)
        next if entry_skip? entry
        list << entry.first
      end
      list.sort
    end

    def entry_skip? entry
      return true unless entry.size > 2
      return true if entry.first.match /^\s*#/
      return true if CFG.hosts.ignore.any?   { |re| entry[2].match Regexp.new(re) }
      return true unless CFG.hosts.load.any? { |re| entry[2].match Regexp.new(re) }

      address = IPAddr.new(entry.first) rescue (return true)
      if CFG.ipv6?
        return true if address.ipv4?
        return true if address == IPAddr.new(CFG.bind.ipv6)
      else
        return true if address.ipv6?
        return true if address == IPAddr.new(CFG.bind.ipv4)
      end
      false
    end

  end

end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ring-sqa-0.0.18 lib/ring/sqa/nodes.rb
ring-sqa-0.0.16 lib/ring/sqa/nodes.rb
ring-sqa-0.0.15 lib/ring/sqa/nodes.rb