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.



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

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/moonshot/tools/asg_rollout/asg.rb', line 9

def name
  @name
end

Instance Method Details

#current_max_and_desiredObject



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

def current_max_and_desired
  asg = load_asg

  [asg.max_size, asg.desired_capacity]
end

#detach_instance(id, decrement:) ⇒ Object



58
59
60
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
# File 'lib/moonshot/tools/asg_rollout/asg.rb', line 58

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
  unless activity
    raise 'Did not receive Activity from DetachInstances call!'
  end

  # 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



92
93
94
95
96
97
# File 'lib/moonshot/tools/asg_rollout/asg.rb', line 92

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



29
30
31
32
33
34
35
# File 'lib/moonshot/tools/asg_rollout/asg.rb', line 29

def non_conforming_instances
  asg = load_asg

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

#set_max_and_desired(max, desired) ⇒ Object



22
23
24
25
26
27
# File 'lib/moonshot/tools/asg_rollout/asg.rb', line 22

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



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

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