lib/capistrano/asg/rolling/autoscale_group.rb in capistrano-asg-rolling-0.4.1 vs lib/capistrano/asg/rolling/autoscale_group.rb in capistrano-asg-rolling-0.5.0

- old
+ new

@@ -11,16 +11,25 @@ LIFECYCLE_STATE_IN_SERVICE = 'InService' LIFECYCLE_STATE_STANDBY = 'Standby' COMPLETED_REFRESH_STATUSES = %w[Successful Failed Cancelled RollbackSuccessful RollbackFailed].freeze + FAILED_REFRESH_STATUS = 'Failed' attr_reader :name, :properties, :refresh_id 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 def exists? aws_autoscaling_group.exists? end @@ -40,14 +49,18 @@ def instance_warmup_time aws_autoscaling_group.health_check_grace_period end - def healthy_percentage - properties.fetch(:healthy_percentage, 100) + def min_healthy_percentage + properties.fetch(:min_healthy_percentage, nil) end + def max_healthy_percentage + properties.fetch(:max_healthy_percentage, nil) + end + def start_instance_refresh(launch_template) @refresh_id = aws_autoscaling_client.start_instance_refresh( auto_scaling_group_name: name, strategy: 'Rolling', desired_configuration: { @@ -56,22 +69,27 @@ version: launch_template.version } }, preferences: { instance_warmup: instance_warmup_time, - min_healthy_percentage: healthy_percentage, - skip_matching: true - } + 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::InstanceRefreshFailed, e + raise Capistrano::ASG::Rolling::StartInstanceRefreshError, e end InstanceRefreshStatus = Struct.new(:status, :percentage_complete) do def completed? COMPLETED_REFRESH_STATUSES.include?(status) end + + def failed? + status == FAILED_REFRESH_STATUS + end end def latest_instance_refresh instance_refresh = most_recent_instance_refresh status = instance_refresh&.dig(:status) @@ -133,9 +151,24 @@ refresh[:instance_refreshes].first end def aws_autoscaling_group @aws_autoscaling_group ||= ::Aws::AutoScaling::AutoScalingGroup.new(name: name, client: aws_autoscaling_client) + end + + def validate_properties! + raise ArgumentError, 'Property `min_healthy_percentage` must be between 0-100.' if min_healthy_percentage && !(0..100).cover?(min_healthy_percentage) + + if max_healthy_percentage + raise ArgumentError, 'Property `max_healthy_percentage` must be between 100-200.' unless (100..200).cover?(max_healthy_percentage) + + if min_healthy_percentage + diff = max_healthy_percentage - min_healthy_percentage + raise ArgumentError, 'The difference between `min_healthy_percentage` and `max_healthy_percentage` must not be greater than 100.' if diff > 100 + else + raise ArgumentError, 'Property `min_healthy_percentage` must be specified when using `max_healthy_percentage`.' + end + end end end end end end