Class: Bosh::Registry::InstanceManager::Aws

Inherits:
Bosh::Registry::InstanceManager show all
Defined in:
lib/bosh/registry/instance_manager/aws.rb

Constant Summary collapse

AWS_MAX_RETRIES =
2

Instance Method Summary collapse

Methods inherited from Bosh::Registry::InstanceManager

#delete_settings, #read_settings, #update_settings

Constructor Details

#initialize(cloud_config) ⇒ Aws

Returns a new instance of Aws.



10
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
# File 'lib/bosh/registry/instance_manager/aws.rb', line 10

def initialize(cloud_config)
  validate_options(cloud_config)

  @logger = Bosh::Registry.logger

  @aws_properties = cloud_config["aws"]
  @aws_options = {
    :max_retries => @aws_properties["max_retries"] || AWS_MAX_RETRIES,
    :ec2_endpoint => @aws_properties['ec2_endpoint'] || "ec2.#{@aws_properties['region']}.amazonaws.com",
    :logger => @logger
  }
  # configure optional parameters
  %w(
    access_key_id
    secret_access_key
    ssl_verify_peer
    ssl_ca_file
    ssl_ca_path
  ).each do |k|
    @aws_options[k.to_sym] = @aws_properties[k] unless @aws_properties[k].nil?
  end

  # credentials_source could be static (default) or env_or_profile
  # - if "static", credentials must be provided
  # - if "env_or_profile", credentials are read from instance metadata
  credentials_source = cloud_config['aws']['credentials_source'] || 'static'
  static_credentials = {}

  if credentials_source == 'static'
    static_credentials[:access_key_id] = cloud_config['aws']['access_key_id']
    static_credentials[:secret_access_key] = cloud_config['aws']['secret_access_key']
  end

  @aws_options[:credential_provider] = Bosh::Registry::AWSCredentialsProvider.new(static_credentials)

  @ec2 = AWS::EC2.new(@aws_options)
end

Instance Method Details

#instance_ips(instance_id) ⇒ Object

Get the list of IPs belonging to this instance



77
78
79
80
81
82
83
84
85
86
# File 'lib/bosh/registry/instance_manager/aws.rb', line 77

def instance_ips(instance_id)
  instance = @ec2.instances[instance_id]
  ips = [instance.private_ip_address, instance.public_ip_address]
  if instance.has_elastic_ip?
    ips << instance.elastic_ip.public_ip
  end
  ips
rescue AWS::Errors::Base => e
  raise Bosh::Registry::AwsError, "AWS error: #{e}"
end

#validate_options(cloud_config) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bosh/registry/instance_manager/aws.rb', line 48

def validate_options(cloud_config)
  unless cloud_config.has_key?("aws") &&
      cloud_config["aws"].is_a?(Hash) &&
      cloud_config["aws"]["region"]
    raise ConfigError, "Invalid AWS configuration parameters"
  end

  credentials_source = cloud_config['aws']['credentials_source'] || 'static'

  if credentials_source != 'env_or_profile' && credentials_source != 'static'
    raise ConfigError, "Unknown credentials_source #{credentials_source}"
  end

  if credentials_source == 'static'
    if cloud_config["aws"]["access_key_id"].nil? || cloud_config["aws"]["secret_access_key"].nil?
        raise ConfigError, "Must use access_key_id and secret_access_key with static credentials_source"
    end
  end

  if credentials_source == 'env_or_profile'
    if cloud_config["aws"]["access_key_id"] || cloud_config["aws"]["secret_access_key"]
        raise ConfigError, "Can't use access_key_id and secret_access_key with env_or_profile credentials_source"
    end
  end


end