Class: Bosh::Providers::Clients::AwsProviderClient

Inherits:
FogProviderClient show all
Includes:
Bosh::Providers::Constants::AwsConstants
Defined in:
lib/bosh/providers/clients/aws_provider_client.rb

Instance Attribute Summary

Attributes inherited from FogProviderClient

#attributes, #fog_compute

Instance Method Summary collapse

Methods included from Bosh::Providers::Constants::AwsConstants

#default_region_code, #region_labels

Methods inherited from FogProviderClient

#authorize_port_range, #cleanup_unused_ip_addresses, #create_key_pair, #create_security_group, #delete_key_pair_if_exists, #delete_servers_with_name, #delete_volumes_with_name, #extract_port_definition, #find_unused_public_ip_address, #initialize, #ip_permissions, #port_open?, #provision_or_reuse_public_ip_address, #set_resource_name

Constructor Details

This class inherits a constructor from Bosh::Providers::Clients::FogProviderClient

Instance Method Details

#associate_ip_address_with_server(ip_address, server) ⇒ Object



54
55
56
57
# File 'lib/bosh/providers/clients/aws_provider_client.rb', line 54

def associate_ip_address_with_server(ip_address, server)
  address = fog_compute.addresses.get(ip_address)
  address.server = server
end

#aws_compute_flavor_idsObject



34
35
36
# File 'lib/bosh/providers/clients/aws_provider_client.rb', line 34

def aws_compute_flavor_ids
  aws_compute_flavors.map { |fl| fl[:id] }
end

#aws_compute_flavorsArray

Example [Hash] { :bits => 0, :cores => 2, :disk => 0,

:id => 't1.micro', :name => 'Micro Instance', :ram => 613}

Returns:

  • (Array)

    of [Hash] for each supported compute flavor



30
31
32
# File 'lib/bosh/providers/clients/aws_provider_client.rb', line 30

def aws_compute_flavors
  Fog::Compute::AWS::FLAVORS
end

#bootstrap(new_attributes = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/bosh/providers/clients/aws_provider_client.rb', line 123

def bootstrap(new_attributes = {})
  new_attributes[:image_id] ||= raring_image_id(fog_compute.region)
  vpc = new_attributes[:subnet_id]

  server = fog_compute.servers.new(new_attributes)

  unless new_attributes[:key_name]
    raise "please provide :key_name attribute"
  end
  unless private_key_path = new_attributes.delete(:private_key_path)
    raise "please provide :private_key_path attribute"
  end

  if vpc
    # TODO setup security group on new server
  else
    # make sure port 22 is open in the first security group
    security_group = fog_compute.security_groups.get(server.groups.first)
    authorized = security_group.ip_permissions.detect do |ip_permission|
      ip_permission['ipRanges'].first && ip_permission['ipRanges'].first['cidrIp'] == '0.0.0.0/0' &&
      ip_permission['fromPort'] == 22 &&
      ip_permission['ipProtocol'] == 'tcp' &&
      ip_permission['toPort'] == 22
    end
    unless authorized
      security_group.authorize_port_range(22..22)
    end
  end

  server.save
  unless Fog.mocking?
    server.wait_for { ready? }
    server.setup(:keys => [private_key_path])
  end
  server
end

#create_and_attach_volume(name, disk_size, server, device) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bosh/providers/clients/aws_provider_client.rb', line 80

def create_and_attach_volume(name, disk_size, server, device)
  volume = fog_compute.volumes.create(
      size: disk_size,
      name: name,
      description: '',
      device: device,
      availability_zone: server.availability_zone)
  # TODO: the following works in fog 1.9.0+ (but which has a bug in bootstrap)
  # https://github.com/fog/fog/issues/1516
  #
  # volume.wait_for { volume.status == 'available' }
  # volume.attach(server.id, "/dev/vdc")
  # volume.wait_for { volume.status == 'in-use' }
  #
  # Instead, using:
  volume.server = server
end

#create_internet_gateway(vpc_id) ⇒ Object



71
72
73
74
# File 'lib/bosh/providers/clients/aws_provider_client.rb', line 71

def create_internet_gateway(vpc_id)
  gateway = fog_compute.internet_gateways.create(vpc_id: vpc_id)
  gateway.id
end

#create_subnet(vpc_id, cidr_block) ⇒ String

Creates a VPC subnet

Returns:

  • (String)

    the subnet_id



66
67
68
69
# File 'lib/bosh/providers/clients/aws_provider_client.rb', line 66

def create_subnet(vpc_id, cidr_block)
  subnet = fog_compute.subnets.create(vpc_id: vpc_id, cidr_block: cidr_block)
  subnet.subnet_id
end

#create_vpc(name, cidr_block) ⇒ Object



59
60
61
62
# File 'lib/bosh/providers/clients/aws_provider_client.rb', line 59

def create_vpc(name, cidr_block)
  vpc = fog_compute.vpcs.create(name: name, cidr_block: cidr_block)
  vpc.id
end

#find_server_device(server, device) ⇒ Object



76
77
78
# File 'lib/bosh/providers/clients/aws_provider_client.rb', line 76

def find_server_device(server, device)
  server.volumes.all.find {|v| v.device == device}
end

#fog_compute_flavor(server_flavor_id) ⇒ Hash

or nil if server_flavor_id is not a supported flavor ID

Returns:

  • (Hash)

    e.g. { :bits => 0, :cores => 2, :disk => 0, :id => ‘t1.micro’, :name => ‘Micro Instance’, :ram => 613}



23
24
25
# File 'lib/bosh/providers/clients/aws_provider_client.rb', line 23

def fog_compute_flavor(server_flavor_id)
  aws_compute_flavors.find { |fl| fl[:id] == server_flavor_id }
end

#provision_public_ip_address(options = {}) ⇒ String

Provision an EC2 or VPC elastic IP addess.

  • VPC - provision_public_ip_address(vpc: true)

  • EC2 - provision_public_ip_address

TODO nil if none available

Returns:

  • (String)

    provisions a new public IP address in target region



43
44
45
46
47
48
49
50
51
52
# File 'lib/bosh/providers/clients/aws_provider_client.rb', line 43

def provision_public_ip_address(options={})
  if options.delete(:vpc)
    options[:domain] = "vpc"
  else
    options[:domain] = options.delete(:domain) || "standard"
  end
  address = fog_compute.addresses.create(options)
  address.public_ip
  # TODO catch error and return nil
end

#ram_for_server_flavor(server_flavor_id) ⇒ Integer

Returns megabytes of RAM for requested flavor of server.

Returns:

  • (Integer)

    megabytes of RAM for requested flavor of server



12
13
14
15
16
17
18
# File 'lib/bosh/providers/clients/aws_provider_client.rb', line 12

def ram_for_server_flavor(server_flavor_id)
  if flavor = fog_compute_flavor(server_flavor_id)
    flavor[:ram]
  else
    raise "Unknown AWS flavor '#{server_flavor_id}'"
  end
end

#raring_image_id(region = nil) ⇒ Object

Ubuntu 13.04



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bosh/providers/clients/aws_provider_client.rb', line 99

def raring_image_id(region=nil)
  region = fog_compute.region
  # http://cloud-images.ubuntu.com/locator/ec2/
  image_id = case region.to_s
  when "ap-northeast-1"
    "ami-6b26ab6a"
  when "ap-southeast-1"
    "ami-2b511e79"
  when "eu-west-1"
    "ami-3d160149"
  when "sa-east-1"
    "ami-28e43e35"
  when "us-east-1"
    "ami-c30360aa"
  when "us-west-1"
    "ami-d383af96"
  when "ap-southeast-2"
    "ami-84a333be"
  when "us-west-2"
    "ami-bf1d8a8f"
  end
  image_id || raise("Please add Ubuntu 13.04 64bit (EBS) AMI image id to aws.rb#raring_image_id method for region '#{region}'")
end

#setup_fog_connectionObject

Construct a Fog::Compute object Uses attributes which normally originates from settings.provider



162
163
164
165
166
167
# File 'lib/bosh/providers/clients/aws_provider_client.rb', line 162

def setup_fog_connection
  configuration = Fog.symbolize_credentials(attributes.credentials)
  configuration[:provider] = "AWS"
  configuration[:region] = attributes.region
  @fog_compute = Fog::Compute.new(configuration)
end