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.



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

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.



13
14
15
# File 'lib/rollo/model/host_cluster.rb', line 13

def last_scaling_activity
  @last_scaling_activity
end

Instance Method Details

#completed_changing_capacity?Boolean

Returns:

  • (Boolean)


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

def completed_changing_capacity?
  scaling_activities.all?(&:complete?)
end

#decrease_capacity_by(capacity_delta, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/rollo/model/host_cluster.rb', line 76

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



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

def desired_capacity
  @asg.desired_capacity
end

#desired_capacity=(capacity) ⇒ Object



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

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

#desired_capacity?Boolean

Returns:

  • (Boolean)


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

def desired_capacity?
  hosts.size == desired_capacity &&
    hosts.all? { |h| h.in_service? && h.healthy? }
end

#ensure_capacity_changed_to(capacity, &block) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/rollo/model/host_cluster.rb', line 87

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

#hostsObject



61
62
63
# File 'lib/rollo/model/host_cluster.rb', line 61

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

#increase_capacity_by(capacity_delta, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/rollo/model/host_cluster.rb', line 65

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



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

def name
  @asg_name
end

#record_latest_scaling_activityObject



128
129
130
# File 'lib/rollo/model/host_cluster.rb', line 128

def record_latest_scaling_activity
  @last_scaling_activity = scaling_activities.first
end

#reloadObject



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

def reload
  @asg.reload
end

#scaling_activitiesObject



47
48
49
# File 'lib/rollo/model/host_cluster.rb', line 47

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

#started_changing_capacity?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/rollo/model/host_cluster.rb', line 51

def started_changing_capacity?
  scaling_activities
    .select { |a| a.started_after_completion_of?(@last_scaling_activity) }
    .size.positive?
end

#wait_for_capacity_change_end(&block) ⇒ Object



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

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

#wait_for_capacity_change_start(&block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/rollo/model/host_cluster.rb', line 95

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

#wait_for_capacity_health(&block) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/rollo/model/host_cluster.rb', line 117

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