Class: Capistrano::ASG::Rolling::AutoscaleGroup

Inherits:
Object
  • Object
show all
Includes:
AWS
Defined in:
lib/capistrano/asg/rolling/autoscale_group.rb

Overview

AWS EC2 Auto Scaling Group.

Defined Under Namespace

Classes: InstanceRefreshStatus

Constant Summary collapse

LIFECYCLE_STATE_IN_SERVICE =
'InService'
LIFECYCLE_STATE_STANDBY =
'Standby'
COMPLETED_REFRESH_STATUSES =
%w[Successful Failed Cancelled RollbackSuccessful RollbackFailed].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AWS

#aws_autoscaling_client, #aws_ec2_client

Constructor Details

#initialize(name, properties = {}) ⇒ AutoscaleGroup

Returns a new instance of AutoscaleGroup.



19
20
21
22
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 19

def initialize(name, properties = {})
  @name = name
  @properties = properties
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 17

def name
  @name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



17
18
19
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 17

def properties
  @properties
end

#refresh_idObject (readonly)

Returns the value of attribute refresh_id.



17
18
19
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 17

def refresh_id
  @refresh_id
end

Instance Method Details

#enter_standby(instance) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 95

def enter_standby(instance)
  instance = aws_autoscaling_group.instances.find { |i| i.id == instance.id }
  return if instance.nil?

  instance.enter_standby(should_decrement_desired_capacity: true)

  loop do
    instance.load
    break if instance.lifecycle_state == LIFECYCLE_STATE_STANDBY

    sleep 1
  end
end

#exists?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 24

def exists?
  aws_autoscaling_group.exists?
end

#exit_standby(instance) ⇒ Object



109
110
111
112
113
114
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 109

def exit_standby(instance)
  instance = aws_autoscaling_group.instances.find { |i| i.id == instance.id }
  return if instance.nil?

  instance.exit_standby
end

#healthy_percentageObject



45
46
47
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 45

def healthy_percentage
  properties.fetch(:healthy_percentage, 100)
end

#instance_warmup_timeObject



41
42
43
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 41

def instance_warmup_time
  aws_autoscaling_group.health_check_grace_period
end

#instancesObject

Returns instances with lifecycle state “InService” for this Auto Scaling Group.



85
86
87
88
89
90
91
92
93
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 85

def instances
  instance_ids = aws_autoscaling_group.instances.select { |i| i.lifecycle_state == LIFECYCLE_STATE_IN_SERVICE }.map(&:instance_id)
  return [] if instance_ids.empty?

  response = aws_ec2_client.describe_instances(instance_ids: instance_ids)
  response.reservations.flat_map(&:instances).map do |instance|
    Instance.new(instance.instance_id, instance.private_ip_address, instance.public_ip_address, instance.image_id, self)
  end
end

#latest_instance_refreshObject



75
76
77
78
79
80
81
82
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 75

def latest_instance_refresh
  instance_refresh = most_recent_instance_refresh
  status = instance_refresh&.dig(:status)
  percentage_complete = instance_refresh&.dig(:percentage_complete)
  return nil if status.nil?

  InstanceRefreshStatus.new(status, percentage_complete)
end

#launch_templateObject



28
29
30
31
32
33
34
35
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 28

def launch_template
  @launch_template ||= begin
    template = aws_autoscaling_group.launch_template
    raise Capistrano::ASG::Rolling::NoLaunchTemplate if template.nil?

    LaunchTemplate.new(template.launch_template_id, template.version, template.launch_template_name)
  end
end

#name_tagObject



120
121
122
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 120

def name_tag
  "Deployment for #{name}"
end

#rolling?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 116

def rolling?
  properties.fetch(:rolling, true)
end

#start_instance_refresh(launch_template) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 49

def start_instance_refresh(launch_template)
  @refresh_id = aws_autoscaling_client.start_instance_refresh(
    auto_scaling_group_name: name,
    strategy: 'Rolling',
    desired_configuration: {
      launch_template: {
        launch_template_id: launch_template.id,
        version: launch_template.version
      }
    },
    preferences: {
      instance_warmup: instance_warmup_time,
      min_healthy_percentage: healthy_percentage,
      skip_matching: true
    }
  ).instance_refresh_id
rescue Aws::AutoScaling::Errors::InstanceRefreshInProgress => e
  raise Capistrano::ASG::Rolling::InstanceRefreshFailed, e
end

#subnet_idsObject



37
38
39
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 37

def subnet_ids
  aws_autoscaling_group.vpc_zone_identifier.split(',')
end