Class: Bosh::AwsCliPlugin::VPC

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh_cli_plugin_aws/vpc.rb

Constant Summary collapse

DEFAULT_CIDR =
"10.0.0.0/16"
DEFAULT_ROUTE =
"0.0.0.0/0"
NAT_INSTANCE_DEFAULTS =
{
    :image_id => "ami-f619c29f",
    :instance_type => "m1.medium"
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ec2, aws_vpc) ⇒ VPC

Returns a new instance of VPC.


12
13
14
15
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 12

def initialize(ec2, aws_vpc)
  @ec2 = ec2
  @aws_vpc = aws_vpc
end

Class Method Details

.create(ec2, cidr = DEFAULT_CIDR, instance_tenancy = nil) ⇒ Object


17
18
19
20
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 17

def self.create(ec2, cidr = DEFAULT_CIDR, instance_tenancy = nil)
  vpc_options = instance_tenancy ? {instance_tenancy: instance_tenancy} : {}
  self.new(ec2, ec2.vpcs.create(cidr, vpc_options))
end

.find(ec2, vpc_id) ⇒ Object


22
23
24
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 22

def self.find(ec2, vpc_id)
  self.new(ec2, ec2.vpcs[vpc_id])
end

Instance Method Details

#attach_internet_gateway(gateway_id) ⇒ Object


174
175
176
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 174

def attach_internet_gateway(gateway_id)
  @aws_vpc.internet_gateway = gateway_id
end

#cidr_blockObject


42
43
44
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 42

def cidr_block
  @aws_vpc.cidr_block
end

#create_dhcp_options(options) ⇒ Object


164
165
166
167
168
169
170
171
172
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 164

def create_dhcp_options(options)
  default_dhcp_opts = @aws_vpc.dhcp_options

  new_dhcp_options = @ec2.dhcp_options.create(options)
  new_dhcp_options.associate(vpc_id)
  #say "\tcreated and associated DHCP options #{new_dhcp_options.id}".make_green

  default_dhcp_opts.delete
end

#create_nat_instances(subnets) ⇒ Object


131
132
133
134
135
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 131

def create_nat_instances(subnets)
  extract_nat_instance_specs(subnets).each do |subnet_spec|
    @ec2.create_nat_instance(subnet_spec)
  end
end

#create_security_groups(groups_specs) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 76

def create_security_groups(groups_specs)
  groups_specs.each do |group_spec|
    if group_name_available group_spec["name"]
      security_group = @aws_vpc.security_groups.create(group_spec["name"])
      Bosh::AwsCloud::ResourceWait.for_sgroup(sgroup: security_group, state: true)

      group_spec["ingress"].each do |ingress|
        range_match = ingress["ports"].to_s.match(/(\d+)\s*-\s*(\d+)/)
        ports = range_match ? (range_match[1].to_i)..(range_match[2].to_i) : ingress["ports"].to_i

        # Wait for eventual consistancy
        ignorable_errors = [AWS::EC2::Errors::InvalidGroup::NotFound]

        Bosh::Common.retryable(tries: 30, on: ignorable_errors) do
          security_group.authorize_ingress(ingress["protocol"], ports, ingress["sources"])
          true
        end
      end
    end
  end
end

#create_subnets(subnets) ⇒ Object


106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 106

def create_subnets(subnets)
  subnets.each_pair do |name, subnet_spec|
    yield "Making subnet #{name} #{subnet_spec["cidr"]}:" if block_given?
    options = {}
    options[:availability_zone] = subnet_spec["availability_zone"] if subnet_spec["availability_zone"]

    subnet = @aws_vpc.subnets.create(subnet_spec["cidr"], options)
    Bosh::AwsCloud::ResourceWait.for_subnet(subnet: subnet, state: :available)

    subnet.add_tag("Name", :value => name)
  end
end

#delete_network_interfacesObject


160
161
162
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 160

def delete_network_interfaces
  @aws_vpc.network_interfaces.each(&:delete)
end

#delete_route_tablesObject


156
157
158
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 156

def delete_route_tables
  @aws_vpc.route_tables.reject(&:main?).each(&:delete)
end

#delete_security_groupsObject


98
99
100
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 98

def delete_security_groups
  @aws_vpc.security_groups.reject { |group| group.name == "default" }.each(&:delete)
end

#delete_subnetsObject


152
153
154
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 152

def delete_subnets
  @aws_vpc.subnets.each(&:delete)
end

#delete_vpcObject


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 62

def delete_vpc
  @aws_vpc.delete
  Bosh::Common.retryable(tries: 30, sleep: 5, on: []) do
    begin
      false if @aws_vpc.state
    rescue AWS::EC2::Errors::InvalidVpcID::NotFound
      true
    end
  end

rescue ::AWS::EC2::Errors::DependencyViolation
  err "#{@aws_vpc.id} has dependencies that this tool does not delete"
end

#dhcp_optionsObject


50
51
52
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 50

def dhcp_options
  @aws_vpc.dhcp_options
end

#extract_nat_instance_specs(specs) ⇒ Object


119
120
121
122
123
124
125
126
127
128
129
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 119

def extract_nat_instance_specs(specs)
  subnet_specs_with_nats = specs.select do |_, subnet_spec|
    subnet_spec.has_key?("nat_instance")
  end

  subnet_specs_with_nats.map do |subnet_name, subnet_spec|
    nat_instance_spec = subnet_spec["nat_instance"]
    nat_instance_spec["subnet_id"] = subnets[subnet_name]
    nat_instance_spec
  end
end

#instances_countObject


46
47
48
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 46

def instances_count
  @aws_vpc.instances.count
end

#make_internet_gateway_default_route_for_subnet(subnet) ⇒ Object


26
27
28
29
30
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 26

def make_internet_gateway_default_route_for_subnet(subnet)
  route_table = @aws_vpc.route_tables.create
  route_table.create_route(DEFAULT_ROUTE, internet_gateway: @aws_vpc.internet_gateway)
  subnet.route_table = route_table
end

#make_nat_instance_default_route_for_subnet(subnet, nat_instance) ⇒ Object


32
33
34
35
36
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 32

def make_nat_instance_default_route_for_subnet(subnet, nat_instance)
  route_table = @aws_vpc.route_tables.create
  route_table.create_route(DEFAULT_ROUTE, instance: nat_instance)
  subnet.route_table = route_table
end

#security_group_by_name(name) ⇒ Object


102
103
104
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 102

def security_group_by_name(name)
  @aws_vpc.security_groups.detect { |sg| sg.name == name }
end

#setup_subnet_routes(subnet_specs) ⇒ Object


137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 137

def setup_subnet_routes(subnet_specs)
  subnet_specs.each_pair do |name, subnet_spec|
    if subnet_spec["default_route"]
      subnet = @aws_vpc.subnets[subnets[name]]
      yield "  Making routing table for #{name}" if block_given?
      yield "  Binding default route to #{subnet_spec["default_route"]}" if block_given?
      if subnet_spec["default_route"] == "igw"
        make_internet_gateway_default_route_for_subnet(subnet)
      else
        make_nat_instance_default_route_for_subnet(subnet, @ec2.get_running_instance_by_name(subnet_spec["default_route"]))
      end
    end
  end
end

#stateObject


54
55
56
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 54

def state
  @aws_vpc.state
end

#subnetsObject


58
59
60
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 58

def subnets
  Hash[@aws_vpc.subnets.map { |subnet| [subnet.tags["Name"], subnet.id] }]
end

#vpc_idObject


38
39
40
# File 'lib/bosh_cli_plugin_aws/vpc.rb', line 38

def vpc_id
  @aws_vpc.id
end