Class: Bosh::AwsCloud::NetworkConfigurator

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/cloud/aws/network_configurator.rb

Overview

Represents AWS instance network config. EC2 instance has single NIC with dynamic IP address and (optionally) a single elastic IP address which instance itself is not aware of (vip). Thus we should perform a number of sanity checks for the network spec provided by director to make sure we don’t apply something EC2 doesn’t understand how to deal with.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#cloud_error, #default_ephemeral_disk_mapping, #ebs_ephemeral_disk_mapping, #extract_security_groups

Constructor Details

#initialize(spec) ⇒ NetworkConfigurator

Creates new network spec

Parameters:

  • spec (Hash)

    raw network spec passed by director



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
# File 'lib/cloud/aws/network_configurator.rb', line 21

def initialize(spec)
  unless spec.is_a?(Hash)
    raise ArgumentError, "Invalid spec, Hash expected, " \
                         "#{spec.class} provided"
  end

  @logger = Bosh::Clouds::Config.logger
  @network = nil
  @vip_network = nil

  spec.each_pair do |name, network_spec|
    network_type = network_spec["type"] || "manual"

    case network_type
      when "dynamic"
        cloud_error("Must have exactly one dynamic or manual network per instance") if @network
        @network = DynamicNetwork.new(name, network_spec)

      when "manual"
        cloud_error("Must have exactly one dynamic or manual network per instance") if @network
        @network = ManualNetwork.new(name, network_spec)

      when "vip"
        cloud_error("More than one vip network for '#{name}'") if @vip_network
        @vip_network = VipNetwork.new(name, network_spec)

      else
        cloud_error("Invalid network type '#{network_type}' for AWS, " \
                    "can only handle 'dynamic', 'vip', or 'manual' network types")
    end
  end

  unless @network
    cloud_error("Exactly one dynamic or manual network must be defined")
  end
end

Instance Attribute Details

#networkObject (readonly)

Returns the value of attribute network.



15
16
17
# File 'lib/cloud/aws/network_configurator.rb', line 15

def network
  @network
end

#vip_networkObject (readonly)

Returns the value of attribute vip_network.



15
16
17
# File 'lib/cloud/aws/network_configurator.rb', line 15

def vip_network
  @vip_network
end

Instance Method Details

#configure(ec2, instance) ⇒ Object

Applies network configuration to the vm

Parameters:

  • ec2 (AWS:EC2)

    instance EC2 client

  • instance (AWS::EC2::Instance)

    EC2 instance to configure



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cloud/aws/network_configurator.rb', line 73

def configure(ec2, instance)
  if @vip_network
    @vip_network.configure(ec2, instance)
  else
    # If there is no vip network we should disassociate any elastic IP
    # currently held by instance (as it might have had elastic IP before)
    elastic_ip = instance.elastic_ip

    if elastic_ip
      @logger.info("Disassociating elastic IP `#{elastic_ip}' " \
                   "from instance `#{instance.id}'")
      instance.disassociate_elastic_ip
    end
  end
end

#private_ipObject



62
63
64
# File 'lib/cloud/aws/network_configurator.rb', line 62

def private_ip
  vpc? ? @network.private_ip : nil
end

#subnetObject



58
59
60
# File 'lib/cloud/aws/network_configurator.rb', line 58

def subnet
  @network.subnet
end

#vpc?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/cloud/aws/network_configurator.rb', line 66

def vpc?
  @network.is_a? ManualNetwork
end