Class: Cloudfinder::EC2::Cluster
- Inherits:
-
Object
- Object
- Cloudfinder::EC2::Cluster
- Defined in:
- lib/cloudfinder-ec2/cluster.rb
Instance Attribute Summary collapse
-
#cluster_name ⇒ Object
readonly
Returns the value of attribute cluster_name.
Instance Method Summary collapse
- #empty? ⇒ bool
- #get_instance(instance_id) ⇒ Object
- #has_instance?(instance_id) ⇒ bool
- #has_role?(role) ⇒ bool
- #initialize(args) ⇒ Cloudfinder::EC2::Cluster constructor
- #list_instances ⇒ Array<Cloudfinder::EC2::Instance>
- #list_role_instances(role) ⇒ Array<Cloudfinder::EC2::Instance>
- #list_roles ⇒ Array<symbol>
- #running? ⇒ bool
-
#to_hash ⇒ Hash
Return the current cluster as a simple nested hash, suitable for rendering to JSON or similar.
Constructor Details
#initialize(args) ⇒ Cloudfinder::EC2::Cluster
9 10 11 12 |
# File 'lib/cloudfinder-ec2/cluster.rb', line 9 def initialize(args) @cluster_name = args[:cluster_name] @instances = args[:instances] end |
Instance Attribute Details
#cluster_name ⇒ Object (readonly)
Returns the value of attribute cluster_name.
5 6 7 |
# File 'lib/cloudfinder-ec2/cluster.rb', line 5 def cluster_name @cluster_name end |
Instance Method Details
#empty? ⇒ bool
15 16 17 |
# File 'lib/cloudfinder-ec2/cluster.rb', line 15 def empty? instances.empty? end |
#get_instance(instance_id) ⇒ Object
36 37 38 39 40 |
# File 'lib/cloudfinder-ec2/cluster.rb', line 36 def get_instance(instance_id) found_instances = instances.select { |instance| instance.instance_id == instance_id } raise(RangeError, "#{instance_id} is not part of the #{@cluster_name} cluster") if found_instances.empty? found_instances.first end |
#has_instance?(instance_id) ⇒ bool
32 33 34 |
# File 'lib/cloudfinder-ec2/cluster.rb', line 32 def has_instance?(instance_id) instances.any? { |instance| instance.instance_id == instance_id } end |
#has_role?(role) ⇒ bool
26 27 28 |
# File 'lib/cloudfinder-ec2/cluster.rb', line 26 def has_role?(role) list_roles.include?(role) end |
#list_instances ⇒ Array<Cloudfinder::EC2::Instance>
48 49 50 |
# File 'lib/cloudfinder-ec2/cluster.rb', line 48 def list_instances instances end |
#list_role_instances(role) ⇒ Array<Cloudfinder::EC2::Instance>
54 55 56 |
# File 'lib/cloudfinder-ec2/cluster.rb', line 54 def list_role_instances(role) instances.select { |instance| instance.role === role } end |
#list_roles ⇒ Array<symbol>
43 44 45 |
# File 'lib/cloudfinder-ec2/cluster.rb', line 43 def list_roles instances.group_by { |instance| instance.role }.keys end |
#running? ⇒ bool
20 21 22 |
# File 'lib/cloudfinder-ec2/cluster.rb', line 20 def running? !empty? end |
#to_hash ⇒ Hash
Return the current cluster as a simple nested hash, suitable for rendering to JSON or similar
{
cluster_name: 'production',
roles: {
db: [
{instance_id: 'i-00000001', public_ip: '123.456.789.123',...}
]
}
}
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cloudfinder-ec2/cluster.rb', line 70 def to_hash hash = { cluster_name: @cluster_name, roles: {} } instances.each do |instance| hash[:roles][instance.role] = [] unless hash[:roles][instance.role] hash[:roles][instance.role] << instance.to_hash end hash end |