Sha256: 6a7d7a503b8840cb9d3a0307304800cd844092b4d40733c96d7d4e9b834beaa5

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

module Clusterfuck
  class Cluster
    attr_reader :graph, :name

    def initialize(name)
      @name = name
      @nodes = {}
      @graph = {}
    end

    def self.create(name)
      @clusters ||= {}
      @clusters[name] = Cluster.new(name)
    end

    def self.[](name)
      @clusters[name]
    end

    def self.all
      @clusters.values
    end

    def nodes
      @nodes.values
    end

    def [](name)
      @nodes[name]
    end

    def connect(subnet_factory, *nodes)
      nodes.each do |node|
        register(node)

        unless in_subnet?(subnet_factory.subnet, node)
          node.subnets << subnet_factory.next
          node.network = self
        end
      end

      nodes.permutation(2).each do |(a, b)|
        @graph[a] << b
      end
    end

    def adjacent?(a, b)
      adjacent(a).include?(b)
    end

    def adjacent(a)
      @graph[a]
    end

    def register(node)
      @nodes[node.name] = node unless @nodes[node.name]
      @graph[node] = [] unless @graph[node]
    end

    def overlapping_subnets(a, b)
      a_subnets, b_subnets = Array(a.subnets), Array(b.subnets)
      a_subnets.select { |a_subnet|
        b_subnets.find { |b_subnet|
          a_subnet.include?(b_subnet)
        }
      }
    end

    private
    def in_subnet?(subnet, node)
      node.subnets.any? { |ip| subnet.include?(ip) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant-clusterfuck-0.0.1 lib/clusterfuck/cluster.rb