lib/capistrano/asg/rolling/autoscale_group.rb in capistrano-asg-rolling-0.3.0 vs lib/capistrano/asg/rolling/autoscale_group.rb in capistrano-asg-rolling-0.4.0

- old
+ new

@@ -10,12 +10,14 @@ include AWS LIFECYCLE_STATE_IN_SERVICE = 'InService' LIFECYCLE_STATE_STANDBY = 'Standby' - attr_reader :name, :properties + COMPLETED_REFRESH_STATUSES = %w[Successful Failed Cancelled RollbackSuccessful RollbackFailed].freeze + attr_reader :name, :properties, :refresh_id + def initialize(name, properties = {}) @name = name @properties = properties end @@ -43,11 +45,11 @@ def healthy_percentage properties.fetch(:healthy_percentage, 100) end def start_instance_refresh(launch_template) - aws_autoscaling_client.start_instance_refresh( + @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, @@ -57,15 +59,30 @@ 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 + InstanceRefreshStatus = Struct.new(:status, :percentage_complete) do + def completed? + COMPLETED_REFRESH_STATUSES.include?(status) + end + end + + 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 + # Returns instances with lifecycle state "InService" for this Auto Scaling Group. 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? @@ -103,9 +120,19 @@ def name_tag "Deployment for #{name}" end private + + def most_recent_instance_refresh + parameters = { + auto_scaling_group_name: name, + max_records: 1 + } + parameters[:instance_refresh_ids] = [@refresh_id] if @refresh_id + refresh = aws_autoscaling_client.describe_instance_refreshes(parameters).to_h + refresh[:instance_refreshes].first + end def aws_autoscaling_group @aws_autoscaling_group ||= ::Aws::AutoScaling::AutoScalingGroup.new(name: name, client: aws_autoscaling_client) end end