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
# 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
  @networks = []
  @vip_network = nil
  @security_groups = []
  @net_ids = []
  @dynamic_network = nil

  spec.each_pair do |name, network_spec|
    initialize_network(name, network_spec)
  end

  cloud_error("At least one dynamic or manual network should be defined") if @networks.empty?
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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cloud/openstack/network_configurator.rb', line 82

def configure(openstack, server)
  @networks.each do |network_info|
    network = network_info["network"]
    network.configure(openstack, server)
  end

  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

#initialize_network(name, network_spec) ⇒ Object

Setup network configuration for one network spec.

Parameters:

  • network (String)

    spec name

  • network (Hash)

    spec configure



44
45
46
47
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/cloud/openstack/network_configurator.rb', line 44

def initialize_network(name, network_spec)
  network_type = network_spec["type"] || "manual"

  case network_type
    when "dynamic"
      cloud_error("Only one dynamic network per instance should be defined") if @dynamic_network
      net_id = extract_net_id(network_spec)
      cloud_error("Dynamic network with id #{net_id} is already defined") if @net_ids.include?(net_id)
      network = DynamicNetwork.new(name, network_spec)
      @security_groups += extract_security_groups(network_spec)
      @networks << {"network" => network, "net_id" => net_id}
      @net_ids << net_id
      @dynamic_network = network
    when "manual"
      net_id = extract_net_id(network_spec)
      cloud_error("Manual network must have net_id") if net_id.nil?
      cloud_error("Manual network with id #{net_id} is already defined") if @net_ids.include?(net_id)
      network = ManualNetwork.new(name, network_spec)
      @security_groups += extract_security_groups(network_spec)
      @networks << {"network" => network, "net_id" => net_id}
      @net_ids << net_id
    when "vip"
      cloud_error("Only one VIP network per instance should be defined") 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

#nicsArray

Returns the nics for this network configuration

Returns:

  • (Array)

    nics



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cloud/openstack/network_configurator.rb', line 137

def nics
  @networks.inject([]) do |memo, network_info|
    net_id = network_info["net_id"]
    network = network_info["network"]
    nic = {}
    nic["net_id"] = net_id if net_id
    nic["v4_fixed_ip"] = network.private_ip if network.is_a?(ManualNetwork)
    memo << nic if nic.any?
    memo
  end
end

#private_ipsArray<String>

Returns the private IP addresses for this network configuration

Returns:

  • (Array<String>)

    private ip addresses



125
126
127
128
129
130
131
# File 'lib/cloud/openstack/network_configurator.rb', line 125

def private_ips
  @networks.inject([]) do |memo, network_info|
    network = network_info["network"]
    memo << network.private_ip if network.is_a?(ManualNetwork)
    memo
  end
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



113
114
115
116
117
118
119
# File 'lib/cloud/openstack/network_configurator.rb', line 113

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