Add ability to configure allowed vlan ranges in deploy configuration
[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(dict):
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         init_dict = {}
57         if type(filename) is str:
58             with open(filename, 'r') as net_env_fh:
59                 init_dict = yaml.load(net_env_fh)
60
61         super().__init__(init_dict)
62         try:
63             enabled_networks = net_settings.enabled_network_list
64         except:
65             raise NetworkEnvException('Invalid Network Setting object')
66         param_def = self['parameter_defaults']
67         reg = self['resource_registry']
68
69         if not net_settings:
70             raise NetworkEnvException("Network Settings does not exist")
71
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():
76             if prefix is None:
77                 prefix = ''
78             m = re.split('%s/\w+\.yaml' % prefix, self[reg][key])
79             if m is not None:
80                 tht_dir = m[0]
81                 break
82         if not tht_dir:
83             raise NetworkEnvException('Unable to parse THT Directory')
84
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']
96         public_range = \
97             net_settings[PUBLIC_NETWORK]['usable_ip_range'].split(',')
98         self[param_def]['ExternalAllocationPools'] = \
99             [{'start':
100               public_range[0],
101               'end': public_range[1]
102               }]
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']
108
109         if public_cidr.version == 6:
110             postfix = '/external_v6.yaml'
111         else:
112             postfix = '/external.yaml'
113
114         for key, prefix in EXTERNAL_RESOURCES.items():
115             if prefix is None:
116                 prefix = ''
117             self[reg][key] = tht_dir + prefix + postfix
118
119         if PRIVATE_NETWORK in enabled_networks:
120             priv_range = net_settings[PRIVATE_NETWORK][
121                 'usable_ip_range'].split(',')
122             self[param_def]['TenantAllocationPools'] = \
123                 [{'start':
124                   priv_range[0],
125                   'end': priv_range[1]
126                   }]
127             priv_cidr = net_settings[PRIVATE_NETWORK]['cidr']
128             self[param_def]['TenantNetCidr'] = str(priv_cidr)
129             if 'vlan_id_range' in net_settings[PRIVATE_NETWORK].keys():
130                 tenant_vlan_range = \
131                     net_settings[PRIVATE_NETWORK]['vlan_id_range'].split(',')
132                 self[param_def]['NeutronNetworkVLANRanges'] = \
133                     "datacentre:" + tenant_vlan_range[0] + ':' \
134                     + tenant_vlan_range[1]
135             if priv_cidr.version == 6:
136                 postfix = '/tenant_v6.yaml'
137             else:
138                 postfix = '/tenant.yaml'
139             if net_settings[PRIVATE_NETWORK]['vlan'] != 'native':
140                 self[param_def]['TenantNetworkVlanID'] = \
141                     net_settings[PRIVATE_NETWORK]['vlan']
142         else:
143             postfix = '/noop.yaml'
144
145         for key, prefix in TENANT_RESOURCES.items():
146             if prefix is None:
147                 prefix = ''
148             self[reg][key] = tht_dir + prefix + postfix
149
150         if STORAGE_NETWORK in enabled_networks:
151             storage_range = net_settings[STORAGE_NETWORK][
152                 'usable_ip_range'].split(',')
153             self[param_def]['StorageAllocationPools'] = \
154                 [{'start':
155                   storage_range[0],
156                   'end':
157                   storage_range[1]
158                   }]
159             storage_cidr = net_settings[STORAGE_NETWORK]['cidr']
160             self[param_def]['StorageNetCidr'] = str(storage_cidr)
161             if storage_cidr.version == 6:
162                 postfix = '/storage_v6.yaml'
163             else:
164                 postfix = '/storage.yaml'
165             if net_settings[STORAGE_NETWORK]['vlan'] != 'native':
166                 self[param_def]['StorageNetworkVlanID'] = \
167                     net_settings[STORAGE_NETWORK]['vlan']
168         else:
169             postfix = '/noop.yaml'
170
171         for key, prefix in STORAGE_RESOURCES.items():
172             if prefix is None:
173                 prefix = ''
174             self[reg][key] = tht_dir + prefix + postfix
175
176         if API_NETWORK in enabled_networks:
177             api_range = net_settings[API_NETWORK][
178                 'usable_ip_range'].split(',')
179             self[param_def]['InternalApiAllocationPools'] = \
180                 [{'start': api_range[0],
181                   'end': api_range[1]
182                   }]
183             api_cidr = net_settings[API_NETWORK]['cidr']
184             self[param_def]['InternalApiNetCidr'] = str(api_cidr)
185             if api_cidr.version == 6:
186                 postfix = '/internal_api_v6.yaml'
187             else:
188                 postfix = '/internal_api.yaml'
189             if net_settings[API_NETWORK]['vlan'] != 'native':
190                 self[param_def]['InternalApiNetworkVlanID'] = \
191                     net_settings[API_NETWORK]['vlan']
192         else:
193             postfix = '/noop.yaml'
194
195         for key, prefix in API_RESOURCES.items():
196             if prefix is None:
197                 prefix = ''
198             self[reg][key] = tht_dir + prefix + postfix
199
200         if compute_pre_config:
201             self[reg][COMPUTE_PRE] = PRE_CONFIG_DIR + "compute/numa.yaml"
202         if controller_pre_config:
203             self[reg][CONTROLLER_PRE] = PRE_CONFIG_DIR + "controller/numa.yaml"
204
205         # Set IPv6 related flags to True. Not that we do not set those to False
206         # when IPv4 is configured, we'll use the default or whatever the user
207         # may have set.
208         if net_settings.get_ip_addr_family() == 6:
209             for flag in IPV6_FLAGS:
210                 self[param_def][flag] = True
211
212
213 class NetworkEnvException(Exception):
214     def __init__(self, value):
215         self.value = value
216
217     def __str__(self):
218             return self.value