22dcc35280ec40fbff38f300dc484cf4477f784e
[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     ADMIN_NETWORK,
14     PRIVATE_NETWORK,
15     STORAGE_NETWORK,
16     PUBLIC_NETWORK,
17     API_NETWORK,
18     CONTROLLER_PRE,
19     COMPUTE_PRE,
20     PRE_CONFIG_DIR
21 )
22
23 PORTS = '/ports'
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}
40
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"]
44
45
46 class NetworkEnvironment:
47     """
48     This class creates a Network Environment to be used in TripleO Heat
49     Templates.
50
51     The class builds upon an existing network-environment file and modifies
52     based on a NetworkSettings object.
53     """
54     def __init__(self, net_settings, filename, compute_pre_config=False,
55                  controller_pre_config=False):
56         with open(filename, 'r') as net_env_fh:
57             self.compute_pre = compute_pre_config
58             self.controller_pre = controller_pre_config
59             self.netenv_obj = yaml.load(net_env_fh)
60             self._update_net_environment(net_settings)
61
62     def _update_net_environment(self, net_settings):
63         """
64         Updates Network Environment according to Network Settings
65         :param: network settings object
66         :return:  None
67         """
68         if not net_settings:
69             raise NetworkEnvException("Network Settings does not exist")
70
71         enabled_networks = net_settings.get_enabled_networks()
72         param_def = 'parameter_defaults'
73         reg = 'resource_registry'
74         for key, prefix in TENANT_RESOURCES.items():
75             if prefix is None:
76                 prefix = ''
77             m = re.split('%s/\w+\.yaml' % prefix, self.netenv_obj[reg][key])
78             if m is not None:
79                 tht_dir = m[0]
80                 break
81         if not tht_dir:
82             raise NetworkEnvException('Unable to parse THT Directory')
83
84         admin_cidr = net_settings[ADMIN_NETWORK]['cidr']
85         admin_prefix = str(admin_cidr.prefixlen)
86         self.netenv_obj[param_def]['ControlPlaneSubnetCidr'] = admin_prefix
87         self.netenv_obj[param_def]['ControlPlaneDefaultRoute'] = \
88             net_settings[ADMIN_NETWORK]['provisioner_ip']
89         public_cidr = net_settings[PUBLIC_NETWORK]['cidr']
90         self.netenv_obj[param_def]['ExternalNetCidr'] = str(public_cidr)
91         if net_settings[PUBLIC_NETWORK]['vlan'] != 'native':
92             self.netenv_obj[param_def]['NeutronExternalNetworkBridge'] = '""'
93             self.netenv_obj[param_def]['ExternalNetworkVlanID'] = \
94                 net_settings[PUBLIC_NETWORK]['vlan']
95         public_range = \
96             net_settings[PUBLIC_NETWORK]['usable_ip_range'].split(',')
97         self.netenv_obj[param_def]['ExternalAllocationPools'] = \
98             [{'start':
99               public_range[0],
100               'end': public_range[1]
101               }]
102         self.netenv_obj[param_def]['ExternalInterfaceDefaultRoute'] = \
103             net_settings[PUBLIC_NETWORK]['gateway']
104         self.netenv_obj[param_def]['EC2MetadataIp'] = \
105             net_settings[ADMIN_NETWORK]['provisioner_ip']
106         self.netenv_obj[param_def]['DnsServers'] = net_settings['dns_servers']
107
108         if public_cidr.version == 6:
109             postfix = '/external_v6.yaml'
110         else:
111             postfix = '/external.yaml'
112
113         for key, prefix in EXTERNAL_RESOURCES.items():
114             if prefix is None:
115                 prefix = ''
116             self.netenv_obj[reg][key] = tht_dir + prefix + postfix
117
118         if PRIVATE_NETWORK in enabled_networks:
119             priv_range = net_settings[PRIVATE_NETWORK][
120                 'usable_ip_range'].split(',')
121             self.netenv_obj[param_def]['TenantAllocationPools'] = \
122                 [{'start':
123                   priv_range[0],
124                   'end': priv_range[1]
125                   }]
126             priv_cidr = net_settings[PRIVATE_NETWORK]['cidr']
127             self.netenv_obj[param_def]['TenantNetCidr'] = str(priv_cidr)
128             if priv_cidr.version == 6:
129                 postfix = '/tenant_v6.yaml'
130             else:
131                 postfix = '/tenant.yaml'
132             if net_settings[PRIVATE_NETWORK]['vlan'] != 'native':
133                 self.netenv_obj[param_def]['TenantNetworkVlanID'] = \
134                     net_settings[PRIVATE_NETWORK]['vlan']
135         else:
136             postfix = '/noop.yaml'
137
138         for key, prefix in TENANT_RESOURCES.items():
139             if prefix is None:
140                 prefix = ''
141             self.netenv_obj[reg][key] = tht_dir + prefix + postfix
142
143         if STORAGE_NETWORK in enabled_networks:
144             storage_range = net_settings[STORAGE_NETWORK][
145                 'usable_ip_range'].split(',')
146             self.netenv_obj[param_def]['StorageAllocationPools'] = \
147                 [{'start':
148                   storage_range[0],
149                   'end':
150                   storage_range[1]
151                   }]
152             storage_cidr = net_settings[STORAGE_NETWORK]['cidr']
153             self.netenv_obj[param_def]['StorageNetCidr'] = str(storage_cidr)
154             if storage_cidr.version == 6:
155                 postfix = '/storage_v6.yaml'
156             else:
157                 postfix = '/storage.yaml'
158             if net_settings[STORAGE_NETWORK]['vlan'] != 'native':
159                 self.netenv_obj[param_def]['StorageNetworkVlanID'] = \
160                     net_settings[STORAGE_NETWORK]['vlan']
161         else:
162             postfix = '/noop.yaml'
163
164         for key, prefix in STORAGE_RESOURCES.items():
165             if prefix is None:
166                 prefix = ''
167             self.netenv_obj[reg][key] = tht_dir + prefix + postfix
168
169         if API_NETWORK in enabled_networks:
170             api_range = net_settings[API_NETWORK][
171                 'usable_ip_range'].split(',')
172             self.netenv_obj[param_def]['InternalApiAllocationPools'] = \
173                 [{'start': api_range[0],
174                   'end': api_range[1]
175                   }]
176             api_cidr = net_settings[API_NETWORK]['cidr']
177             self.netenv_obj[param_def]['InternalApiNetCidr'] = str(api_cidr)
178             if api_cidr.version == 6:
179                 postfix = '/internal_api_v6.yaml'
180             else:
181                 postfix = '/internal_api.yaml'
182             if net_settings[API_NETWORK]['vlan'] != 'native':
183                 self.netenv_obj[param_def]['InternalApiNetworkVlanID'] = \
184                     net_settings[API_NETWORK]['vlan']
185         else:
186             postfix = '/noop.yaml'
187
188         for key, prefix in API_RESOURCES.items():
189             if prefix is None:
190                 prefix = ''
191             self.netenv_obj[reg][key] = tht_dir + prefix + postfix
192
193         if self.compute_pre:
194             self.netenv_obj[reg][COMPUTE_PRE] = PRE_CONFIG_DIR + \
195                 "compute/numa.yaml"
196         if self.controller_pre:
197             self.netenv_obj[reg][CONTROLLER_PRE] = PRE_CONFIG_DIR + \
198                 "controller/numa.yaml"
199
200         # Set IPv6 related flags to True. Not that we do not set those to False
201         # when IPv4 is configured, we'll use the default or whatever the user
202         # may have set.
203         if net_settings.get_ip_addr_family() == 6:
204             for flag in IPV6_FLAGS:
205                 self.netenv_obj[param_def][flag] = True
206
207     def get_netenv_settings(self):
208         """
209         Getter for netenv settings
210         :return: Dictionary of network environment settings
211         """
212         return self.netenv_obj
213
214
215 class NetworkEnvException(Exception):
216     def __init__(self, value):
217         self.value = value
218
219     def __str__(self):
220             return self.value