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
FAILED_REFRESH_STATUS =
'Failed'

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.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 20

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

  if properties[:healthy_percentage]
    properties[:min_healthy_percentage] = properties.delete(:healthy_percentage)

    Kernel.warn('WARNING: the property `healthy_percentage` is deprecated and will be removed in a future release. Please update to `min_healthy_percentage`.')
  end

  validate_properties!
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



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

def properties
  @properties
end

#refresh_idObject (readonly)

Returns the value of attribute refresh_id.



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

def refresh_id
  @refresh_id
end

Instance Method Details

#enter_standby(instance) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 113

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)


33
34
35
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 33

def exists?
  aws_autoscaling_group.exists?
end

#exit_standby(instance) ⇒ Object



127
128
129
130
131
132
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 127

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

  instance.exit_standby
end

#instance_warmup_timeObject



50
51
52
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 50

def instance_warmup_time
  aws_autoscaling_group.health_check_grace_period
end

#instancesObject

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



103
104
105
106
107
108
109
110
111
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 103

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



93
94
95
96
97
98
99
100
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 93

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



37
38
39
40
41
42
43
44
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 37

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

#max_healthy_percentageObject



58
59
60
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 58

def max_healthy_percentage
  properties.fetch(:max_healthy_percentage, nil)
end

#min_healthy_percentageObject



54
55
56
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 54

def min_healthy_percentage
  properties.fetch(:min_healthy_percentage, nil)
end

#name_tagObject



138
139
140
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 138

def name_tag
  "Deployment for #{name}"
end

#rolling?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 134

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

#start_instance_refresh(launch_template) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/capistrano/asg/rolling/autoscale_group.rb', line 62

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,
      skip_matching: true,
      min_healthy_percentage: min_healthy_percentage,
      max_healthy_percentage: max_healthy_percentage
    }.compact
  ).instance_refresh_id
rescue Aws::AutoScaling::Errors::InstanceRefreshInProgress => e
  raise Capistrano::ASG::Rolling::StartInstanceRefreshError, e
end

#subnet_idsObject



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

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