lib/clusterfuck/cluster.rb in vagrant-clusterfuck-0.0.1 vs lib/clusterfuck/cluster.rb in vagrant-clusterfuck-0.0.2

- old
+ new

@@ -1,74 +1,65 @@ module Clusterfuck class Cluster - attr_reader :graph, :name + attr_reader :name, :network def initialize(name) @name = name - @nodes = {} @graph = {} end def self.create(name) - @clusters ||= {} - @clusters[name] = Cluster.new(name) + @@clusters ||= {} + @@clusters[name] = self.new(name) end def self.[](name) - @clusters[name] + @@clusters[name] end def self.all - @clusters.values + @@clusters.values end - def nodes - @nodes.values + def find_by_name(name) + nodes.find { |node| node.name == name } end + alias_method :[], :find_by_name - def [](name) - @nodes[name] + def nodes + @graph.keys 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 - + def connect(*nodes, subnet: SubnetFactory.next_subnet) + # Create a complete graph (a graph where all nodes are connected to each + # other) between all the nodes, because that's what a subnet is. nodes.permutation(2).each do |(a, b)| + @graph[a] ||= [] @graph[a] << b end - end - def adjacent?(a, b) - adjacent(a).include?(b) - end + nodes.each do |node| + node.ips << subnet.next_ip + node.cluster = self + end - def adjacent(a) - @graph[a] + subnet.last_ip 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) + def ip_in_same_subnet(a, b) + a.ips.find { |a_ip| + b.ips.find { |b_ip| + a_ip.include?(b_ip) } } end + alias_method :neighbor?, :ip_in_same_subnet + end - private - def in_subnet?(subnet, node) - node.subnets.any? { |ip| subnet.include?(ip) } + class BGPCluster < Cluster + def bgp_neighbors(node) + @graph[node].select { |neighbor| + neighbor.is_a?(BGPPeer) + } end end end