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.



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

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: 720, timeout: 30, delay: 5)
end

Instance Attribute Details

#last_scaling_activityObject (readonly)

Returns the value of attribute last_scaling_activity.



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

def last_scaling_activity
  @last_scaling_activity
end

Instance Method Details

#decrease_capacity_by(capacity_delta, &block) ⇒ Object



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

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



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

def desired_capacity
  @asg.desired_capacity
end

#desired_capacity=(capacity) ⇒ Object



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

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

#ensure_capacity_changed_to(capacity, &block) ⇒ Object



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

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)


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

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

#has_desired_capacity?Boolean

Returns:

  • (Boolean)


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

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)


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

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

#hostsObject



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

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

#increase_capacity_by(capacity_delta, &block) ⇒ Object



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

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



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

def name
  @asg_name
end

#record_latest_scaling_activityObject



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

def record_latest_scaling_activity
  @last_scaling_activity = scaling_activities.first
end

#reloadObject



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

def reload
  @asg.reload
end

#scaling_activitiesObject



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

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

#wait_for_capacity_change_end(&block) ⇒ Object



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

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



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

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



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

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