Sha256: 6b57f7dcf7ea252f1be1814090d95e22894e1631e6e044fb81d13d961e5dec3a

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

require 'net/http'
require 'json'

module JFlow
  class TerminationProtector

    def region
      instance_data['region']
    end

    def instance_id
      instance_data['instanceId']
    end

    # Returns a hash of instance data, including region, instance id + more
    def instance_data
      @instance_data ||= JSON.parse(Net::HTTP.get(URI.parse('http://169.254.169.254/latest/dynamic/instance-identity/document')))
    end

    def get_asg_name
      ec2_client = Aws::EC2::Client.new(region: region, credentials: Aws::InstanceProfileCredentials.new)
      instance_tags = ec2_client.describe_tags(filters: [
        {
          name: "resource-id",
          values: [instance_id]
        }
      ])[0]
      asg_name = instance_tags.select{|tag| tag.key == "aws:autoscaling:groupName"}.first.value
      JFlow.configuration.logger.debug "Discovered autoscaling group name #{asg_name}"

      asg_name
    end

    def set_protection(protect_status)
      JFlow.configuration.logger.debug "Setting termination protection status to #{protect_status} for instance #{instance_id} in region #{region}"
      begin
        asg_client = Aws::AutoScaling::Client.new(region: region, credentials: Aws::InstanceProfileCredentials.new)
        asg_client.set_instance_protection({
          instance_ids: [instance_id],
          auto_scaling_group_name: get_asg_name,
          protected_from_scale_in: protect_status
        })
      rescue => e
        JFlow.configuration.logger.debug "Something went wrong setting termination proection: #{e.inspect}"
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jflow-0.5.0 lib/jflow/termination_protector.rb
jflow-0.4.5 lib/jflow/termination_protector.rb
jflow-0.4.4 lib/jflow/termination_protector.rb