Sha256: 6041b287b179e2097e131a5fd667f9aa9325400910a3b13974c2d6ed16123010

Contents?: true

Size: 1.65 KB

Versions: 10

Compression:

Stored size: 1.65 KB

Contents

class Marty::Diagnostic::Aws::Ec2Instance < Marty::Aws::Request
  attr_reader :tag,
              :nodes,
              :instances

  class InstancesSet
    STATES = [
      :pending, :running, :shutting_down, :terminated, :stopping, :stopped
    ].freeze

    attr_reader *STATES

    def get_state instances, state
      instances.map do
        |i|
        i.except('state') if i['state']['name'] == state
      end.compact
    end

    def initialize instances
      STATES.each do |s|
        instance_variable_set("@#{s}", get_state(instances, s.to_s))
      end
    end
  end

  def initialize
    super
    @service   = 'ec2'
    @tag       = get_tag
    @instances = InstancesSet.new(get_instances)
    @nodes     = get_private_ips
  end

  private
  def ec2_request action, params = {}
    resp = request({action: action}, params)
    Hash.from_xml(resp)["#{action}Response"]
  end

  def get_tag
    params = {'Filter.1.Name'    => 'resource-id',
              'Filter.1.Value.1' => get_instance_id,
              'Filter.2.Name'    => 'key',
              'Filter.2.Value.1' => 'Name'}
    ec2_request('DescribeTags', params)['tagSet']['item']['value']
  end

  def get_instances
    params = {'Filter.1.Name'    => 'tag-value',
              'Filter.1.Value.1' => @tag}

    resp = ec2_request('DescribeInstances', params)

    instances = ensure_resp(
      ['reservationSet', 'item', 'instancesSet', 'item'],
      resp
    ).map do |i|
      {
        'id'    => i['instanceId'],
        'ip'    => i['privateIpAddress'],
        'state' => i['instanceState'],
      }
    end.flatten(1)
  end

  def get_private_ips
    @instances.running.map{|i| i['ip']}.compact
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
marty-2.5.2 other/marty/diagnostic/aws/ec2_instance.rb
marty-2.5.1 other/marty/diagnostic/aws/ec2_instance.rb
marty-2.5.0 other/marty/diagnostic/aws/ec2_instance.rb
marty-2.4.9 other/marty/diagnostic/aws/ec2_instance.rb
marty-2.4.8 other/marty/diagnostic/aws/ec2_instance.rb
marty-2.4.7 other/marty/diagnostic/aws/ec2_instance.rb
marty-2.4.6 other/marty/diagnostic/aws/ec2_instance.rb
marty-2.4.5 other/marty/diagnostic/aws/ec2_instance.rb
marty-2.4.4 other/marty/diagnostic/aws/ec2_instance.rb
marty-2.4.3 other/marty/diagnostic/aws/ec2_instance.rb