Migrate to stable/newton
[apex.git] / lib / python / apex / network_environment.py
1 ##############################################################################
2 # Copyright (c) 2016 Tim Rozet (trozet@redhat.com) and others.
3 #
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 ##############################################################################
9
10 import yaml
11 import re
12 from .common.constants import (
13     CONTROLLER,
14     COMPUTE,
15     ADMIN_NETWORK,
16     TENANT_NETWORK,
17     STORAGE_NETWORK,
18     EXTERNAL_NETWORK,
19     API_NETWORK,
20     CONTROLLER_PRE,
21     COMPUTE_PRE,
22     PRE_CONFIG_DIR
23 )
24
25 HEAT_NONE = 'OS::Heat::None'
26 PORTS = '/ports'
27 # Resources defined by <resource name>: <prefix>
28 EXTERNAL_RESOURCES = {'OS::TripleO::Network::External': None,
29                       'OS::TripleO::Network::Ports::ExternalVipPort': PORTS,
30                       'OS::TripleO::Controller::Ports::ExternalPort': PORTS,
31                       'OS::TripleO::Compute::Ports::ExternalPort': PORTS}
32 TENANT_RESOURCES = {'OS::TripleO::Network::Tenant': None,
33                     'OS::TripleO::Controller::Ports::TenantPort': PORTS,
34                     'OS::TripleO::Compute::Ports::TenantPort': PORTS}
35 STORAGE_RESOURCES = {'OS::TripleO::Network::Storage': None,
36                      'OS::TripleO::Network::Ports::StorageVipPort': PORTS,
37                      'OS::TripleO::Controller::Ports::StoragePort': PORTS,
38                      'OS::TripleO::Compute::Ports::StoragePort': PORTS}
39 API_RESOURCES = {'OS::TripleO::Network::InternalApi': None,
40                  'OS::TripleO::Network::Ports::InternalApiVipPort': PORTS,
41                  'OS::TripleO::Controller::Ports::InternalApiPort': PORTS,
42                  'OS::TripleO::Compute::Ports::InternalApiPort': PORTS}
43
44 # A list of flags that will be set to true when IPv6 is enabled
45 IPV6_FLAGS = ["NovaIPv6", "MongoDbIPv6", "CorosyncIPv6", "CephIPv6",
46               "RabbitIPv6", "MemcachedIPv6"]
47
48 reg = 'resource_registry'
49 param_def = 'parameter_defaults'
50
51
52 class NetworkEnvironment(dict):
53     """
54     This class creates a Network Environment to be used in TripleO Heat
55     Templates.
56
57     The class builds upon an existing network-environment file and modifies
58     based on a NetworkSettings object.
59     """
60     def __init__(self, net_settings, filename, compute_pre_config=False,
61                  controller_pre_config=False):
62         """
63         Create Network Environment according to Network Settings
64         """
65         init_dict = {}
66         if type(filename) is str:
67             with open(filename, 'r') as net_env_fh:
68                 init_dict = yaml.safe_load(net_env_fh)
69
70         super().__init__(init_dict)
71         try:
72             enabled_nets = net_settings.enabled_network_list
73         except:
74             raise NetworkEnvException('Invalid Network Setting object')
75
76         self._set_tht_dir()
77
78         nets = net_settings['networks']
79
80         admin_cidr = nets[ADMIN_NETWORK]['cidr']
81         admin_prefix = str(admin_cidr.prefixlen)
82         self[param_def]['ControlPlaneSubnetCidr'] = admin_prefix
83         self[param_def]['ControlPlaneDefaultRoute'] = \
84             nets[ADMIN_NETWORK]['installer_vm']['ip']
85         self[param_def]['EC2MetadataIp'] = \
86             nets[ADMIN_NETWORK]['installer_vm']['ip']
87         self[param_def]['DnsServers'] = net_settings['dns_servers']
88
89         if EXTERNAL_NETWORK in enabled_nets:
90             external_cidr = nets[EXTERNAL_NETWORK][0]['cidr']
91             self[param_def]['ExternalNetCidr'] = str(external_cidr)
92             if type(nets[EXTERNAL_NETWORK][0]['installer_vm']['vlan']) is int:
93                 self[param_def]['NeutronExternalNetworkBridge'] = '""'
94                 self[param_def]['ExternalNetworkVlanID'] = \
95                     nets[EXTERNAL_NETWORK][0]['installer_vm']['vlan']
96             external_range = nets[EXTERNAL_NETWORK][0]['usable_ip_range']
97             self[param_def]['ExternalAllocationPools'] = \
98                 [{'start': str(external_range[0]),
99                   'end': str(external_range[1])}]
100             self[param_def]['ExternalInterfaceDefaultRoute'] = \
101                 nets[EXTERNAL_NETWORK][0]['gateway']
102
103             if external_cidr.version == 6:
104                 postfix = '/external_v6.yaml'
105             else:
106                 postfix = '/external.yaml'
107         else:
108             postfix = '/noop.yaml'
109
110         # apply resource registry update for EXTERNAL_RESOURCES
111         self._config_resource_reg(EXTERNAL_RESOURCES, postfix)
112
113         if TENANT_NETWORK in enabled_nets:
114             tenant_range = nets[TENANT_NETWORK]['usable_ip_range']
115             self[param_def]['TenantAllocationPools'] = \
116                 [{'start': str(tenant_range[0]),
117                   'end': str(tenant_range[1])}]
118             tenant_cidr = nets[TENANT_NETWORK]['cidr']
119             self[param_def]['TenantNetCidr'] = str(tenant_cidr)
120             if tenant_cidr.version == 6:
121                 postfix = '/tenant_v6.yaml'
122             else:
123                 postfix = '/tenant.yaml'
124
125             tenant_vlan = self._get_vlan(nets[TENANT_NETWORK])
126             if type(tenant_vlan) is int:
127                 self[param_def]['TenantNetworkVlanID'] = tenant_vlan
128         else:
129             postfix = '/noop.yaml'
130
131         # apply resource registry update for TENANT_RESOURCES
132         self._config_resource_reg(TENANT_RESOURCES, postfix)
133
134         if STORAGE_NETWORK in enabled_nets:
135             storage_range = nets[STORAGE_NETWORK]['usable_ip_range']
136             self[param_def]['StorageAllocationPools'] = \
137                 [{'start': str(storage_range[0]),
138                   'end': str(storage_range[1])}]
139             storage_cidr = nets[STORAGE_NETWORK]['cidr']
140             self[param_def]['StorageNetCidr'] = str(storage_cidr)
141             if storage_cidr.version == 6:
142                 postfix = '/storage_v6.yaml'
143             else:
144                 postfix = '/storage.yaml'
145             storage_vlan = self._get_vlan(nets[STORAGE_NETWORK])
146             if type(storage_vlan) is int:
147                 self[param_def]['StorageNetworkVlanID'] = storage_vlan
148         else:
149             postfix = '/noop.yaml'
150
151         # apply resource registry update for STORAGE_RESOURCES
152         self._config_resource_reg(STORAGE_RESOURCES, postfix)
153
154         if API_NETWORK in enabled_nets:
155             api_range = nets[API_NETWORK]['usable_ip_range']
156             self[param_def]['InternalApiAllocationPools'] = \
157                 [{'start': str(api_range[0]),
158                   'end': str(api_range[1])}]
159             api_cidr = nets[API_NETWORK]['cidr']
160             self[param_def]['InternalApiNetCidr'] = str(api_cidr)
161             if api_cidr.version == 6:
162                 postfix = '/internal_api_v6.yaml'
163             else:
164                 postfix = '/internal_api.yaml'
165             api_vlan = self._get_vlan(nets[API_NETWORK])
166             if type(api_vlan) is int:
167                 self[param_def]['InternalApiNetworkVlanID'] = api_vlan
168         else:
169             postfix = '/noop.yaml'
170
171         # apply resource registry update for API_RESOURCES
172         self._config_resource_reg(API_RESOURCES, postfix)
173
174         if compute_pre_config:
175             self[reg][COMPUTE_PRE] = PRE_CONFIG_DIR + "compute/numa.yaml"
176         if controller_pre_config:
177             self[reg][CONTROLLER_PRE] = PRE_CONFIG_DIR + "controller/numa.yaml"
178
179         # Set IPv6 related flags to True. Not that we do not set those to False
180         # when IPv4 is configured, we'll use the default or whatever the user
181         # may have set.
182         if net_settings.get_ip_addr_family() == 6:
183             for flag in IPV6_FLAGS:
184                 self[param_def][flag] = True
185
186     def _get_vlan(self, network):
187         if type(network['nic_mapping'][CONTROLLER]['vlan']) is int:
188             return network['nic_mapping'][CONTROLLER]['vlan']
189         elif type(network['nic_mapping'][COMPUTE]['vlan']) is int:
190             return network['nic_mapping'][COMPUTE]['vlan']
191         else:
192             return 'native'
193
194     def _set_tht_dir(self):
195         self.tht_dir = None
196         for key, prefix in TENANT_RESOURCES.items():
197             if prefix is None:
198                 prefix = ''
199             m = re.split('%s/\w+\.yaml' % prefix, self[reg][key])
200             if m is not None and len(m) > 1:
201                 self.tht_dir = m[0]
202                 break
203         if not self.tht_dir:
204             raise NetworkEnvException('Unable to parse THT Directory')
205
206     def _config_resource_reg(self, resources, postfix):
207         for key, prefix in resources.items():
208             if prefix is None:
209                 if postfix == '/noop.yaml':
210                     self[reg][key] = HEAT_NONE
211                     continue
212                 prefix = ''
213             self[reg][key] = self.tht_dir + prefix + postfix
214
215
216 class NetworkEnvException(Exception):
217     def __init__(self, value):
218         self.value = value
219
220     def __str__(self):
221             return self.value