Class: Rollo::Model::HostCluster

Inherits:
Object
  • Object
show all
Defined in:
lib/rollo/model/host_cluster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(asg_name, region, asg_resource = nil, waiter = nil) ⇒ HostCluster

Returns a new instance of HostCluster.



12
13
14
15
16
17
18
19
20
21
# File 'lib/rollo/model/host_cluster.rb', line 12

def initialize(asg_name, region, asg_resource = nil, waiter = nil)
  @region = region
  @asg_name = asg_name
  @asg_resource = asg_resource ||
      Aws::AutoScaling::Resource.new(region: region)
  @asg = @asg_resource.group(@asg_name)
  record_latest_scaling_activity

  @waiter = waiter || Wait.new(attempts: 300, timeout: 30, delay: 5)
end

Instance Attribute Details

#last_scaling_activityObject (readonly)

Returns the value of attribute last_scaling_activity.



10
11
12
# File 'lib/rollo/model/host_cluster.rb', line 10

def last_scaling_activity
  @last_scaling_activity
end

Instance Method Details

#decrease_capacity_by(capacity_delta, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/rollo/model/host_cluster.rb', line 72

def decrease_capacity_by(capacity_delta, &block)
  initial = desired_capacity
  decreased = initial - capacity_delta

  callbacks_for(block).try_respond_with(
      :prepare, initial, decreased)

  ensure_capacity_changed_to(decreased, &block)
end

#desired_capacityObject



31
32
33
# File 'lib/rollo/model/host_cluster.rb', line 31

def desired_capacity
  @asg.desired_capacity
end

#desired_capacity=(capacity) ⇒ Object



35
36
37
# File 'lib/rollo/model/host_cluster.rb', line 35

def desired_capacity=(capacity)
  @asg.set_desired_capacity({desired_capacity: capacity})
end

#ensure_capacity_changed_to(capacity, &block) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/rollo/model/host_cluster.rb', line 82

def ensure_capacity_changed_to(capacity, &block)
  self.desired_capacity = capacity
  wait_for_capacity_change_start(&block)
  wait_for_capacity_change_end(&block)
  wait_for_capacity_health(&block)
  record_latest_scaling_activity
end

#has_completed_changing_capacity?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/rollo/model/host_cluster.rb', line 54

def has_completed_changing_capacity?
  scaling_activities.all?(&:is_complete?)
end

#has_desired_capacity?Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/rollo/model/host_cluster.rb', line 39

def has_desired_capacity?
  hosts.size == desired_capacity &&
      hosts.all? {|h| h.is_in_service? && h.is_healthy?}
end

#has_started_changing_capacity?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/rollo/model/host_cluster.rb', line 48

def has_started_changing_capacity?
  scaling_activities
      .select {|a| a.started_after_completion_of?(@last_scaling_activity)}
      .size > 0
end

#hostsObject



58
59
60
# File 'lib/rollo/model/host_cluster.rb', line 58

def hosts
  @asg.instances.collect {|h| Host.new(h)}
end

#increase_capacity_by(capacity_delta, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/rollo/model/host_cluster.rb', line 62

def increase_capacity_by(capacity_delta, &block)
  initial = desired_capacity
  increased = initial + capacity_delta

  callbacks_for(block).try_respond_with(
      :prepare, initial, increased)

  ensure_capacity_changed_to(increased, &block)
end

#nameObject



27
28
29
# File 'lib/rollo/model/host_cluster.rb', line 27

def name
  @asg_name
end

#record_latest_scaling_activityObject



117
118
119
# File 'lib/rollo/model/host_cluster.rb', line 117

def record_latest_scaling_activity
  @last_scaling_activity = scaling_activities.first
end

#reloadObject



23
24
25
# File 'lib/rollo/model/host_cluster.rb', line 23

def reload
  @asg.reload
end

#scaling_activitiesObject



44
45
46
# File 'lib/rollo/model/host_cluster.rb', line 44

def scaling_activities
  @asg.activities.collect {|a| ScalingActivity.new(a)}
end

#wait_for_capacity_change_end(&block) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/rollo/model/host_cluster.rb', line 99

def wait_for_capacity_change_end(&block)
  @waiter.until do |attempt|
    reload
    callbacks_for(block)
        .try_respond_with(:waiting_for_end, attempt) if block
    has_completed_changing_capacity?
  end
end

#wait_for_capacity_change_start(&block) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/rollo/model/host_cluster.rb', line 90

def wait_for_capacity_change_start(&block)
  @waiter.until do |attempt|
    reload
    callbacks_for(block)
        .try_respond_with(:waiting_for_start, attempt) if block
    has_started_changing_capacity?
  end
end

#wait_for_capacity_health(&block) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/rollo/model/host_cluster.rb', line 108

def wait_for_capacity_health(&block)
  @waiter.until do |attempt|
    reload
    callbacks_for(block)
        .try_respond_with(:waiting_for_health, attempt) if block
    has_desired_capacity?
  end
end