Class: InstanceSelector::Providers::AWS

Inherits:
Object
  • Object
show all
Defined in:
lib/instance_selector/providers/aws.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AWS

Returns a new instance of AWS.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/instance_selector/providers/aws.rb', line 4

def initialize(options={})
  @fog = options.delete(:fog_client)

  unless @fog
    @options = {
      region: 'us-east-1',
    }.merge(options)

    connect
  end
end

Instance Method Details

#args_to_filters(args) ⇒ Object



72
73
74
75
# File 'lib/instance_selector/providers/aws.rb', line 72

def args_to_filters(args)
  filters = {}
  filters.merge! parse_tags(args[:tags])
end

#connectObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/instance_selector/providers/aws.rb', line 54

def connect
  begin
    @fog = Fog::Compute.new(provider:              'AWS',
                            region:                @options[:region],
                            aws_access_key_id:     ENV['AWS_ACCESS_KEY_ID'],
                            aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'])
  rescue
    @fog = Fog::Compute.new(provider: 'AWS', region: @options[:region])
  ensure
    unless @fog
      abort <<-EOS
        Could not authenticate with AWS.
        Please create a .fog file or set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
      EOS
    end
  end
end

#dns_from_instance_reservation_set(reservation_set) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/instance_selector/providers/aws.rb', line 17

def dns_from_instance_reservation_set(reservation_set)
  reservation_set.inject({}) do |memo, i|
    # Each instancesSet can have multiple instances
    # Odd, but explains why it's plural.
    i['instancesSet'].each do |instance|
      key = instance['dnsName'].empty? ? instance['ipAddress'] : instance['dnsName']
      memo[key] = {name: instance['tagSet']['Name'], instance_id: instance['instanceId']}
    end

    memo
  end
end

#instances(filters = {}) ⇒ Object



50
51
52
# File 'lib/instance_selector/providers/aws.rb', line 50

def instances(filters={})
  on_demand_instances(filters).merge(spot_instances(filters))
end

#on_demand_instances(filters = {}) ⇒ Object



30
31
32
33
34
# File 'lib/instance_selector/providers/aws.rb', line 30

def on_demand_instances(filters={})
  instances = @fog.describe_instances({"instance-state-name" => "running"}.merge(filters))

  dns_from_instance_reservation_set instances.body['reservationSet']
end

#parse_tags(tags) ⇒ Object



77
78
79
80
81
82
# File 'lib/instance_selector/providers/aws.rb', line 77

def parse_tags(tags)
  tags.inject({}) do |memo, tag|
    memo["tag:#{tag[0]}"] = tag[1]
    memo
  end
end

#spot_instances(filters = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/instance_selector/providers/aws.rb', line 36

def spot_instances(filters={})
  requests = @fog.describe_spot_instance_requests({'state' => 'active'}.merge(filters))
  requests.body['spotInstanceRequestSet'].inject({}) do |memo, req|

    if req['instanceId'] && !req['instanceId'].empty?
      instances = @fog.describe_instances('instance-id' => req['instanceId'])
      memo.merge! dns_from_instance_reservation_set(instances.body['reservationSet'])
    end

    memo
  end
end