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



44
45
46
47
# File 'lib/instance_selector/providers/aws.rb', line 44

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

#connectObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/instance_selector/providers/aws.rb', line 26

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

#instances(filters = {}) ⇒ Object



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

def instances(filters={})
  instances = @fog.describe_instances({"instance-state-name" => "running"}.merge(filters))
  instances.body['reservationSet'].inject({}) do |memo, i|
    instance = i['instancesSet'][0]
    key = instance['dnsName'].empty? ? instance['ipAddress'] : instance['dnsName']
    memo[key] = instance['tagSet']['Name']
    memo
  end
end

#parse_tags(tags) ⇒ Object



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

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