Class: Bosh::OpenStackCloud::NetworkConfigurator

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

Overview

Represents OpenStack server network config. OpenStack server has single NIC with a dynamic or manual IP’s address and (optionally) a single floating IP address which server 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 OpenStack doesn’t understand how to deal with.

Constant Summary

Constants included from Helpers

Helpers::DEFAULT_RETRY_TIMEOUT, Helpers::DEFAULT_STATE_TIMEOUT, Helpers::MAX_RETRIES

Instance Method Summary collapse

Methods included from Helpers

#cloud_error, #parse_openstack_response, #task_checkpoint, #wait_resource, #with_openstack

Constructor Details

#initialize(spec) ⇒ NetworkConfigurator

Creates new network spec

Parameters:

  • spec (Hash)

    Raw network spec passed by director



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

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
  @security_groups = []
  @net_id = 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)
        @security_groups += extract_security_groups(network_spec)
        @net_id = extract_net_id(network_spec)

      when "manual"
        cloud_error("Must have exactly one dynamic or manual network per instance") if @network
        @network = ManualNetwork.new(name, network_spec)
        @security_groups += extract_security_groups(network_spec)
        @net_id = extract_net_id(network_spec)
        cloud_error("Manual network must have net_id") if @net_id.nil?

      when "vip"
        cloud_error("More than one vip network") if @vip_network
        @vip_network = VipNetwork.new(name, network_spec)
        @security_groups += extract_security_groups(network_spec)

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

  cloud_error("At least one dynamic or manual network should be defined") if @network.nil?
end

Instance Method Details

#configure(openstack, server) ⇒ Object

Applies network configuration to the vm

Parameters:

  • openstack (Fog::Compute::OpenStack)

    Fog OpenStack Compute client

  • server (Fog::Compute::OpenStack::Server)

    OpenStack server to configure



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cloud/openstack/network_configurator.rb', line 68

def configure(openstack, server)
  @network.configure(openstack, server)

  if @vip_network
    @vip_network.configure(openstack, server)
  else
    # If there is no vip network we should disassociate any floating IP
    # currently held by server (as it might have had floating IP before)
    with_openstack do
      addresses = openstack.addresses
      addresses.each do |address|
        if address.instance_id == server.id
          @logger.info("Disassociating floating IP `#{address.ip}' " \
                       "from server `#{server.id}'")
          address.server = nil
        end
      end
    end
  end
end

#nicsArray

Returns the nics for this network configuration

Returns:

  • (Array)

    nics



116
117
118
119
120
121
# File 'lib/cloud/openstack/network_configurator.rb', line 116

def nics
  nic = {}
  nic["net_id"] = @net_id if @net_id
  nic["v4_fixed_ip"] = @network.private_ip if @network.is_a? ManualNetwork
  nic.any? ? [nic] : []
end

#private_ipString

Returns the private IP address for this network configuration

Returns:

  • (String)

    private ip address



108
109
110
# File 'lib/cloud/openstack/network_configurator.rb', line 108

def private_ip
  @network.is_a?(ManualNetwork) ? @network.private_ip : nil   
end

#security_groups(default) ⇒ Array

Returns the security groups for this network configuration, or the default security groups if the configuration does not contain security groups

Parameters:

  • default (Array)

    Default security groups

Returns:

  • (Array)

    security groups



96
97
98
99
100
101
102
# File 'lib/cloud/openstack/network_configurator.rb', line 96

def security_groups(default)
  if @security_groups.empty? && default
    default
  else
    @security_groups.sort
  end
end