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 ADMIN_NETWORK
13 from .common.constants import PRIVATE_NETWORK
14 from .common.constants import STORAGE_NETWORK
15 from .common.constants import PUBLIC_NETWORK
16 from .common.constants import API_NETWORK
19 # Resources defined by <resource name>: <prefix>
20 EXTERNAL_RESOURCES = {'OS::TripleO::Network::External': None,
21 'OS::TripleO::Network::Ports::ExternalVipPort': PORTS,
22 'OS::TripleO::Controller::Ports::ExternalPort': PORTS,
23 'OS::TripleO::Compute::Ports::ExternalPort': PORTS}
24 TENANT_RESOURCES = {'OS::TripleO::Network::Tenant': None,
25 'OS::TripleO::Controller::Ports::TenantPort': PORTS,
26 'OS::TripleO::Compute::Ports::TenantPort': PORTS}
27 STORAGE_RESOURCES = {'OS::TripleO::Network::Storage': None,
28 'OS::TripleO::Network::Ports::StorageVipPort': PORTS,
29 'OS::TripleO::Controller::Ports::StoragePort': PORTS,
30 'OS::TripleO::Compute::Ports::StoragePort': PORTS}
31 API_RESOURCES = {'OS::TripleO::Network::InternalApi': None,
32 'OS::TripleO::Network::Ports::InternalApiVipPort': PORTS,
33 'OS::TripleO::Controller::Ports::InternalApiPort': PORTS,
34 'OS::TripleO::Compute::Ports::InternalApiPort': PORTS}
36 # A list of flags that will be set to true when IPv6 is enabled
37 IPV6_FLAGS = ["NovaIPv6", "MongoDbIPv6", "CorosyncIPv6", "CephIPv6",
38 "RabbitIPv6", "MemcachedIPv6"]
41 class NetworkEnvironment:
43 This class creates a Network Environment to be used in TripleO Heat
46 The class builds upon an existing network-environment file and modifies
47 based on a NetworkSettings object.
49 def __init__(self, net_settings, filename):
50 with open(filename, 'r') as net_env_fh:
51 self.netenv_obj = yaml.load(net_env_fh)
52 self._update_net_environment(net_settings)
54 def _update_net_environment(self, settings_obj):
56 Updates Network Environment according to Network Settings
57 :param: network settings object
61 raise NetworkEnvException("Network Settings does not exist")
63 net_settings = settings_obj.get_network_settings()
64 enabled_networks = settings_obj.get_enabled_networks()
65 param_def = 'parameter_defaults'
66 reg = 'resource_registry'
67 for key, prefix in TENANT_RESOURCES.items():
70 m = re.split('%s/\w+\.yaml' % prefix, self.netenv_obj[reg][key])
75 raise NetworkEnvException('Unable to parse THT Directory')
77 admin_cidr = net_settings[ADMIN_NETWORK]['cidr']
78 admin_prefix = str(admin_cidr.prefixlen)
79 self.netenv_obj[param_def]['ControlPlaneSubnetCidr'] = admin_prefix
80 self.netenv_obj[param_def]['ControlPlaneDefaultRoute'] = \
81 net_settings[ADMIN_NETWORK]['provisioner_ip']
82 public_cidr = net_settings[PUBLIC_NETWORK]['cidr']
83 self.netenv_obj[param_def]['ExternalNetCidr'] = str(public_cidr)
84 if net_settings[PUBLIC_NETWORK]['vlan'] != 'native':
85 self.netenv_obj[param_def]['ExternalNetworkVlanID'] = \
86 net_settings[PUBLIC_NETWORK]['vlan']
88 net_settings[PUBLIC_NETWORK]['usable_ip_range'].split(',')
89 self.netenv_obj[param_def]['ExternalAllocationPools'] = \
92 'end': public_range[1]
94 self.netenv_obj[param_def]['ExternalInterfaceDefaultRoute'] = \
95 net_settings[PUBLIC_NETWORK]['gateway']
96 self.netenv_obj[param_def]['EC2MetadataIp'] = \
97 net_settings[ADMIN_NETWORK]['provisioner_ip']
98 self.netenv_obj[param_def]['DnsServers'] = net_settings['dns_servers']
100 if public_cidr.version == 6:
101 postfix = '/external_v6.yaml'
103 postfix = '/external.yaml'
105 for key, prefix in EXTERNAL_RESOURCES.items():
108 self.netenv_obj[reg][key] = tht_dir + prefix + postfix
110 if PRIVATE_NETWORK in enabled_networks:
111 priv_range = net_settings[PRIVATE_NETWORK][
112 'usable_ip_range'].split(',')
113 self.netenv_obj[param_def]['TenantAllocationPools'] = \
118 priv_cidr = net_settings[PRIVATE_NETWORK]['cidr']
119 self.netenv_obj[param_def]['TenantNetCidr'] = str(priv_cidr)
120 if priv_cidr.version == 6:
121 postfix = '/tenant_v6.yaml'
123 postfix = '/tenant.yaml'
124 if net_settings[PRIVATE_NETWORK]['vlan'] != 'native':
125 self.netenv_obj[param_def]['TenantNetworkVlanID'] = \
126 net_settings[PRIVATE_NETWORK]['vlan']
128 postfix = '/noop.yaml'
130 for key, prefix in TENANT_RESOURCES.items():
133 self.netenv_obj[reg][key] = tht_dir + prefix + postfix
135 if STORAGE_NETWORK in enabled_networks:
136 storage_range = net_settings[STORAGE_NETWORK][
137 'usable_ip_range'].split(',')
138 self.netenv_obj[param_def]['StorageAllocationPools'] = \
144 storage_cidr = net_settings[STORAGE_NETWORK]['cidr']
145 self.netenv_obj[param_def]['StorageNetCidr'] = str(storage_cidr)
146 if storage_cidr.version == 6:
147 postfix = '/storage_v6.yaml'
149 postfix = '/storage.yaml'
150 if net_settings[STORAGE_NETWORK]['vlan'] != 'native':
151 self.netenv_obj[param_def]['StorageNetworkVlanID'] = \
152 net_settings[STORAGE_NETWORK]['vlan']
154 postfix = '/noop.yaml'
156 for key, prefix in STORAGE_RESOURCES.items():
159 self.netenv_obj[reg][key] = tht_dir + prefix + postfix
161 if API_NETWORK in enabled_networks:
162 api_range = net_settings[API_NETWORK][
163 'usable_ip_range'].split(',')
164 self.netenv_obj[param_def]['InternalApiAllocationPools'] = \
165 [{'start': api_range[0],
168 api_cidr = net_settings[API_NETWORK]['cidr']
169 self.netenv_obj[param_def]['InternalApiNetCidr'] = str(api_cidr)
170 if api_cidr.version == 6:
171 postfix = '/internal_api_v6.yaml'
173 postfix = '/internal_api.yaml'
174 if net_settings[API_NETWORK]['vlan'] != 'native':
175 self.netenv_obj[param_def]['InternalApiNetworkVlanID'] = \
176 net_settings[API_NETWORK]['vlan']
178 postfix = '/noop.yaml'
180 for key, prefix in API_RESOURCES.items():
183 self.netenv_obj[reg][key] = tht_dir + prefix + postfix
185 # Set IPv6 related flags to True. Not that we do not set those to False
186 # when IPv4 is configured, we'll use the default or whatever the user
188 if settings_obj.get_ip_addr_family() == 6:
189 for flag in IPV6_FLAGS:
190 self.netenv_obj[param_def][flag] = True
192 def get_netenv_settings(self):
194 Getter for netenv settings
195 :return: Dictionary of network environment settings
197 return self.netenv_obj
200 class NetworkEnvException(Exception):
201 def __init__(self, value):