1 ##############################################################################
2 # Copyright (c) 2016 Tim Rozet (trozet@redhat.com) and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
12 from .common.constants import (
24 # Resources defined by <resource name>: <prefix>
25 EXTERNAL_RESOURCES = {'OS::TripleO::Network::External': None,
26 'OS::TripleO::Network::Ports::ExternalVipPort': PORTS,
27 'OS::TripleO::Controller::Ports::ExternalPort': PORTS,
28 'OS::TripleO::Compute::Ports::ExternalPort': PORTS}
29 TENANT_RESOURCES = {'OS::TripleO::Network::Tenant': None,
30 'OS::TripleO::Controller::Ports::TenantPort': PORTS,
31 'OS::TripleO::Compute::Ports::TenantPort': PORTS}
32 STORAGE_RESOURCES = {'OS::TripleO::Network::Storage': None,
33 'OS::TripleO::Network::Ports::StorageVipPort': PORTS,
34 'OS::TripleO::Controller::Ports::StoragePort': PORTS,
35 'OS::TripleO::Compute::Ports::StoragePort': PORTS}
36 API_RESOURCES = {'OS::TripleO::Network::InternalApi': None,
37 'OS::TripleO::Network::Ports::InternalApiVipPort': PORTS,
38 'OS::TripleO::Controller::Ports::InternalApiPort': PORTS,
39 'OS::TripleO::Compute::Ports::InternalApiPort': PORTS}
41 # A list of flags that will be set to true when IPv6 is enabled
42 IPV6_FLAGS = ["NovaIPv6", "MongoDbIPv6", "CorosyncIPv6", "CephIPv6",
43 "RabbitIPv6", "MemcachedIPv6"]
46 class NetworkEnvironment(dict):
48 This class creates a Network Environment to be used in TripleO Heat
51 The class builds upon an existing network-environment file and modifies
52 based on a NetworkSettings object.
54 def __init__(self, net_settings, filename, compute_pre_config=False,
55 controller_pre_config=False):
57 if type(filename) is str:
58 with open(filename, 'r') as net_env_fh:
59 init_dict = yaml.load(net_env_fh)
61 super().__init__(init_dict)
63 enabled_networks = net_settings.enabled_network_list
65 raise NetworkEnvException('Invalid Network Setting object')
66 param_def = self['parameter_defaults']
67 reg = self['resource_registry']
70 raise NetworkEnvException("Network Settings does not exist")
72 enabled_networks = net_settings.get_enabled_networks()
73 param_def = 'parameter_defaults'
74 reg = 'resource_registry'
75 for key, prefix in TENANT_RESOURCES.items():
78 m = re.split('%s/\w+\.yaml' % prefix, self[reg][key])
83 raise NetworkEnvException('Unable to parse THT Directory')
85 admin_cidr = net_settings[ADMIN_NETWORK]['cidr']
86 admin_prefix = str(admin_cidr.prefixlen)
87 self[param_def]['ControlPlaneSubnetCidr'] = admin_prefix
88 self[param_def]['ControlPlaneDefaultRoute'] = \
89 net_settings[ADMIN_NETWORK]['provisioner_ip']
90 public_cidr = net_settings[PUBLIC_NETWORK]['cidr']
91 self[param_def]['ExternalNetCidr'] = str(public_cidr)
92 if net_settings[PUBLIC_NETWORK]['vlan'] != 'native':
93 self[param_def]['NeutronExternalNetworkBridge'] = '""'
94 self[param_def]['ExternalNetworkVlanID'] = \
95 net_settings[PUBLIC_NETWORK]['vlan']
97 net_settings[PUBLIC_NETWORK]['usable_ip_range'].split(',')
98 self[param_def]['ExternalAllocationPools'] = \
101 'end': public_range[1]
103 self[param_def]['ExternalInterfaceDefaultRoute'] = \
104 net_settings[PUBLIC_NETWORK]['gateway']
105 self[param_def]['EC2MetadataIp'] = \
106 net_settings[ADMIN_NETWORK]['provisioner_ip']
107 self[param_def]['DnsServers'] = net_settings['dns_servers']
109 if public_cidr.version == 6:
110 postfix = '/external_v6.yaml'
112 postfix = '/external.yaml'
114 for key, prefix in EXTERNAL_RESOURCES.items():
117 self[reg][key] = tht_dir + prefix + postfix
119 if PRIVATE_NETWORK in enabled_networks:
120 priv_range = net_settings[PRIVATE_NETWORK][
121 'usable_ip_range'].split(',')
122 self[param_def]['TenantAllocationPools'] = \
127 priv_cidr = net_settings[PRIVATE_NETWORK]['cidr']
128 self[param_def]['TenantNetCidr'] = str(priv_cidr)
129 if priv_cidr.version == 6:
130 postfix = '/tenant_v6.yaml'
132 postfix = '/tenant.yaml'
133 if net_settings[PRIVATE_NETWORK]['vlan'] != 'native':
134 self[param_def]['TenantNetworkVlanID'] = \
135 net_settings[PRIVATE_NETWORK]['vlan']
137 postfix = '/noop.yaml'
139 for key, prefix in TENANT_RESOURCES.items():
142 self[reg][key] = tht_dir + prefix + postfix
144 if STORAGE_NETWORK in enabled_networks:
145 storage_range = net_settings[STORAGE_NETWORK][
146 'usable_ip_range'].split(',')
147 self[param_def]['StorageAllocationPools'] = \
153 storage_cidr = net_settings[STORAGE_NETWORK]['cidr']
154 self[param_def]['StorageNetCidr'] = str(storage_cidr)
155 if storage_cidr.version == 6:
156 postfix = '/storage_v6.yaml'
158 postfix = '/storage.yaml'
159 if net_settings[STORAGE_NETWORK]['vlan'] != 'native':
160 self[param_def]['StorageNetworkVlanID'] = \
161 net_settings[STORAGE_NETWORK]['vlan']
163 postfix = '/noop.yaml'
165 for key, prefix in STORAGE_RESOURCES.items():
168 self[reg][key] = tht_dir + prefix + postfix
170 if API_NETWORK in enabled_networks:
171 api_range = net_settings[API_NETWORK][
172 'usable_ip_range'].split(',')
173 self[param_def]['InternalApiAllocationPools'] = \
174 [{'start': api_range[0],
177 api_cidr = net_settings[API_NETWORK]['cidr']
178 self[param_def]['InternalApiNetCidr'] = str(api_cidr)
179 if api_cidr.version == 6:
180 postfix = '/internal_api_v6.yaml'
182 postfix = '/internal_api.yaml'
183 if net_settings[API_NETWORK]['vlan'] != 'native':
184 self[param_def]['InternalApiNetworkVlanID'] = \
185 net_settings[API_NETWORK]['vlan']
187 postfix = '/noop.yaml'
189 for key, prefix in API_RESOURCES.items():
192 self[reg][key] = tht_dir + prefix + postfix
194 if compute_pre_config:
195 self[reg][COMPUTE_PRE] = PRE_CONFIG_DIR + "compute/numa.yaml"
196 if controller_pre_config:
197 self[reg][CONTROLLER_PRE] = PRE_CONFIG_DIR + "controller/numa.yaml"
199 # Set IPv6 related flags to True. Not that we do not set those to False
200 # when IPv4 is configured, we'll use the default or whatever the user
202 if net_settings.get_ip_addr_family() == 6:
203 for flag in IPV6_FLAGS:
204 self[param_def][flag] = True
207 class NetworkEnvException(Exception):
208 def __init__(self, value):