Sha256: 5738449e400616be69b7ab33162837d706700b1036ed3a1f0b35dc6a9aa2304b

Contents?: true

Size: 1.3 KB

Versions: 6

Compression:

Stored size: 1.3 KB

Contents

require 'aws-sdk-autoscaling'
require 'net/http'
require 'uri'

module JobsAutoscaling
  class AwsAction
    attr_reader :asg_name, :client

    def initialize(asg_name:, aws_config: {}, instance_id: nil)
      @asg_name = asg_name
      @client = Aws::AutoScaling::Client.new(aws_config.reverse_merge(retry_limit: 10))
      @instance_id = instance_id
    end

    def idle
      @client.set_instance_protection(
        protected_from_scale_in: false,
        auto_scaling_group_name: asg_name,
        instance_ids: [instance_id],
      )
    end

    # it's intentional that if this call fails, the error bubbles up to
    # inst-jobs and errors the WorkQueue. We don't want to start running the
    # job if we weren't able to block scaledowns.
    def busy
      @client.set_instance_protection(
        protected_from_scale_in: true,
        auto_scaling_group_name: asg_name,
        instance_ids: [instance_id],
      )
    end

    # This is the hard-coded EC2 endpoint for getting instance metadata.
    # Oddly the ruby SDK doesn't include a method to get this information,
    # you are expected to just hit the endpoint yourself.
    INSTANCE_ID_ENDPOINT = 'http://169.254.169.254/latest/meta-data/instance-id'

    def instance_id
      @instance_id ||= Net::HTTP.get(URI.parse(INSTANCE_ID_ENDPOINT))
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
inst-jobs-autoscaling-2.1.1 lib/jobs_autoscaling/aws_action.rb
inst-jobs-autoscaling-2.1.0 lib/jobs_autoscaling/aws_action.rb
inst-jobs-autoscaling-2.0.0 lib/jobs_autoscaling/aws_action.rb
inst-jobs-autoscaling-1.0.5 lib/jobs_autoscaling/aws_action.rb
inst-jobs-autoscaling-1.0.4 lib/jobs_autoscaling/aws_action.rb
inst-jobs-autoscaling-1.0.3 lib/jobs_autoscaling/aws_action.rb