Class: Moonshot::Tools::ASGRollout::ASG

Inherits:
Object
  • Object
show all
Defined in:
lib/moonshot/tools/asg_rollout/asg.rb

Overview

Abstration layer with AWS Auto Scaling Groups, for the Rollout tool.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ASG

Returns a new instance of ASG.



13
14
15
16
# File 'lib/moonshot/tools/asg_rollout/asg.rb', line 13

def initialize(name)
  @name = name
  @last_seen_ids = []
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/moonshot/tools/asg_rollout/asg.rb', line 11

def name
  @name
end

Instance Method Details

#current_max_and_desiredObject



18
19
20
21
22
# File 'lib/moonshot/tools/asg_rollout/asg.rb', line 18

def current_max_and_desired
  asg = load_asg

  [asg.max_size, asg.desired_capacity]
end

#detach_instance(id, decrement:) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/moonshot/tools/asg_rollout/asg.rb', line 61

def detach_instance(id, decrement:)
  # Store the current instance IDs for the next call to wait_for_new_instance.
  load_asg

  resp = autoscaling.detach_instances(
    auto_scaling_group_name: @name,
    instance_ids: [id],
    should_decrement_desired_capacity: decrement
  )

  activity = resp.activities.first
  raise 'Did not receive Activity from DetachInstances call!' unless activity

  # Wait for the detach activity to complete:
  loop do
    resp = autoscaling.describe_scaling_activities(
      auto_scaling_group_name: @name
    )

    current_status = resp.activities
                         .find { |a| a.activity_id == activity.activity_id }
                         .status_code

    case current_status
    when 'Failed', 'Cancelled'
      raise 'Detachment did not complete successfully!'
    when 'Successful'
      return
    end

    sleep 1
  end
end

#instance_health(id) ⇒ Object



95
96
97
98
99
100
# File 'lib/moonshot/tools/asg_rollout/asg.rb', line 95

def instance_health(id)
  elb_status = nil
  elb_status = elb_instance_state(id) if elb_name

  InstanceHealth.new(asg_instance_state(id), elb_status)
end

#non_conforming_instancesObject



32
33
34
35
36
37
38
# File 'lib/moonshot/tools/asg_rollout/asg.rb', line 32

def non_conforming_instances
  asg = load_asg

  asg.instances
     .reject { |i| i.launch_configuration_name == asg.launch_configuration_name }
     .map(&:instance_id)
end

#set_max_and_desired(max, desired) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/moonshot/tools/asg_rollout/asg.rb', line 24

def set_max_and_desired(max, desired)
  autoscaling.update_auto_scaling_group(
    auto_scaling_group_name: @name,
    max_size: max,
    desired_capacity: desired
  )
end

#wait_for_new_instanceObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/moonshot/tools/asg_rollout/asg.rb', line 40

def wait_for_new_instance
  # Query the ASG until an instance appears which is not in
  # @last_seen_ids, then add it to @last_seen_ids and return
  # it.
  previous_ids = @last_seen_ids

  loop do
    load_asg

    new_ids = @last_seen_ids - previous_ids
    previous_ids = @last_seen_ids

    unless new_ids.empty?
      @last_seen_ids << new_ids.first
      return new_ids.first
    end

    sleep 3
  end
end