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