Class: Assh::AwsProvider

Inherits:
Provider show all
Defined in:
lib/providers/aws_provider.rb

Instance Method Summary collapse

Methods inherited from Provider

#add_host, load_configuration!, register_provider!, status, verbose!

Constructor Details

#initialize(configuration) ⇒ AwsProvider

Returns a new instance of AwsProvider.



7
8
9
# File 'lib/providers/aws_provider.rb', line 7

def initialize(configuration)
  super(configuration)
end

Instance Method Details

#parse_config!(config) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/providers/aws_provider.rb', line 11

def parse_config!(config)
  return unless config
  raise 'Unexpected config type' unless config.is_a? Hash

  name_match = config.delete 'NameMatch'
  access_key_id = config.delete 'AccessKeyId'
  secret_access_key = config.delete 'SecretAccessKey'

  AWS.config(
      access_key_id: access_key_id,
      secret_access_key: secret_access_key)


  Provider::status "Downloading regions list..."

  regions = AWS.ec2.regions.map { |r| r.name }

  Provider::status "Downloading instances..."

  regions.each do |region|

    ec2 = AWS::EC2.new(
        access_key_id: access_key_id,
        secret_access_key: secret_access_key,
        region: region)

    hosts = {}
    ec2.instances.each do |i|
      Provider::status '.', true
      name = (i.tags.Name || i.instance_id).underscore
      ip = i.public_ip_address
      hosts[name] = ip
    end

    hosts.each do |name, ip|

      if name_match
        m = Regexp.new(name_match).match(name)
        if m
          name = m.captures[0]
        end
      end

      add_host(region, name, Host.new(config.merge({'Host' => name, 'HostName' => ip})))
    end

  end

  Provider::status

end