Class: Clusterfuck::Cluster
- Inherits:
-
Object
- Object
- Clusterfuck::Cluster
- Defined in:
- lib/clusterfuck/cluster.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#network ⇒ Object
readonly
Returns the value of attribute network.
Class Method Summary collapse
Instance Method Summary collapse
- #connect(*nodes, subnet: SubnetFactory.next_subnet) ⇒ Object
- #find_by_name(name) ⇒ Object (also: #[])
-
#initialize(name) ⇒ Cluster
constructor
A new instance of Cluster.
- #ip_in_same_subnet(a, b) ⇒ Object (also: #neighbor?)
- #nodes ⇒ Object
Constructor Details
#initialize(name) ⇒ Cluster
Returns a new instance of Cluster.
5 6 7 8 |
# File 'lib/clusterfuck/cluster.rb', line 5 def initialize(name) @name = name @graph = {} end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/clusterfuck/cluster.rb', line 3 def name @name end |
#network ⇒ Object (readonly)
Returns the value of attribute network.
3 4 5 |
# File 'lib/clusterfuck/cluster.rb', line 3 def network @network end |
Class Method Details
.[](name) ⇒ Object
15 16 17 |
# File 'lib/clusterfuck/cluster.rb', line 15 def self.[](name) @@clusters[name] end |
.all ⇒ Object
19 20 21 |
# File 'lib/clusterfuck/cluster.rb', line 19 def self.all @@clusters.values end |
.create(name) ⇒ Object
10 11 12 13 |
# File 'lib/clusterfuck/cluster.rb', line 10 def self.create(name) @@clusters ||= {} @@clusters[name] = self.new(name) end |
Instance Method Details
#connect(*nodes, subnet: SubnetFactory.next_subnet) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/clusterfuck/cluster.rb', line 32 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 nodes.each do |node| node.ips << subnet.next_ip node.cluster = self end subnet.last_ip end |
#find_by_name(name) ⇒ Object Also known as: []
23 24 25 |
# File 'lib/clusterfuck/cluster.rb', line 23 def find_by_name(name) nodes.find { |node| node.name == name } end |
#ip_in_same_subnet(a, b) ⇒ Object Also known as: neighbor?
48 49 50 51 52 53 54 |
# File 'lib/clusterfuck/cluster.rb', line 48 def ip_in_same_subnet(a, b) a.ips.find { |a_ip| b.ips.find { |b_ip| a_ip.include?(b_ip) } } end |
#nodes ⇒ Object
28 29 30 |
# File 'lib/clusterfuck/cluster.rb', line 28 def nodes @graph.keys end |