Class: Cloudfinder::EC2::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudfinder-ec2/cluster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Cloudfinder::EC2::Cluster

Parameters:

  • args (Hash)

    containing the cluster name and the instances



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_nameObject (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

Returns:

  • (bool)


15
16
17
# File 'lib/cloudfinder-ec2/cluster.rb', line 15

def empty?
  instances.empty?
end

#get_instance(instance_id) ⇒ Object

Raises:

  • (RangeError)


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

Parameters:

  • instance_id (string)

Returns:

  • (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

Parameters:

  • role (symbol)

Returns:

  • (bool)


26
27
28
# File 'lib/cloudfinder-ec2/cluster.rb', line 26

def has_role?(role)
  list_roles.include?(role)
end

#list_instancesArray<Cloudfinder::EC2::Instance>

Returns:



48
49
50
# File 'lib/cloudfinder-ec2/cluster.rb', line 48

def list_instances
  instances
end

#list_role_instances(role) ⇒ Array<Cloudfinder::EC2::Instance>

Parameters:

  • role (symbol)

Returns:



54
55
56
# File 'lib/cloudfinder-ec2/cluster.rb', line 54

def list_role_instances(role)
  instances.select { |instance| instance.role === role }
end

#list_rolesArray<symbol>

Returns:

  • (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

Returns:

  • (bool)


20
21
22
# File 'lib/cloudfinder-ec2/cluster.rb', line 20

def running?
  !empty?
end

#to_hashHash

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',...}
     ]
   }
}

Returns:

  • (Hash)


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